Public: anyone can read this; the crew can change it.

An MCP client with no token can initialize and list tools, and sees the five token-free ones #13899

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A client with no token can now start an MCP session over POST /mcp. Type check and the MCP tests I ran pass. I did not run the full suite or bun scripts/design-lint.ts; the forge runs the full check on push.

Notes

shipped production is serving it deploy 4adf612

opened this

A client with no token can now start an MCP session over POST /mcp. Type check and the MCP tests I ran pass. I did not run the full suite or bun scripts/design-lint.ts; the forge runs the full check on push.

What changed

  • src/http/mcp.ts has a new openSession step in the anonymous branch.
    • initialize and ping answer 200.
    • tools/list answers with exactly the five TOKEN_FREE_TOOLS and their schemas: report, offer, start, explore, search.
    • Any notifications/* message gets a 202, so notifications/initialized works.
  • I did not widen the free list. It still comes from TOKEN_FREE_TOOLS, the list /.well-known/mcp.json prints.
  • Signed-in and token callers take the old path and still see the full list.
  • Every other anonymous method (for example resources/list) still answers 401 with the -32001 sign-in error and the OAuth challenge. So does a tools/call of any tool that is not token-free.
  • initialize for a stranger returns a short instruction naming the free tools. Its capabilities leave out resources.
  • A signed-in browser cookie still isn't a way in, but a tools/list on it now gets the free list instead of a 401.

Tests

  • New test/mcp-anonymous-session.test.ts covers the initialize, notifications/initialized, ping, tools/list, tools/call explore sequence and the refusals.
  • Three old assertions expected an anonymous tools/list to answer 401: two in test/mcp.test.ts and the probe in test/mcp-discovery-walk.test.ts. They now use resources/list as the not-free method, so they still check the 401 and the OAuth challenge.
  • bun x tsc --noEmit, test/mcp-size.test.ts, test/mcp-anonymous.test.ts and test/mcp-tools.test.ts pass. So do test/mcp-hints-parity.test.ts, the new test and the edited ones.
  • bun scripts/gate.ts --fix rewrapped comments in one file. I committed the tree after it ran, so that rewrap is in the first commit.

Left out

  • An anonymous client gets no resources capability and no paging for tools/list, because five tools fit in one page.

Try it

curl -s localhost:PORT/mcp -H 'content-type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

Closes #13882

2 earlier comments · 1 event
commented

A client with no token can now start an MCP session over POST /mcp. Type check and the MCP tests I ran pass. I did not run the full suite or bun scripts/design-lint.ts; the forge runs the full check on push.

What changed

  • src/http/mcp.ts has a new openSession step in the anonymous branch.
    • initialize and ping answer 200.
    • tools/list answers with exactly the five TOKEN_FREE_TOOLS and their schemas: report, offer, start, explore, search.
    • Any notifications/* message gets a 202, so notifications/initialized works.
  • I did not widen the free list. It still comes from TOKEN_FREE_TOOLS, the list /.well-known/mcp.json prints.
  • Signed-in and token callers take the old path and still see the full list.
  • Every other anonymous method (for example resources/list) still answers 401 with the -32001 sign-in error and the OAuth challenge. So does a tools/call of any tool that is not token-free.
  • initialize for a stranger returns a short instruction naming the free tools. Its capabilities leave out resources.
  • A signed-in browser cookie still isn't a way in, but a tools/list on it now gets the free list instead of a 401.

Tests

  • New test/mcp-anonymous-session.test.ts covers the initialize, notifications/initialized, ping, tools/list, tools/call explore sequence and the refusals.
  • Three old assertions expected an anonymous tools/list to answer 401: two in test/mcp.test.ts and the probe in test/mcp-discovery-walk.test.ts. They now use resources/list as the not-free method, so they still check the 401 and the OAuth challenge.
  • bun x tsc --noEmit, test/mcp-size.test.ts, test/mcp-anonymous.test.ts and test/mcp-tools.test.ts pass. So do test/mcp-hints-parity.test.ts, the new test and the edited ones.
  • bun scripts/gate.ts --fix rewrapped comments in one file. I committed the tree after it ran, so that rewrap is in the first commit.

Left out

  • An anonymous client gets no resources capability and no paging for tools/list, because five tools fit in one page.

Try it

curl -s localhost:PORT/mcp -H 'content-type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
commented

The fix is pushed (14742b650) and pull #13899 now points at it. The first push reported "the remote end hung up", but it had gone through. I haven't seen the forge's check re-run on it yet.

What changed

  • A request that carries an Authorization header no longer gets the anonymous initialize, ping or tools/list answers. A garbage, expired or revoked token now gets 401 with invalid_token and the plain challenge, as before. That cleared the revoke, expiry, grant and invalid_token failures in oauth-revoke, oauth-grants and oauth-challenge.
  • Three tests asserted that a bare /mcp call with no token gets 401, using initialize or tools/list, which a stranger can now call on purpose. I moved them to resources/list, which stays refused: oauth-challenge, report-discovery and tesla-journey. The tests were wrong here, not the code.
  • I also fixed a missing space in const ANON_HINT ="…".

Checked here

  • The oauth, report-discovery, tesla-journey, mcp and mcp-anonymous-session suites pass (90 tests). bun x tsc --noEmit and bun scripts/gate.ts --fix are clean.
  • The take-expect fast-forward failure from shard 6 passes when I run it alone (5 of 5). I treated it as flaky and changed nothing for it.

Left out

  • Anonymous tools/call for tools outside the five token-free ones still gets the sign-in refusal.
Changes requested — before the latest push

Behaviour matches the issue (boxes 1, 3, 4 are met), but two things to fix in one round:

  1. The authorization-header guard (ctx.request.headers.has("authorization") in the /mcp handler) is a behaviour this pull adds, and no test fails without it. Add a test: a request with a garbage Bearer token and tools/list / initialize / ping must answer 401 with invalid_token, not the anonymous free list. Without it, a revoked or expired token silently degrades to five tools.
  2. tools/list in openSession filters the same list twice (TOOLS.filter(...) then .filter(...) again). Drop one.

Also decide and state (a test or a line in the pull) what a signed-in cookie caller gets. The issue box 2 says a signed-in caller sees the full list; the cookie is deliberately not a way in to /mcp, so it now gets the five-tool list rather than 401. That is probably fine, but pin it with a test so it is intentional.

commented

All three review points are addressed and pushed to #13899 (6863183fe). The MCP and anonymous-session suites pass (52 tests), tsc --noEmit is clean, and gate.ts --fix changed nothing. I did not run the other suites this time.

  • Header guard test: a request with Authorization: Bearer forge_nonsense and initialize, ping or tools/list now has a test in test/mcp-anonymous-session.test.ts. Each must answer 401 with error="invalid_token". It fails without the guard, because those three methods would otherwise get the anonymous answers.
  • Double filter: tools/list in openSession now filters by TOKEN_FREE_TOOLS once, in src/http/mcp.ts.
  • Cookie caller: a signed-in browser session sending tools/list gets 200 with exactly the five token-free tools, not 401 and not the full list. A test in test/mcp.test.ts pins that. I kept this on purpose: a cookie still isn't a way in to /mcp, so it is treated as a stranger. A caller who wants the full list sends a token.

I disagree with nothing in the review.

Approved

All three earlier asks are met on the new head. The header-guard test pins 401 invalid_token for initialize, ping and tools/list when a bad token is sent. tools/list filters by TOKEN_FREE_TOOLS once. The cookie-caller behaviour is pinned by a test in mcp.test.ts. The anonymous path is narrow: 2.0 envelope, string or number id, only initialize, ping, tools/list and notifications. Everything else keeps the sign-in refusal. Nothing was added to README and there are no new dependencies. No new findings.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs