A2A MCP服务器
一个mcp服务器,将模型上下文协议(mcp)与代理到代理(A2A)协议连接起来,使mcp兼容的AI助手(如Claude)能够与A2A代理无缝交互。
概述
该项目作为两个尖端AI代理协议之间的集成层:
- **模型上下文协议(MCP)**MCP由Anthropic开发,允许AI助手连接到外部工具和数据源。它规范了人工智能应用程序和大型语言模型如何以安全、可组合的方式连接到外部资源。
- **代理对代理协议(A2A)**A2A由谷歌开发,通过标准化的JSON-RPC接口实现不同AI代理之间的通信和互操作性。
通过桥接这些协议,该服务器允许MCP客户端(如Claude)通过统一的接口发现、注册、通信和管理A2A代理上的任务。
演示
1、运行A2A示例中的货币代理
also support cloud deployed Agent
2、使用Claude注册货币代理人
3、使用Claude向货币代理发送任务并获取结果
特性
-
代理管理
-
在网桥服务器上注册A2A代理
-
列出所有注册代理人
-
不再需要时注销代理
-
沟通
-
向A2A代理发送消息并接收响应
-
实时流式传输A2A代理的响应
-
任务管理
-
跟踪哪个A2A代理处理哪个任务
-
使用任务ID检索任务结果
-
取消正在运行的任务
-
运输支持
-
多种传输类型:stdio、可流式传输http、SSE
-
使用MCP_transport环境变量配置传输类型
安装
通过Smithery安装
通过以下方式自动安装Claude Desktop的A2A网桥服务器 史密瑟里:
BASH``` 1npx -y @smithery/cli install @GongRzhe/A2A-MCP-Server —client claude
### 选项1:从PyPI安装
BASH```
1pip install a2a-mcp-server
选项2:本地安装
- 克隆存储库:
BASH``` 1 2git clone https://github.com/GongRzhe/A2A-MCP-Server.git cd A2A-MCP-Server
2. 设置虚拟环境:
BASH```
1
2python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
- 安装依赖项:
BASH``` 1pip install -r requirements.txt
## 配置
### 环境变量
使用以下环境变量配置MCP服务器的运行方式:
BASH```
1
2
3
4
5
6
7
8
9
10
11
12# Transport type: stdio, streamable-http, or sse
export MCP_TRANSPORT="streamable-http"
# Host for the MCP server
export MCP_HOST="0.0.0.0"
# Port for the MCP server (when using HTTP transports)
export MCP_PORT="8000"
# Path for the MCP server endpoint (when using HTTP transports)
export MCP_PATH="/mcp"
# Path for SSE endpoint (when using SSE transport)
export MCP_SSE_PATH="/sse"
# Enable debug logging
export MCP_DEBUG="true"
运输类型
A2A MCP服务器支持多种传输类型:
- 标准 (默认):使用标准输入/输出进行通信
- 非常适合命令行使用和测试
- 未启动HTTP服务器
- Claude Desktop需要
- 可流式传输http (建议用于web客户端):支持流媒体的HTTP传输
- 建议用于生产部署
- 启动HTTP服务器以处理MCP请求
- 支持大响应的流式传输
- SSE:服务器发送事件传输
- 提供实时事件流
- 可用于实时更新
要指定传输类型,请执行以下操作:
BASH``` 1 2 3 4 5# Using environment variable export MCP_TRANSPORT=“streamable-http” uvx a2a-mcp-server
Or directly in the command
MCP_TRANSPORT=streamable-http uvx a2a-mcp-server
## 运行服务器
### 从命令行
BASH```
1
2
3
4# Using default settings (stdio transport)
uvx a2a-mcp-server
# Using HTTP transport on specific host and port
MCP_TRANSPORT=streamable-http MCP_HOST=127.0.0.1 MCP_PORT=8080 uvx a2a-mcp-server
在Claude Desktop中配置
Claude Desktop允许您在 claude_desktop_config.json 文件。此文件通常位于:
- 视窗:
%APPDATA%\Claude\claude_desktop_config.json - macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
方法1:PyPI安装(推荐)
将以下内容添加到 mcpServers 你的部分 claude_desktop_config.json:
JSON``` 1 2 3 4 5 6”a2a”: { “command”: “uvx”, “args”: [ “a2a-mcp-server” ] }
请注意,对于Claude Desktop,您必须使用 `"MCP_TRANSPORT": "stdio"` 因为Claude需要与MCP服务器进行stdio通信。
### 方法2:本地安装
如果您已克隆存储库并希望从本地安装运行服务器:
JSON```
1
2
3
4
5
6
7
8
9
10"a2a": {
"command": "C:\\path\\to\\python.exe",
"args": [
"C:\\path\\to\\A2A-MCP-Server\\a2a_mcp_server.py"
],
"env": {
"MCP_TRANSPORT": "stdio",
"PYTHONPATH": "C:\\path\\to\\A2A-MCP-Server"
}
}
替换 C:\\path\\to\\ 使用系统上的实际路径。
使用配置创建器
此存储库包括 config_creator.py 帮助您生成配置的脚本:
BASH``` 1 2# If using local installation python config_creator.py
脚本将:
- 尽可能自动检测Python、脚本和存储库路径
- 配置Claude Desktop所需的stdio传输
- 如果需要,可以添加任何其他环境变量
- 创建或更新您的Claude Desktop配置文件
### 完整示例
以下是一个完整的示例 `claude_desktop_config.json` 配置了A2A MCP服务器的文件:
JSON```
1
2
3
4
5
6
7
8
9
10{
"mcpServers": {
"a2a": {
"command": "uvx",
"args": [
"a2a-mcp-server"
]
}
}
}
与MCP客户端一起使用
克劳德
Claude可以通过此服务器提供的MCP工具使用A2A代理。以下是如何设置它:
- 对于Claude Web:使用可流式传输的http启动MCP服务器:
BASH``` 1MCP_TRANSPORT=streamable-http MCP_HOST=127.0.0.1 MCP_PORT=8000 uvx a2a-mcp-server
2. 对于Claude Web:在Claude Web界面中,在“工具”菜单中启用MCP URL连接。
- 使用URL: `http://127.0.0.1:8000/mcp`
3. 对于Claude Desktop:将配置添加到您的 `claude_desktop_config.json` 如上所述的文件。最简单的方法是使用提供的 `config_creator.py` 该脚本将自动检测路径并创建正确的配置。
4. 在Claude中,您现在可以使用以下函数:
**注册A2A代理:**
1 2I need to register a new agent. Can you help me with that? (Agent URL: http://localhost:41242)
**向代理发送消息:**
1Ask the agent at http://localhost:41242 what it can do.
**检索任务结果:**
1Can you get the results for task ID: 550e8400-e29b-41d4-a716-446655440000?
### 光标IDE
Cursor IDE可以连接到MCP服务器,为其AI助手添加工具:
1. 使用可流式传输的http运行A2A MCP服务器:
BASH```
1MCP_TRANSPORT=streamable-http MCP_HOST=127.0.0.1 MCP_PORT=8000 uvx a2a-mcp-server
- 在Cursor IDE中,转到设置>AI>MCP服务器
- 使用URL添加新的MCP服务器:
http://127.0.0.1:8000/mcp - 启用服务器
- 现在,您可以在Cursor的AI助手中使用A2A工具。
Windsurf浏览器
Windsurf是一款内置MCP支持的浏览器:
- 使用可流式传输的http运行A2A MCP服务器:
BASH``` 1MCP_TRANSPORT=streamable-http MCP_HOST=127.0.0.1 MCP_PORT=8000 uvx a2a-mcp-server
2. 在Windsurf浏览器中,转到“设置”>“MCP连接”
- 使用URL添加新的MCP连接: `http://127.0.0.1:8000/mcp`
- 启用连接
3. 您现在可以在Windsurf的AI助手中使用A2A工具。
## 可用的MCP工具
服务器公开了以下MCP工具,用于与Claude等LLM集成:
### 代理管理
- **注册代理**:在网桥服务器上注册A2A代理
JSON```
1
2
3
4
5
6{
"name": "register_agent",
"arguments": {
"url": "http://localhost:41242"
}
}
- 列表_代理:获取所有注册代理人的列表
JSON``` 1 2 3 4{ “name”: “list_agents”, “arguments”: {} }
- **注销代理**:从网桥服务器中删除A2A代理
JSON```
1
2
3
4
5
6{
"name": "unregister_agent",
"arguments": {
"url": "http://localhost:41242"
}
}
消息处理
- send_message:向代理发送消息并获取响应的task_id
JSON``` 1 2 3 4 5 6 7 8{ “name”: “send_message”, “arguments”: { “agent_url”: “http://localhost:41242”, “message”: “What’s the exchange rate from USD to EUR?”, “session_id”: “optional-session-id” } }
- **send_message_stream**:发送消息并流式传输响应
JSON```
1
2
3
4
5
6
7
8{
"name": "send_message_stream",
"arguments": {
"agent_url": "http://localhost:41242",
"message": "Tell me a story about AI agents.",
"session_id": "optional-session-id"
}
}
任务管理
- get_task_result:使用任务的ID检索任务的结果
JSON``` 1 2 3 4 5 6 7{ “name”: “get_task_result”, “arguments”: { “task_id”: “b30f3297-e7ab-4dd9-8ff1-877bd7cfb6b1”, “history_length”: null } }
- **cancel_task**:取消正在运行的任务
JSON```
1
2
3
4
5
6{
"name": "cancel_task",
"arguments": {
"task_id": "b30f3297-e7ab-4dd9-8ff1-877bd7cfb6b1"
}
}
使用示例
基本工作流程
1
2
3
4
51. Client registers an A2A agent
↓
2. Client sends a message to the agent (gets task_id)
↓
3. Client retrieves the task result using task_id
以Claude作为MCP客户端的示例
1
2
3
4
5
6
7
8
9User: Register an agent at http://localhost:41242
Claude uses: register_agent(url="http://localhost:41242")
Claude: Successfully registered agent: ReimbursementAgent
User: Ask the agent what it can do
Claude uses: send_message(agent_url="http://localhost:41242", message="What can you do?")
Claude: I've sent your message. Here's the task_id: b30f3297-e7ab-4dd9-8ff1-877bd7cfb6b1
User: Get the answer to my question
Claude uses: get_task_result(task_id="b30f3297-e7ab-4dd9-8ff1-877bd7cfb6b1")
Claude: The agent replied: "I can help you process reimbursement requests. Just tell me what you need to be reimbursed for, including the date, amount, and purpose."
建筑
A2A MCP服务器由几个关键组件组成:
- FastMCP服务器:向MCP客户端公开工具
- A2A客户端:与注册的A2A代理商沟通
- 任务管理器:处理任务转发和管理
- 代理取卡器:检索有关A2A代理的信息
通信流
1
2
3MCP Client → FastMCP Server → A2A Client → A2A Agent
↑ ↓
└──── Response ──┘
任务ID管理
当向A2A代理发送消息时,服务器:
- 生成唯一
task_id - 将此ID映射到代理的URL
task_agent_mapping词典 - 返回
task_id到MCP客户端 - 使用此映射来路由任务检索和取消请求
错误处理
服务器为常见问题提供详细的错误消息:
- 代理人未注册
- 找不到任务ID
- 与代理的连接错误
- 解析响应中的错误
故障排除
代理注册问题
如果代理无法注册:
- 验证代理URL是否正确且可访问
- 检查代理人是否持有正确的代理人卡
/.well-known/agent.json
邮件传递问题
如果邮件未送达:
- 确保代理已注册(使用
list_agents) - 验证代理是否正在运行且可访问
任务结果检索问题
如果无法检索任务结果:
- 确保你使用了正确的task_id
- 检查是否已经过去了太多时间(一些代理可能会丢弃旧任务)
运输问题
如果您对特定的运输类型有问题:
- stdio问题:确保输入/输出流未被重定向或修改
- 可流式传输的http问题:检查端口是否可用且未被防火墙阻止
- sse问题:验证客户端是否支持服务器发送事件
Claude桌面配置问题
如果Claude Desktop未启动A2A MCP服务器:
- 检查您的路径
claude_desktop_config.json是正确的 - 如果使用以下命令,请验证Python是否在您的PATH中
"command": "python" - 对于本地安装,确保PYTHONPATH正确
- 确保
MCP_TRANSPORT设置为"stdio"在env章节 - 尝试手动运行该命令,看看它是否在Claude之外工作
- 使用
config_creator.py自动路径检测和配置脚本
发展
添加新工具方法
要向服务器添加新功能,请添加用 @mcp.tool() 在 a2a_mcp_server.py 文件。
自定义任务管理器
服务器使用自定义 A2AServerTaskManager 扩展类 InMemoryTaskManager。您可以通过修改此类来自定义其行为。
项目结构
1
2
3
4
5
6
7
8
9
10
11
12a2a-mcp-server/
├── a2a_mcp_server.py # Main server implementation
├── common/ # A2A protocol code (from google/A2A)
│ ├── client/ # A2A client implementation
│ ├── server/ # A2A server implementation
│ ├── types.py # Common type definitions
│ └── utils/ # Utility functions
├── config_creator.py # Script to help create Claude Desktop configuration
├── .gitignore # Git ignore file
├── pyproject.toml # Project metadata and dependencies
├── README.md # This file
└── requirements.txt # Project dependencies
许可证
此项目根据Apache许可证2.0版获得许可-请参阅 许可证 文件以获取详细信息。
代码在 common/ 目录来自 谷歌A2A项目 并且还根据Apache许可证2.0版获得许可。