Skip to content

Python: Fix MCP header provider across transport tasks#6162

Open
kiwigitops wants to merge 1 commit into
microsoft:mainfrom
kiwigitops:fix-mcp-header-provider-runtime-headers
Open

Python: Fix MCP header provider across transport tasks#6162
kiwigitops wants to merge 1 commit into
microsoft:mainfrom
kiwigitops:fix-mcp-header-provider-runtime-headers

Conversation

@kiwigitops
Copy link
Copy Markdown

Fixes #4808.

MCPStreamableHTTPTool currently stores per-call header_provider output in a ContextVar that the httpx request hook reads later. With the streamable HTTP transport, outbound requests can be sent from a transport task that was created before call_tool() set that context, so the hook can see an empty value and skip the dynamic headers.

This keeps the active headers on the tool instance for the duration of the tool call, so the existing request hook reads the current header snapshot even when the transport sends from another task. The previous value is restored after the call completes.

Tested:

  • python -m pytest python/packages/core/tests/core/test_mcp.py -k header_provider -q
  • python -m ruff check python/packages/core/agent_framework/_mcp.py python/packages/core/tests/core/test_mcp.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 28, 2026 18:42
@github-actions github-actions Bot changed the title Fix MCP header provider across transport tasks Python: Fix MCP header provider across transport tasks May 28, 2026
Signed-off-by: kiwigitops <kiwisclubco@gmail.com>
@kiwigitops kiwigitops force-pushed the fix-mcp-header-provider-runtime-headers branch from 0f26b90 to 2943e29 Compare May 30, 2026 19:37
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.

Python: [Bug]: Unable to pass AgentContext to MCPStreamableHTTPTool

2 participants