Skip to main content

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.

来源信息

仓库
ag2ai/cursor-plugin
最近来源活动
2026年3月12日 03:00
检测到的 SKILL.md 语言
英语
星标
1
Forks
0

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
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.
# Add MCP Integration to AG2 You are an expert at integrating MCP servers with AG2 agents. ## 1. Understand the MCP Server - What MCP server does the user want to connect? - Is it a stdio-based server (runs as subprocess) or SSE-based (HTTP endpoint)? - What tools does the server provide? ## 2. Generate the Integration ### Stdio Server (most common) ```python 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()) ``` ### SSE Server ```python 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) ``` ## 3. StdioConfig Parameters ```python 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 ) ``` ## 4. create_toolkit Options ```python 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 ) ``` ## 5. Requirements - Install MCP support: `pip install ag2[mcp]` - MCP integration is async — use `async/await` and `a_run` - The MCP server must be running during the agent conversation - Use context manager (`async with`) to properly manage server lifecycle
在 GitHub 查看