A2A 协议规范
本文档详细介绍了Agent-to-Agent (A2A) 协议的技术规范,包括通信格式、消息结构、状态管理等核心要素。
协议概述
A2A 协议是一种专为智能代理(Agent)之间通信设计的协议框架,旨在实现多个智能体之间的协作、信息交换和任务委托。协议基于标准化的JSON消息格式,支持异步通信和会话管理。
设计原则
- 简单性:易于实现和集成到现有系统
- 灵活性:适应不同类型的代理和任务场景
- 可扩展性:支持自定义消息类型和功能扩展
- 安全性:内置身份验证和权限控制机制
消息结构
A2A协议中的所有通信都采用标准化的消息结构:
{
"message_id": "msg_123456789",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:30:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "request",
"content": {
// 消息具体内容,根据消息类型而异
},
"metadata": {
// 可选的元数据
}
}
核心字段说 明
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
| message_id | string | 是 | 消息的唯一标识符 |
| session_id | string | 是 | 会话的唯一标识符 |
| timestamp | string | 是 | ISO 8601格式的时间戳 |
| sender | object | 是 | 发送者信息 |
| recipient | object | 是 | 接收者信息 |
| message_type | string | 是 | 消息类型(如request、response、error等) |
| content | object | 是 | 消息主体内容 |
| metadata | object | 否 | 附加信息 |
消息类型
A2A协议支持以下基本消息类型:
1. 请求消息 (Request)
用于代理向其他代理请求信息或服务。
{
"message_id": "msg_req_12345",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:30:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "request",
"content": {
"request_type": "information",
"query": "What is the current weather in New York?",
"priority": "normal",
"timeout": 30
}
}
2. 响应消息 (Response)
用于代理回应其他代理的请求。
{
"message_id": "msg_res_67890",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:32:00Z",
"sender": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"recipient": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"message_type": "response",
"content": {
"in_response_to": "msg_req_12345",
"status": "success",
"data": {
"temperature": 72,
"conditions": "sunny",
"location": "New York, NY"
}
}
}
3. 通知消息 (Notification)
用于代理向其他代理发送主动通知,无需回复。
{
"message_id": "msg_not_24680",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:40:00Z",
"sender": {
"agent_id": "agent_system",
"agent_type": "system"
},
"recipient": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"message_type": "notification",
"content": {
"notification_type": "alert",
"message": "System maintenance scheduled in 30 minutes",
"severity": "info"
}
}
4. 错误消息 (Error)
用于代理返回错误状态或异常情况。
{
"message_id": "msg_err_13579",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:35:00Z",
"sender": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"recipient": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"message_type": "error",
"content": {
"in_response_to": "msg_req_12345",
"error_code": "resource_not_found",
"error_message": "The requested weather data is not available",
"severity": "warning"
}
}
会话管理
A2A协议使用会话(Session)来管理相关消息的上下文和状态。
会话创建
会话创建通过特殊的会话初始化消息完成:
{
"message_id": "msg_init_11111",
"session_id": "session_new_123456",
"timestamp": "2023-04-18T10:25:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "session_control",
"content": {
"action": "create",
"session_parameters": {
"purpose": "weather_inquiry",
"timeout": 300,
"priority": "normal"
}
}
}
会话终止
会话可以通过会话控制消息显式终止:
{
"message_id": "msg_term_22222",
"session_id": "session_987654321",
"timestamp": "2023-04-18T11:00:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "session_control",
"content": {
"action": "terminate",
"reason": "task_completed"
}
}
安全机制
A2A协议实现了多层安全机制:
身份验证
每个代理必须提供有效的认证凭证:
{
"message_id": "msg_auth_33333",
"timestamp": "2023-04-18T10:20:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_auth",
"agent_type": "system"
},
"message_type": "authentication",
"content": {
"auth_method": "token",
"credentials": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expiration": "2023-04-19T10:20:00Z"
}
}
}
加密
A2A协议支持端到端加密,可选择不同的加密算法:
{
"message_id": "msg_enc_44444",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:22:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "session_control",
"content": {
"action": "set_encryption",
"encryption_parameters": {
"algorithm": "AES-256-GCM",
"public_key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A..."
}
},
"metadata": {
"encryption": {
"status": "initialization"
}
}
}
任务委托模式
A2A协议支持代理之间的任务委托,允许一个代理将任务分配给其他代理处理:
{
"message_id": "msg_task_55555",
"session_id": "session_987654321",
"timestamp": "2023-04-18T10:45:00Z",
"sender": {
"agent_id": "agent_alice",
"agent_type": "assistant"
},
"recipient": {
"agent_id": "agent_bob",
"agent_type": "expert"
},
"message_type": "task_delegation",
"content": {
"task_id": "task_123456",
"task_type": "data_analysis",
"description": "Analyze weather patterns in New York for the last 30 days",
"parameters": {
"data_source": "weather_database",
"time_range": {
"start": "2023-03-19T00:00:00Z",
"end": "2023-04-18T00:00:00Z"
},
"location": "New York, NY"
},
"deadline": "2023-04-18T11:45:00Z",
"priority": "high"
}
}