[Feature Request] Propagate W3C trace context into MCP tool requests
Feature request: propagate W3C trace context into MCP tools/call requests
The Agent SDK already emits OTel spans for claude_code.tool.execution and forwards TRACEPARENT / TRACESTATE env vars to spawned Claude Code and Bash processes. The natural next step is to inject the active span's trace context into outgoing MCP requests as _meta.traceparent (and tracestate), per the OTel MCP semantic conventions: https://opentelemetry.io/docs/specs/semconv/gen-ai/mcp/
Today the propagation stops at the MCP boundary. An OTel-instrumented MCP server sees disconnected sub-trees rather than a connected trace from claude_code.tool.execution down through the tool's work.
Concretely:
- For stdio MCP transports: populate the _meta field on tools/call (and ideally other request types) with traceparent and tracestate carrying the current OTel context.
- For HTTP MCP transports: inject the same as traceparent / tracestate HTTP headers (this is mostly what the OTel HTTP client instrumentation would do anyway, but worth being explicit).
This mirrors the existing pattern for Bash, where the SDK already does the equivalent via env vars. It's also consistent with the broader OTel ecosystem — receiving MCP servers that follow the spec are already prepared to read _meta.traceparent and use it as their parent context.
Use case: I run an LLM agent that calls a self-hosted MCP server (instrumented with OTel and ready to read _meta.traceparent). Right now I have two disjoint trace trees per turn — one from the SDK, one per MCP call — with no edge between them, so I can't follow latency or errors across the boundary. The receiver side is ready; only the SDK's outbound injection is missing.
A transport-level middleware hook (e.g. mcpServers[name].onRequest) would also work and would unblock users who want to add it themselves while waiting.
Environment Info
- Platform: darwin
- Terminal: xterm-256color
- Version: 2.1.148
- Feedback ID: d52ad5ba-a57f-4f80-99ba-d11a853cf71b
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