Skip to content

The MCP tool context is built from a session twice, so the MCP DB tests and evals run on a copy of production's code #1141

Backlog

Description

@richshaw

What happens

Turning a signed-in organizer into the context an MCP tool runs with is written out three times, with two identical types:

  • Production: resolveBearerActor in src/auth/bearer.ts loads the session, calls toActor, checks the actor is an organizer, then returns actor: { ...actor, via: { clientId } }, scopes, clientId, defaultWorkspaceId, workspaces: bearerWorkspaces(session) and locale.
  • Tests and evals: contextForOrganizer in src/mcp/testing/context.ts repeats the same steps by hand, field by field. It's what src/mcp/tools.db.test.ts and src/mcp/evals/followups.ts run the tools with.
  • The route: src/app/api/mcp/route.ts copies the six fields from the resolution into ToolContext one at a time.
  • Types: BearerWorkspace (src/auth/bearer.ts) and ToolWorkspace (src/mcp/context.ts) are the same { id, slug, name, role }. ToolContext restates the fields of BearerResolution's ok branch, doc comments included, plus db.

Today the copies give the same values. But a field added to the bearer path (as locale once was) has to be added in three places, and if the test copy is missed, the MCP DB tests and evals keep passing on a context production no longer builds.

Nobody using OpenSignup notices.

Fix, in outline

  • One builder in src/auth/bearer.ts: export a BearerOrganizer type (actor, scopes, clientId, defaultWorkspaceId, workspaces, locale, with the existing doc comments moved onto it) and a pure bearerOrganizer(session, clientId, scopes): BearerOrganizer | null that calls toActor, narrows the kind and builds the fields. It absorbs bearerWorkspaces, which nothing else then uses.
  • resolveBearerActor calls it with parseScopeString(...) and returns { ok: true, ...organizer, authInfo }. BearerResolution's ok branch becomes BearerOrganizer & { ok: true; authInfo }.
  • src/mcp/context.ts: ToolContext becomes BearerOrganizer & { db }; ToolWorkspace goes.
  • contextForOrganizer: load the session, call bearerOrganizer, throw if null, return { db, ...organizer }.
  • The route: spread the resolution into the context instead of copying fields.

A verify agent made this change in a scratch tree: 4 files, about 23 lines fewer.

Watch for

  • The seam rule. CLAUDE.md says resolveBearerActor is the only auth import under MCP routes and tool handlers. The new builder lives in bearer.ts, the seam module, and the only new import outside it is a type-only BearerOrganizer in src/mcp/context.ts. Tool handlers still import nothing from @/auth. Keep it that way.
  • Keep the spread. toActor is React-cached, so the actor is copied with via, never mutated; the comment saying so stays with the code.
  • Scopes. Production filters through parseScopeString; the test helper passes typed scopes as given. Keep that: the builder takes scopes already parsed.
  • This touches src/auth/, so the PR waits for the owner before merging. An open PR changes the key-error logging in bearer.ts; the lines don't overlap, but whichever lands second has a small merge.
  • route.test.ts mocks resolveBearerActor. Its mock still fits the new type; check it does.

Done when

  • grep -rn "ToolWorkspace\|bearerWorkspaces" src finds nothing, and contextForOrganizer builds its context by calling the same function resolveBearerActor does.
  • pnpm lint && pnpm format:check && pnpm typecheck && pnpm knip && pnpm test pass, and pnpm test:db passes (tools.db.test.ts and the OAuth flow tests use this path).

Found by the pipeline's refactor scout in the MCP server and its auth seam (src/mcp, src/auth/bearer.ts, src/app/api/mcp), at 2c037aa. Checked by an independent verify agent. It quoted every copy, confirmed they give equal values today, found no comment calling the copy deliberate and no open PR fixing it, then made the change in a scratch worktree: typecheck, lint, knip, prettier and pnpm test src/mcp src/auth src/app/api/mcp (250 tests) passed. pnpm test:db couldn't run there (no Postgres). Then it removed the worktree. https://claude.ai/code/session_016YZ9SU4eo5cjeZjMfXY5sy

Activity

  1. added
    enhancement
    pipeline:working
    and removed on Nov 27, 2026
  2. self-assigned this
    on Nov 27, 2026
  3. removed
    pipeline:working
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementpipeline:building

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions