[v2] JSON-RPC error responses leave client OpenTelemetry spans UNSET
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 76/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- python
- Bereich
- backend-api-design, observability
Rechercherichtung
Beginne mit send_raw_request() in src/mcp/shared/jsonrpc_dispatcher.py, insbesondere mit dem Span-Scope und der Konvertierung von ErrorData zu MCPError, und vergleiche anschließend die Attribute und die Statusbehandlung in src/mcp/server/_otel.py. Führe den bereitgestellten In-Memory-Reproduzierer für Erfolg/Fehler aus und überprüfe, dass fehlgeschlagene Client-Spans von erfolgreichen Spans unterscheidbar sind und die JSON-RPC-Fehlerinformationen enthalten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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 (11934c90aeff5e1e68aee223edd00c5d1fce1d5c), JSON-RPC error responses handled by JSONRPCDispatcher leave the corresponding client OpenTelemetry span looking like a normal exit:
- span status:
UNSET error.type: absentrpc.response.status_code: absent- exception events: none
The request still fails correctly with MCPError; this report is limited to client-side telemetry correctness.
The ordering appears to explain the result: send_raw_request() 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.
This differs from the current server OTel middleware, which records the error attributes and status, and from the design assumption documented in #2381 that a propagated MCPError would be recorded by the client span.
I ran the same in-memory success/error pair five times. Every run produced:
METHOD=resources/list CONTROL=UNSET RED_CODE=-32602 RED_MESSAGE='forced failure' RED_STATUS=UNSET ERROR_TYPE=None RPC_STATUS=None EVENTS=0
Expected: 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.
Scope 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.
Closest 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.
Disclosure: 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.
Example Code
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import SpanKind
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)
import anyio
from mcp.client.client import Client
from mcp.server.lowlevel.server import Server
from mcp.shared.exceptions import MCPError
from mcp_types import INVALID_PARAMS, ListResourcesResult
state = {"fail": False}
async def list_resources(ctx, params):
if state["fail"]:
raise MCPError(INVALID_PARAMS, "forced failure")
return ListResourcesResult(resources=[])
async def main():
server = Server(name="otel-audit", version="0.0.0", on_list_resources=list_resources)
async with Client(server, mode="legacy", cache=None) as client:
state["fail"] = False
exporter.clear()
await client.session.list_resources()
[control] = [s for s in exporter.get_finished_spans() if s.kind == SpanKind.CLIENT]
state["fail"] = True
exporter.clear()
try:
await client.session.list_resources()
except MCPError as exc:
assert exc.error.code == INVALID_PARAMS
else:
raise AssertionError("expected MCPError")
[failed] = [s for s in exporter.get_finished_spans() if s.kind == SpanKind.CLIENT]
print(
"control=", control.status.status_code.name,
"failed=", failed.status.status_code.name,
"error.type=", failed.attributes.get("error.type"),
"rpc.response.status_code=", failed.attributes.get("rpc.response.status_code"),
"events=", len(failed.events),
)
assert control.name == failed.name == "MCP send resources/list"
assert failed.status.status_code.name == "UNSET"
assert failed.attributes.get("error.type") is None
assert failed.attributes.get("rpc.response.status_code") is None
assert len(failed.events) == 0
anyio.run(main)
Python & MCP Python SDK
Python: 3.12.10
mcp: 2.0.0b2.dev33+11934c90
opentelemetry-sdk: 1.39.1
OS: Windows 11
Clean checkout command:
uv run --python 3.12 --frozen --no-default-groups --with opentelemetry-sdk==1.39.1 python repro.py
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 30
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/python-sdk
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
modelcontextprotocol/python-sdk#3566 ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
modelcontextprotocol/python-sdk#3546 · 5 Kommentare ·
-
v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
modelcontextprotocol/python-sdk#3545 · 1 Kommentar ·
-
v1 v2
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 91/100
modelcontextprotocol/python-sdk#3508 · 2 Kommentare ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
modelcontextprotocol/python-sdk#3504 ·
Alle Issues in modelcontextprotocol/python-sdk
Ähnliche Issues
-
essnmx good first issue
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 95/100
-
[Feature] 奇物选择添加优先级 Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
syfoud/Simulated_Scepter#174 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Giskard-AI/giskard-oss#2840 · 1 Kommentar ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Offenarea: repo bug perceived difficulty: 2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
yeti-platform/yeti#1380 ·