You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: [Bug]: mcp server can not receive self-define header #7161
🤖 Automated triage reproduction notes(agent-authored — trust but verify)
Agent analysis
Repro: MCPStreamableHTTPTool.call_tool() in packages/core/agent_framework/_mcp.py sets _mcp_call_headers ContextVar at line 3106, but the _inject_headers httpx event hook at line 3074 reads it in the MCP transport's post_writer task context (spawned during connect via streamable_http_client), which never has the var set. Minimal repro: create an MCPStreamableHTTPTool with a header_provider, connect it, then call call_tool—the headers will be empty in the actual HTTP request because the transport's task group was created before call_tool set the ContextVar. A fix must propagate headers through a mechanism that crosses task boundaries (e.g., attaching headers to the session message metadata or write stream rather than relying on contextvars).
I'd like to take this one — could you assign it to me?
The reporter's debugging and the triage notes match what I see in the code: _inject_headers runs inside the transport's post_writer task, which got its own copy of the context when the connection was opened, so the _mcp_call_headers.set() that happens later in call_tool() is never visible there and the hook reads {}. Since a ContextVar can't cross that task boundary, the headers need to travel another way — either a per-call snapshot on the tool instance that the hook falls back to, or propagation through the request metadata as the triage notes suggest. I'll start with a regression test that fails on current main, and can go with whichever fix shape you prefer.
Activity
github-actions commentedon Jul 17, 2026
🤖 Automated triage reproduction notes (agent-authored — trust but verify)
Agent analysis
Repro: MCPStreamableHTTPTool.call_tool() in packages/core/agent_framework/_mcp.py sets _mcp_call_headers ContextVar at line 3106, but the _inject_headers httpx event hook at line 3074 reads it in the MCP transport's post_writer task context (spawned during connect via streamable_http_client), which never has the var set. Minimal repro: create an MCPStreamableHTTPTool with a header_provider, connect it, then call call_tool—the headers will be empty in the actual HTTP request because the transport's task group was created before call_tool set the ContextVar. A fix must propagate headers through a mechanism that crosses task boundaries (e.g., attaching headers to the session message metadata or write stream rather than relying on contextvars).
python/packages/core/tests/core/test_mcp_header_contextvar_bug.py1.11.01.11.0robbiebusinessacc commentedon Jul 17, 2026
I'd like to take this one — could you assign it to me?
The reporter's debugging and the triage notes match what I see in the code:
_inject_headersruns inside the transport'spost_writertask, which got its own copy of the context when the connection was opened, so the_mcp_call_headers.set()that happens later incall_tool()is never visible there and the hook reads{}. Since a ContextVar can't cross that task boundary, the headers need to travel another way — either a per-call snapshot on the tool instance that the hook falls back to, or propagation through the request metadata as the triage notes suggest. I'll start with a regression test that fails on current main, and can go with whichever fix shape you prefer.moonbox3 commentedon Jul 20, 2026
Please have at it, Robbie Walmsley (@robbiebusinessacc). Let us know when there's a PR to review. Thank you.