Return 401 challenge for unauthenticated /mcp requests - #6
Merged
Conversation
An /mcp request without a Bearer token built the MCP server with a nil server_context. MCP::ServerContext wraps it and delegates unknown methods via method_missing, so tools/list still returned 200 while any actual tool call failed with an opaque JSON-RPC -32603: undefined method 'imap_config' for an instance of MCP::ServerContext Reject the request up front with a 401 and the WWW-Authenticate resource_metadata pointer, so MCP clients re-run the OAuth flow instead. Extracted the response builder into unauthorized_response, reused by the existing JwtService::Error rescue. Note: bare OPTIONS /mcp preflights are now also rejected. The app has no CORS handling today, so browser clients were already broken; adding one will need to bypass this check. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Problem
An
/mcprequest without aBearertoken returned[nil, nil]fromresolve_mcp_context, so the MCP server was built with anilserver_context. The mcp gem wraps whatever it is given inMCP::ServerContext, which delegates unknown methods to the wrapped object viamethod_missing. Withnilwrapped,tools/liststill succeeded with a 200 — but any actual tool call blew up with an opaque JSON-RPC-32603:Fix
Reject the request up front with a
401and theWWW-Authenticate: Bearer resource_metadata=…pointer, which is what tells an MCP client to re-run the OAuth flow. Extracted the response builder into a privateunauthorized_response(description)helper, reused by the existingJwtService::Errorrescue.Testing
Added a spec for the missing-header case. Verified it fails without the
app.rbchange (returns 200, sincetools/listnever touches the context — the same shape as the original bug). Full suite: 78 examples, 0 failures. Rubocop clean.Note
Bare
OPTIONS /mcppreflights are now also rejected. The app has no CORS handling today, so browser-based clients were already broken; if one is added later it will need to bypass this check.Gemfile.lockcarries an incidentalmail_mcp (1.0.0)→(1.0.1)sync picked up when bundler resolved against the v1.0.1 version file.🤖 Generated with Claude Code