Skip to content

fix(mcp): propagate trace context when metadata is absent#4380

Open
patrickswedish wants to merge 3 commits into
traceloop:mainfrom
patrickswedish:agent/mcp-trace-context
Open

fix(mcp): propagate trace context when metadata is absent#4380
patrickswedish wants to merge 3 commits into
traceloop:mainfrom
patrickswedish:agent/mcp-trace-context

Conversation

@patrickswedish

@patrickswedish patrickswedish commented Jul 26, 2026

Copy link
Copy Markdown

Creates MCP request metadata when callers omit it, injects the full W3C trace carrier while preserving existing metadata, and adds regression coverage for absent and pre-populated metadata. Verification: all 7 MCP instrumentation tests pass; Ruff passes on both changed files. Fixes #4374.

Summary by CodeRabbit

  • Bug Fixes
    • Improved trace-context propagation for MCP client requests when request metadata is missing.
    • Preserves any existing custom request metadata while adding trace context.
    • Injects the full set of available trace-context fields (e.g., traceparent and tracestate) into request metadata.
  • Tests
    • Added unit tests for both missing-metadata and pre-existing-custom-metadata scenarios to verify correct injection behavior.

@CLAassistant

CLAassistant commented Jul 26, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ba562de5-dea9-4067-9a1a-53814bfe5297

📥 Commits

Reviewing files that changed from the base of the PR and between 0367276 and 5773bd8.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py

📝 Walkthrough

Walkthrough

The MCP client instrumentation now injects all propagated trace-context fields into request metadata, creates metadata when absent, preserves existing custom metadata, and adds parametrized tests for both cases.

Changes

MCP trace context propagation

Layer / File(s) Summary
Metadata injection and validation
packages/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/instrumentation.py, packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py
patch_mcp_client creates RequestParams.Meta() when needed, injects the full carrier into params.meta, and preserves existing custom metadata; tests cover absent and pre-existing metadata.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: propagating trace context when MCP metadata is absent.
Linked Issues check ✅ Passed The changes create metadata when absent, inject full trace context, preserve existing fields, and add regression tests as requested.
Out of Scope Changes check ✅ Passed The PR stays focused on MCP trace-context propagation and related tests, with no obvious unrelated code changes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@patrickswedish
patrickswedish force-pushed the agent/mcp-trace-context branch from 5d6740e to fda8f7e Compare July 26, 2026 20:32
@patrickswedish
patrickswedish marked this pull request as ready for review July 26, 2026 20:43

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py`:
- Around line 30-41: Update the regression test around patch_mcp_client to
exercise the tools/call branch: set message.root.method to "tools/call",
configure the instrumentor’s _handle_tool_call mock to return "sent", and assert
that result while preserving the existing trace metadata assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ce3eddf0-c7e6-4a38-8a5d-f65f3cab0c5a

📥 Commits

Reviewing files that changed from the base of the PR and between 93429cf and fda8f7e.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-mcp/opentelemetry/instrumentation/mcp/instrumentation.py
  • packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py

Comment thread packages/opentelemetry-instrumentation-mcp/tests/test_client_trace_context.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: MCP client instrumentation never propagates trace context for ordinary tool calls (regression from #3179)

2 participants