112 lines
3.4 KiB
TypeScript
112 lines
3.4 KiB
TypeScript
/**
|
|
* Runnable stdio entrypoint for the headless MCP research copilot server.
|
|
*
|
|
* Parses `RESEARCH_MCP_*` environment variables, composes the Vault-fenced
|
|
* PostgreSQL executor, DuckDB market data store, and read-only library card
|
|
* reader, and serves newline-delimited JSON-RPC 2.0 over stdio.
|
|
*
|
|
* Fail-closed: configuration or host errors write a single fixed line to
|
|
* stderr and exit with code 1 without exposing DSNs, passwords, or raw error
|
|
* objects. Stdout carries only MCP protocol bytes.
|
|
*/
|
|
|
|
import { randomUUID } from 'node:crypto';
|
|
import {
|
|
type LibraryCardReaderHandle,
|
|
type ManagedMarketDataStore,
|
|
type ManagedPostgresExecutor,
|
|
type ResearchCopilotHostConfig,
|
|
type ResearchCopilotStdioHostDeps,
|
|
type StdioInput,
|
|
type StdioOutput,
|
|
createLibraryCardReader,
|
|
createNodeLibraryCardFs,
|
|
parseResearchCopilotHostConfig,
|
|
runResearchCopilotStdioHost,
|
|
} from '@stock-bot/research/mcp';
|
|
|
|
export const RESEARCH_COPILOT_CONFIG_ERROR_LINE =
|
|
'Research copilot MCP server failed: invalid RESEARCH_MCP_* configuration.\n';
|
|
|
|
export const RESEARCH_COPILOT_HOST_ERROR_LINE =
|
|
'Research copilot MCP server failed while running stdio host.\n';
|
|
|
|
export interface ResearchCopilotStderr {
|
|
write(chunk: string): boolean;
|
|
}
|
|
|
|
export interface ResearchCopilotMcpDeps {
|
|
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
readonly input?: StdioInput;
|
|
readonly output?: StdioOutput;
|
|
readonly stderr?: ResearchCopilotStderr;
|
|
readonly openExecutor?: ResearchCopilotStdioHostDeps['openExecutor'];
|
|
readonly openStore?: ResearchCopilotStdioHostDeps['openStore'];
|
|
readonly createLibraryCardReader?: ResearchCopilotStdioHostDeps['createLibraryCardReader'];
|
|
readonly newId?: () => string;
|
|
readonly now?: () => Date | number;
|
|
}
|
|
|
|
async function defaultOpenExecutor(
|
|
config: ResearchCopilotHostConfig
|
|
): Promise<ManagedPostgresExecutor> {
|
|
const { openResearchCopilotExecutor } = await import(
|
|
'../libs/data/postgres/src/research-copilot-executor'
|
|
);
|
|
return openResearchCopilotExecutor(config);
|
|
}
|
|
|
|
async function defaultOpenStore(
|
|
config: ResearchCopilotHostConfig
|
|
): Promise<ManagedMarketDataStore> {
|
|
const { openResearchCopilotStore } = await import(
|
|
'../libs/data/duckdb/src/research-copilot-store'
|
|
);
|
|
return openResearchCopilotStore(config);
|
|
}
|
|
|
|
function defaultCreateLibraryCardReader(
|
|
config: ResearchCopilotHostConfig
|
|
): LibraryCardReaderHandle {
|
|
return createLibraryCardReader({
|
|
root: config.libraryRoot,
|
|
fs: createNodeLibraryCardFs(),
|
|
});
|
|
}
|
|
|
|
export async function runResearchCopilotMcp(
|
|
deps: ResearchCopilotMcpDeps = {}
|
|
): Promise<number> {
|
|
const stderr = deps.stderr ?? process.stderr;
|
|
|
|
let config: ResearchCopilotHostConfig;
|
|
try {
|
|
config = parseResearchCopilotHostConfig(deps.env ?? process.env);
|
|
} catch {
|
|
stderr.write(RESEARCH_COPILOT_CONFIG_ERROR_LINE);
|
|
return 1;
|
|
}
|
|
|
|
try {
|
|
await runResearchCopilotStdioHost({
|
|
config,
|
|
openExecutor: deps.openExecutor ?? defaultOpenExecutor,
|
|
openStore: deps.openStore ?? defaultOpenStore,
|
|
createLibraryCardReader: deps.createLibraryCardReader ?? defaultCreateLibraryCardReader,
|
|
newId: deps.newId ?? randomUUID,
|
|
now: deps.now ?? Date.now,
|
|
stdio: {
|
|
input: deps.input ?? process.stdin,
|
|
output: deps.output ?? process.stdout,
|
|
},
|
|
});
|
|
return 0;
|
|
} catch {
|
|
stderr.write(RESEARCH_COPILOT_HOST_ERROR_LINE);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
process.exitCode = await runResearchCopilotMcp();
|
|
}
|