常见交互流程与示例
这些示例取自官方规范第 6 章,展示了 A2A 常见交互在网络上的实际形态。示例使用 HTTP+JSON/REST 绑定;其他绑定承载的是同样的对象(见协议绑定详解 )。其中的 ID、令牌和主机名均为占位值,部分载荷有所省略。
基础任务执行
场景:客户端提出一个问题,收到一个已完成的任务。
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [{"text": "What is the weather today?"}],
"messageId": "msg-uuid"
}
}响应是一个 Task,结果以 Artifact 返回:
HTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"task": {
"id": "task-uuid",
"contextId": "context-uuid",
"status": {"state": "TASK_STATE_COMPLETED"},
"artifacts": [{
"artifactId": "artifact-uuid",
"name": "Weather Report",
"parts": [{"text": "Today will be sunny with a high of 75°F"}]
}]
}
}流式任务执行
场景:客户端发起一个耗时较长的任务,并希望实时收到进展。
POST /message:stream HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [{"text": "Write a detailed report on climate change"}],
"messageId": "msg-uuid"
}
}SSE 流以 Task 开头,随后是产出物更新与状态更新事件,任务进入终态时关闭:
HTTP/1.1 200 OK
Content-Type: text/event-stream
data: {"task": {"id": "task-uuid", "contextId": "context-uuid", "status": {"state": "TASK_STATE_WORKING"}}}
data: {"artifactUpdate": {"taskId": "task-uuid", "contextId": "context-uuid", "artifact": {"parts": [{"text": "# Climate Change Report\n\n"}]}}}
data: {"statusUpdate": {"taskId": "task-uuid", "contextId": "context-uuid", "status": {"state": "TASK_STATE_COMPLETED"}}}多轮交互
场景:智能体需要更多输入才能完成任务。
初始请求:
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [{"text": "Book me a flight"}],
"messageId": "msg-1"
}
}任务进入 TASK_STATE_INPUT_REQUIRED,状态消息询问缺少的细节:
HTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"task": {
"id": "task-uuid",
"status": {
"state": "TASK_STATE_INPUT_REQUIRED",
"message": {
"role": "ROLE_AGENT",
"parts": [{"text": "I need more details. Where would you like to fly from and to?"}]
}
}
}
}后续请求带上同一个 taskId,即可继续该任务(contextId 由它推断,标识符规则见核心协议规范
):
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"taskId": "task-uuid",
"role": "ROLE_USER",
"parts": [{"text": "From San Francisco to New York"}],
"messageId": "msg-2"
}
}列出任务与分页
场景:客户端想查看某个上下文的全部任务,或处于某个状态的全部任务。结果只包含调用方可见的任务,按状态时间戳降序排列。
某个上下文的全部任务,每个任务最多返回最近 3 条历史消息:
GET /tasks?contextId=c295ea44-7543-4f78-b524-7a38915ad6e4&pageSize=10&historyLength=3 HTTP/1.1
Host: agent.example.com
Authorization: Bearer tokenHTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"tasks": [
{
"id": "3f36680c-7f37-4a5f-945e-d78981fafd36",
"contextId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
"status": {
"state": "TASK_STATE_COMPLETED",
"timestamp": "2024-03-15T10:15:00Z"
}
}
],
"totalSize": 5,
"pageSize": 10,
"nextPageToken": ""
}所有上下文中处于工作状态的任务:
GET /tasks?status=TASK_STATE_WORKING&pageSize=20 HTTP/1.1
Host: agent.example.com
Authorization: Bearer token获取下一页时,把上一次响应中的 nextPageToken 作为 pageToken 传入。nextPageToken 非空表示还有更多结果:
GET /tasks?contextId=c295ea44-7543-4f78-b524-7a38915ad6e4&pageSize=10&pageToken=base64-encoded-cursor-token HTTP/1.1
Host: agent.example.com
Authorization: Bearer tokenHTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"tasks": [
/* ... additional tasks */
],
"totalSize": 15,
"pageSize": 10,
"nextPageToken": "base64-encoded-next-cursor-token"
}推送通知的设置与使用
场景:客户端请求生成一份耗时较长的报告,并希望完成时通过 webhook 收到通知。webhook 配置放在 SendMessageConfiguration 中传入:
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [
{
"text": "Generate the Q1 sales report. This usually takes a while. Notify me when it's ready."
}
],
"messageId": "6dbc13b5-bd57-4c2b-b503-24e381b6c8d6"
},
"configuration": {
"taskPushNotificationConfig": {
"url": "https://client.example.com/webhook/a2a-notifications",
"authentication": {
"scheme": "Bearer",
"credentials": "secure-client-token-for-task-aaa"
}
}
}
}任务以已提交状态被接受:
HTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"task": {
"id": "43667960-d455-4453-b0cf-1bae4955270d",
"contextId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
"status": {
"state": "TASK_STATE_SUBMITTED",
"timestamp": "2024-03-15T11:00:00Z"
}
}
}之后,服务端用配置中提供的凭据认证,向 webhook 发送一条通知:
POST /webhook/a2a-notifications HTTP/1.1
Host: client.example.com
Authorization: Bearer secure-client-token-for-task-aaa
Content-Type: application/a2a+json
{
"statusUpdate": {
"taskId": "43667960-d455-4453-b0cf-1bae4955270d",
"contextId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
"status": {
"state": "TASK_STATE_COMPLETED",
"timestamp": "2024-03-15T18:30:00Z"
}
}
}这一交互双方各自的安全要求见认证、授权与安全考量 。
文件交换
场景:客户端发送一张图片请求分析,智能体返回处理后的图片。Part 既可以用 raw 内联携带文件内容(JSON 中为 base64),也可以用 url 以引用方式携带:
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [
{
"text": "Analyze this image and highlight any faces."
},
{
"raw": "iVBORw0KGgoAAAANSUhEUgAAAAUA...",
"filename": "input_image.png",
"mediaType": "image/png"
}
],
"messageId": "6dbc13b5-bd57-4c2b-b503-24e381b6c8d6"
}
}响应以文件引用的方式返回结果:
HTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"task": {
"id": "43667960-d455-4453-b0cf-1bae4955270d",
"contextId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
"status": {
"state": "TASK_STATE_COMPLETED",
"timestamp": "2024-03-15T12:05:00Z"
},
"artifacts": [
{
"artifactId": "9b6934dd-37e3-4eb1-8766-962efaab63a1",
"name": "processed_image_with_faces.png",
"parts": [
{
"url": "https://storage.example.com/processed/task-bbb/output.png",
"filename": "output.png",
"mediaType": "image/png"
}
]
}
]
}
}结构化数据交换
场景:客户端请求以指定的 JSON 格式返回未关闭的支持工单列表。期望的 Schema 放在请求文本部分的 metadata 中传入:
POST /message:send HTTP/1.1
Host: agent.example.com
Content-Type: application/a2a+json
Authorization: Bearer token
{
"message": {
"role": "ROLE_USER",
"parts": [
{
"text": "Show me a list of my open IT tickets",
"metadata": {
"mediaType": "application/json",
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ticketNumber": { "type": "string" },
"description": { "type": "string" }
}
}
}
}
}
],
"messageId": "85b26db5-ffbb-4278-a5da-a7b09dea1b47"
}
}智能体在产出物中返回数据:
HTTP/1.1 200 OK
Content-Type: application/a2a+json
{
"task": {
"id": "d8c6243f-5f7a-4f6f-821d-957ce51e856c",
"contextId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
"status": {
"state": "TASK_STATE_COMPLETED",
"timestamp": "2025-04-17T17:47:09.680794Z"
},
"artifacts": [
{
"artifactId": "c5e0382f-b57f-4da7-87d8-b85171fad17c",
"parts": [
{
"text": "[{\"ticketNumber\":\"REQ12312\",\"description\":\"request for VPN access\"},{\"ticketNumber\":\"REQ23422\",\"description\":\"Add to DL - team-gcp-onboarding\"}]"
}
]
}
]
}
}获取需认证的扩展 Agent Card
场景:客户端从公开 Agent Card 得知智能体支持扩展卡片,并希望获取完整信息。
客户端通过
GET /.well-known/agent-card.json获取公开 Agent Card。响应中声明了extendedAgentCard与一个安全方案:{ "capabilities": { "extendedAgentCard": true }, "securitySchemes": { "google": { "openIdConnectSecurityScheme": { "openIdConnectUrl": "https://accounts.google.com/.well-known/openid-configuration" } } } }客户端通过带外方式(例如 OAuth 2.0 流程)取得凭据;
客户端用这些凭据获取扩展 Agent Card:
GET /extendedAgentCard HTTP/1.1 Host: agent.example.com Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...HTTP/1.1 200 OK Content-Type: application/a2a+json { "name": "Extended Agent with Additional Skills", "skills": [ /* Extended skills available to authenticated users */ ] }
相关页面
- 任务的生命周期 与流式传输与异步操作 :这些流程背后的概念;
- 数据结构
:
Task、Message、Part、Artifact的字段; - Python 快速开始 :用 SDK 运行智能体并交换这些消息。
来源:A2A 官方规范