A2A(Agent-to-Agent 协议)
→ 返回 AI Agent
Google 于 2025 年发布的开放协议,定义 AI Agent 之间如何互相发现、通信和协作完成任务。与 MCP(模型到工具)互补,解决多 Agent 系统的通信标准化问题。
核心概念
| 概念 | 说明 |
|---|
| Agent Card | Agent 的能力声明文件(JSON),发布在 /.well-known/agent.json |
| Task | Agent 间协作的基本单元,含状态机和生命周期 |
| Message | 通信载体,支持文本、文件、结构化数据 |
| Artifact | Task 的输出产物 |
| Part | 内容片段(TextPart / FilePart / DataPart) |
与 MCP 的区别
| 维度 | MCP | A2A |
|---|
| 通信对象 | 模型 ↔ 工具/资源 | Agent ↔ Agent |
| 发起方 | LLM 调用工具 | Agent 委托另一个 Agent |
| 状态管理 | 无状态工具调用 | 有状态 Task 生命周期 |
| 能力发现 | 工具列表(tools) | Agent Card(JSON) |
| 适用场景 | 单 Agent 扩展能力 | 多 Agent 协作编排 |
| 制定方 | Anthropic | Google |
协同使用
用户请求
↓
编排 Agent(通过 A2A 分配子任务)
├── 搜索 Agent ──── MCP ──→ 搜索工具
├── 代码 Agent ──── MCP ──→ 代码执行环境
└── 写作 Agent ──── MCP ──→ 文件系统
Agent Card
{
"name": "CodeReviewAgent",
"description": "专注于 Java/Python 代码审查的 Agent",
"url": "https://agent.example.com",
"version": "1.0.0",
"capabilities": {
"streaming": true,
"pushNotifications": false
},
"skills": [
{
"id": "review_code",
"name": "代码审查",
"description": "审查代码质量、安全性和性能",
"inputModes": ["text"],
"outputModes": ["text"]
}
]
}
Task 生命周期
submitted → working → (input-required) → completed
↘ failed / canceled
| 状态 | 说明 |
|---|
submitted | 任务已提交,等待处理 |
working | Agent 正在处理 |
input-required | 需要用户补充信息 |
completed | 任务完成,Artifact 可取 |
failed | 执行失败 |
基本交互
import httpx
# 1. 获取 Agent Card
card = httpx.get("https://agent.example.com/.well-known/agent.json").json()
# 2. 发送任务
task = httpx.post("https://agent.example.com/tasks/send", json={
"id": "task-001",
"message": {
"role": "user",
"parts": [{"type": "text", "text": "请审查以下 Java 代码:..."}]
}
}).json()
# 3. 获取结果
result = httpx.get(f"https://agent.example.com/tasks/{task['id']}").json()
print(result["artifacts"][0]["parts"][0]["text"])
多 Agent 协作模式
顺序委托
编排 Agent → 研究 Agent → 分析 Agent → 报告 Agent
并行委托
编排 Agent ──→ 数据 Agent A ──┐
──→ 数据 Agent B ──┴→ 聚合结果
相关文档