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 #404

Closed opened this in answer to
Closed without merging

The Claude app and Grok can now attach to /mcp through OAuth. The person signs in with their forge session and picks Watch (ro) or Watch and act (rw), a name, and how long access lasts.

Notes

stopped closed without shipping deploy 66b37d0

opened this

The Claude app and Grok can now attach to /mcp through OAuth. The person signs in with their forge session and picks Watch (ro) or Watch and act (rw), a name, and how long access lasts. What comes back is the same token row that Settings → Access tokens mints, lists and revokes, and /mcp accepts it with no changes on its side.

  • Discovery: /.well-known/oauth-protected-resource (also served at /mcp under that path) and /.well-known/oauth-authorization-server. An unauthenticated /mcp now answers 401 with WWW-Authenticate: Bearer realm="forge", resource_metadata="…".
  • Clients (src/core/oauth.ts, src/http/oauth.ts): RFC 7591 registration for public clients only, keeping the name and up to 5 redirect URIs. A client metadata document also works: a client_id that is an HTTPS URL is fetched with no redirects, a 5 s limit and a 16 KB cap. It must be on a named public host and must name the same client_id.
  • Redirect URIs match byte for byte against what the client registered. HTTPS is allowed anywhere, plain HTTP only to loopback, and fragments never. An unknown client or redirect gets an error on the page and is never redirected to.
  • /oauth/authorize needs a session or edge sign-in, never a token, and the form post must come from the forge itself. The consent page adds only the registered redirect's origin to its form-action, so the browser lets it send the person back.
  • /oauth/token accepts PKCE S256 only. A code lasts two minutes and the first attempt to redeem it uses it up, whether or not that attempt succeeds. It must also come with the client and redirect it was issued for. If the person picked a lifetime, it becomes expiresAt and the response carries expires_in.
  • Supporting changes: migration 39 adds oauth_client and oauth_code. Unused codes are deleted on the existing worker tick. createToken in actions/users.ts takes an optional expiry, and via: "oauth" is recorded on the token.created event.

Not done:

  • No refresh tokens. A token lasts until revoked or until its lifetime runs out, and then the client asks again.
  • No DNS check before fetching a metadata document. IP literals and localhost are refused, but a public name that resolves to a private address would still be fetched.
  • No pruning of registered clients. Registration is rate-limited to 60 a minute across the whole forge instead.

docs/MCP.md explains how to attach from Claude (Settings → Connectors → Add custom connector, URL https://<forge>/mcp) and from Grok (grok.com/connectors → Custom).

test/oauth.test.ts is new and added to the forge.json check, which passes in full. It covers the full PKCE flow with a fake client, using the RFC 7636 example verifier. It checks that an rw bearer minted this way calls apps and can act, and that an ro one is refused writes. It also covers using a code twice, a wrong verifier or client, exact redirect matching, consent without a person at a browser, escaping on the consent page, and metadata documents.

To try it on the preview: curl -i -XPOST <preview>/mcp shows the hint. Adding the preview's /mcp as a custom connector in Claude should then go through consent and add a token to Settings → Access tokens.

🤖 Generated with Claude Code

Closes #384

commented

Operator: main moved under this branch (forge.json's check command was shortened, among others). Merge main into the branch, resolve the conflicts keeping this change's intent, run the check locally, and push.

commented

Parked by the crew: the preview's deploy failed and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs