Propagate W3C trace context in outbound MCP _meta - #5964
Merged
Conversation
JAORMX
requested review from
ChrisJBurns,
amirejaz,
jerm-dro,
jhrozek and
tgrunnagle
as code owners
July 24, 2026 09:46
vMCP terminates the MCP protocol and re-issues requests to backends as a client, but it only propagated W3C trace context via HTTP headers. A spec-compliant MCP 2026-07-28 backend reads trace context from params._meta (SEP-414), so backend spans reached through vMCP were not joined to the client->proxy->server trace. The InjectMetaTraceContext helper added for this in #3682 was left unwired (dead code). Add MetaWithTraceContext, a copy-safe wrapper that enriches a _meta map with the active traceparent/tracestate/baggage, and wire it into the vMCP CallTool/ReadResource/GetPrompt calls on both the pooled-client and session-backed backend paths. It returns nil when there is no active trace context so no _meta is emitted for 2025-11-25 peers, keeping the change backward compatible. Note: mcpcompat v0.0.32 does not forward Params.Meta on the non-resume ReadResource/GetPrompt paths yet, so those two are set for forward compatibility and covered by a tripwire test that fails loudly once the SDK starts forwarding them. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MRh394VZgnqbYjJwjrcJWm
JAORMX
force-pushed
the
propagate-trace-context-meta-5758
branch
from
July 24, 2026 09:50
b7dbee8 to
baf668c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5964 +/- ##
==========================================
- Coverage 71.86% 71.84% -0.02%
==========================================
Files 713 713
Lines 73228 73242 +14
==========================================
Hits 52624 52624
- Misses 16834 16848 +14
Partials 3770 3770 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
rdimitrov
approved these changes
Jul 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
vMCP terminates the MCP protocol and re-issues requests to backends as a client, but it only propagated W3C trace context via HTTP headers. A spec-compliant MCP 2026-07-28 backend reads trace context from
params._meta(SEP-414), so backend spans reached through vMCP were not joined to the client → proxy → server trace. The inbound half (extracting_metatrace context and theInjectMetaTraceContexthelper) already landed in #3682 — but the helper was left unwired (dead code). This completes the outbound half.telemetry.MetaWithTraceContext(ctx, meta)— a copy-safe wrapper that returns a clone of the caller's_metaenriched with the activetraceparent/tracestate/baggage, reusing the existingInjectMetaTraceContext/MetaCarrier. Returnsnilwhen there is no active trace context, so no_metais emitted for 2025-11-25 peers (backward compatible).CallTool/ReadResource/GetPrompton both backend paths: the pooledhttpBackendClient(pkg/vmcp/client) and the session-backedmcpSession(pkg/vmcp/session/internal/backend).tracePropagatingRoundTripper) — the two are complementary and carry identical values from the same propagator; the receiver prioritizes_meta.Fixes #5758
Type of change
Test plan
task test)task lint-fix)Ran
go test -race(with the Taskfile's ldflags) on./pkg/telemetry/...,./pkg/vmcp/client/...,./pkg/vmcp/session/...— all pass.task lint-fixreports 0 issues. New/updated coverage:TestMetaWithTraceContext— injection, backward-compat nil, copy-before-mutate (input map unmutated), baggage, caller-meta coexistence.TestOutboundMetaTraceContext(client) andmcp_session_meta_propagation_test.go(session) — asserttraceparentlands in outbound_metaon the wire forCallTool, that the value matches the active span'sTraceID, that_metais omitted with no active span, and that a callerprogressTokencoexists with the injectedtraceparent.Changes
pkg/telemetry/propagation.goMetaWithTraceContextcopy-safe wrapper; doc/security notespkg/vmcp/client/client.go_metatrace context inCallTool/ReadResource/GetPromptpkg/vmcp/session/internal/backend/mcp_session.gopkg/telemetry/propagation_test.goTestMetaWithTraceContextpkg/vmcp/client/meta_integration_test.go_metaassertions + tripwirepkg/vmcp/session/internal/backend/mcp_session_capabilities_test.gometaByMethodwire-capture on the fake backendpkg/vmcp/session/internal/backend/mcp_session_meta_propagation_test.go_meta+ coexistence testsDoes this introduce a user-facing change?
Yes (observability): when ToolHive telemetry is enabled, distributed traces now join across client → vMCP → backend for
tools/callvia MCP_meta, so a single trace spans the whole call path. No configuration change; harmless for peers that don't emit trace context.Special notes for reviewers
mcpcompatv0.0.32 does not forwardParams.Metaon the non-resumeReadResource/GetPromptpaths, so those two assignments are currently a no-op on the wire. They are set for forward-compatibility and guarded by a tripwire assertion that fails loudly (with instructions) the moment the SDK starts forwarding them — at which point the forward-compat NOTE comments should be removed and positivetraceparentassertions added.CallToolforwards correctly today. For all three, trace context also still reaches the backend via HTTP headers.completion/completehas noMetafield inmcpcompatv0.0.32, so it propagates via HTTP headers only — an inherent SDK asymmetry, not a regression.baggagecrosses the trust boundary into (possibly untrusted) backends. It is intentionally not stripped — SEP-414 explicitly reservestraceparent/tracestate/baggageas bare_metakeys.MetaWithTraceContext's doc warns callers never to put secrets/PII in baggage. Stripping/trust-gating baggage on the backend-bound path (both_metaand the pre-existing header path) is a reasonable follow-up if desired.Implementation plan
Approved implementation plan
Architect (opus) gap analysis established that #3682 already implemented inbound
_metaextraction, theMetaCarrier, theInjectMetaTraceContexthelper (dead code), the compositeTraceContext+Baggagepropagator, and vMCP outbound header injection. Transparent-proxy passthrough forwards the body verbatim, so inbound_metais preserved with no change. The remaining gap was outbound_metainjection at the vMCP protocol-terminating client.Steps:
MetaWithTraceContext(ctx, meta)topkg/telemetry/propagation.go— clone caller map, inject via existing helper, return nil when empty.pkg/vmcp/client/client.goCallTool/ReadResource/GetPrompt.pkg/vmcp/session/internal/backend/mcp_session.gofor the same three.propagation_test.go) + wire-level integration on both paths.Reviewed by a 5-axis opus panel (security, code quality, MCP correctness, observability, test coverage) — all APPROVE-WITH-NITS, no blockers; SEP-414 key naming verified against the live spec. Panel nits addressed before this PR.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MRh394VZgnqbYjJwjrcJWm