Skip to content

feat(mcp): accept Client ID Metadata Documents (00511) - #2274

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-oauth-cimd
Nov 10, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-oauth-cimd

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PR 5 of the MCP over OAuth plan (#2141). Stacked on #2272 (the consent page), which is on main: merge #2272 first. Until then this diff also shows #2272's commit. It is still behind MCP_OAUTH_ENABLED.

What changes: an app can now name itself by an https URL serving a JSON document about it, instead of registering first. These are Client ID Metadata Documents (CIMD).

  • The MCP authorization spec 2025-11-25 says servers SHOULD support them, and 2026-07-28 deprecates dynamic registration in their favour.
  • Claude and ChatGPT use one when the server advertises it, and Claude Code always has one (https://claude.ai/oauth/claude-code-client-metadata).
  • Anthropic's connector docs recommend CIMD over registration for directory traffic: registration makes a new client on every fresh connection, while CIMD gives one client per app. That is why this comes before the launch.
  • Dynamic registration stays for the apps that don't use CIMD.

What's built

  • Metadata: client_id_metadata_document_supported: true, next to "none" in token_endpoint_auth_methods_supported. Claude needs both before it uses CIMD.
  • What counts as a document URL (isClientIdUrl): https, a path, and no query, fragment, credentials, other port or ./.. segments. Anything else is a registered client id.
  • Fetching it (web/src/lib/mcp-oauth/safe-fetch.ts, the guards of the API server's safe-fetch.js ported to the web for JSON):
    • every address the host resolves to must be public (the private, local, shared, documentation and reserved ranges are blocked, IPv4 and IPv6). The check runs in the connection's own DNS lookup, so a name can't resolve to a public address for the check and a private one for the request;
    • https on port 443 only, no redirects followed, 5 seconds, 10 KB, JSON content type.
  • Checking it (web/src/lib/mcp-oauth/cimd.ts):
    • client_id must equal the URL exactly; even a trailing slash is refused;
    • redirect_uris follow the same rules as registration (1 to 5; https or loopback; no custom schemes);
    • it must be a public client (token_endpoint_auth_method absent or none);
    • grant_types may only be authorization_code and refresh_token, and response_types only code;
    • the name is cleaned, and only six descriptive fields are kept, never fetched or shown.
  • Caching it: kept as a cimd client, by its URL, for as long as its Cache-Control allows, clamped to between 5 minutes and a day (an hour when it says nothing), then fetched again. A document that can't be fetched or doesn't check out leaves no client, and the request ends on Ansvisor's "unknown app" page.
  • At /api/oauth/authorize: a URL client id goes through this. The request's redirect URI must be one the document lists; loopback matches on any port, which Claude Code needs.
  • The token endpoint finds the cached client by its URL, as for any public client.
  • The consent page names a CIMD app by its URL's host, as the spec says, not by the name the document gives itself:
    • claude.ai is recognised as Claude, so Claude Code shows "Recognised app" and "You'll go back to localhost (this computer)";
    • any other host is the app's name, marked "Not checked by Ansvisor".

Migration 00511_mcp_oauth_cimd.sql

  • oauth_clients.kind may now be cimd.
  • Two new columns: metadata_fetched_at and metadata_expires_at.
  • A new check, oauth_clients_cimd_check: a cimd client must be https, public (none, no secret) and cached.
  • I applied it after 00510 on a scratch PostgreSQL 16 database, twice (it can be re-run). The checks refused a cimd row without the cache columns, a confidential cimd client and an unknown kind.
  • supabase/build-schema.sh was run, and schema.sql is committed. There is no brand_id and no new function.

Tests (web/src/lib/mcp-oauth/cimd.test.ts, 52 tests)

  • which client ids are document URLs (12 cases);
  • Claude Code's own document, as Claude's docs describe it, is accepted;
  • 9 refused documents (a trailing slash in client_id, an http or custom-scheme redirect, no redirects, private_key_jwt, client_credentials, token, not an object);
  • the cache: fetched once and reused while fresh, clamped to 5 minutes and 24 hours, fetched again and updated in place when stale;
  • fetch failures and refused documents leave no client;
  • the safe fetch: JSON and its max-age; an http or other-port URL, a redirect, 404, HTML, bad JSON and over 10 KB are each refused; 13 address cases (public, loopback, RFC 1918, link-local and metadata 169.254.169.254, CGNAT, ::1, v4-mapped loopback, ULA, link-local v6); the lookup refuses a name with any private address;
  • the metadata advertises it;
  • /api/oauth/authorize with Claude Code's URL and localhost:3118, a redirect URI the document doesn't list (Ansvisor's page), and a document that can't be fetched (unknown_client);
  • the consent naming for CIMD apps.

The OAuth suites from #2262 and #2272 still pass unchanged.

No Help or docs change: nothing is switched on. Help and docs come with PR 7.

Related issue

Part of #2141

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd web && npx vitest run src/lib/mcp-oauth/cimd.test.ts.
  2. With MCP_OAUTH_ENABLED=true and 00510 and 00511 applied, open `/api/oauth/authorize?response_type=code&client_id=''https://claude.ai/oauth/claude-code-client-metadata&redirect_uri=http://localhost:3118/callback&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256':''
    • the consent page names Claude, recognised;
    • oauth_clients has a cimd row for that URL with metadata_expires_at set.
  3. In Claude Code, claude mcp add --transport http ansvisor <dev URL>/api/mcp, then /mcp to sign in: Claude Code uses its CIMD instead of registering.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change) — see help/README.md: nothing is switched on; Help comes with PR 7

Checks run:

  • yarn build passes (with CI's placeholder env).
  • Tests (after merging the updated consent branch, which includes upstream/main):
    • web: src/components/oauth, src/lib/mcp-oauth, src/app/api/oauth, src/app/api/mcp, src/lib/mcp, both mcp-auth tests, embed-hints, subscription-guard-coverage and brand-access-functions: 72 files, 694 tests passed;
    • server tests that read the migrations (brand-access-policies, -schema, -account-tables, demo/coverage): 4 files, 74 tests passed.
  • After merging current main (f947d77, with the Slack and HubSpot migrations): schema.sql was rebuilt, its only conflict. The branch's test files, Help, yarn typecheck and the 4 server migration tests pass again.

Generated by Claude Code

PR 5 of MCP over OAuth (ansvisor#2141), stacked on feature/mcp-oauth-consent.
Still behind MCP_OAUTH_ENABLED.

An app can name itself by an https URL serving a JSON document about it,
instead of registering (MCP authorization spec 2025-11-25, "SHOULD";
2026-07-28 deprecates dynamic registration in its favour). Claude and
ChatGPT use one when the server advertises it, Claude Code always has
one, and Anthropic recommends it over registration for directory
traffic: one client per app, not one per connection.

- The authorization server metadata says client_id_metadata_document_
  supported, beside "none" among the token endpoint auth methods.
- At /api/oauth/authorize, a client id that is a URL (https, a path, no
  query, fragment, credentials or dot segments) names its document. It
  is fetched safely (safe-fetch.ts: every address the host resolves to
  must be public, checked in the connection's own lookup; no redirects;
  5 s; 10 KB; JSON only), checked (client_id equal to the URL exactly,
  redirect URIs as for registration, a public client, code with
  authorization_code and refresh_token only) and kept as a 'cimd'
  client for as long as its Cache-Control allows, five minutes to a
  day. One that can't be fetched or doesn't check out ends on
  Ansvisor's page.
- The consent page names such an app by its URL's host (Claude for
  claude.ai), not by the name the document gives itself.
- 00511: oauth_clients kind 'cimd', metadata_fetched_at and
  metadata_expires_at; a cimd client must be https, public and cached.

Part of ansvisor#2141.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 10, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSRF: checkFetchUrl/isClientIdUrl accept an IP-literal host (https://10.0.0.1/x, https://169.254.169.254/x, https://[fd00::1]/x), and Node skips the custom lookup for IP hosts, so publicOnlyLookup's private-range check never runs; anyone can make the server GET internal addresses via /api/oauth/authorize?client_id=. Please reject hosts where net.isIP(hostname) is non-zero (or check them against the blocklist), and make the 5 s limit an overall timeout (AbortSignal.timeout), not the socket idle one.

… 5 s

A client ID URL with an IP host (https://10.0.0.1/x, https://169.254.169.254/x,
https://[fd00::1]/x) connects without a DNS lookup, so publicOnlyLookup's
private-range check never ran. Both isClientIdUrl and checkFetchUrl now refuse
any IP host; the URL parser has already normalised 2130706433 and 0x7f.1.

The 5 s limit was the socket's idle timeout, which a server sending a byte at
a time keeps resetting. It is now one deadline for the whole request
(AbortSignal.timeout), and a response cut off part way fails instead of
resolving with a partial body.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

Copy link
Copy Markdown
Collaborator Author

@gkhngyk thanks, both fixed in 2a36936:

  • IP hosts: isClientIdUrl and checkFetchUrl now refuse any host where net.isIP is non-zero (brackets stripped for IPv6; the URL parser has already turned 2130706433 / 0x7f.1 into 127.0.0.1). Public IPs are refused too, since a client ID document lives at a name.
  • Timeout: httpsGet now uses one AbortSignal.timeout(5 s) for the whole request instead of the socket idle timeout. A response cut off part way now fails instead of resolving with a partial body.
  • Tests: new safe-fetch.test.ts. It covers the IP cases you listed plus ::1, ::ffff:10.0.0.1, 2130706433, 0x7f.1 and 8.8.8.8. Against a local server it checks a hang, a body trickling a byte every 20 ms, an over-10 KB body, and that the default lookup still blocks localhost. The same IP cases are added to the isClientIdUrl table.
  • Checks: OAuth suites 197/197, typecheck and lint clean.

Generated by Claude Code

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gkhngyk
gkhngyk merged commit 27369b2 into ansvisor:main Nov 10, 2026
12 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 11, 2026
PR 7 of the MCP over OAuth plan (#2141). Merge after #2274 and after
00510 and 00511 are applied in production.

- App sign-in is on unless MCP_OAUTH_ENABLED=false (the kill switch;
  0, off and no too). It still needs an https issuer, so a self-hosted
  install on plain http stays off.
- GET /.well-known/openai-apps-challenge serves OPENAI_APPS_CHALLENGE as
  text/plain, no-store, trimmed; 404 while unset.
- The new key's Claude Desktop line says Claude can sign in instead.
- Tool descriptions, the role refusal and get_site_audit_quota's error
  no longer assume an API key; the MCP coach skill likewise.
- Gemini CLI: --scope user, so the key isn't written into the project's
  .gemini/settings.json; the docs keep $ANSVISOR_TOKEN a reference and
  say it writes url with type http, and how Gemini CLI can sign in.
- Help (approved, regenerated), a what's-new entry (not approved), and
  docs: the MCP guide's app sign-in section, authentication, env vars.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants