fix: propagate HTTP errors from transports instead of silently logging#2249
Open
MaxwellCalkin wants to merge 4 commits intomodelcontextprotocol:mainfrom
Open
fix: propagate HTTP errors from transports instead of silently logging#2249MaxwellCalkin wants to merge 4 commits intomodelcontextprotocol:mainfrom
MaxwellCalkin wants to merge 4 commits intomodelcontextprotocol:mainfrom
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.
Note: This PR was authored by Claude (AI), operated by @MaxwellCalkin.
Summary
Fixes #2110
When an MCP server returns non-2xx HTTP status codes (401/403/404/5xx), the Streamable HTTP and SSE transports silently log the error but don't propagate it to the caller. The caller hangs indefinitely waiting for a response on the read stream.
Changes
New
HttpErrorexception class (src/mcp/shared/exceptions.py):is_auth_errorproperty to distinguish 401/403 from other failuresbodyfield for response body contextmcppackageStreamable HTTP transport (
src/mcp/client/streamable_http.py):_handle_post_request: 401/403 responses now raiseHttpError(in addition to sending a JSONRPCError for request messages), so the caller gets an exception instead of hanging_handle_post_request: Generic 4xx/5xx responses also raiseHttpErrorinstead of silently returning_handle_post_request: HTTP 404 error message now includes "(HTTP 404)" anddata={"http_status": 404}to preserve HTTP-level context_handle_post_request: All error responses includedata={"http_status": <code>}in the JSONRPCErrorpost_writer:handle_request_asyncnow wraps calls in try/except and forwards exceptions throughread_stream_writer.send(exc)so callers don't hangpost_writer: Outer exception handler also forwards errors through the read streamSSE transport (
src/mcp/client/sse.py):post_writer: 401/403 responses detected beforeraise_for_status()and forwarded asHttpErrorthrough the read streampost_writer:httpx.HTTPStatusErrorfromraise_for_status()converted toHttpErrorand forwarded through the read streampost_writer: Generic exceptions forwarded through the read stream instead of being silently loggedBefore
After
Test plan
HttpErrorwithis_auth_error == TrueHttpErrorand don't cause caller to hangHttpErroris importable frommcptop-level package