Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/mcp/client/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
else:
error_data = ErrorData(code=INVALID_REQUEST, message="Session terminated")
else:
error_data = ErrorData(code=INTERNAL_ERROR, message="Server returned an error response")
error_data = ErrorData(
code=INTERNAL_ERROR,
message="Server returned an error response",
data={
"http_status": response.status_code,
"reason": response.reason_phrase,
},
)
session_message = SessionMessage(JSONRPCError(jsonrpc="2.0", id=message.id, error=error_data))
await ctx.read_stream_writer.send(session_message)
return
Expand Down
6 changes: 5 additions & 1 deletion tests/client/test_notification_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ async def test_http_error_status_sends_jsonrpc_error() -> None:
async with ClientSession(read_stream, write_stream) as session: # pragma: no branch
await session.initialize()

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


async def test_http_error_on_notification_does_not_hang() -> None:
Expand Down
Loading