# Why does an instance lose trace parentage when an MCP tool call re-enters the agent through an A2A peer: the trace-context rebinding failure **Date of Record:** 2026 November 29 **Scope:** Applies to observability correlation in agent systems where a single logical task spans an orchestrator LLM instance, one or more MCP tool servers, and one or more A2A peer agents, and where trace/span propagation is carried in-band via headers or JSON-RPC metadata. Does not apply to single-process agents that never cross a transport boundary, nor to systems where a sidecar transparently injects trace context at the network layer. ## Core Distinction The distinction that matters here is between *trace identity* (the correlation ID that ties spans into one trace) and *causal parentage* (the `parent_span_id` that defines the tree edge). Most MCP and A2A implementations propagate the former and silently corrupt the latter. When an orchestrator calls an MCP tool, the MCP server emits its span as a child of the tool-call span — correct. But when that same MCP tool, acting as a "tool," internally invokes an A2A peer to fulfill the request, the A2A client on the MCP server typically has no span context to attach to the outbound `message/send`, because the MCP server was never given the *calling* span's ID — only the trace ID. The peer then either starts a fresh root span under the same trace ID or rebinds to whatever ambient context its own runtime holds. The result: one trace ID, two disjoint span trees, and a causal link that observability backends cannot reconstruct. The failure is not "lost trace" — traces appear complete in dashboards. The failure is *orphaned parentage under a shared trace ID*, which is worse because it looks healthy. ## What Was Observed During a multi-hop incident replay, an orchestrator (instance `orch-1`) received a user request, opened span `S0` (trace `T`), and called MCP tool `kb.search` on server `mcp-kb`. The MCP server emitted `S1` as a child of `S0` — the trace context arrived correctly via the `traceparent`-equivalent field in the JSON-RPC `_meta`. Inside `kb.search`, the tool needed a second opinion and issued an A2A `message/send` to peer `auditor-7`. The MCP server's A2A client constructed the outbound request with `trace_id = T` but `parent_span_id = null`, because the MCP server had received only the trace ID and the *inbound* span ID (`S0`'s ID), not `S1`'s own ID — it had no way to know its own emitted span ID at the moment it built the outbound context, since span finalization happens after the call returns. `auditor-7` received the request, saw a trace ID with no parent, and — depending on its SDK — either (a) created a new root span `S2` under `T` with no parent, or (b) rebound to a *stale ambient context* from a previous request still resident in its async task-local storage. Observed divergence across three runs of the same input: | Run | `auditor-7` behavior | Resulting topology | Backend verdict | |-----|---------------------|--------------------|-----------------| | 1 | new root span | `T` has roots `S0` and `S2` | "2 root spans" warning, causal edge `S1→S2` missing | | 2 | stale ambient rebind | `S2.parent = S_prev` from prior trace | `S2` attached to *wrong trace* `T_prev` | | 3 | correct (ambient happened to be empty) | `S1→S2` intact | healthy | Run 2 is the dangerous one: the auditor's span was silently attributed to a *different* user's prior request, producing a cross-tenant causal edge in the trace store. This was only detected because a latency alert fired on `T_prev` and an operator noticed an impossible dependency. ## Condition of Validity The rebinding failure manifests only when all of the following hold simultaneously: 1. The trace context is propagated **in-band** (header or JSON-RPC metadata) rather than by a transport-level injector that can observe the actual emitted span. 2. The intermediate hop (here, the MCP server) is **stateless with respect to span identity** — it knows the trace ID but not its own span ID at outbound-construction time. 3. The downstream peer (`auditor-7`) uses **task-local or thread-local ambient context** for parent resolution, and that storage is not explicitly cleared between requests. 4. The peer's SDK resolves a missing `parent_span_id` by **falling back to ambient context** rather than by starting an explicit, marked root. If any one condition is absent, the failure does not occur. This is why it is intermittent and environment-dependent: condition 3 fails on a freshly warmed instance and succeeds on a reused one, which is exactly the cold-start-vs-steady-state asymmetry that makes it hard to reproduce in staging. ## Decision Boundary The architectural invariant required is: **a span context crossing a transport boundary must carry both `trace_id` and the sender's own `parent_span_id`, and a receiver must never resolve a null parent from ambient storage.** Concretely: - The MCP server must **pre-allocate its span ID** before constructing the outbound A2A request, so it can set `parent_span_id = S1.span_id` on the `message/send`. Deferring span finalization until after the call returns is the root cause; the fix is to allocate-then-finalize, not finalize-then-send. - The A2A peer must treat a missing `parent_span_id` as an **explicit error condition** (or an explicitly-marked synthetic root), never as a cue to consult ambient context. Ambient fallback is the mechanism that produces the cross-tenant attribution in Run 2. - The receiving runtime must **clear task-local span context at request entry**, before any parent resolution, so a stale context from a prior request cannot leak. The boundary is therefore not "did we propagate the trace ID" (we did) but "did we propagate *parentage* and forbid ambient fallback." Systems that pass the first check and fail the second are the ones that generate plausible-looking but causally wrong traces. ## Operational Effect The observable symptom is a trace store that reports **multiple root spans under one trace ID** and, in the worse variant, **causal edges attributed to the wrong trace**. Alerting on "trace has >1 root" catches Run 1 but not Run 2, because Run 2 produces a single well-formed root — just the wrong one. The only reliable detector is a **parentage-consistency check at span ingest**: every non-root span's `parent_span_id` must resolve to a span that exists *in the same trace*, and every `message/send`-derived span must have a parent whose `service.name` matches the caller. When that check is enforced, Run 2 fails closed at ingest instead of silently corrupting the trace graph, and the MCP server's allocate-before-send fix becomes verifiable rather than aspirational. The continuity invariant to record: **trace ID equality is necessary but not sufficient for causal reconstruction; parentage must be transported explicitly and resolved without ambient fallback, or the trace graph is silently wrong in a way that survives every dashboard check.** ```python class TraceParentageError(RuntimeError): """Raised when a span crosses a transport boundary without resolvable parentage.""" pass def resolve_parent_span(inbound_meta: dict, ambient_context: dict | None) -> str: """ Resolve the parent span for an inbound A2A/MCP request. Invariant: parentage must come from the wire, never from ambient storage. """ trace_id = inbound_meta.get("trace_id") parent_span_id = inbound_meta.get("parent_span_id") if trace_id is None: raise TraceParentageError("inbound request carries no trace_id; cannot correlate") if parent_span_id is None: # The dangerous path: SDKs that fall back to ambient context here # produce cross-trace attribution (the Run 2 failure). if ambient_context is not None: raise TraceParentageError( f"null parent_span_id on trace {trace_id} with non-empty ambient " f"context {ambient_context.get('span_id')!r}; refusing ambient rebind" ) # No ambient context and no parent: only legal as an explicit marked root. raise TraceParentageError( f"null parent_span_id on trace {trace_id}; sender must pre-allocate " f"its span ID before outbound construction (allocate-then-finalize)" ) return parent_span_id def emit_outbound_context(server_span_id: str, trace_id: str) -> dict: """MCP server must call this BEFORE issuing the A2A message/send.""" if not server_span_id: raise TraceParentageError( "outbound A2A send attempted before server span was allocated; " "trace parentage would be lost" ) return {"trace_id": trace_id, "parent_span_id": server_span_id} ```