diff --git a/src/mcp/client/streamable_http.py b/src/mcp/client/streamable_http.py index 053f1f10a9..a5ba551d72 100644 --- a/src/mcp/client/streamable_http.py +++ b/src/mcp/client/streamable_http.py @@ -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 diff --git a/tests/client/test_notification_response.py b/tests/client/test_notification_response.py index b21e734fa3..60be9739dc 100644 --- a/tests/client/test_notification_response.py +++ b/tests/client/test_notification_response.py @@ -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: