Skip to content

Python: Fix HTTP call to MCP losing tracing parent (#3623)#3934

Draft
alliscode wants to merge 1 commit intomicrosoft:mainfrom
alliscode:issue-3623
Draft

Python: Fix HTTP call to MCP losing tracing parent (#3623)#3934
alliscode wants to merge 1 commit intomicrosoft:mainfrom
alliscode:issue-3623

Conversation

@alliscode
Copy link
Member

Add OpenTelemetry trace context propagation to MCPStreamableHTTPTool's httpx client via an event hook that injects W3C traceparent/tracestate headers into outgoing HTTP requests. This ensures the remote MCP server's spans are correctly parented under the agent framework's Execute Tool span.

  • Add _inject_otel_context async event hook using opentelemetry.propagate.inject
  • Add _get_instrumented_httpx_client to ensure all httpx clients have the hook
  • Cache auto-created httpx clients; clean up in close()
  • Update and add tests for trace context injection

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Add OpenTelemetry trace context propagation to MCPStreamableHTTPTool's
httpx client via an event hook that injects W3C traceparent/tracestate
headers into outgoing HTTP requests. This ensures the remote MCP server's
spans are correctly parented under the agent framework's Execute Tool span.

- Add _inject_otel_context async event hook using opentelemetry.propagate.inject
- Add _get_instrumented_httpx_client to ensure all httpx clients have the hook
- Cache auto-created httpx clients; clean up in close()
- Update and add tests for trace context injection

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _mcp.py4246684%72, 74, 117–118, 128–133, 144, 149, 195–196, 206–211, 221–222, 274, 283, 346, 354, 375, 489, 556, 591, 593, 597–598, 600–601, 655, 670, 688, 729, 835, 848–853, 875, 921–922, 928–930, 949, 974–975, 979–983, 1000–1004, 1148
TOTAL20494321784% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3945 225 💤 0 ❌ 0 🔥 1m 9s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants