Joel Stevick

Production RAG, line by line

MCP server

The demo app's MCP server lets Claude Code, or any other MCP host, search, ask and read the PDFs a visitor uploaded to their demo account (their row in the demos table, which owns their files and questions). OAuth 2.1 comes from Cloudflare's workers-oauth-provider library and the protocol from the official MCP SDK. The demo app's own code is the consent page, what a grant holds (a grant is the OAuth library's record of what one app may do for one demo account, kept in the Cloudflare KV store OAUTH_KV), the token exchange (RFC 8693), the rate limits, three tools, the files as resources, one prompt and the list of connected apps. The site forwards every MCP and OAuth request to the demo Worker. The public repository's examples/search-loop is an MCP host of one's own: it signs in through the official MCP client SDK and lets Claude call search_documents in a loop.

Pages Function: Site forwarding: the site's Pages Functions forward every MCP and OAuth request to the demo Worker (outside the flow)PAGES FUNCTIONSite forwardingthe site's PagesFunctions forwardevery MCP andOAuth request to thedemo WorkerConnected apps: the demo page lists the allowed apps; Disconnect revokes one (outside the flow)Connected appsthe demo page liststhe allowed apps;Disconnect revokesoneYour machine: Your own host: agentic search: examples/search-loop: Claude calls search_documents in a loop, signed in with the SDK's OAuth client (outside the flow)YOUR MACHINEYour own host:agentic searchexamples/search-loop: Claude callssearch_documents ina loop, signed in withthe SDK's OAuthclientDemo Worker: Discovery documents: RFC 9728 and RFC 8414; tell the host where to register and get a tokenDEMO WORKERDiscoverydocumentsRFC 9728 and RFC8414; tell the hostwhere to register andget a tokenDemo Worker: Client registration: by a client ID metadata document or by dynamic registration (RFC 7591)DEMO WORKERClient registrationby a client IDmetadata documentor by dynamicregistration (RFC7591)Demo Worker: Consent page: the visitor allows docs:read, and docs:write if tickedDEMO WORKERConsent pagethe visitor allowsdocs:read, anddocs:write if tickedDemo Worker: Token endpoint: trades the code for access and refresh tokens; also does token exchangeDEMO WORKERToken endpointtrades the code foraccess and refreshtokens; also doestoken exchangeDemo Worker: Resource server: checks the bearer token and the rate limitsDEMO WORKERResource serverchecks the bearertoken and the ratelimitsDemo Worker: Tools: search_documents, ask_documents and remove_fileDEMO WORKERToolssearch_documents,ask_documents andremove_fileDemo Worker: Resources: each file and page of the account at a demo:// URIDEMO WORKERResourceseach file and page ofthe account at ademo:// URIDemo Worker: Prompt: brief_me asks for a briefing on one file or all of themDEMO WORKERPromptbrief_me asks for abriefing on one file orall of them
Pages Function: the site's server code on Cloudflare Pages, functions/Demo Worker: the private Worker behind the DEMO service binding, demo/src/Your machine: the developer's machine: npm test (vitest, demo/tests/), npm run eval (Node and wrangler dev, evals/) and npm run search (Node, examples/)New to Cloudflare? How these fit together

1,533 of 1,533 lines explained (100%)

Discovery

An MCP host learns where to get a token from two documents: the protected resource metadata (RFC 9728) and the authorization server metadata (RFC 8414). The library publishes both under /.well-known/, built from the site's origin or a local run's, and the site's middleware forwards both paths to the demo Worker.

79 of 79 lines explained (100%)

  1. The discovery documents /.well-known/ 79 of 79 lines explained (100%)

Client registration

An MCP host becomes a known client in one of two ways: by a client ID metadata document at an HTTPS URL it controls, or by dynamic client registration (RFC 7591) when it has no such URL. The library implements both. The demo app turns them on and rate-limits registration by address.

