fix: record JSON-RPC errors on client OpenTelemetry spans

Closed
MI
3mo ago
+27-2

Summary

Client OpenTelemetry spans did not record JSON-RPC error responses because ErrorData was converted to MCPError after the span context exited. This made failed client requests indistinguishable from successful requests in telemetry.

This change records StatusCode.ERROR, error.type, and rpc.response.status_code while the client span is still active. The existing MCPError behavior is unchanged.

Tests

  • uv run --frozen pytest tests/server/test_otel.py -q (17 passed)
  • uv run --frozen ruff check src/mcp/shared/jsonrpc_dispatcher.py tests/server/test_otel.py
  • uv run --frozen ruff format --check src/mcp/shared/jsonrpc_dispatcher.py tests/server/test_otel.py
  • uv run --frozen pyright src/mcp/shared/jsonrpc_dispatcher.py tests/server/test_otel.py
  • Full suite with PYTHONUTF8=1: 5574 passed, 16 skipped, 1 xfailed; one unrelated local HTTP 502 failure in tests/client/test_transport_stream_cleanup.py::test_sse_client_closes_all_streams_on_connection_error.

AI assistance was used during development, and the change was reviewed and verified locally.

Fixes #3174