Repository navigation
Let the Worker reach its own JWKS - #7
Merged
Merged
Conversation
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
Deploying with
|
| 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
marked this pull request as ready for review
August 31, 2026 21:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
claude mcp listfailed once authorization succeeded:What was wrong
Verifying an access token reads the authorization server's key set over HTTP —
@better-auth/mcpfetches${BETTER_AUTH_URL}/api/auth/jwks, which on this deployment is this same Worker.By default a Worker's
fetchto 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: …").requireMcpAuthhands verification errors totoChallengeResponse, 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 theglobal_fetch_strictly_publiccompatibility 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.tsregenerated withwrangler 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 opaqueHTTPError, 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 wherewrangler tailshows 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.tspins the envelope, the 503, and the logging, using the realJwks failed: Not Foundmessage.bun run check,bun run test:unit(443 passed) andbun run test:e2e(14 passed) are green; the full OAuth + MCP flow was also driven against avite build+wrangler devWorker with the flag on.The flag itself only changes behaviour on Cloudflare's edge, so nothing local can prove it — verification is
claude mcp listagainst production once this deploys. If it still fails, the new error body will name the cause instead of hiding it.Generated by Claude Code