Skip to main content
When using MCP tools, you will often want to send information to the MCP server via HTTP headers. For example, you may want to pass information about the current user, or authorization related data. To do this dynamically, you can use the header_provider parameter when initializing the MCPTools class. You will also be able to access the RunContext object, which contains the current run’s context, including the user ID, session ID, and metadata, together with the contextual Agent or Team. Simply add the run_context, agent, and team parameters to your header_provider function.
Dynamic headers are only relevant when using HTTP-based transports (streamable-http or sse). The stdio transport does not support headers.

The header_provider function

The header_provider function you pass to your MCPTools instance will be used to update the headers every run. The function is expected to return a dict of header name-value pairs. The following parameters will be automatically injected into the function and can be useful to generate the headers: You can read more about the RunContext object and its fields in the RunContext reference.

Complete Example

  1. Run the example MCP server:
server.py
  1. Run the example client:
  1. You can check the logs of your MCP server to see the headers that were sent.

Usage with MultiMCPTools

Dynamic headers work the same way with MultiMCPTools: