Title: fix(client): surface non-2xx HTTP responses to the waiting caller by himanshu-commits · Pull Request #3092 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(client): surface non-2xx HTTP responses to the waiting caller by himanshu-commits · Pull Request #3092 · modelcontextprotocol/python-sdk
X Title: fix(client): surface non-2xx HTTP responses to the waiting caller by himanshu-commits · Pull Request #3092 · modelcontextprotocol/python-sdk
Description: Relates to #3091. A 4xx/5xx response to a message POST hit a bare response.raise_for_status() inside the POST's background task. Nothing caught the exception and nothing resolved the request, so the caller sat until its read timeout fired: a 401 was indistinguishable from a slow server, and the escaping error tore down the transport's task group with it. Non-2xx statuses are now answered rather than raised, generalising the handling 404 already had: a JSONRPCError stamped with the original request's id, sent as a SessionMessage over the read stream, so it resolves through the normal response-correlation path. A spec-compliant JSON-RPC error in the body is preferred over the status-derived stand-in. The connection stays usable — a 500 on one tools/list no longer kills the next call. Motivation and Context Reported in #3091: HTTP status errors never reached the caller, and were indistinguishable from a timeout. This is fixed on the 2.0 line already; per the discussion on the issue, this PR backports the equivalent behaviour to v1.x, where the bug still reproduces (1.25.0 / 1.28.1). One deliberate difference from 2.0 2.0 checks the response body before the 404 fallback; this PR checks 404 first. A terminated session 404s with a JSON-RPC error body, so preferring it would replace this line's Session terminated / code 32600 — which callers match on for session expiry — with the server's own wording. Two existing tests caught this. Behaviour is identical to 2.0 for every status this issue is actually about. Related: that 32600 is positive, where JSON-RPC codes are negative (2.0 corrected it to -32600). I've left it alone — changing it would break anyone checking error.code == 32600 on a stable release. Happy to change it if you'd rather. Note on 3xx raise_for_status() used to raise for any non-2xx; the new guard is >= 400, so a redirect falls through to the content-type dispatch rather than the correlated-error path. In practice create_mcp_http_client sets follow_redirects=True, so this only affects user-supplied clients with redirects disabled — and 2.0 carries the identical >= 400 guard, so it's parity, not a regression. Tracked separately rather than widened here. What this does not fix Callers still cannot tell a terminal 401 from a retryable 503: both arrive as -32603 Server returned an error response, exactly as on 2.0. The issue's stated impact (retry policy) therefore isn't fully addressed here. Carrying the originating status on error.data would close that, but belongs on the 2.0 line first — happy to follow up. How Has This Been Tested? Ten new tests in tests/shared/test_streamable_http.py, driven in-memory via httpx.MockTransport: 401 / 403 / 500 / 503 each reach the waiting caller. Each is wrapped in anyio.fail_after(5), so without the fix they hang rather than pass vacuously — that's the regression pin. 404 still reports Session terminated (guard on the unchanged behaviour) a JSON-RPC error body is preferred, re-stamped with the request's id a JSON body that isn't a JSON-RPC error (proxy/gateway JSON) falls back to the stand-in a non-2xx body that dies mid-read still resolves the caller a non-2xx to a notification resolves nobody and leaves the session alive a failed request leaves the session usable Full suite passes (1149), 100% branch coverage. Six # pragma: no cover markers removed from the 404 path, which was previously untested and is now exercised. Breaking Changes None. 404 handling is preserved byte-for-byte, including its code and message. Every other non-2xx previously raised an exception that escaped the transport; it now resolves the caller instead. Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context _handle_resumption_request still carries a bare raise_for_status() — the same bug class on the resumption GET path, unfixed on 2.0 as well. Out of scope here; noting it so it isn't lost.
Open Graph Description: Relates to #3091. A 4xx/5xx response to a message POST hit a bare response.raise_for_status() inside the POST's background task. Nothing caught the exception and nothing resolved the request, s...
X Description: Relates to #3091. A 4xx/5xx response to a message POST hit a bare response.raise_for_status() inside the POST's background task. Nothing caught the exception and nothing resolved the reques...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3092
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:ab314fe0-3c38-96d2-1ad0-b224c7af023e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AEFC:117C62:1DE6779:27D819E:6B017233 |
| html-safe-nonce | 60b6517d5a812c6cdfb3b14281b08d83b9acc7564d5dc5a59dfac418f8778971 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUZDOjExN0M2MjoxREU2Nzc5OjI3RDgxOUU6NkIwMTcyMzMiLCJ2aXNpdG9yX2lkIjoiNzMwODIwNTcwNTcyMTM3NzMzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 22233367caafac85df119ae0d95192af4f2d6975a4ebe78a6ad7039e22c786bd |
| hovercard-subject-tag | pull_request:4048513295 |
| 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/3092/files |
| twitter:image | https://avatars.githubusercontent.com/u/82858309?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/82858309?s=400&v=4 |
| og:image:alt | Relates to #3091. A 4xx/5xx response to a message POST hit a bare response.raise_for_status() inside the POST's background task. Nothing caught the exception and nothing resolved the request, s... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width