Title: [v2] JSON-RPC error responses leave client OpenTelemetry spans UNSET · Issue #3174 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: [v2] JSON-RPC error responses leave client OpenTelemetry spans UNSET · Issue #3174 · modelcontextprotocol/python-sdk
X Title: [v2] JSON-RPC error responses leave client OpenTelemetry spans UNSET · Issue #3174 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue Description At current main (11934...
Open Graph Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...
X Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening t...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3174
X: @github
Domain: redirect.github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[v2] JSON-RPC error responses leave client OpenTelemetry spans UNSET","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\nAt current `main` (`11934c90aeff5e1e68aee223edd00c5d1fce1d5c`), JSON-RPC error responses handled by `JSONRPCDispatcher` leave the corresponding client OpenTelemetry span looking like a normal exit:\n\n- span status: `UNSET`\n- `error.type`: absent\n- `rpc.response.status_code`: absent\n- exception events: none\n\nThe request still fails correctly with `MCPError`; this report is limited to client-side telemetry correctness.\n\nThe ordering appears to explain the result: [`send_raw_request()`](https://github.com/modelcontextprotocol/python-sdk/blob/11934c90aeff5e1e68aee223edd00c5d1fce1d5c/src/mcp/shared/jsonrpc_dispatcher.py#L368-L434) receives `outcome` inside the client span, exits the span, and only afterward converts `ErrorData` into `MCPError` at lines 432–433. The span context manager therefore observes no exception.\n\nThis differs from the current [server OTel middleware](https://github.com/modelcontextprotocol/python-sdk/blob/11934c90aeff5e1e68aee223edd00c5d1fce1d5c/src/mcp/server/_otel.py#L35-L60), which records the error attributes and status, and from the design assumption documented in [#2381](https://github.com/modelcontextprotocol/python-sdk/pull/2381) that a propagated `MCPError` would be recorded by the client span.\n\nI ran the same in-memory success/error pair five times. Every run produced:\n\n```text\nMETHOD=resources/list CONTROL=UNSET RED_CODE=-32602 RED_MESSAGE='forced failure' RED_STATUS=UNSET ERROR_TYPE=None RPC_STATUS=None EVENTS=0\n```\n\nExpected: the failed client span should be distinguishable from the successful control and carry the JSON-RPC error code/status consistently with the SDK's server-side instrumentation.\n\nScope and claim boundary: this concerns JSON-RPC/stream-backed dispatcher calls, not direct in-process dispatch generally. The reproducer is entirely in-memory. I have not tested or claimed anything about a deployed transport, collector, or production frequency.\n\nClosest adjacent work found: #2132 targets the removed `BaseSession` architecture and predates the current dispatcher; #2854 adds GenAI attributes but does not handle JSON-RPC errors. I did not find an issue or current PR covering this ordering on the present dispatcher.\n\nDisclosure: I used AI assistance to help reproduce and analyze this issue. I reviewed the source, reran the evidence independently, and take responsibility for the report.\n\n### Example Code\n\n```Python\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import SimpleSpanProcessor\nfrom opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter\nfrom opentelemetry.trace import SpanKind\n\nexporter = InMemorySpanExporter()\nprovider = TracerProvider()\nprovider.add_span_processor(SimpleSpanProcessor(exporter))\ntrace.set_tracer_provider(provider)\n\nimport anyio\nfrom mcp.client.client import Client\nfrom mcp.server.lowlevel.server import Server\nfrom mcp.shared.exceptions import MCPError\nfrom mcp_types import INVALID_PARAMS, ListResourcesResult\n\nstate = {\"fail\": False}\n\nasync def list_resources(ctx, params):\n if state[\"fail\"]:\n raise MCPError(INVALID_PARAMS, \"forced failure\")\n return ListResourcesResult(resources=[])\n\nasync def main():\n server = Server(name=\"otel-audit\", version=\"0.0.0\", on_list_resources=list_resources)\n async with Client(server, mode=\"legacy\", cache=None) as client:\n state[\"fail\"] = False\n exporter.clear()\n await client.session.list_resources()\n [control] = [s for s in exporter.get_finished_spans() if s.kind == SpanKind.CLIENT]\n\n state[\"fail\"] = True\n exporter.clear()\n try:\n await client.session.list_resources()\n except MCPError as exc:\n assert exc.error.code == INVALID_PARAMS\n else:\n raise AssertionError(\"expected MCPError\")\n\n [failed] = [s for s in exporter.get_finished_spans() if s.kind == SpanKind.CLIENT]\n print(\n \"control=\", control.status.status_code.name,\n \"failed=\", failed.status.status_code.name,\n \"error.type=\", failed.attributes.get(\"error.type\"),\n \"rpc.response.status_code=\", failed.attributes.get(\"rpc.response.status_code\"),\n \"events=\", len(failed.events),\n )\n assert control.name == failed.name == \"MCP send resources/list\"\n assert failed.status.status_code.name == \"UNSET\"\n assert failed.attributes.get(\"error.type\") is None\n assert failed.attributes.get(\"rpc.response.status_code\") is None\n assert len(failed.events) == 0\n\nanyio.run(main)\n```\n\n### Python & MCP Python SDK\n\n```Text\nPython: 3.12.10\nmcp: 2.0.0b2.dev33+11934c90\nopentelemetry-sdk: 1.39.1\nOS: Windows 11\n\nClean checkout command:\nuv run --python 3.12 --frozen --no-default-groups --with opentelemetry-sdk==1.39.1 python repro.py\n```","author":{"url":"https://github.com/HarperZ9","@type":"Person","name":"HarperZ9"},"datePublished":"2026-07-26T04:29:12Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3174"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:90466bde-bd44-6057-61fa-ccc4c035eb9b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC90:E6B97:F17239:14BB5E3:6B055D26 |
| html-safe-nonce | 2164a7a9ae0682fcdfbe04a377d816115e420e88fba43b87dc059b5305f33c6e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzkwOkU2Qjk3OkYxNzIzOToxNEJCNUUzOjZCMDU1RDI2IiwidmlzaXRvcl9pZCI6Ijc3MDc0NDMxMjY5ODA2ODI2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7e14a3ef52818e673e6c8794f4fd7202a2b9d6be5eedbae88bce225377b02613 |
| hovercard-subject-tag | issue:4979103793 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3174/issue_layout |
| twitter:image | https://opengraph.githubassets.com/16821d0684c962dd1250f10d01c116ea0c507ff394026e37ad83008efaeb0dfe/modelcontextprotocol/python-sdk/issues/3174 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/16821d0684c962dd1250f10d01c116ea0c507ff394026e37ad83008efaeb0dfe/modelcontextprotocol/python-sdk/issues/3174 |
| og:image:alt | Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | HarperZ9 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8a6760da3cce5e2d079d07e3b62df71194316c4478db1a5e38050f44fce72c72 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8327739ac0648b9c3c722b5f401f7338553936b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width