MCP代理编排系统
使用模型上下文协议(MCP)的基于状态的代理编排系统的Python实现。
什么是MCP?
模型上下文协议(MCP)允许应用程序以标准化的方式为LLM提供上下文,将提供上下文的关注点与实际的LLM交互分开。使用MCP,您可以构建公开以下内容的服务器:
- 资源:向LLM提供信息的数据源
- 工具:允许LLM执行操作的功能
- 提示:LLM交互的可重用模板
安装
先决条件
- Python 3.10或更高版本
- MCP Python SDK 1.2.0或更高版本
设置您的环境
使用紫外线(推荐)
# 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.py- 在单独的终端中,运行客户端以查看正在运行的编排:
python orchestrator_client.py与Claude for Desktop集成
1.安装桌面版Claude
确保您安装了Claude for Desktop。您可以从以下网址下载最新版本 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- 添加编排器服务器配置:
{
"mcpServers": {
"agent-orchestrator": {
"command": "python",
"args": [
"/ABSOLUTE/PATH/TO/YOUR/PROJECT/orchestrator.py"
]
}
}
}将路径替换为orchestrator.py文件的绝对路径。
- 保存配置文件并重新启动Claude for Desktop。
3.使用克劳德的编曲
配置后,您可以:
- 打开Claude桌面版
- 点击侧栏中的MCP服务器图标
- 从可用服务器列表中选择“代理编排器”
- 开始与编排系统交互
克劳德将能够:
- 不同代理状态之间的转换
- 存储和检索知识库中的信息
- 跨状态转换维护对话上下文
- 访问状态特定提示
代理国
编排系统实现了具有以下状态的状态机:
- 闲置:等待指示
- 规划:为任务创建结构化计划
- 研究:收集任务所需的信息
- 执行:执行计划的行动
- 审查:评估结果并确定下一步行动
- 错误:处理错误或意外情况
自定义系统
添加新州
- 将状态添加到
AgentState枚举在orchestrator.py - 为新状态创建提示函数
- 更新中的转换逻辑
_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许可证获得许可-有关详细信息,请参阅许可证文件。
