MCP 和 A2A 协议
LibreFang 支持 MCP (Model Context Protocol) 和 A2A (Agent to Agent) 协议。
MCP (Model Context Protocol)
MCP 是一个标准化协议,用于连接 LLM 与外部工具和服务。
概述
┌─────────────┐ MCP ┌─────────────┐
│ LibreFang │ ◄─────────────► │ MCP Server │
│ (Client) │ JSON-RPC 2.0 │ (Server) │
└─────────────┘ └─────────────┘
MCP 服务器配置
[[mcp_servers]]
name = "filesystem"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
[[mcp_servers]]
name = "github"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_TOKEN = "your-token" }
MCP 工具命名
MCP 工具命名格式:
mcp_{server}_{tool}
例如:
mcp_filesystem_read_filemcp_github_create_issue
内置 MCP 服务器
LibreFang 包含内置 MCP 服务器:
| 服务器 | 工具 |
|---|---|
| filesystem | read_file, write_file, list_directory |
| github | create_issue, get_pr, search_repos |
| postgres | query, execute, list_tables |
MCP 客户端
作为 MCP 客户端连接到外部服务器:
[[mcp_servers]]
name = "custom"
command = "python"
args = ["./mcp_server.py"]
开发 MCP 服务器
from mcp.server import Server
from mcp.types import Tool, TextContent
app = Server("my-server")
@app.list_tools()
async def list_tools():
return [
Tool(
name="my_tool",
description="My custom tool",
inputSchema={"type": "object", "properties": {}}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict):
return [TextContent(type="text", text="result")]
A2A (Agent to Agent)
A2A 协议支持 LibreFang agents 之间的通信。
概述
┌─────────────┐ A2A ┌─────────────┐
│ Agent A │ ◄─────────────► │ Agent B │
└─────────────┘ JSON over └─────────────┘
HTTP/WebSocket
Agent Card
每个 Agent 发布 Agent Card:
{
"name": "researcher",
"description": "Deep research agent",
"url": "http://localhost:4200/a2a",
"version": "1.0.0",
"capabilities": {
"streaming": true,
"pushNotifications": false
},
"skills": [
{ "id": "research", "name": "Research" }
]
}
A2A 端点
| 端点 | 方法 | 说明 |
|---|---|---|
/.well-known/agent.json | GET | Agent Card |
/a2a/tasks | POST | 创建任务 |
/a2a/tasks/{id} | GET | 获取任务状态 |
/a2a/tasks/{id}/messages | GET | 获取消息历史 |
发送任务
curl -X POST http://localhost:4200/a2a/tasks \
-H "Content-Type: application/json" \
-d '{
"id": "task-123",
"message": {
"role": "user",
"parts": [{ "type": "text", "text": "Research AI trends" }]
}
}'
接收任务
# 轮询
curl http://localhost:4200/a2a/tasks/task-123
# 或使用 SSE
curl -N http://localhost:4200/a2a/tasks/task-123/events
对比
| 特性 | MCP | A2A |
|---|---|---|
| 用途 | LLM → 工具 | Agent ↔ Agent |
| 协议 | JSON-RPC 2.0 | HTTP/WebSocket |
| 方向 | 单向 | 双向 |
| 示例 | 文件系统、GitHub | Agent 协作 |
使用场景
MCP 场景
- 文件系统操作
- 数据库查询
- GitHub API 调用
- 自定义工具集成
A2A 场景
- 多 Agent 协作
- 任务委派
- 跨实例通信
配置示例
完整 MCP 配置
# MCP 服务器
[[mcp_servers]]
name = "filesystem"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]
[[mcp_servers]]
name = "github"
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
# A2A 配置
[a2a]
enabled = true
agent_card_path = "/a2a/agent.json"
CLI 命令
# 列出 MCP 服务器
librefang mcp list
# 测试 MCP 服务器
librefang mcp test filesystem
# 启动 MCP 模式
librefang mcp