From 082c253df0908298a2f64749648ac4b47a3e4a30 Mon Sep 17 00:00:00 2001 From: g97iulio1609 Date: Sat, 28 Feb 2026 09:10:04 +0100 Subject: [PATCH 1/2] fix: preserve HTTP status code in streamable HTTP client error messages When the streamable HTTP transport receives a 4xx/5xx response it currently reports a generic 'Server returned an error response' message, discarding the actual HTTP status code and any response body. This makes it very hard to debug upstream issues (e.g. 401 vs 403 vs 502). Include the HTTP status code and up to 200 chars of the response body in the error message so callers get actionable diagnostics. Fixes #2110 --- src/mcp/client/streamable_http.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/mcp/client/streamable_http.py b/src/mcp/client/streamable_http.py index 9f3dd5e0b..2d08effa4 100644 --- a/src/mcp/client/streamable_http.py +++ b/src/mcp/client/streamable_http.py @@ -276,7 +276,14 @@ async def _handle_post_request(self, ctx: RequestContext) -> None: if response.status_code >= 400: if isinstance(message, JSONRPCRequest): - error_data = ErrorData(code=INTERNAL_ERROR, message="Server returned an error response") + try: + response_text = (await response.aread()).decode(errors="replace") + except Exception: + response_text = "" + error_data = ErrorData( + code=INTERNAL_ERROR, + message=f"HTTP {response.status_code}: {response_text[:200]}" if response_text else f"HTTP {response.status_code}", + ) session_message = SessionMessage(JSONRPCError(jsonrpc="2.0", id=message.id, error=error_data)) await ctx.read_stream_writer.send(session_message) return From d2fa2f2f63054008cb761703b2519f440c5bf362 Mon Sep 17 00:00:00 2001 From: g97iulio1609 Date: Sat, 28 Feb 2026 14:05:50 +0100 Subject: [PATCH 2/2] style: apply ruff format --- src/mcp/client/streamable_http.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mcp/client/streamable_http.py b/src/mcp/client/streamable_http.py index 2d08effa4..a8229dfb2 100644 --- a/src/mcp/client/streamable_http.py +++ b/src/mcp/client/streamable_http.py @@ -282,7 +282,9 @@ async def _handle_post_request(self, ctx: RequestContext) -> None: response_text = "" error_data = ErrorData( code=INTERNAL_ERROR, - message=f"HTTP {response.status_code}: {response_text[:200]}" if response_text else f"HTTP {response.status_code}", + message=f"HTTP {response.status_code}: {response_text[:200]}" + if response_text + else f"HTTP {response.status_code}", ) session_message = SessionMessage(JSONRPCError(jsonrpc="2.0", id=message.id, error=error_data)) await ctx.read_stream_writer.send(session_message)