Skip to content

Commit 0798ab5

Browse files
committed
fix(client): include HTTP status and reason in streamable HTTP error data
1 parent 77001c9 commit 0798ab5

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎src/mcp/client/streamable_http.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,14 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
423423
else:
424424
error_data = ErrorData(code=INVALID_REQUEST, message="Session terminated")
425425
else:
426-
error_data = ErrorData(code=INTERNAL_ERROR, message="Server returned an error response")
426+
error_data = ErrorData(
427+
code=INTERNAL_ERROR,
428+
message="Server returned an error response",
429+
data={
430+
"http_status": response.status_code,
431+
"reason": response.reason_phrase,
432+
},
433+
)
427434
session_message = SessionMessage(JSONRPCError(jsonrpc="2.0", id=message.id, error=error_data))
428435
await ctx.read_stream_writer.send(session_message)
429436
return

‎tests/client/test_notification_response.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@ async def test_http_error_status_sends_jsonrpc_error() -> None:
147147
async with ClientSession(read_stream, write_stream) as session: # pragma: no branch
148148
await session.initialize()
149149

150-
with pytest.raises(MCPError, match="Server returned an error response"): # pragma: no branch
150+
with pytest.raises(MCPError, match="Server returned an error response") as exc_info: # pragma: no branch
151151
await session.list_tools()
152+
assert exc_info.value.error.data == {
153+
"http_status": 500,
154+
"reason": "Internal Server Error",
155+
}
152156

153157

154158
async def test_http_error_on_notification_does_not_hang() -> None:

0 commit comments

Comments
 (0)