6 of 6 lines explained (100%)

  1. Registering a client Metadata document or dynamic registration 6 of 6 lines explained (100%)

Authorize and consent

The authorization endpoint shows the consent page. The visitor is the demo account whose access key is in their browser's demo cookie. The page says which app is asking and where the access will be sent. A grant (the library's record of what the app may do) always includes docs:read; the visitor ticks docs:write to let the app remove a file.

178 of 178 lines explained (100%)

  1. The consent page /oauth/authorize 178 of 178 lines explained (100%)

Token

The token endpoint, /oauth/token, and token exchange. An MCP host posts the authorization code with its PKCE verifier and gets an access token and a refresh token, and later posts the refresh token for a new pair. The OAuth library stores the grants and the tokens in the KV namespace OAUTH_KV. With token exchange (RFC 8693), the MCP server gets a token for the document store without passing on the visitor's token.

273 of 273 lines explained (100%)

  1. Tokens and grants /oauth/token 12 of 12 lines explained (100%)
  2. Token exchange (RFC 8693) /api/docs-store 261 of 261 lines explained (100%)

Resource server

The resource server. Every MCP request is first counted against the caller's address. The OAuth library then validates the request's bearer token, serveMcp loads the demo account the token was issued for and applies that account's limit, and the official MCP SDK serves the request from a server built for it.

184 of 184 lines explained (100%)

  1. Token validation and limits Every MCP request 184 of 184 lines explained (100%)

Tools

The MCP server's three tools. search_documents runs the pipeline's own retrieval and returns passages without writing an answer. ask_documents runs the whole ask and spends one of the demo account's questions. remove_file deletes a file; it is the MCP server's only write and needs the docs:write scope.

224 of 224 lines explained (100%)

  1. TOOL 1 search_documents 82 of 82 lines explained (100%)
  2. TOOL 2 ask_documents 119 of 119 lines explained (100%)
  3. TOOL 3 remove_file 23 of 23 lines explained (100%)

Resources

The demo account's uploaded files as MCP resources. Each file and each page has a demo:// URI, a long page is read in parts of 20,000 characters, and every file name is cleaned before a model reads it.

135 of 135 lines explained (100%)

  1. Files and pages demo://files/… 135 of 135 lines explained (100%)

Prompt

The MCP server's one prompt, brief_me. Its wording is fixed in code, and it asks the model for a briefing on one file or on all of them, built from search_documents results.

27 of 27 lines explained (100%)

  1. PROMPT brief_me 27 of 27 lines explained (100%)

Connected apps

The demo page's Connected apps list shows the apps the visitor has allowed, read from the library's grants. Disconnect revokes one app's grants and tokens.

116 of 116 lines explained (100%)

  1. Listing and disconnecting apps GET and DELETE /api/grants 116 of 116 lines explained (100%)

Site forwarding

Three of the site's Pages Functions, and its middleware for the discovery documents, forward the MCP endpoint, the OAuth endpoints and the document store to the demo Worker through the DEMO service binding. Each request keeps its own URL, so the issuer and the resource the Worker publishes are on the site's origin.

89 of 89 lines explained (100%)

  1. The Pages Functions /mcp/demo and /oauth/* 89 of 89 lines explained (100%)

Your own host: agentic search

An MCP host of one's own, in the public repository's examples/search-loop/: a program that connects to the demo's MCP server as Claude Code does, signs in with OAuth through the official MCP client SDK, and lets Claude call the server's read-only tool, search_documents, in a loop until it can answer with the file and page of each fact. loop.ts is the tool loop; main.ts is the command line and the sign-in. Its test, demo/tests/search-loop.test.ts, runs the loop against the server in the Worker with a scripted model.

222 of 222 lines explained (100%)

  1. Signing in: the SDK's OAuth client main.ts 121 of 121 lines explained (100%)
  2. The loop: Claude with the server's read-only tools loop.ts 101 of 101 lines explained (100%)
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.