Programmatic Usage(Python)
MCP with AutoGen
Use MCP tools inside AutoGen multi-agent systems
Overview
AutoGen supports MCP through McpWorkbench, which connects to MCP servers
and exposes their tools to AssistantAgent instances.
Setup
pip install autogen-agentchat autogen-ext[openai,mcp]Step 1: Create the MCP Workbench
from autogen_ext.tools.mcp import McpWorkbench, SseServerParams
server_params = SseServerParams(
url="https://api.mcphero.app/mcp/{server_id}/sse",
headers={"Authorization": "Bearer <your-api-key>"},
)Step 2: Create an Agent
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(model="gpt-4o")Step 3: Run the Agent
import asyncio
async def main():
async with McpWorkbench(server_params) as workbench:
agent = AssistantAgent(
name="assistant",
model_client=model_client,
workbench=workbench,
reflect_on_tool_use=True,
)
result = await agent.run(
task="Find customer John Smith and return their last order"
)
print(result.messages[-1].content)
await model_client.close()
asyncio.run(main())When to Use AutoGen
- Multi-agent collaboration
- Tool delegation across agent teams
- Research or planning workflows