T-144: M2.1g — MCP copilot composition: one createResearchCopilotMcpServer factory registering all six tools + headless stdio end-to-end study test (pure, injected ports, no new deps)
This commit is contained in:
parent
29af43c1c8
commit
1d7bb55f41
4 changed files with 1327 additions and 0 deletions
110
libs/research/src/mcp/copilot-server.ts
Normal file
110
libs/research/src/mcp/copilot-server.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { type McpServer, type McpServerOptions, createMcpServer } from './server';
|
||||
import { type AnalyzeSignalToolOptions, createAnalyzeSignalTool } from './tools/analyze-signal';
|
||||
import { type EventStudyToolOptions, createEventStudyTool } from './tools/event-study';
|
||||
import { type FileFindingToolOptions, createFileFindingTool } from './tools/file-finding';
|
||||
import { type QueryDataToolOptions, createQueryDataTool } from './tools/query-data';
|
||||
import { type ReadKnowledgeToolOptions, createReadKnowledgeTool } from './tools/read-knowledge';
|
||||
import { type SearchSymbolsToolOptions, createSearchSymbolsTool } from './tools/search-symbols';
|
||||
|
||||
/**
|
||||
* Fixed, documented registration order of the six MCP tools exposed by
|
||||
* {@link createResearchCopilotMcpServer}.
|
||||
*/
|
||||
export const RESEARCH_COPILOT_TOOL_NAMES = Object.freeze([
|
||||
'search_symbols',
|
||||
'query_data',
|
||||
'analyze_signal',
|
||||
'event_study',
|
||||
'file_finding',
|
||||
'read_knowledge',
|
||||
] as const);
|
||||
|
||||
/**
|
||||
* Composed server options for {@link createResearchCopilotMcpServer}.
|
||||
*
|
||||
* Shares the common Vault-fenced `MarketDataStore`, `vaultContext`,
|
||||
* `TrialLedgerExecutor`, and `principalId` across the tools that consume them
|
||||
* while passing each tool's remaining injected ports unchanged.
|
||||
*/
|
||||
export interface ResearchCopilotMcpServerOptions
|
||||
extends Pick<McpServerOptions, 'name' | 'version'>,
|
||||
Pick<QueryDataToolOptions, 'store' | 'vaultContext'>,
|
||||
Pick<AnalyzeSignalToolOptions, 'ledger' | 'principalId'> {
|
||||
readonly searchSymbols: Omit<SearchSymbolsToolOptions, 'vaultContext'>;
|
||||
readonly eventStudy: Pick<EventStudyToolOptions, 'resolveExchangeCalendars'>;
|
||||
readonly fileFinding: FileFindingToolOptions;
|
||||
readonly readKnowledge: ReadKnowledgeToolOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Composes all six research MCP tools (`search_symbols`, `query_data`,
|
||||
* `analyze_signal`, `event_study`, `file_finding`, `read_knowledge`) into a
|
||||
* single transport-agnostic {@link McpServer}.
|
||||
*
|
||||
* Keeps the shared `store`, `vaultContext`, `ledger`, and `principalId`
|
||||
* authoritative across tools and fails closed if any tool is duplicated,
|
||||
* missing, unexpected, or out of order.
|
||||
*/
|
||||
export function createResearchCopilotMcpServer(
|
||||
options: ResearchCopilotMcpServerOptions
|
||||
): McpServer {
|
||||
if (typeof options !== 'object' || options === null) {
|
||||
throw new Error('Invalid `options`: expected a ResearchCopilotMcpServerOptions object.');
|
||||
}
|
||||
if (typeof options.searchSymbols !== 'object' || options.searchSymbols === null) {
|
||||
throw new Error('Invalid `searchSymbols`: expected a SearchSymbolsToolOptions object.');
|
||||
}
|
||||
if (typeof options.eventStudy !== 'object' || options.eventStudy === null) {
|
||||
throw new Error('Invalid `eventStudy`: expected an EventStudyToolOptions object.');
|
||||
}
|
||||
|
||||
const tools = [
|
||||
createSearchSymbolsTool({
|
||||
...options.searchSymbols,
|
||||
vaultContext: options.vaultContext,
|
||||
}),
|
||||
createQueryDataTool({
|
||||
store: options.store,
|
||||
vaultContext: options.vaultContext,
|
||||
}),
|
||||
createAnalyzeSignalTool({
|
||||
store: options.store,
|
||||
ledger: options.ledger,
|
||||
vaultContext: options.vaultContext,
|
||||
principalId: options.principalId,
|
||||
}),
|
||||
createEventStudyTool({
|
||||
store: options.store,
|
||||
ledger: options.ledger,
|
||||
vaultContext: options.vaultContext,
|
||||
principalId: options.principalId,
|
||||
resolveExchangeCalendars: options.eventStudy.resolveExchangeCalendars,
|
||||
}),
|
||||
createFileFindingTool(options.fileFinding),
|
||||
createReadKnowledgeTool(options.readKnowledge),
|
||||
];
|
||||
|
||||
const server = createMcpServer({
|
||||
name: options.name,
|
||||
version: options.version,
|
||||
tools,
|
||||
});
|
||||
|
||||
const registered = server.listTools();
|
||||
if (registered.length !== RESEARCH_COPILOT_TOOL_NAMES.length) {
|
||||
throw new Error(
|
||||
`Research copilot MCP server expected ${RESEARCH_COPILOT_TOOL_NAMES.length} tools, got ${registered.length}.`
|
||||
);
|
||||
}
|
||||
for (let i = 0; i < RESEARCH_COPILOT_TOOL_NAMES.length; i += 1) {
|
||||
const expectedName = RESEARCH_COPILOT_TOOL_NAMES[i];
|
||||
const actualName = registered[i]?.name;
|
||||
if (actualName !== expectedName) {
|
||||
throw new Error(
|
||||
`Research copilot MCP server tool mismatch at index ${i}: expected "${expectedName}", got "${String(actualName)}".`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * from './copilot-server';
|
||||
export * from './protocol';
|
||||
export * from './server';
|
||||
export * from './stdio';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue