Skip to content

Build the MCP tool context from a session in one place - #1144

Open
richshaw wants to merge 1 commit into
mainfrom
claude/issue-1141-bearer-organizer
Open

richshaw wants to merge 1 commit into
mainfrom
claude/issue-1141-bearer-organizer

Conversation

@richshaw

Copy link
Copy Markdown
Owner

Fixes #1141.
Session: https://claude.ai/code/session_016YZ9SSbFFg2F4VY28wUau4

The problem

Turning a signed-in organizer into the context an MCP tool runs with was written out three times. resolveBearerActor built it in production, contextForOrganizer (which the MCP DB tests and the follow-up evals use) rebuilt it field by field, and the route copied the six fields into ToolContext one at a time. BearerWorkspace and ToolWorkspace were the same type. A field added to the bearer path but missed in the test copy would leave the MCP tests running on a context production no longer builds.

The fix

  • src/auth/bearer.ts: a new exported BearerOrganizer type (actor, scopes, clientId, defaultWorkspaceId, workspaces, locale, with their doc comments) and a pure bearerOrganizer(session, clientId, scopes). It calls toActor, narrows the kind, copies the actor with via (the comment about not mutating the React-cached actor moved with it), and maps the memberships. It replaces bearerWorkspaces. BearerResolution's ok branch is now BearerOrganizer & { ok: true; authInfo }, and resolveBearerActor passes parseScopeString(...) as before, so the builder takes scopes already parsed.
  • src/mcp/context.ts: ToolContext is BearerOrganizer & { db }. ToolWorkspace is gone. The only new import is a type-only BearerOrganizer from the seam module, and tool handlers still import nothing from @/auth.
  • src/mcp/testing/context.ts: loads the session, calls bearerOrganizer, and throws if it gives null.
  • The route destructures ok and authInfo out of the resolution and spreads the rest into the context. I left authInfo out of the context on purpose: it carries the raw token, and the context is attached to it, not the other way round.

No behaviour changes, so there's no CHANGELOG entry. It touches src/auth/ and the MCP route, so it waits for the owner. #1135 (from a fork) changes key-error logging in bearer.ts. The lines don't overlap, but whichever lands second has a small merge.

Tests

No new tests. The existing ones cover both paths: route.test.ts mocks the resolution and checks the context the tools get, and tools.db.test.ts plus the OAuth flow DB tests run through contextForOrganizer and resolveBearerActor.

  • grep -rn "ToolWorkspace\|bearerWorkspaces" src finds nothing.
  • pnpm lint && pnpm format:check && pnpm typecheck && pnpm knip && pnpm test pass (3894 tests).
  • pnpm test:db passes (916 tests).

Generated by Claude Code

resolveBearerActor and the MCP tests' contextForOrganizer each turned an
organizer's session into the tool context by hand, and the route copied
the fields over once more. They now share bearerOrganizer in the bearer
seam, ToolContext is its type plus db, and the route spreads it, so a
field added to the bearer path can't be missed by the tests and evals.

Fixes #1141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YZ9SSbFFg2F4VY28wUau4
@richshaw richshaw added pipeline:tophat pipeline:working A session is working on it right now and removed pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

Tophat: passed

See it: https://claude.ai/artifact/7Wf3Ne1VPv5k821mqtju7T
What changes for people: Nothing. A connected AI assistant sees the same tools, answers and refusals as before. Only the way the server works out who a token belongs to has been tidied up.

Check Result What I saw
Same MCP calls on base and head ✅ Real token from the consent screen. tools/list (18), list_workspaces, list_signups, create_signup and get_signup gave identical normalised results on both commits, apart from the signup count, which the first run raised by one.
Organizer's language reaches tools ✅ With the account set to Swedish, create_signup with no locale made a sv signup, before and after.
Client recorded in activity ✅ The signup.created row carries viaClientId: e2e-client, the same on both commits.
Viewer in a second workspace ✅ list_workspaces shows owner and viewer roles. create_signup into the viewer workspace is refused with forbidden.
Token whose organizer was deleted ✅ Got a token, deleted the organizer, called again: 401 invalid_token "unknown account". This is the branch the PR rewrote.
No token, bad token, missing scope ✅ 401 with resource_metadata, 401 invalid_token, and 403 insufficient_scope (signups:write) for a read-only token. Same before and after.

Worth a look: the route now spreads the whole resolution, minus ok and authInfo, into the tool context. A field added to the resolution later reaches tools automatically. That's the point of the change, but a secret added there would reach them too.

Not checked on the base: the viewer and deleted-organizer cases. I ran them on the head only, and their wording comes from code this PR doesn't touch.

Ran 09a688e against a820439 locally, MCP over HTTP with a token from the real OAuth flow, made-up data. https://claude.ai/code/session_016YZ9STFWdWM98i7mGfTd2w


Generated by Claude Code

@richshaw richshaw added pipeline:review pipeline:working A session is working on it right now and removed pipeline:tophat pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude

@richshaw richshaw left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: passed (round 1)

bearerOrganizer is now the only place a session becomes a tool context. resolveBearerActor, contextForOrganizer and the route all go through it, and they build the same fields as before, including the 401 for an organizer who is gone. Nothing in src still uses ToolWorkspace or bearerWorkspaces. Both "Done when" items are met, and so is the issue's outline.

Blocking (0)
Optional (3):

  • route.ts:109: the context now takes everything except ok and authInfo, so any field added later to the resolution reaches every tool. The tophat flagged this too. Returning { ok, organizer, authInfo } would make adding a field a deliberate choice.
  • bearer.ts:24: the file's seam comment now sits on the exported BearerOrganizer, so it shows as that type's hover doc. Moving it onto resolveBearerActor would fix that.
  • context.ts:1: the type-only BearerOrganizer import is what the issue planned. CLAUDE.md's "only auth import" line could name it.

Earlier threads: none
Checks: CI is green on 09a688e (lint-type-test, e2e, CodeQL). The tophat passed on this SHA. I ran code-review at high effort.

This waits for you because it touches src/auth/bearer.ts and the MCP route, which are high-risk. It changes no behaviour. Card: https://claude.ai/artifact/7Wf3Ne1VPv5k821mqtju7T

https://claude.ai/code/session_016YZ9SVJHryH4EUPxis53cd


Generated by Claude Code

@richshaw richshaw added pipeline:owner Passed; waits for the owner (UX or high-risk) and removed pipeline:review pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pipeline:owner Passed; waits for the owner (UX or high-risk)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants