feat(mcp): MCP endpoint on the companion (route, token authentication, read tools) (#202) #215

Open
maximus wants to merge 8 commits from issue-202-mcp-endpoint into issue-208-put-ownership

8 commits

Author SHA1 Message Date
le king fu
4a99d32615 docs(review): name the two reads that filter a task twice on the account (#202)
The bullet on the account reads of the MCP tools said every read filters a
task by its account and by its list; queryAccountTasks and loadAccountTask
do, loadSubtasks and loadOpenTaskCounts filter on sl_tasks.user_id alone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
22a0981fe0 test(mcp): build the JWT-shaped test value instead of writing it (#202)
The refused Authorization values of the route test held a literal in three
base64url parts starting with `eyJ`: a made-up value, but one a secret scanner
reads as a token. It is now built by the test. No assertion changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
06908acc28 docs(review): the MCP endpoint in the review rules and in the high-level table (#202)
- Niveaux, table Haut, row « API et isolation des comptes »:
  `web/src/lib/mcp/**` is added. Nothing is removed or relaxed: the directory
  is new, and no commit on master touches it, so the calibration on the
  merged PRs is unchanged.
- scripts/review-levels.py: the same pattern in HIGH_ALWAYS
  (`python3 scripts/review-levels.py --check` passes: 31 always, 11
  conditional; without the pattern in the script it exits 1).
- Zone Authentification: the bullet of the routes that call neither
  requireAuth() nor requireAssistantsSession() counts six and says why
  /api/mcp is one; a new bullet gives the order of the checks of the route,
  the 401 without OAuth metadata, the middleware pass-through and what the
  server log lines may carry. verifyAgentToken now has its caller.
- Zone API et isolation des comptes: one McpServer per request and nothing
  at module level; arrays of a tool schema bounded by `.max()` and by
  maxToolInputElements; every tool exception through the envelope
  (internal_error) and every row through shape.ts; the three rate
  categories; the account reads of the tools (two account filters, cursor on
  the millisecond). The sentence « no body size bound » now excepts /api/mcp.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
c09564c45f docs(mcp): the comment of the tool input bound says what comes from where (#202)
The advisory GHSA-46jr-4wx8-v453 is about the SDK setting no limit unless one
is given; that zod validates the elements of an array before its length is a
fact of zod (REVIEW.md, #147). The comment tied the second to the advisory.
Comment only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
18fa391bf6 test(mcp): the factory guard of the handler and the logged error name (#202)
Two guards of the endpoint had no test of their own:
- the factory builds no server for a request that does not carry the
  account the route writes in authInfo (the SDK handler verifies nothing);
- the server log keeps of an exception its class name only when that name
  reads as one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
c04f5451ff feat(mcp): MCP endpoint on the companion: route, token authentication, read tools (#202)
POST /api/mcp serves the account of an agent token to an AI assistant over
the Model Context Protocol: tools only, no resources, prompts or
subscriptions. The route authenticates itself, in this order: an Origin
outside the list of origin.ts (403), verifyAgentToken (401 with
`WWW-Authenticate: Bearer`, no OAuth metadata), the pilot (403), the
`mcp-http` backstop (429 with Retry-After), then the SDK handler, which
receives the account of the token and never its value. A session cookie or a
Logto access token opens nothing. One server log line per request, without
the token, the account or any content.

- middleware: /api/mcp passes without a cookie or a Bearer, so that a client
  without a token gets the 401 of the route and not a redirect to /auth; it
  stays in config.matcher, which the body size cut depends on.
- lib/mcp/server.ts: createMcpHandler is called once, with a body bound of
  256 KiB and maxSubscriptions 0 (the SDK serves subscriptions/listen by
  default); every request gets a new McpServer with maxToolInputElements 64
  and the tools of its token; nothing but the handler lives at module level.
  The same factory serves the clients of 2026 and those of 2025.
- tool envelope: the rate limit of the tool, refused by a result the model
  reads; a coded refusal (errors.ts) becomes an isError result
  `{ error, message }`; any other exception becomes internal_error, and only
  the class name of the error is logged; a success returns structuredContent
  and the same JSON as text.
- lib/mcp/shape.ts: what a tool returns of a list, a tag and a task, column by
  column, never user_id nor deleted_at.
- lib/mcp/readTools.ts: list_lists, list_tags, list_tasks and get_task, with
  strict input schemas; a deleted row and a row of another account answer
  like an unknown one.

Resolves #202

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
9ee15d6ca9 feat(web): account-wide task reads and the MCP rate limit categories (#202)
taskQueries.ts gains the reads of the MCP read tools, each scoped to the
account:
- queryAccountTasks(userId, filters, page): top-level tasks, not deleted, in
  the live lists of the account, across lists; filters by list, status,
  inclusive due date bounds, tags (at least one, live and of the account),
  priority and text (title or notes, case-insensitive, `%`, `_` and `\`
  escaped); total order on the creation instant cut to the millisecond, then
  the id; cursor on that key; one row more than the page is read to know
  whether another page follows;
- loadAccountTask, loadAccountLists and loadOpenTaskCounts.
created_at holds microseconds and a JavaScript Date milliseconds: the order
and the cursor both use date_trunc('milliseconds', created_at), otherwise a
cursor would stand before its own row and the next page would return it again.
queryListTasks shares its tag sub-query with the new function.

rateLimit.ts gains the categories mcp-read (120 a minute), mcp-write (30) and
mcp-http (600), per account, and rateLimitDelay(userId, category), which
returns the delay in seconds, or null when the call is allowed and counted.
rateLimit() keeps its signature and is written on top of it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00
le king fu
04efc4e2cc chore(web): add @modelcontextprotocol/server 2.4.0, pinned (#202)
The official MCP SDK, for POST /api/mcp. Installed from web/ with
`npm install --save-exact`, npm 10.9.4 on Node 22 (the npm of node:22-alpine).

The lock gains two entries and loses none, both from registry.npmjs.org,
without an install script nor a bin:
- @modelcontextprotocol/server 2.4.0
- @modelcontextprotocol/core 2.4.0 (its only dependency besides zod, already
  present in 4.6.7)
Their `integrity` values are those of the registry (`npm view <pkg>@2.4.0
dist.integrity`). No other entry changes but the root, which lists the new
dependency.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:31 -05:00