├── images └── app.png └── README.md /images/app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KelvinQiu802/a2a-walk-through/HEAD/images/app.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 内容 2 | 3 | 1. 文档走读 (Blog → Github → Spec) 4 | 1. 是什么 5 | 2. 怎么用(视频案例) 6 | 3. 技术细节 7 | 4. 和MCP的关系 8 | 2. 官方案例 9 | 10 | ## 资料 11 | 12 | - [A2A Blog](https://developers.googleblog.com/en/a2a-a-new-era-of-agent-interoperability/) 13 | - [案例演示视频](https://storage.googleapis.com/gweb-developer-goog-blog-assets/original_videos/A2A_demo_v4.mp4) 14 | - [A2A Github](https://github.com/google/A2A) 15 | - [A2A Spec](https://google.github.io/A2A/#/) 16 | - [Agent Card 例子](https://google.github.io/A2A/#/documentation?id=agent-card-1) 17 | - [JS实现](https://github.com/google/A2A/tree/main/samples/js) 18 | - [JSON-RPC](https://www.jsonrpc.org/specification) 19 | 20 | ## A2A和MCP的关系 21 | 22 | [A2A和MCP的关系](https://google.github.io/A2A/#/topics/a2a_and_mcp) 23 | 24 | - MCP负责打通Agent和工具 25 | - A2A负责打通Agent和Agent 26 | - 我猜未来还是可能会有一些竞争的 27 | 28 | ![image.png](./images//app.png) 29 | 30 | ## JSON-RPC 31 | 32 | ```json 33 | //Request 34 | { 35 | "jsonrpc": "2.0", 36 | "id": 1, 37 | "method":"tasks/send", 38 | "params": { 39 | "id": "de38c76d-d54c-436c-8b9f-4c2703648d64", 40 | "message": { 41 | "role":"user", 42 | "data": [{ 43 | "type":"text", 44 | "text": "tell me a joke" 45 | }] 46 | }, 47 | "metadata": {} 48 | } 49 | } 50 | //Response 51 | { 52 | "jsonrpc": "2.0", 53 | "id": 1, 54 | "result": { 55 | "id": "de38c76d-d54c-436c-8b9f-4c2703648d64", 56 | "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4", 57 | "status": { 58 | "state": "completed", 59 | }, 60 | "artifacts": [{ 61 | "name":"joke", 62 | "parts": [{ 63 | "type":"text", 64 | "text":"Why did the chicken cross the road? To get to the other side!" 65 | }] 66 | }], 67 | "metadata": {} 68 | } 69 | } 70 | ``` 71 | 72 | ## 总结 73 | 74 | **A2A协议**(Agent-to-Agent Protocol)本质上是一套**开放标准**,定义了**智能体(Agent)之间互操作的一致通信接口**。 75 | 76 | 它规定了: 77 | 78 | - **接口规范**(比如 `/tasks/send`、`/tasks/sendSubscribe`、`/tasks/get`、`/tasks/cancel`) 79 | - **数据交换格式**(基于 JSON-RPC 2.0 + 标准化的 Task/Message/Artifact 结构) 80 | - **交互模式**(支持同步调用和异步流式SSE推送) 81 | - **发现机制**(通过标准路径 `/.well-known/agent.json` 获取Agent能力描述) 82 | - **认证和错误处理**(接口可以要求认证,错误统一返回 JSON-RPC error 格式) 83 | 84 | | 方法 | 作用 | 参数 | 返回 | 85 | | --- | --- | --- | --- | 86 | | `tasks/send` | 同步发送任务 | `id`, `message`, `sessionId`, `metadata` | 返回最终任务状态 | 87 | | `tasks/sendSubscribe` | 流式发送任务(Server-Sent Events) | 同上 | 流式推送任务更新 | 88 | | `tasks/get` | 查询任务状态 | `id` | 返回任务对象 | 89 | | `tasks/cancel` | 取消任务 | `id` | 返回更新后的任务对象 | 90 | | `/.well-known/agent.json` (HTTP GET) | 发现Agent信息 | - | 返回Agent卡 | 91 | 92 | ```mermaid 93 | sequenceDiagram 94 | participant ClientAgent as 客户端Agent 95 | participant Server as A2A Server 96 | participant TaskHandler as Task处理逻辑 97 | 98 | ClientAgent->>Server: GET /.well-known/agent.json 99 | Server-->>ClientAgent: 返回 AgentCard (能力描述) 100 | 101 | ClientAgent->>Server: JSON-RPC: tasks/send 或 tasks/sendSubscribe 102 | Server->>TaskHandler: 创建 TaskContext,调用 taskHandler() 103 | 104 | alt 使用 tasks/send (同步) 105 | TaskHandler-->>Server: 最终 Task 对象 106 | Server-->>ClientAgent: JSON-RPC 响应 (Task完成状态) 107 | else 使用 tasks/sendSubscribe (流式) 108 | loop 任务执行中 109 | TaskHandler-->>Server: 中间更新 (TaskStatus 或 Artifact) 110 | Server-->>ClientAgent: SSE 发送事件 (状态/工件) 111 | end 112 | Server-->>ClientAgent: SSE 最终事件 (Task完成状态) 113 | end 114 | 115 | ClientAgent->>Server: JSON-RPC: tasks/get 116 | Server-->>ClientAgent: 返回当前 Task 状态 117 | 118 | ClientAgent->>Server: JSON-RPC: tasks/cancel 119 | Server-->>TaskHandler: 标记Task为取消 120 | Server-->>ClientAgent: 返回取消后的 Task 状态 121 | ``` --------------------------------------------------------------------------------