AI Focus

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

  1. 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).
  2. Run the 6-step handshake (either all at once or hop by hop) from unauthenticated 401 WWW-Authenticate discovery through WebCrypto S256 PKCE authorization, single-use code exchange, and an authorized MCP tools/call.
  3. 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.
  4. Inspect the Live HTTP Wire Trace and OAuthLedger SQLite Durable Object to verify every header, CSP frame-ancestors 'none' consent lock, and hashed token-family state transition.
Live 6-step MCP OAuth 2.1 (2025-11-25) workbench and 5 red-team attacks testing RFC 9728 protected-resource metadata, RFC 8414 discovery, Unsigned/Signed DCR vs. HTTPS CIMD, WebCrypto PKCE S256 burn-before-check codes, RFC 8707 audience binding, and SQLite refresh-token family revocation. Open mcp oauth 2.1 handshake lab full screen ↗

Hypothesis

Combining RFC 9728 path-scoped discovery, WebCrypto PKCE S256 with burn-before-check codes, RFC 8707 resource audience binding (/mcp/docs vs. /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/docs and /mcp/billing), RFC 8414 /.well-known/oauth-authorization-server, valid vs. spoofed Client ID Metadata Documents (/cimd/valid-client.json, /cimd/spoofed-client.json), and POST /oauth/register comparing unsigned DCR (c_<b64url>), HMAC-signed DCR (cs_<b64url>.<sig>), and HTTPS CIMD URL matching.
  • Consent, PKCE & Token Family Lifecycle — Serves /oauth/authorize with frame-ancestors 'none' and same-origin CSRF checks, verifies WebCrypto S256 PKCE after atomically burning the authorization code in SQLite (preventing brute-force retries), binds tokens to their RFC 8707 resource audience, and revokes the entire token_families lineage whenever a spent refresh token is replayed.
  • Protected MCP Execution & DNS-Rebinding Defense — Checks Origin/Host (403 tested live from <iframe sandbox="allow-scripts" src="/probe/rebind-frame">), emits 401 with WWW-Authenticate: Bearer resource_metadata="...", and enforces audience-scoped JSON-RPC 2.0 tools/call across /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-resource document pointed to by WWW-Authenticate so 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/docs token 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_id directly 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, WebCrypto S256 PKCE burn-before-check codes, RFC 8707 audience checks across /mcp/docs and /mcp/billing, refresh-token family revocation, and Origin/Host DNS-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.

More in agent discovery, auth & protocol bridges

Connected labs

← agent discovery, auth & protocol bridges · All labs