AI 领域 A2A 是什么?

一、定义

智能体是可以通过使用工具、推理和用户交互来完成新任务的自主应用程序。

二、A2A 是什么?

A2A(Agent-to-Agent) 是谷歌于 2025 年 4 月推出的开源协议,旨在为不同框架、供应商开发的 AI 代理(Agent)提供标准化的通信和协作机制。其核心目标是解决异构 AI 代理之间的互操作性问题,类似于为 AI 世界建立一种“通用语言”,使它们能够像人类团队一样协同工作。

类比

  • AI 代理的“HTTP 协议”:A2A 提供了一套基于 HTTP/JSON-RPC 的通信规范,使代理能跨平台、跨技术栈协作。

  • 与 MCP 协议互补

    • MCP(Anthropic 的 Model Context Protocol)专注于连接 AI 代理与外部工具(如 API、数据库),相当于“工具调用层”。

    • A2A 则专注于代理间的协作,解决“如何让多个代理共同完成复杂任务”的问题,例如旅行规划中机票代理与酒店代理的协作。

image.png

三、核心概念与重要组件

1. Agent Card(代理卡片)

https://agent2agent.ren/docs/protocol/agent-card

  • 作用:代理的“数字名片”,描述其能力、接口、认证等信息。

  • 格式:JSON 文件,通常位于 /.well-known/agent.json。

内容示例

{
2  "name": "Google Maps Agent",
3  "description": "Plan routes, remember places, and generate directions",
4  "url": "https://maps-agent.google.com",
5  "provider": {
6    "organization": "Google",
7    "url": "https://google.com"
8  },
9  "version": "1.0.0",
10  "authentication": {
11    "schemes": "OAuth2"
12  },
13  "defaultInputModes": ["text/plain"],
14  "defaultOutputModes": ["text/plain", "application/html"],
15  "capabilities": {
16    "streaming": true,
17    "pushNotifications": false
18  },
19  "skills": [
20    {
21      "id": "route-planner",
22      "name": "Route planning",
23      "description": "Helps plan routing between two locations",
24      "tags": ["maps", "routing", "navigation"],
25      "examples": [
26        "plan my route from Sunnyvale to Mountain View",
27        "what's the commute time from Sunnyvale to San Francisco at 9AM",
28        "create turn by turn directions from Sunnyvale to Mountain View"
29      ],
30      // can return a video of the route
31      "outputModes": ["application/html", "video/mp4"]
32    },
33    {
34      "id": "custom-map",
35      "name": "My Map",
36      "description": "Manage a custom map with your own saved places",
37      "tags": ["custom-map", "saved-places"],
38      "examples": [
39        "show me my favorite restaurants on the map",
40        "create a visual of all places I've visited in the past year"
41      ],
42      "outputModes": ["application/html"]
43    }
44  ]
45}

2. 任务(Task)

  • 任务:工作的基本单元,客户端通过发送消息(tasks/send或)来启动任务。任务具有唯一 ID ,tasks/sendSubscribe并会经历不同的状态(submittedworkinginput-requiredcompletedfailedcanceled)。

3.消息(Message)

  • 消息:代理间通信的数据单元,支持文本、文件、结构化数据等格式。 表示客户端(role: "user")和代理(role: "agent")之间的通信轮次。消息包含(如文本部分、文件部分、数据部分等)Parts

  • Artifact:由 Agent 在执行任务过程中生成的输出结果。它与 Message 的差别在于,Artifact 通常是「结果物」或产物,而 Message 常用于「对话或指令」。

  • Part:Message或中的基本内容单元Artifact。可以是TextPartFilePart(包含内联字节或 URI)或DataPart(用于结构化 JSON,例如表单)

4.****Push Notification

可选功能,如果 Agent 支持 pushNotifications,就可以向客户端指定的 URL 主动发起任务进度更新,而无需客户端轮询。

5.Streaming

如果 Agent 支持 streaming 功能,就可以在处理某个任务时,通过 tasks/sendSubscribe 使用 SSE 进行分段或实时地输出状态与结果。

6. A2A Server 与 Client

  • A2A Server:暴露 HTTP 端点,接收并管理任务(如 /tasks/send)。

  • A2A Client:发送请求的应用程序或其他代理,通过 JSON-RPC 调用服务。

四、工作原理

1. 通信流程https://agent2agent.ren/docs/protocol/sample-methods

    能力发现:客户端通过 Agent Card 发现可用代理。 https://agent2agent.ren/docs/key-topics/agent-discovery

    任务启动:客户端发送任务请求(如 POST /tasks/send)。

    状态管理:服务器返回任务 ID,并通过 SSE(Server-Sent Events)推送状态更新。

    协作执行:代理间交换消息和上下文,直至任务完成或失败。

image.png

2. 技术实现

  • 协议栈:基于 HTTP、JSON-RPC 2.0 和 SSE,兼容现有 Web 技术。

  • 安全机制:默认支持 OAuth 2.0、生物认证等企业级安全方案。

  • 多模态支持:通过“部分(Part)”协商内容格式(如文本、视频、表单)。

五、如何使用 A2A?

https://storage.googleapis.com/gweb-developer-goog-blog-assets/original_videos/A2A_demo_v4.mp4

1. 基础步骤

    部署代理:实现 A2A Server。

    定义 Agent Card:描述代理能力并托管到指定路径。

    客户端集成:通过 A2A Client 库调用远程代理。

2. 前端开发示例

// 发现代理能力
fetch('https://agent.example.com/.well-known/agent.json')
  .then(response => response.json())
  .then(agentCard => {
    // 根据技能动态生成 UI(如显示“机票查询”按钮)
  });

// 发送任务请求
const task = {
  skill: "机票比价",
  params: { origin: "北京", destination: "东京" }
};
fetch('https://agent.example.com/tasks/send', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(task)
});

// 监听任务状态(SSE)
const eventSource = new EventSource('https://agent.example.com/tasks/updates');
eventSource.onmessage = (e) => {
  console.log('任务状态更新:', JSON.parse(e.data));
};

通过 A2A,前端开发者可以更高效地构建支持 AI 协作的 Web 应用,而无需深入底层 AI 模型细节。其基于 Web 标准的特性,使得集成成本大幅降低,是未来智能应用开发的重要基础设施。

参考

https://github.com/google/A2A

https://agent2agent.ren/docs/introduction

https://google.github.io/A2A/#/

https://developers.googleblog.com/zh-hans/a2a-a-new-era-of-agent-interoperability/

https://zhuanlan.zhihu.com/p/1895786881360316389

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容