Skip to content

feat(mcp): OAuth 2.1 (DCR + PKCE) for claude.ai / ChatGPT connectors on /api/agent/mcp - #85

Merged
ralyodio merged 1 commit into
masterfrom
mcp-oauth
Nov 11, 2026
Merged

ralyodio merged 1 commit into
masterfrom
mcp-oauth

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

Makes the b1dz MCP server (https://b1dz.com/api/agent/mcp) listable in the Claude Connectors Directory and usable as a ChatGPT app: OAuth 2.1 per the MCP authorization spec, ported from profullstack/threatcrush#450 (Supabase variant as in smshub#140 / crawlproof.com#449).

What

  • 401 on any unauthenticated request (initialize included) with WWW-Authenticate: Bearer resource_metadata="https://b1dz.com/.well-known/oauth-protected-resource", scope="mcp"; a bad credential adds error="invalid_token". GET is 401 signed out, 405 signed in.
  • Discovery: /.well-known/oauth-protected-resource (+ path-suffixed form), resource exactly https://b1dz.com/api/agent/mcp; /.well-known/oauth-authorization-server (+ suffix) and /.well-known/openid-configuration.
  • POST /api/oauth/register: RFC 7591 DCR, public clients only (none); https redirects, http loopback on any port (RFC 8252).
  • /oauth/authorize: consent page naming the client. Signed out: /login?next= round trip (password or CoinPay, which binds by sub). The user picks read-only (default) or a capped crypto trading budget per day/week/month. The decision posts JSON to /api/oauth/authorize with the cookie session (same origin only) or a Bearer Supabase session, and the page navigates itself (a form POST that 303s to claude.ai would be blocked by our CSP form-action). iss (RFC 9207) on every redirect.
  • POST /api/oauth/token: form-encoded (or JSON) authorization_code with PKCE S256, single-use 5-minute code bound to client + redirect + user; rotating refresh tokens (90 d), access tokens b1dz_oat_ (1 h). POST /api/oauth/revoke (RFC 7009).
  • Same rights model as agent keys: each grant gets its own agent_tokens row (" (connected app)"), created at code exchange with the scopes/budget chosen on consent, and the access token resolves to the same AuthedAgent a b1dz_agent_ key does, so every tool still goes through agent-trade.ts (scope, allowlist, per-token budget, audit log). It appears with the user's agent tokens in Settings; revoking either side ends the grant. OAuth tokens are accepted only by the MCP route.
  • Existing b1dz_agent_ keys work unchanged.
  • Tools: title + readOnlyHint/destructiveHint/idempotentHint/openWorldHint annotations (place_order is destructive + open-world); tools/list carries resultType/ttlMs/cacheScope; protocol version negotiation (2025-06-18, 2025-03-26, 2024-11-05); instructions.
  • Proxy: /oauth, /register, /authorize, /token are public, so an MCP client's DCR fallback gets a 404, never the HTML login page.
  • Migration 20261111150000_mcp_oauth.sql: oauth_clients, oauth_codes, oauth_tokens (hashes only, RLS on, service role only).

Tests

  • apps/web/src/app/api/oauth/__tests__/oauth-flow.test.ts (16): discovery, 401 + resource_metadata (initialize and GET), invalid_token, old b1dz_agent_ key still works, DCR + refusals, consent auth (signed out, cross-site cookie refused, Bearer session), PKCE required, deny, loopback port, full code -> token -> tools/list -> refresh rotation -> replay refused -> revoke, read-only default vs granted budget, wrong verifier / reused code / redirect mismatch, revoking the agent token ends the grant, hashes only.
  • route.test.ts updated for the 401, annotations and cache fields.
  • pnpm typecheck (68 tasks) and pnpm test green locally; web suite 190 passed.

🤖 Generated with Claude Code

… can sign in to /api/agent/mcp

Any unauthenticated request to /api/agent/mcp is now 401 with
WWW-Authenticate: Bearer resource_metadata=... Existing b1dz_agent_ keys
work unchanged. Each OAuth grant is backed by its own agent_tokens
sub-account (read-only unless the user grants a capped trading budget on
the consent screen), so an OAuth token never has wider rights than an
agent key. Ported from profullstack/threatcrush#450.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

ThreatCrush Security Scan

7 finding(s)

MEDIUM: 7

Severity Rule Location
MEDIUM js-timing-unsafe-mac-compare apps/daemon/src/user-config.ts:491
MEDIUM js-unescaped-html-sink apps/web/src/app/layout.tsx:126
MEDIUM js-unescaped-html-sink apps/web/src/app/page.tsx:435
MEDIUM js-open-redirect apps/web/src/app/store/install-button.tsx:53
MEDIUM js-unescaped-html-sink apps/web/src/components/pit-footer.tsx:17
MEDIUM js-dynamic-code-execution packages/core/src/runtime-cache.ts:248
MEDIUM js-dynamic-code-execution packages/core/src/runtime-cache.ts:268

Snippets are redacted; ThreatCrush never prints matched credential material.

@ralyodio
ralyodio merged commit 99c3af4 into master Nov 11, 2026
7 checks passed
@ralyodio
ralyodio deleted the mcp-oauth branch November 11, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant