Preserve MCP request trace context across transport workers (#47212)

## Why

MCP requests pass through background transport queues, so HTTP requests need the caller's trace context carried explicitly to preserve trace ancestry.

## What changed

- Capture W3C trace context in request metadata for tool and resource operations, custom requests, and event streams.
- Restore that context in an `mcp.http.request` span around HTTP POSTs, linking outgoing HTTP traces to the originating request.
- Preserve application metadata while replacing trace fields with the current caller's context and removing stale `tracestate`. Read the SDK's merged metadata so extension fields retain precedence over parameter metadata.

## Testing

Add a unit test for metadata merge precedence and an integration test covering concurrent paginated requests, tool calls, custom requests, and tool-call continuations across legacy and `2026-07-28` protocol modes. Verify HTTP trace ancestry, metadata preservation, and removal of stale `tracestate`.

GitOrigin-RevId: 0b22cd75f31f9fcb855b27fa8d34ecdffd6e3549
This commit is contained in:
Ben Romano
2026-09-22 06:47:07 +00:00
committed by copyberry
parent 064e701b0f
commit 94174e44cb
8 changed files with 371 additions and 17 deletions
+1
View File
@@ -4283,6 +4283,7 @@ dependencies = [
"codex-http-client",
"codex-keyring-store",
"codex-network-proxy",
"codex-otel",
"codex-protocol",
"codex-secrets",
"codex-utils-cargo-bin",