MCP代理编排系统
使用模型上下文协议 (MCP) 的基于状态的代理编排系统的 Python 实现。
什么是 MCP?
模型上下文协议 (MCP) 允许应用程序以标准化的方式为 LLM 提供上下文,从而将提供上下文的关注点与实际的 LLM 交互分离开来。使用 MCP,您可以构建服务器来公开:
- 资源:为法学硕士提供信息的数据源
- 工具:允许 LLM 执行操作的功能
- 提示:可重复使用的 LLM 交互模板
安装
先决条件
- Python 3.10 或更高版本
- MCP Python SDK 1.2.0 或更高版本
设置您的环境
使用 uv(推荐)
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create a new directory for our project
uv init mcp-agents-orchestra
cd mcp-agents-orchestra
# Create virtual environment and activate it
uv venv
source .venv/bin/activate # On Unix/macOS
.venv\Scripts\activate # On Windows
# Install dependencies
uv add "mcp[cli]" httpx
使用 pip
# Create a new directory for our project
mkdir mcp-agents-orchestra
cd mcp-agents-orchestra
# Create a virtual environment
python -m venv venv
source venv/bin/activate # On Unix/macOS
venv\Scripts\activate # On Windows
# Install dependencies
pip install "mcp[cli]" httpx
克隆或下载项目文件
将项目文件放在您的目录中:
orchestrator.py
- 实现状态机的主 MCP 服务器orchestrator_client.py
- 演示编排流程的客户端requirements.txt
- 项目依赖项.gitignore
- Git 忽略文件
项目结构
orchestrator.py
- 实现状态机的主 MCP 服务器orchestrator_client.py
- 演示编排流程的客户端requirements.txt
- 项目依赖项
运行编排系统
- 直接启动编排服务器进行测试:
- 在单独的终端中,运行客户端以查看实际的编排过程:
python orchestrator_client.py
与 Claude 桌面版集成
1. 安装 Claude 桌面版
确保已安装 Claude 桌面版。您可以从Anthropic 网站下载最新版本。
2. 配置 Claude 桌面版
- 打开您的 Claude for Desktop 配置文件:macOS/Linux:
# Create or edit the configuration file
code ~/Library/Application\ Support/Claude/claude_desktop_config.json
视窗:# Path may vary depending on your Windows version
code %APPDATA%\Claude\claude_desktop_config.json
- 添加 Orchestrator 服务器配置:
{
"mcpServers": {
"agent-orchestrator": {
"command": "python",
"args": [
"/ABSOLUTE/PATH/TO/YOUR/PROJECT/orchestrator.py"
]
}
}
}
将该路径替换为 Orchestrator.py 文件的绝对路径。 - 保存配置文件并重新启动 Claude for Desktop。
3. 在 Claude 中使用 Orchestrator
配置完成后,您可以:
- 打开 Claude 桌面版
- 点击侧边栏中的 MCP 服务器图标
- 从可用服务器列表中选择“agent-orchestrator”
- 开始与编排系统交互
克劳德将能够:
- 不同代理状态之间的转换
- 从知识库存储和检索信息
- 在状态转换过程中保持对话上下文
- 访问特定于状态的提示
代理状态
编排系统实现了一个具有以下状态的状态机:
- IDLE :等待指令
- 规划:为任务创建结构化计划
- 研究:收集任务所需的信息
- 执行:执行计划的行动
- 审查:评估结果并确定下一步行动
- ERROR :处理错误或意外情况
定制系统
添加新州
- 将状态添加到
orchestrator.py
中的AgentState
枚举中 - 为新状态创建提示函数
- 更新
_get_available_transitions()
中的转换逻辑 - 在资源访问函数中添加新状态的处理程序
创建自定义工具
通过创建用@mcp.tool()
装饰的函数来添加新工具:
@mcp.tool()
def my_custom_tool(arg1: str, arg2: int, ctx: Context) -> str:
"""Description of what this tool does
Args:
arg1: Description of arg1
arg2: Description of arg2
"""
# Implementation here
return "Result"
开发和测试
使用 MCP CLI
MCP CLI 提供开发和测试工具:
# Install MCP CLI if you haven't already
pip install "mcp[cli]"
# Test your server with the MCP Inspector
mcp dev orchestrator.py
# Install in Claude Desktop
mcp install orchestrator.py
使用 Python 进行手动测试
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async with stdio_client(StdioServerParameters(command="python", args=["orchestrator.py"])) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Test state transitions
await session.call_tool("transition_state", arguments={"new_state": "PLANNING"})
资源
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。