Title: fix: propagate HTTP errors from transports instead of silently logging by MaxwellCalkin · Pull Request #2249 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix: propagate HTTP errors from transports instead of silently logging by MaxwellCalkin · Pull Request #2249 · modelcontextprotocol/python-sdk
X Title: fix: propagate HTTP errors from transports instead of silently logging by MaxwellCalkin · Pull Request #2249 · modelcontextprotocol/python-sdk
Description: 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 HttpError exception class (src/mcp/shared/exceptions.py):
Preserves the original HTTP status code
is_auth_error property to distinguish 401/403 from other failures
Optional body field for response body context
Exported from the top-level mcp package
Streamable HTTP transport (src/mcp/client/streamable_http.py):
_handle_post_request: 401/403 responses now raise HttpError (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 raise HttpError instead of silently returning
_handle_post_request: HTTP 404 error message now includes "(HTTP 404)" and data={"http_status": 404} to preserve HTTP-level context
_handle_post_request: All error responses include data={"http_status": } in the JSONRPCError
post_writer: handle_request_async now wraps calls in try/except and forwards exceptions through read_stream_writer.send(exc) so callers don't hang
post_writer: Outer exception handler also forwards errors through the read stream
SSE transport (src/mcp/client/sse.py):
post_writer: 401/403 responses detected before raise_for_status() and forwarded as HttpError through the read stream
post_writer: httpx.HTTPStatusError from raise_for_status() converted to HttpError and forwarded through the read stream
post_writer: Generic exceptions forwarded through the read stream instead of being silently logged
Before
# Caller hangs forever waiting for a response that will never come
async with streamable_http_client("https://example.com/mcp") as (read, write):
await write.send(SessionMessage(request))
response = await read.receive() # blocks indefinitely on 401/403/5xx
After
# Caller receives an HttpError exception through the read stream
async with streamable_http_client("https://example.com/mcp") as (read, write):
await write.send(SessionMessage(request))
response = await read.receive() # receives HttpError or JSONRPCError with http_status
# Or catch it directly
from mcp.shared.exceptions import HttpError
try:
...
except HttpError as e:
if e.is_auth_error:
print(f"Auth failed: {e.status_code}")
Test plan
Verify existing tests pass (no behavioral change for 2xx responses)
Verify 401/403 responses raise HttpError with is_auth_error == True
Verify 404 responses include HTTP status in error data
Verify 5xx responses raise HttpError and don't cause caller to hang
Verify HttpError is importable from mcp top-level package
Open Graph Description: 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 transp...
X Description: 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 transp...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2249
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6c3784e9-e93f-eed8-42c9-44a19d74b700 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B1A6:3DA5AD:1C25AC7:292F7F5:6A60B65E |
| html-safe-nonce | f91c8de6023596510073c777ed841093278cd074ed86c675162e815aa1f7142c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUE2OjNEQTVBRDoxQzI1QUM3OjI5MkY3RjU6NkE2MEI2NUUiLCJ2aXNpdG9yX2lkIjoiNzg0NjcyMTA0MDQyOTM5NzU5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 24f7671b85029de0894cbdc0d7a8c37b93ebc9a121a8c3246e599dfc423b1517 |
| hovercard-subject-tag | pull_request:3369387394 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2249/files |
| twitter:image | https://avatars.githubusercontent.com/u/101308415?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/101308415?s=400&v=4 |
| og:image:alt | 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 transp... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 35671f8a6946a7a2a81d51ae49f5465bdc5f925c23c9cb839206b24345dbe8d9 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 746dc49090e6e3995bb502dce0f8807393e36eda |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width