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

OAuth for the MCP: the Claude app and Grok connectors can attach, minting the same ro/rw tokens #548

Closed opened this in answer to
Closed without merging

The forge now has a minimal OAuth server, so the Claude app and Grok connectors can attach to /mcp and are handed an ordinary ro or rw forge token. The new test/oauth.test.ts and the existing test/mcp.test.ts pass. tsc could not run here (no nodemodules, so no Bun types), so it is unchecked.

Notes

stopped closed without shipping deploy e05406b

opened this

The forge now has a minimal OAuth server, so the Claude app and Grok connectors can attach to /mcp and are handed an ordinary ro or rw forge token. The new test/oauth.test.ts and the existing test/mcp.test.ts pass. tsc could not run here (no node_modules, so no Bun types), so it is unchecked. test/migrate.test.ts fails the same way with my changes stashed. I did not run the full forge.json check; I only added oauth to its shard list.

What changed

  • Discovery:
    • /.well-known/oauth-protected-resource and /.well-known/oauth-authorization-server are served.
    • The 401 on /mcp now carries resource_metadata="<origin>/.well-known/oauth-protected-resource".
  • Registration: POST /oauth/register (RFC 7591) stores the client id, name and redirect URIs. An https:// client id is also accepted as a client metadata document.
  • Sign-in and consent:
    • GET/POST /oauth/authorize needs a browser session; a bearer token is sent to sign-in.
    • The consent page asks for a name and Watch (ro) or Act (rw). admin is never offered.
    • The POST is same-origin checked.
  • Token exchange:
    • POST /oauth/token takes authorization_code with PKCE S256 and mints a normal token row via createToken, so it shows up and can be revoked under Settings → Access tokens.
    • A code lives 10 minutes and is single-use; a wrong verifier burns it.
    • The token has no expiry unless the client sends expires_in (at most a year).
  • Redirects and CORS: redirect URIs are exact-match only, https or http on localhost. The discovery, register and token endpoints answer CORS preflight.
  • Where it lives:
    • New: src/core/oauth.ts, src/http/oauth.ts, test/oauth.test.ts.
    • Edited: a schema migration (oauth_client, oauth_code), the 401 in src/http/mcp.ts, route wiring in src/http/server.ts, and docs/MCP.md.
  • Tests:
    • A fake client runs the full dance.
    • An rw bearer calls apps and tell.
    • A ro bearer reads and is refused writes.
    • Also covered: single-use codes, PKCE mismatch, expired codes, expires_in, exact-match redirects, cross-origin refusal, HTML escaping and the metadata document rules.
  • Docs: docs/MCP.md has attach steps for Claude and Grok. I wrote those from the issue text and did not try them against either app.

Left out, deliberately

  • Refresh tokens: a non-expiring token needs none, and an expiring one is asked for again.
  • Client secrets: public clients only.
  • Redirect after approval: the page CSP has form-action 'self', which browsers also apply to a redirect after a form POST. So the consent page answers 200 with a "Return to …" link rather than a 302.
  • Metadata document fetching: it is narrow (https, named host, no IP literals or localhost, no redirects, 16 KB, 3 s). It does not resolve DNS first, so a public name that points at a private address is not blocked. The client row is cached with no refresh.
  • Registration cap: unauthenticated registration is capped at 5000 clients and has no per-IP limit.

To try it Run the forge, then curl -i -X POST $FORGE/mcp and follow the resource_metadata hint. In the Claude app, add a custom connector with https://<forge>/mcp.

🤖 Generated with Claude Code

Closes #384

commented

The forge can now act as a small OAuth server for the MCP, so the Claude app and Grok connectors can attach and are handed an ordinary ro or rw forge token. The failed check was a type error in src/http/oauth.ts: the token endpoint typed its parsed form as the global FormData, which the repo's strict settings reject. It now uses the repo's Form type from web/form.ts. bun x tsc --noEmit is clean and test/oauth.test.ts and test/mcp.test.ts pass (23 tests). I did not run the rest of the check's test shards locally.

What changed

  • Discovery: /.well-known/oauth-protected-resource and /.well-known/oauth-authorization-server are served, and the 401 on /mcp carries a resource_metadata hint.
  • Registration: POST /oauth/register (RFC 7591) stores the client id, name and redirect URIs, and an https:// client id is accepted as a client metadata document.
  • Authorize: /oauth/authorize needs a browser session, and the consent page asks for a name and Watch (ro) or Act (rw). admin is never offered.
  • Token: POST /oauth/token takes authorization_code with PKCE S256 and mints a normal token row. That row appears and can be revoked under Settings → Access tokens, and /mcp accepts it unchanged.
  • Redirects: redirect URIs are https or http on localhost and are matched exactly. A code is single-use and lives ten minutes. The token does not expire unless the client sends expires_in (at most a year).
  • Tests:
    • Full dance: a fake client runs it end to end.
    • rw bearer: calls apps and tell.
    • ro bearer: reads and is refused writes.
    • Refusals: the other tests cover single-use codes, PKCE mismatch, expired codes, exact-match redirects, cross-origin refusal and the metadata-document rules.
  • Docs: docs/MCP.md has attach steps for Claude and Grok, written from the issue text and not tried against either app.

Left out, deliberately

  • Refresh tokens and client secrets: only public clients are supported, and a non-expiring token needs no refresh.
  • Redirect after approval: the consent page answers 200 with a "Return to …" link rather than a 302, because the page's form-action 'self' rule would block a redirect to another site.
  • Metadata document fetch: it is https only, a named host, no redirects, 16 KB and 3 seconds. It does not resolve DNS first, so a public name pointing at a private address is not blocked.
  • Registration limits: registration is capped at 5000 clients and has no per-IP limit.

To try it: run the forge, curl -i -X POST $FORGE/mcp and follow the resource_metadata hint. In the Claude app, add a custom connector with https://<forge>/mcp.

🤖 Generated with Claude Code

commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs