T-129: M2.1a — MCP copilot core: dependency-free JSON-RPC/MCP stdio server + tool registry + vault-fenced query_data in libs/research/src/mcp/

This commit is contained in:
Studio 2026-11-30 15:34:20 -05:00
parent 0bea9a6f1d
commit 73ac978bb3
10 changed files with 2266 additions and 0 deletions

View file

@ -196,3 +196,24 @@ Fixed-seed (`20261129`) local benchmark covering `5,040` business sessions × `3
- **Command:** `bun run libs/research/scripts/benchmark-event-panel.ts`
- **Observed (Bun 1.2.16):** `elapsedMs = 731.06 ms`, `barsPerSec = 20,682,279 bars/s`, `eventsPerSec = 328,290 events/s` (`5,020` pre-Vault sessions, `240,000` anchored events, `20` Vault-excluded dates, pinned checksum `01b356d3`)
## MCP Copilot Core
Server-only, dependency-free JSON-RPC 2.0 / Model Context Protocol core exported via the `@stock-bot/research/mcp` subpath (`libs/research/src/mcp/index.ts`). It is intentionally **not** re-exported from the browser-safe `@stock-bot/research` default entrypoint.
### Registering Tools & Running Stdio Framing
- **`defineMcpTool({ name, description, inputSchema, jsonSchema?, handler })`** — Registers a strict Zod-validated tool, converts `inputSchema` into a strict JSON Schema (`additionalProperties: false`, or uses an explicit `jsonSchema` override), returns `isError: true` on invalid arguments without calling the handler, and sanitizes thrown handler errors so stack traces never leak.
- **`createMcpServer({ name, version, tools })`** — Transport-agnostic JSON-RPC 2.0 dispatcher implementing `initialize`, `notifications/initialized`, `ping`, `tools/list`, and `tools/call` via `handleMessage(raw: string): Promise<string | null>`. Rejects duplicate tool names at creation time.
- **`runStdioServer(server, { input?, output?, maxLineBytes? })`** — Newline-delimited JSON-RPC stdio transport over injectable streams (defaulting to `process.stdin` / `process.stdout`), with streaming UTF-8 decoding across arbitrary chunk boundaries, strict sequential dispatch in arrival order, `maxLineBytes` frame-size protection (default `4 MiB`), and zero non-protocol bytes on `output`.
### `query_data` Tool (Vault-Fenced)
- **`createQueryDataTool({ store, vaultContext })`** — Wraps an injected `MarketDataStore` for explicit `listingIds` (max `QUERY_DATA_MAX_LISTING_IDS = 500`) or a survivorship-free `universe` ref, with bounded output rows (`QUERY_DATA_DEFAULT_LIMIT = 500`, hard max `QUERY_DATA_MAX_LIMIT = 5_000`).
- Always enforces `scope: 'vault_fenced'` via `applyVaultFence` before any store call, omits `scope` / `vault_evaluation_only` from the strict schema (rejecting unknown keys with zero store calls), drops any post-cutoff row (`ts >= vaultCutoffTs` or `knownAt >= vaultCutoffTs`) as defense in depth, and maps `VaultHoldoutFenceError`, `VaultScopeNotAuthorizedError`, `MarketDataStoreValidationError`, `MarketDataStoreNotImplementedError`, and `AdjFactorsSnapshotError` to `isError: true` responses naming the error class.
### Remaining M2 MCP Items (Later Tasks)
- Additional research tools: `search_symbols`, `analyze_signal`, `event_study`, `file_finding`, and `read_knowledge`.
- SSE/HTTP transport wrapper in `apps/stock/web-api`.
- Runnable CLI binary wired to real DuckDB/Parquet and PostgreSQL adapters.