I wanted two kinds of clients to use the same MCP URL. Claude and ChatGPT add a remote server as a connector and expect to sign in with OAuth. Developer tools such as Claude Code, Codex and Cursor are happy with a bearer API key in a header. I did not want two endpoints, two sets of tools or a second Worker.
This is how it fits together in one Cloudflare Worker that also serves an Astro site. All code is copied from the repository, which is MIT licensed. The versions are Astro 7, @astrojs/cloudflare 14, @modelcontextprotocol/server 2.4.0 and @cloudflare/workers-oauth-provider 1.2.4.
One Worker, three jobs
The Worker entry is short. Astro's fetch handler goes in as the default handler, and the OAuth provider sits in front of it.
// src/worker.ts const astro : ExportedHandler < Env > = { fetch : handle } ;
export default { fetch ( request , env , context ) { return handleOAuthRequest ( request , env , context , astro ) ; } , async scheduled ( _controller , env ) { // ...…