Skip to content

Python: [Bug]: header_provider not applied to MCP initialize() request #7079

Description

@efranken

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

Metadata

Metadata

Assignees

Labels

pythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Fields

No fields configured for Bug.

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions