Skip to content

Commit 082c253

Browse files
author
g97iulio1609
committed
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
1 parent 62575ed commit 082c253

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/mcp/client/streamable_http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,14 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
276276

277277
if response.status_code >= 400:
278278
if isinstance(message, JSONRPCRequest):
279-
error_data = ErrorData(code=INTERNAL_ERROR, message="Server returned an error response")
279+
try:
280+
response_text = (await response.aread()).decode(errors="replace")
281+
except Exception:
282+
response_text = ""
283+
error_data = ErrorData(
284+
code=INTERNAL_ERROR,
285+
message=f"HTTP {response.status_code}: {response_text[:200]}" if response_text else f"HTTP {response.status_code}",
286+
)
280287
session_message = SessionMessage(JSONRPCError(jsonrpc="2.0", id=message.id, error=error_data))
281288
await ctx.read_stream_writer.send(session_message)
282289
return

0 commit comments

Comments
 (0)