Skip to content

Implement SEP-414: W3C Trace Context propagation in _meta #886

@DaleSeo

Description

@DaleSeo

SEP-414: W3C Trace Context propagation in _meta — rust-sdk implementation

Spec PR: modelcontextprotocol/modelcontextprotocol#414
Track: Specification · Stage: final · Priority: P2 · Theme: Transport Evolution and Scalability
Needs code changes: Yes (Small) — additive (typed accessors + constants)

Summary

Formalizes three W3C Trace Context fields as standardized _meta keys, enabling distributed
tracing across MCP hops without inventing a parallel mechanism:

  • traceparent — W3C Trace Context v1 trace parent header value
  • tracestate — W3C Trace Context v1 trace state header value
  • baggage — W3C Baggage header value

Servers and clients that emit or consume OpenTelemetry spans use these keys to propagate span context
through the _meta field of any MCP request.

Why this needs code changes in rust-sdk

Meta in crates/rmcp/src/model/meta.rs is a transparent JsonObject (line 199), so the values
can be stored today via raw map operations. But the SDK currently provides no typed support:

  • No key-name constants analogous to PROGRESS_TOKEN_FIELD
  • No typed accessors (get_traceparent, set_traceparent, etc.) analogous to get_progress_token/set_progress_token
  • No extractor/injector helpers for integration with the opentelemetry or tracing-opentelemetry ecosystems

Without these, every caller must hardcode string literals, making the feature invisible to SDK users
and impossible to test in isolation.

Proposed work

  • Add string constants in meta.rs:
    const TRACEPARENT_FIELD: &str = "traceparent";
    const TRACESTATE_FIELD: &str = "tracestate";
    const BAGGAGE_FIELD: &str = "baggage";
  • Add typed read/write accessors on Meta following the progressToken pattern:
    • get_traceparent(&self) -> Option<&str>
    • set_traceparent(&mut self, value: impl Into<String>)
    • get_tracestate(&self) -> Option<&str> / set_tracestate
    • get_baggage(&self) -> Option<&str> / set_baggage
  • Add a feature-gated (e.g. feature = "opentelemetry") helper module that implements
    the OpenTelemetry TextMapPropagator interface using these three fields, enabling:
    • Injection: write span context from an active opentelemetry Context into Meta
    • Extraction: restore a Context from a Meta received on an incoming request
  • Add unit tests for round-trip set/get on all three fields, and for inject/extract when the
    feature flag is enabled.

Affected areas

crates/rmcp/src/model/meta.rs (constants + typed accessors); optional new
crates/rmcp/src/telemetry.rs or model/trace_context.rs for the propagator impl.
Cargo.toml for the optional opentelemetry dependency.

Notes / risks

  • The typed accessors are purely additive and carry no risk.
  • The opentelemetry feature gate keeps the dependency optional so users who don't need
    distributed tracing don't pay the compile cost.
  • Coordinate with SEP-2243 (Implement SEP-2243: HTTP Standardization #872) — the HTTP transport headers (traceparent etc.) and the
    _meta fields carry the same logical data; the propagator helper bridges both.

Related existing issues

Metadata

Metadata

Assignees

Labels

P2Medium: important but non-blocking improvementT-enhancementNew features and enhancementsT-modelModel/data structure changesT-transportTransport layer changes

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions