Skip to content

Let the Worker reach its own JWKS - #7

Merged
afternoon merged 1 commit into
mainfrom
claude/mcp-https-redirect-uri-cc9b65
Aug 31, 2026
Merged

afternoon merged 1 commit into
mainfrom
claude/mcp-https-redirect-uri-cc9b65

Conversation

@afternoon

Copy link
Copy Markdown
Owner

claude mcp list failed once authorization succeeded:

Failed to connect — HTTP 500: Streamable HTTP error: Error POSTing to endpoint:
{"status":500,"unhandled":true,"message":"HTTPError"}

What was wrong

Verifying an access token reads the authorization server's key set over HTTP — @better-auth/mcp fetches ${BETTER_AUTH_URL}/api/auth/jwks, which on this deployment is this same Worker.

By default a Worker's fetch to its own zone is routed to the zone's origin server, skipping every Worker mapped to the URL. There is no origin behind this hostname but the Worker itself, so that subrequest came back as a Cloudflare error page rather than a key set.

A non-OK answer there throws a plain Error("Jwks failed: …"). requireMcpAuth hands verification errors to toChallengeResponse, which re-throws anything it cannot read as an authorization failure — so it escaped the route, and every call carrying a valid token failed. Confirmed by reproducing each shape against the built Worker: an unreachable JWKS host is swallowed into a clean 401, while a JWKS URL that answers non-OK throws exactly this.

The change

  • wrangler.jsonc — add the global_fetch_strictly_public compatibility flag. It sends a same-zone subrequest back through Cloudflare's front door, where it reaches this Worker (the loop limit is 16 invocations; this is one). Nothing else here fetches its own origin, so that is the whole of the flag's reach. worker-configuration.d.ts regenerated with wrangler types.
  • src/server/mcpFailure.ts — the reason this took a morning instead of a minute: an exception escaping a server route is answered by the runtime as an opaque HTTPError, message withheld, and that is all an agent can show its operator. The route now answers such a failure itself: a JSON-RPC error naming it, logged where wrangler tail shows it, and a 503 rather than a 401 — the token was never the problem, and a challenge would only send somebody back through sign-in to fail the same way.

Tests

src/server/mcpFailure.test.ts pins the envelope, the 503, and the logging, using the real Jwks failed: Not Found message. bun run check, bun run test:unit (443 passed) and bun run test:e2e (14 passed) are green; the full OAuth + MCP flow was also driven against a vite build + wrangler dev Worker with the flag on.

The flag itself only changes behaviour on Cloudflare's edge, so nothing local can prove it — verification is claude mcp list against production once this deploys. If it still fails, the new error body will name the cause instead of hiding it.


Generated by Claude Code

An authorized MCP call died with a 500 whose body said only

    {"status":500,"unhandled":true,"message":"HTTPError"}

Verifying an access token reads the authorization server's key set over
HTTP: `@better-auth/mcp` fetches `${BETTER_AUTH_URL}/api/auth/jwks`, which
on this deployment is this same Worker. By default a Worker's fetch to its
own zone is routed to the zone's *origin server*, skipping every Worker
mapped to the URL — and there is no origin behind this hostname but the
Worker, so the subrequest came back as a Cloudflare error page rather than
a key set. A non-OK answer there throws a plain `Error`, which
`requireMcpAuth` cannot read as an authorization failure, so it is
re-thrown rather than turned into a challenge, and every call with a valid
token failed. `global_fetch_strictly_public` sends such a request back
through Cloudflare's front door, where it reaches this Worker; the loop
limit is 16 invocations and this is one. Nothing else here fetches its own
origin, so that is the whole of the flag's reach.

The message being withheld is what turned a one-line cause into a
morning's work: an exception escaping a server route is answered by the
runtime as an opaque `HTTPError`, since a framework cannot know what is
safe to say, and that is all an agent could show. `mcpFailure.ts` wraps
the route so the endpoint says it itself — a JSON-RPC error naming the
failure, logged where `wrangler tail` will show it, and a 503 rather than
a 401, because the token was never the problem and a challenge would only
send somebody back through sign-in to fail the same way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MMUJEj5inbsz7XKDrCU6Aa
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
htmlcsspdf 1785811 Commit Preview URL

Branch Preview URL
Aug 31 2026, 09:45 PM

@afternoon
afternoon marked this pull request as ready for review August 31, 2026 21:47
@afternoon
afternoon merged commit 9aaa61f into main Aug 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants