# Where must an instance re-mint traceparent when an MCP server terminates the inbound trace context mid-hop **Date of Record:** 2026 November 27 **Scope:** Applies to multi-hop agent systems where a caller agent propagates a W3C `traceparent` header into an MCP tool call, and the MCP server (or an intermediate proxy/gateway) either drops, rewrites, or fails to echo that context into downstream agent-to-agent (A2A) hops or nested MCP calls. Does not apply to single-hop in-process tool invocations where no transport boundary exists, nor to systems using a proprietary correlation scheme with explicit context handoff semantics. ## Core Distinction The failure is not "traceparent is missing." It is the distinction between **trace continuity** (same `trace-id` across all spans of a logical operation) and **span linkage** (a child `parent-id` that actually points at the span that causally produced the work). W3C `traceparent` encodes both: `version-traceid-parentid-flags`. Most MCP integrations preserve the `trace-id` because it is easy to pass through opaquely as a string, while silently breaking `parent-id` linkage because the MCP server treats the inbound header as *its own* span parent and then re-mints a fresh `parent-id` for its outbound call — or worse, generates a new `trace-id` entirely when the SDK auto-instruments and finds no ambient context. The result is a trace that *looks* correlated (same trace-id, all spans present in the backend) but is causally wrong: the downstream agent's span is parented to the MCP server's request handler span rather than to the agent turn that issued the tool call. When the MCP server multiplexes concurrent requests or retries, the `parent-id` it emits collides across unrelated logical operations, and the trace graph becomes a many-to-many mesh instead of a tree. ## What Was Observed A three-hop pipeline: `orchestrator-agent → MCP-server-A → subagent-B → MCP-server-C`. The orchestrator emitted a valid `traceparent` with `trace-id=T`, `parent-id=P0` (its own turn span). MCP-server-A was instrumented with an auto-instrumenting SDK that, on receiving the request, checked for an ambient active span in its own process. None existed (the header arrived as a raw HTTP/gRPC field, not injected into the SDK's context carrier). The SDK therefore **started a new root span with a new `trace-id=T'`** and re-emitted `traceparent: ...-T'-P_A-01` on the A2A call to subagent-B. Downstream, subagent-B's spans and MCP-server-C's spans all landed under `T'`, not `T`. The orchestrator's trace (`T`) terminated at the MCP-server-A client span with no children. Two disconnected trace trees existed for one logical operation. Operators searching by `T` saw a one-span trace; searching by `T'` saw the "real" work but had no link back to the originating agent turn, the user session, or the retry budget that governed it. A second, subtler variant: MCP-server-A *did* extract the header and continue the trace correctly, but on a **retry of the same tool call** (transient timeout, then re-invoke) it reused the cached `parent-id=P0` from the first attempt. Two sibling spans under `P0` now represent the same logical step, and the downstream subagent-B — keyed on `parent-id` for deduplication — either double-executed or suppressed the legitimate retry depending on ordering. The trace was continuous but the span identity was not unique per attempt. ## Condition of Validity The invariant that must hold: **for every span emitted on behalf of a logical agent step, `parent-id` must equal the `span-id` of the specific attempt that causally invoked it, and `trace-id` must be invariant across the entire logical operation including all retries and all MCP/A2A hops.** This holds only when three conditions are simultaneously satisfied: 1. **Explicit extraction, not ambient lookup.** The MCP server must parse `traceparent` from the transport carrier (HTTP header, gRPC metadata, JSON-RPC `params._meta`) and construct a remote-parent span context explicitly, rather than relying on the instrumentation SDK to discover an ambient span that does not exist across a process boundary. 2. **Per-attempt span minting.** Each retry or re-invocation must mint a fresh `span-id` for the outbound hop and carry the *attempt's* span-id as `parent-id`. Caching the inbound `parent-id` across retries violates uniqueness. 3. **Trace-id is write-once.** No hop may generate a new `trace-id` if a valid one was received. Only a genuine root (no inbound `traceparent`, or an invalid/unparseable one) may mint a new `trace-id`, and it must be flagged so the discontinuity is visible rather than silent. The invariant fails silently in the common case because trace backends accept orphaned and mis-parented spans without error. ## Decision Boundary The boundary is: **does the receiving MCP server treat the inbound `traceparent` as authoritative context to be continued, or as an opaque string to be forwarded?** - If *continued*: it must extract, validate (version `00`, 32-hex trace-id, 16-hex parent-id, non-zero), and adopt the trace-id while minting a new child span-id. This is correct and preserves linkage. - If *forwarded opaquely*: the server must not emit any spans of its own under that trace, or it will produce spans with no valid parent. Forwarding without continuing is only safe for a pure pass-through proxy that adds no spans. - If *dropped* (the failure): the server's SDK mints a new root. This is the boundary crossing where continuity dies. The decision to drop must be made explicit and logged as a trace discontinuity, never left to SDK default behavior. The non-obvious part: an MCP server that *both* forwards the header verbatim *and* emits its own spans creates the worst outcome — its spans are parented to the orchestrator's `P0` directly, bypassing the MCP server's own handler span, which flattens the causal depth and makes latency attribution across the hop impossible. ## Operational Effect Mis-parented traces destroy three operational capabilities at once: (a) **latency attribution** — you cannot tell whether time was spent in the orchestrator's reasoning, the MCP transport, or the subagent, because the spans are siblings rather than nested; (b) **retry forensics** — duplicate `parent-id` across attempts makes it impossible to distinguish "the subagent ran twice" from "the trace backend deduplicated," which is exactly the question asked during an incident; (c) **causal replay** — reconstructing which agent turn produced a poisoned or hallucinated tool output requires an unbroken parent chain, and a broken chain means the audit cannot attribute the bad output to the step that generated it. The guardrail: validate continuity at every hop boundary and fail loudly on discontinuity rather than emitting an orphaned trace. ```python import re TRACEPARENT_RE = re.compile( r"^(?P[0-9a-f]{2})-" r"(?P[0-9a-f]{32})-" r"(?P[0-9a-f]{16})-" r"(?P[0-9a-f]{2})$" ) ZERO_TRACE = "0" * 32 ZERO_PARENT = "0" * 16 class TraceContinuityError(RuntimeError): """Raised when an MCP/A2A hop breaks traceparent causal linkage.""" pass def parse_traceparent(header: str) -> dict: m = TRACEPARENT_RE.match(header.strip().lower()) if not m: raise TraceContinuityError(f"malformed traceparent: {header!r}") tp = m.groupdict() if tp["trace_id"] == ZERO_TRACE or tp["parent_id"] == ZERO_PARENT: raise TraceContinuityError("traceparent carries all-zero id (invalid)") if tp["version"] == "ff": raise TraceContinuityError("traceparent version ff is forbidden") return tp def continue_trace(inbound_header: str | None, outbound_span_id: str, attempt: int, seen_parents: set[str]) -> str: """Mint an outbound traceparent that preserves trace-id and links per-attempt.""" if inbound_header is None: # Genuine root: mint new trace-id, but this is a visible discontinuity. raise TraceContinuityError( "no inbound traceparent at MCP hop; refusing to silently root a new trace" ) tp = parse_traceparent(inbound_header) if len(outbound_span_id) != 16 or not re.fullmatch(r"[0-9a-f]{16}", outbound_span_id): raise TraceContinuityError(f"bad outbound span-id: {outbound_span_id!r}") if outbound_span_id in seen_parents: raise TraceContinuityError( f"span-id {outbound_span_id} reused across attempts; retry would collide" ) seen_parents.add(outbound_span_id) # trace-id is write-once: inherited verbatim from inbound context. return f"00-{tp['trace_id']}-{outbound_span_id}-{tp['flags']}" def verify_hop_continuity(inbound: str, outbound: str, expected_attempt: int) -> None: """Assert the outbound hop is a true child of the inbound context.""" src = parse_traceparent(inbound) dst = parse_traceparent(outbound) if dst["trace_id"] != src["trace_id"]: raise TraceContinuityError( f"trace-id mutated across hop: {src['trace_id']} -> {dst['trace_id']}" ) if dst["parent_id"] == src["parent_id"]: raise TraceContinuityError( "outbound parent-id equals inbound parent-id (retry collision or flat linkage)" ) if expected_attempt < 0: raise TraceContinuityError("attempt index must be non-negative") ``` The `verify_hop_continuity` check is the enforcement point: it must run at the MCP egress boundary before any outbound A2A or nested MCP call, and its failure must abort the hop rather than degrade to an orphaned trace. A trace that cannot prove its parent chain is worse than no trace, because it is trusted.