> ## Documentation Index
> Fetch the complete documentation index at: https://phidatainc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AgentOS with MCPTools using dynamic headers

> Forward per-run user and session context from AgentOS to an MCP server with dynamic HTTP headers.

```python client.py theme={null}
"""
AgentOS with MCPTools using dynamic headers.

This example shows how to pass user context to external MCP servers.
The header_provider receives run_context, agent, and team - allowing you to
forward user info, session data, or entity names to MCP tools.

Usage:
1. Start the MCP server: python server.py
2. Start AgentOS: python client.py
3. Test at http://localhost:7777/docs
   - Call the standalone agent: POST /agents/greeting-agent/runs
   - Call the team: POST /teams/greeting-team/runs
"""

from typing import TYPE_CHECKING, Optional

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIChat
from agno.os import AgentOS
from agno.run import RunContext
from agno.team.team import Team
from agno.tools.mcp import MCPTools

# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------

if TYPE_CHECKING:
    from agno.agent import Agent as AgentType
    from agno.team.team import Team as TeamType


# We will use this tool to generate headers dinamically for our MCP tools.
def header_provider(
    run_context: RunContext,
    agent: Optional["AgentType"] = None,
    team: Optional["TeamType"] = None,
) -> dict:
    """
    Generate headers from run context to pass to external MCP server.

    When users call the AgentOS API with user_id/session_id, those values
    flow through run_context and get forwarded to the MCP server.
    """
    return {
        "X-User-ID": run_context.user_id or "anonymous",
        "X-Session-ID": run_context.session_id or "unknown",
        "X-Agent-Name": agent.name if agent else "unknown",
        "X-Team-Name": team.name if team else "none",
    }


db = SqliteDb(db_file="tmp/agentos.db")

# MCP tools with dynamic headers - shared by all agents
mcp_tools = MCPTools(
    url="http://localhost:8000/mcp",
    header_provider=header_provider,
)


# Agent with MCP tools
greeting_agent = Agent(
    name="greeting-agent",
    role="Greet users in a friendly, casual manner",
    model=OpenAIChat(id="gpt-5"),
    tools=[mcp_tools],
)

# Team containing multiple agents with MCP tools
greeting_team = Team(
    id="greeting-team",
    model=OpenAIChat(id="gpt-5"),
    members=[greeting_agent],
    instructions="Choose the appropriate greeter based on context. Use the greet tool.",
    db=db,
)

# AgentOS with both standalone agent and team
agent_os = AgentOS(
    description="AgentOS showcasing dynamic headers for MCP tools",
    teams=[greeting_team],
    agents=[greeting_agent],
)

app = agent_os.get_app()

# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    agent_os.serve(app="client:app")
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U "agno[mcp,os]" openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Start the MCP server">
    Follow the [server example](/examples/agent-os/mcp-demo/dynamic-headers/server) to save `server.py`, then start it in another terminal and keep it running:

    ```bash theme={null}
    python server.py
    ```
  </Step>

  <Step title="Run the example">
    Save the code above as `client.py`, then run:

    ```bash theme={null}
    python client.py
    ```
  </Step>
</Steps>

Full source: [cookbook/05\_agent\_os/mcp\_demo/dynamic\_headers/client.py](https://github.com/agno-agi/agno/blob/main/cookbook/05_agent_os/mcp_demo/dynamic_headers/client.py)
