Programmatic Usage(Python)
MCP with Pydantic AI
Use MCP tools inside Pydantic AI agents
Overview
Pydantic AI has native MCP support via MCPServerStreamableHTTP, allowing agents to automatically discover
and invoke tools exposed by your MCP server.
Setup
pip install pydantic-ai mcpStep 1: Create the MCP Server Abstraction
from pydantic_ai.mcp import MCPServerStreamableHTTP
server = MCPServerStreamableHTTP(
"https://api.mcphero.app/mcp/{server_id}/mcp",
headers={"Authorization": "Bearer <your-api-key>"},
)Step 2: Create an Agent
from pydantic_ai import Agent
agent = Agent(
"openai:gpt-4o",
toolsets=[server],
)Step 3: Run the Agent
async def main():
result = await agent.run(
"Find customer John Smith and return their last order"
)
print(result.output)