mcp oauth 2.1 handshake lab
Why this matters
Connecting AI agents to remote Model Context Protocol (MCP) servers without pasting long-lived API keys chains six specifications together: RFC 9728 Protected Resource Metadata, RFC 8414 server metadata, Dynamic Client Registration (DCR) vs. HTTPS Client ID Metadata Documents (CIMD), OAuth 2.1 + PKCE (RFC 7636), RFC 8707 Resource Indicators, and refresh-token reuse revocation. Skipping a single check quietly opens cross-server token replay, clickjacked consent, or forged client names.
What this opens up
Standard browser OAuth 2.1 + PKCE delegation for remote MCP tools—letting users grant scoped access to /mcp/docs or /mcp/billing with one approval click, zero copied API secrets, zero server-side client-registration bloat, and automatic token-family revocation if a refresh token is replayed.
How the tests work
- Pick a client identity mode and target resource (
/mcp/docs,/mcp/billing, or/mcp) comparing Unsigned stateless DCR (c_<b64url>), HMAC-Signed DCR (cs_<b64url>.<sig>), and origin-verified HTTPS CIMD (/cimd/valid-client.json). - Run the 6-step handshake (either all at once or hop by hop) from unauthenticated
401 WWW-Authenticatediscovery through WebCryptoS256PKCE authorization, single-use code exchange, and an authorized MCPtools/call. - Fire all 5 live red-team attacks to test spent refresh-token family revocation, bad-PKCE burn-before-check code destruction, RFC 8707 cross-resource token replay (
/mcp/docs->/mcp/billing), opaque-iframe cross-origin DNS rebinding (403), and unsigned DCR name spoofing vs. CIMD validation. - Inspect the Live HTTP Wire Trace and
OAuthLedgerSQLite Durable Object to verify every header, CSPframe-ancestors 'none'consent lock, and hashed token-family state transition.
Hypothesis
Combining RFC 9728 path-scoped discovery, WebCrypto PKCE
S256with burn-before-check codes, RFC 8707resourceaudience binding (/mcp/docsvs./mcp/billing),frame-ancestors 'none'consent CSP, and SQLite refresh-token family revocation prevents cross-resource replay, code interception, and clickjacked consent—while origin-verified CIMD closes unsigned DCR consent-name spoofing.
What confirms it: Authorized handshakes complete all 6 steps cleanly against OAuthLedger, while replaying a /mcp/docs Bearer token on /mcp/billing fails with 401 RFC8707_RESOURCE_MISMATCH, replaying a rotated refresh token revokes the entire token family in SQLite, a bad PKCE verifier burns the code before retry, spoofed CIMD fails validation, and opaque-origin DNS-rebinding POSTs fail with 403.
What bounds or disproves it: Stateless unsigned DCR (c_<b64url>) eliminates client-registration database rows at the cost of allowing any caller to forge an arbitrary client_name offline, and full OAuth redirect flows add latency and consent friction for short-lived agent tool sessions.
Under the hood
MCP OAuth 2.1 Handshake Lab (GitHub) executes the MCP OAuth 2.1 (2025-11-25) handshake end to end inside a Cloudflare Worker (src/router.ts, src/oauth-core.ts) backed by a SQLite Durable Object (OAuthLedger) that stores only SHA-256 digests of codes, access tokens, and refresh tokens:
- Discovery & Client Identity — Serves RFC 9728
/.well-known/oauth-protected-resource(including path-insertion routes for/mcp/docsand/mcp/billing), RFC 8414/.well-known/oauth-authorization-server, valid vs. spoofed Client ID Metadata Documents (/cimd/valid-client.json,/cimd/spoofed-client.json), andPOST /oauth/registercomparing unsigned DCR (c_<b64url>), HMAC-signed DCR (cs_<b64url>.<sig>), and HTTPS CIMD URL matching. - Consent, PKCE & Token Family Lifecycle — Serves
/oauth/authorizewithframe-ancestors 'none'and same-origin CSRF checks, verifies WebCryptoS256PKCE after atomically burning the authorization code in SQLite (preventing brute-force retries), binds tokens to their RFC 8707resourceaudience, and revokes the entiretoken_familieslineage whenever a spent refresh token is replayed. - Protected MCP Execution & DNS-Rebinding Defense — Checks
Origin/Host(403tested live from<iframe sandbox="allow-scripts" src="/probe/rebind-frame">), emits401withWWW-Authenticate: Bearer resource_metadata="...", and enforces audience-scoped JSON-RPC 2.0tools/callacross/mcp,/mcp/docs, and/mcp/billing.
Key terms & further reading
- MCP Authorization (2025-11-25) ↗
- Model Context Protocol specification defining OAuth 2.1 discovery, client identity, and resource-scoped access for remote HTTP MCP servers.
- RFC 9728 Protected Resource Metadata ↗
- Standard
/.well-known/oauth-protected-resourcedocument pointed to byWWW-Authenticateso clients discover the right authorization server per MCP path. - RFC 8414 Authorization Server Metadata ↗
- Discovery document advertising authorize, token, and registration routes plus supported PKCE challenge methods (
S256). - OAuth 2.1 + PKCE (RFC 7636) ↗
- Proof Key for Code Exchange using a WebCrypto SHA-256 challenge so an intercepted authorization code cannot be redeemed without the original verifier.
- RFC 8707 Resource Indicators ↗
- OAuth extension binding access tokens to a single target audience URL so a
/mcp/docstoken cannot be replayed against/mcp/billing. - Dynamic Client Registration (RFC 7591) & CIMD ↗
- Dynamic registration lets agents mint client IDs on the fly, whereas Client ID Metadata Documents tie
client_iddirectly to a verifiable HTTPS JSON document.
Open questions
- How quickly can MCP clients migrate from open Dynamic Client Registration to origin-verified Client ID Metadata Documents (CIMD)?
- Where should consent boundaries sit when one host orchestrates calls across five separate OAuth-protected MCP servers?
Iteration log
- — Deployed full MCP OAuth 2.1 (
2025-11-25) Worker + SQLite Durable Object (OAuthLedger) implementing RFC 9728 path-insertion metadata, RFC 8414 discovery, unsigned/HMAC-signed DCR + valid/spoofed CIMD fixtures,frame-ancestors 'none'consent CSP, WebCryptoS256PKCE burn-before-check codes, RFC 8707 audience checks across/mcp/docsand/mcp/billing, refresh-token family revocation, andOrigin/HostDNS-rebinding guards.
Primitives tested: OAuth 2.1 (PKCE S256), RFC 9728 Protected Resource Metadata, RFC 8414 Authorization Server Metadata, RFC 8707 Resource Indicators, DCR & Client ID Metadata Documents (CIMD), Durable Object SQLite (OAuthLedger).
This is an active lab under /labs/ and stays off the main RSS feed until graduated into a full essay.