mit einem Klick
add-mcp-integration
// Wire a Model Context Protocol (MCP) server into AG2 agents using create_toolkit. Use when the user wants to connect external tools via MCP.
// Wire a Model Context Protocol (MCP) server into AG2 agents using create_toolkit. Use when the user wants to connect external tools via MCP.
Add code execution capability to AG2 agents using LocalCommandLineCodeExecutor or Docker. Use when the user wants agents that can write and run Python code.
Add safety guardrails to AG2 agents using LLMGuardrail and RegexGuardrail. Use when the user wants to enforce safety constraints, filter PII, or redirect off-topic responses.
Build an AG2 handoff-driven workflow with DefaultPattern, agent handoffs, context variables, and routing. Use when the user wants customer service routing, state-machine workflows, or explicit agent-to-agent transitions.
Build a Retrieval-Augmented Generation (RAG) agent using AG2's RetrieveUserProxyAgent with vector database support. Use when the user wants agents that can query documents or knowledge bases.
Build an AG2 ReasoningAgent that uses tree-of-thought reasoning with beam search, MCTS, or LATS strategies. Use when the user needs advanced reasoning for complex problem solving.
Build a complete web research agent team using AG2 with search tools, web crawling, and structured output. Use when the user wants a practical research or information-gathering workflow.
| name | add-mcp-integration |
| description | Wire a Model Context Protocol (MCP) server into AG2 agents using create_toolkit. Use when the user wants to connect external tools via MCP. |
You are an expert at integrating MCP servers with AG2 agents.
import asyncio
from autogen import ConversableAgent, LLMConfig
from autogen.mcp import create_toolkit
from autogen.mcp.mcp_client import StdioConfig
async def main():
llm_config = LLMConfig(
{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}
)
agent = ConversableAgent(
name="assistant",
llm_config=llm_config,
human_input_mode="NEVER",
)
# Configure the MCP server
stdio_config = StdioConfig(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
)
# Create toolkit from MCP server and register with agent
async with stdio_config.connect() as session:
toolkit = await create_toolkit(session)
toolkit.register_for_llm(agent)
toolkit.register_for_execution(agent)
# Now use the agent — it has all MCP tools available
user_proxy = ConversableAgent(
name="user",
llm_config=False,
human_input_mode="NEVER",
)
result = await user_proxy.a_run(
agent,
message="List files in the directory",
)
await result.process()
asyncio.run(main())
from autogen.mcp.mcp_client import SseConfig
sse_config = SseConfig(
url="http://localhost:8080/sse",
headers={"Authorization": f"Bearer {os.environ['MCP_TOKEN']}"},
timeout=5,
sse_read_timeout=300,
)
async with sse_config.connect() as session:
toolkit = await create_toolkit(session)
toolkit.register_for_llm(agent)
toolkit.register_for_execution(agent)
StdioConfig(
command="npx", # Command to run
args=["-y", "server-package"], # Arguments
environment={"KEY": "value"}, # Optional env vars
working_dir="/path/to/dir", # Optional working directory
encoding="utf-8", # Default
)
toolkit = await create_toolkit(
session,
use_mcp_tools=True, # Import tools from MCP server
use_mcp_resources=True, # Import resources from MCP server
resource_download_folder="./mcp_resources", # Where to save resources
)
pip install ag2[mcp]async/await and a_runasync with) to properly manage server lifecycle