Menu

One Cloudflare Worker, two kinds of auth: a stateless MCP server with OAuth 2.1 and API keys
📰

One Cloudflare Worker, two kinds of auth: a stateless MCP server with OAuth 2.1 and API keys

DEV Community: astro·Vladimir Terekhov·about 11 hours ago
#HMNslY9A
#devtoastro#devto#dev#oauth#server
Long article (2228 words) — no AI summary yet.
Reading 0:00
15s threshold

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 ) { // ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.