Description
header_provider on MCPStreamableHTTPTool only gets called from call_tool(). The initialize handshake sent during connect() goes out before any tool call happens, so it never gets headers — for MCP servers that require auth on initialize (Azure AI Search knowledge-base MCP endpoints, for example), this means an immediate 401 and the tool never connects.
Root cause is in _mcp.py — _header_provider is only invoked in MCPStreamableHTTPTool.call_tool(), and session.initialize() runs earlier in connect()/_connect_on_owner(), before any tool call could happen.
I haven't contributed to a large repo like this before, attempting to be as good of a steward as possible but may have missed guidance along the way. Happy to make any corrections.
Code Sample
\`\`\`python
def auth_headers(_kwargs: dict) -> dict[str, str]:
print("called") # never prints
return {"Authorization": f"Bearer {get_token()}"}
tool = MCPStreamableHTTPTool(
name="kb",
url="https://<search-service>.search.windows.net/knowledgebases/<kb>/mcp?api-version=2025-11-01-Preview",
header_provider=auth_headers,
)
agent = Agent(client=client, tools=[tool])
await agent.run("hello") # 401 on initialize, header_provider never runs
\`\`\`
Error Messages / Stack Traces
`Error MCP server failed to initialize: Cancelled via cancel scope #####`
As seen when using a hosted Foundry agent to connect to an Azure AI Search Knowledgebase
Package Versions
agent-framework-core 1.1.0
Python Version
3.1.2
Additional Context
No response
Description
header_provideronMCPStreamableHTTPToolonly gets called fromcall_tool(). Theinitializehandshake sent duringconnect()goes out before any tool call happens, so it never gets headers — for MCP servers that require auth oninitialize(Azure AI Search knowledge-base MCP endpoints, for example), this means an immediate 401 and the tool never connects.Root cause is in
_mcp.py—_header_provideris only invoked inMCPStreamableHTTPTool.call_tool(), andsession.initialize()runs earlier inconnect()/_connect_on_owner(), before any tool call could happen.I haven't contributed to a large repo like this before, attempting to be as good of a steward as possible but may have missed guidance along the way. Happy to make any corrections.
Code Sample
Error Messages / Stack Traces
Package Versions
agent-framework-core 1.1.0
Python Version
3.1.2
Additional Context
No response