Skip to content

fix(mcp/http): advertise Bearer scheme on 401 (RFC 6750)#22

Merged
PauGuirao merged 1 commit into
developfrom
fix/mcp-www-authenticate
May 15, 2026
Merged

fix(mcp/http): advertise Bearer scheme on 401 (RFC 6750)#22
PauGuirao merged 1 commit into
developfrom
fix/mcp-www-authenticate

Conversation

@PauGuirao
Copy link
Copy Markdown
Collaborator

Summary

  • Without WWW-Authenticate: Bearer realm="zernio-mcp" on 401, MCP clients like mcp-remote interpret the 401 as "OAuth required" and probe /.well-known/oauth-authorization-server. That 404s with plain-text "Not Found" and surfaces to the user as HTTP 404: Invalid OAuth error response: SyntaxError: Unexpected token 'N', 'Not Found' is not valid JSON.
  • This server uses static API-key auth, not OAuth. Per RFC 6750 we should advertise the bearer scheme on 401 so clients send their static token and skip OAuth discovery.
  • Applied to both 401 paths: the SSE handler (handle_sse) and the Streamable HTTP wrapper (_send_json_error, used by StreamableHTTPAuthHandler).

Why this surfaced

A Windows user (Claude Desktop + mcp-remote) reported HTTP 404: Invalid OAuth error response even after pasting our documented mcp-remote config. The root cause was a missing Authorization header (env-var indirection broken on Windows), but the 404 came from this WWW-Authenticate gap. Fixing it means even malformed bearer setups fail cleanly with the "Missing API key" JSON we already return, instead of leading clients into an OAuth-discovery dead end.

Test plan

  • curl -i https://mcp.zernio.com/mcp returns 401 with WWW-Authenticate: Bearer realm="zernio-mcp"
  • curl -i https://mcp.zernio.com/sse returns the same
  • curl -i -H 'Authorization: Bearer not-a-real-key' https://mcp.zernio.com/mcp returns 401 {"error":"Invalid API key"} with the same header
  • npx mcp-remote@latest https://mcp.zernio.com/mcp --header "Authorization: Bearer sk_<real_key>" connects and lists tools without OAuth-discovery noise

🤖 Generated with Claude Code

Without `WWW-Authenticate: Bearer realm="..."` on 401 responses, MCP
clients like `mcp-remote` fall back to OAuth discovery, probe
`/.well-known/oauth-authorization-server`, hit a 404 with plain-text
"Not Found", and surface a misleading JSON parse error to the user.

We use static API-key auth, not OAuth. Advertising the bearer scheme
tells spec-compliant clients to send the static Bearer token they
already have and skip OAuth discovery entirely. Applied on both the SSE
and Streamable HTTP 401 paths.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@PauGuirao PauGuirao merged commit c8329dd into develop May 15, 2026
4 checks passed
@PauGuirao PauGuirao deleted the fix/mcp-www-authenticate branch May 15, 2026 11:22
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.

1 participant