Programmatic Usage(Python)
MCP with OpenAI Agents
Use MCP tools with the OpenAI Agents SDK
Overview
The OpenAI Agents SDK has built-in MCP support via MCPServerStreamableHttp,
allowing agents to discover and call your MCP tools natively.
Setup
pip install openai-agents mcpStep 1: Create the MCP Server Abstraction
from agents.mcp import MCPServerStreamableHttp
server = MCPServerStreamableHttp(
name="mcphero",
params={
"url": "https://api.mcphero.app/mcp/{server_id}/mcp",
"headers": {"Authorization": "Bearer <your-api-key>"},
},
cache_tools_list=True,
)Step 2: Create an Agent
from agents import Agent
agent = Agent(
name="Assistant",
instructions="Use the MCP tools to answer questions.",
mcp_servers=[server],
)Step 3: Run the Agent
import asyncio
from agents import Runner
async def main():
async with server:
result = await Runner.run(
agent,
"Find customer John Smith and summarize their account",
)
print(result.final_output)
asyncio.run(main())Why OpenAI Agents
- Native MCP integration
- Built-in tool calling and agent loop
- Supports handoffs between multiple agents
- Built-in tracing for debugging