Fix TypeError on 403 OAuth error body with a string#146
Open
orthodoX wants to merge 1 commit into
Open
Conversation
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.
Summary
A 403 from an OAuth-protected MCP server crashes the StreamableHTTP transport with
TypeError: String does not have #dig methodinstead of producing a usable error. Thisturns token rejected signal into a crash that surfaces far from its
cause (the transport runs the request on a background thread, so the original backtrace is
lost and the re-raised error is a bare
TransportError).Root cause
handle_oauth_authorization_errorassumes the 403 body is a JSON-RPC error and digs twolevels in:
But when a server's OAuth/resource layer rejects the token, the body is an RFC 6749 OAuth
error response (§5.2), where
erroris a string, not a nested object:{ "error": "invalid_token", "error_description": "The access token expired" }Hash#dig("error", "message")then calls#digon theStringvalue oferrorand raisesTypeError: String does not have #dig method. The method's only rescue isrescue JSON::ParserError, so theTypeErrorescapes uncaught.This path is reached specifically for
status_code == 403 && @oauth_providerwhen there is nowww-authenticatechallenge header (handle_authorization_challenge→handle_client_error→handle_oauth_authorization_error), which is exactly how many OAuthresource servers respond to an expired/invalid token.
Reproduction
Backwards compatibility
None broken. The exception class and message format are preserved; the JSON-RPC branch
behaves exactly as before. The only change is that the previously-crashing OAuth-shaped body
now yields a normal
TransportError.