Joel Stevick

MCP server · Client registration · Metadata document or dynamic registration · stage 1 of 1

Registering a client

Before an MCP host can send a visitor to the consent page, the authorization server must know the host as a client, with its name and the redirect URIs it may use. The demo app turns on both of the library's registration methods. With a client ID metadata document, the client_id is an HTTPS URL that the library fetches. With dynamic client registration (RFC 7591), a client that has no such URL POSTs its metadata to /oauth/register.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/oauth.ts6 / 6 lines on this page explainedlines 77-79,163-165 of 391; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo/src/oauth.ts The OAuth side of the demo Worker: the two registration settings and the per-address limit on POSTs to the library.

serversFor(): REGISTRATION

segment · lines 77–79View on GitHub

The two settings of the authorization server that decide how a client becomes known.

    // Dynamic registration, for a client with no metadata document of its own; open to anyone, so limited by address.    clientRegistrationEndpoint: '/oauth/register',

Dynamic client registration MCP request

WhatSetting clientRegistrationEndpoint to /oauth/register makes the library serve RFC 7591 registration at that path and publish it as registration_endpoint in the authorization server metadata. A client POSTs a JSON metadata document (client_name, redirect_uris, grant types, token_endpoint_auth_method) and receives a client_id. The library stores the record in OAUTH_KV under client:<client_id>.

WhyDynamic registration serves MCP hosts that have no HTTPS URL to publish a metadata document at. The endpoint is unauthenticated by design, so anyone can create client records. For that reason every POST is counted by address (line 164), which caps each address but not the total across addresses, and the consent page labels a dynamically registered client's name as unverified. The library expires a registration after 90 days. A client that keeps using the token endpoint has its registration renewed once half that time has passed.

registration_endpoint: <origin>/oauth/registerlibrary 1.2.4: clientRegistrationTTL default 90 dayslibrary 1.2.4: body over 1 MiB → 413

    clientIdMetadataDocumentEnabled: true,

Client ID metadata documents MCP request

WhatWith clientIdMetadataDocumentEnabled, the library treats a client_id that is an HTTPS URL with a path as the address of the client's metadata document. It fetches the document (10 s timeout, 5 KiB cap), checks that the document's client_id equals the URL, and takes the client's name and redirect URIs from it. The Workers cache keeps the document for as long as the publisher's Cache-Control allows, at most 7 days. Nothing is written to KV for such a client. The authorization server metadata advertises client_id_metadata_document_supported: true.

WhyThe MCP authorization specification (2025-11-25) prefers this method. The client is identified by a domain it controls, so the consent page can say who published it, and the authorization server keeps no registration record for it. Claude Code uses it (client_id https://claude.ai/oauth/claude-code-client-metadata, docs/mcp-oauth-flow.md step 8). The fetch goes from the Worker to a URL the caller chose. The Worker therefore runs with the global_fetch_strictly_public flag (demo/wrangler.toml), which sends every fetch over the public internet, even a fetch to the Worker's own zone, so the URL cannot reach the zone's origin directly. The library treats the flag as its SSRF guard. Without the flag, the library refuses to fetch and advertises client_id_metadata_document_supported: false. The library fetches the document when the client opens /oauth/authorize. It fetches it again when the client POSTs to /oauth/token for a code or refresh grant, unless the cache still holds the document. Both requests are counted by address first.

library 1.2.4: CIMD_FETCH_TIMEOUT_MS = 10,000library 1.2.4: CIMD_MAX_SIZE_BYTES = 5 KiBlibrary 1.2.4: cache at most 7 dayscompatibility_flags = ["global_fetch_strictly_public"]

Explained in Ep 2: The client ID as a link to the app's metadata document From MCP deep dive, Ep 2: OAuth Deep Dive with an MCP Server (OAuth 2.1, PKCE)

oauthRoute(): REGISTRATION

segment · lines 163–165View on GitHub

The per-address limit that runs before the library's open endpoints.

Calls anonymousAllowed() Token validation and limits tooMany Token validation and limits

  // Registration and the token endpoint are open to anyone.  if (request.method === 'POST' && !(await anonymousAllowed(request, env))) return tooMany()

Open endpoints, counted by address MCP request

WhatAny POST that is not to the consent page, the MCP endpoint or the document store is counted by anonymousAllowed. In practice that means /oauth/register and /oauth/token, which is also the revocation endpoint. The count is kept in the D1 table mcp_hits under addr:<SHA-256 of the caller's address>, and a request past 120 in the current UTC minute is refused with 429. The MCP endpoint, the document store and the consent page count against the same per-address budget. GET requests, such as the one for the authorization server metadata, are not counted.

WhyNeither endpoint requires an access token, so the caller's address is the only identity available. The site forwards it as X-Client-Address, taken from CF-Connecting-IP, or as 'unknown' when that header is missing, in which case all such callers share one bucket. The count is taken before the library runs, so one address cannot register clients or call the token endpoint more than 120 times a minute. The limit bounds each address, not the total across many addresses. The demo app leaves registration open so that any MCP host can register itself.

PER_MINUTE_ANONYMOUS = 120429, Retry-After: 60