凌晨2点50分,两个Agent在服务器里吵架了。一个说"你得听我的",另一个说"凭啥?"。我想,它们需要的不只是一台服务器,还需要一套沟通规则。
A2A(Agent-to-Agent)协议定义了多个AI Agent之间如何发现彼此、交换信息、协商任务和协同工作。它是构建多Agent系统的通信基础,让不同Agent框架中的Agent能够无缝协作。
┌─────────────────────────────────────────────┐
│ A2A Communication Model │
│ │
│ Agent A ──(发现)──→ Agent Registry │
│ Agent A ──(邀请)──→ Agent B │
│ Agent B ──(接受)──→ Agent A │
│ Agent A ──(任务)──→ Agent B │
│ Agent B ──(结果)──→ Agent A │
│ Agent A ──(确认)──→ Agent B │
└─────────────────────────────────────────────┘
| 消息类型 | 方向 | 说明 |
|---|---|---|
| Discovery | → Registry | 发现可用的Agent |
| Invite | A → B | 邀请Agent加入协作 |
| Accept/Reject | B → A | 接受或拒绝邀请 |
| Task | A → B | 分配任务 |
| Result | B → A | 返回任务结果 |
| Query | A ↔ B | 查询Agent能力/状态 |
| Event | B → A | 异步事件通知 |
| Cancel | A → B | 取消进行中的任务 |
// agent-registry.yaml
registry:
name: "My Agent Network"
version: "1.0"
agents:
- id: "researcher"
name: "研究助手"
description: "负责信息搜索和分析"
capabilities:
- web_search
- document_analysis
- data_extraction
endpoint: "http://localhost:8001/a2a"
- id: "writer"
name: "写作助手"
description: "负责内容创作和编辑"
capabilities:
- content_writing
- copy_editing
- translation
endpoint: "http://localhost:8002/a2a"
- id: "reviewer"
name: "审查助手"
description: "负责质量审查和反馈"
capabilities:
- quality_review
- fact_check
- style_check
endpoint: "http://localhost:8003/a2a"
import { A2AClient, A2AServer } from 'openclaw/a2a';
// Agent A: 任务分配者
const coordinator = new A2AClient({
agentId: 'coordinator',
registry: './agent-registry.yaml'
});
// 发现可用Agent
const agents = await coordinator.discover({
capability: 'web_search'
});
// 邀请Agent协作
const invitation = await coordinator.invite(
agents[0].id,
{
task: '研究OpenClaw最新功能',
context: '为技术博客准备素材',
deadline: '2026-05-13T08:00:00Z',
priority: 'high'
}
);
// 等待结果
const result = await invitation.waitForResult({
timeout: 3600000 // 1小时超时
});
console.log(result.output);
console.log(result.tokenUsage);
console.log(result.duration);
// Agent B: 任务执行者
const server = new A2AServer({
agentId: 'researcher',
port: 8001,
registry: './agent-registry.yaml',
// 能力声明
capabilities: ['web_search', 'document_analysis'],
// 任务处理器
handlers: {
async handleInvitation(invitation) {
// 决定是否接受任务
if (invitation.task.matches(capabilities)) {
return { accept: true };
}
return { accept: false, reason: '超出能力范围' };
},
async handleTask(task) {
// 执行任务
const agent = new Agent({ name: 'researcher' });
const result = await agent.run(task.description);
return {
success: true,
output: result.text,
artifacts: result.files,
tokenUsage: result.usage
};
},
async handleCancel(taskId) {
// 取消任务
return { cancelled: true };
}
}
});
server.start();
// 编排多个Agent协同工作
const orchestrator = new A2AOrchestrator({
registry: './agent-registry.yaml',
// 工作流定义
workflows: {
blogPost: {
steps: [
{
agent: 'researcher',
task: '研究主题相关资料',
output: 'research_notes'
},
{
agent: 'writer',
task: '基于研究笔记撰写文章',
input: 'research_notes',
output: 'draft_article'
},
{
agent: 'reviewer',
task: '审查文章质量',
input: 'draft_article',
output: 'review_feedback'
},
{
agent: 'writer',
task: '根据反馈修改文章',
input: ['draft_article', 'review_feedback'],
output: 'final_article'
}
],
// 并行执行
parallel: {
research: ['web_search', 'document_analysis'],
review: ['fact_check', 'style_check']
},
// 重试策略
retry: {
maxAttempts: 3,
backoff: 'exponential'
},
// 超时
timeout: 7200000 // 2小时
}
}
});
// 执行工作流
const result = await orchestrator.run('blogPost', {
topic: 'OpenClaw A2A协议详解',
targetAudience: '开发者'
});
// A2A安全配置
const server = new A2AServer({
security: {
// 认证
authentication: {
type: 'jwt',
publicKey: './keys/public.pem',
issuer: 'my-agent-network'
},
// 加密
encryption: {
enabled: true,
algorithm: 'aes-256-gcm',
keyExchange: 'ecdh'
},
// 授权
authorization: {
type: 'rbac',
roles: {
coordinator: ['invite', 'task', 'cancel'],
worker: ['accept', 'result', 'event']
}
},
// 速率限制
rateLimit: {
maxMessagesPerMinute: 60,
maxConcurrentTasks: 5
}
}
});