Skip to content

core: move MCP tool handler logic into shared operations that the CLI and doctor also call #1349

Closed

Description

@schlessera

Part of #1341.

Context

startMcpServer is 600 lines (packages/core/src/mcp-server.ts:91-691). Tool handlers contain logic other surfaces need: the index-staleness check (indexStalenessWarning, packages/core/src/mcp-server.ts:134-174), reimplemented by doctor (packages/core/src/cli/commands/doctor.ts:312-330), and the brain_update read-modify-write-reindex flow (packages/core/src/mcp-server.ts:564-640).

Scope

  • Move each tool handler's body into a function under packages/core/src/lib/ops/ (one module per operation or per family) that takes resolved dependencies and returns the result object.
  • mcp-server.ts keeps schema registration and thin wrappers that map results to MCP content.
  • doctor's staleness check calls the shared staleness function.

Out of scope

  • Adding a CLI update command (see the separate brain_update issue).
  • Changing any MCP input schema, tool name or output text.
  • Provider resolution (separate issue).

Acceptance criteria

  • startMcpServer is under ~200 lines and contains no SQL.
  • One staleness function is called by both MCP and doctor; a test asserts both report the same stale count on one fixture.
  • packages/core/tests/mcp-contract.test.ts and mcp-input-schemas.json pass unchanged.
  • Each extracted operation has a direct unit test against the fixture corpus.

Verification

bun run test packages/core
bun run typecheck
bun run lint

Contract impact

None intended; contract snapshots must not change.

Blocked by #1350

2026-10-09: sequenced against the other code-quality issues to avoid overlapping edits; see the epic's planned-order comment.

Activity

  1. added
    type: refactor
    area: core
    area: mcp
    priority: p2
    agent-ready
    on Oct 9, 2026
  2. added
    blocked
    and removed
    blocked
    on Oct 9, 2026
  3. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Unblocked: #1350 merged in #1458 (a8f884f), its only listed blocker.

  4. self-assigned this
    on Oct 10, 2026
  5. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Claimed (token w1349-a). Working on branch refactor/1349-mcp-shared-ops.

  6. added this to the 0.41.0 milestone on Oct 10, 2026
  7. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Delivered in #1468, squash-merged as 62cf4d6 on main. The tool handler bodies now live in packages/core/src/lib/ops/ (staleness.ts, read.ts, write.ts). startMcpServer went from 594 lines to 68 and contains no SQL. brain doctor's db check calls the shared indexStaleness. index-staleness.test.ts asserts that MCP and doctor both report 3 stale files on one fixture; it failed first against a divergent doctor copy, and fails again when the shared function is mutated. Every extracted op has a direct test against the fixture corpus in ops.test.ts. The MCP contract snapshots are unchanged. listDocuments applies the limit it is given, which is the seam #1351 needs. All CI jobs passed on the PR head (run 38029506593, checkout of the merge with main a8f884f). The dependants #1351, #1353, #1356 and #1398 are no longer blocked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: corearea: mcppriority: p2type: refactor

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions