A2A

A2A 通信协议解析:Agent 间的标准化对话机制

深入分析 Agent-to-Agent(A2A)通信协议的设计原理、消息格式和交互模式,构建标准化的 Agent 间通信框架。

当多个 Agent 需要协作完成任务时,它们之间如何”对话”就成了关键问题。A2A(Agent-to-Agent)通信协议定义了 Agent 间交互的标准方式,包括消息格式、交互模式和语义约定。

为什么需要 A2A 协议

在没有标准协议的情况下,每个 Agent 框架都定义了自己的通信方式。A 框架的 Agent 无法与 B 框架的 Agent 通信,导致 Agent 生态碎片化。

A2A 协议的目标是:

  • 互操作性——不同框架、不同语言的 Agent 可以直接通信
  • 能力发现——Agent 可以动态发现其他 Agent 的能力
  • 任务委派——Agent 可以将子任务委派给专门的 Agent
  • 状态同步——Agent 之间可以共享任务进度和状态

协议架构

┌────────────────────────────────────────┐
│           A2A Protocol Layer           │
│                                        │
│  ┌──────────┐  ┌──────────┐           │
│  │ Agent Card│  │ Task     │           │
│  │ Discovery │  │ Protocol │           │
│  └──────────┘  └──────────┘           │
│                                        │
│  ┌──────────┐  ┌──────────┐           │
│  │ Message  │  │ Streaming│           │
│  │ Format   │  │ Protocol │           │
│  └──────────┘  └──────────┘           │
└────────────────────────────────────────┘
         │              │
    ┌────┴────┐    ┌────┴────┐
    │ HTTP/   │    │ JSON-   │
    │ SSE     │    │ RPC     │
    └─────────┘    └─────────┘

Agent Card

Agent Card 是 Agent 的”名片”,描述了 Agent 的能力、端点和交互方式。

{
  "name": "research-agent",
  "description": "专业的信息研究和分析 Agent",
  "version": "1.0.0",
  "url": "https://agent.example.com/a2a",
  "capabilities": {
    "streaming": true,
    "pushNotifications": false,
    "stateTransitionHistory": true
  },
  "skills": [
    {
      "id": "web-search",
      "name": "网络搜索",
      "description": "在互联网上搜索信息",
      "examples": ["搜索最新 AI 论文", "查找技术文档"]
    },
    {
      "id": "data-analysis",
      "name": "数据分析",
      "description": "分析结构化数据并生成洞察"
    }
  ],
  "authentication": {
    "schemes": ["bearer"]
  }
}

任务协议

A2A 的核心是任务(Task)协议——Agent 之间通过创建和管理任务来协作。

任务生命周期

submitted → working → completed
                    → failed
                    → canceled

发送任务

{
  "jsonrpc": "2.0",
  "id": "task-001",
  "method": "tasks/send",
  "params": {
    "id": "task-001",
    "message": {
      "role": "user",
      "parts": [
        {
          "type": "text",
          "text": "分析过去一周的 AI 领域重要进展"
        }
      ]
    },
    "metadata": {
      "priority": "high",
      "deadline": "2026-04-23T12:00:00Z"
    }
  }
}

任务响应

{
  "jsonrpc": "2.0",
  "id": "task-001",
  "result": {
    "id": "task-001",
    "status": {
      "state": "completed",
      "timestamp": "2026-04-22T10:30:00Z"
    },
    "artifacts": [
      {
        "parts": [
          {
            "type": "text",
            "text": "过去一周 AI 领域的重要进展包括:..."
          }
        ]
      }
    ]
  }
}

流式交互

对于长时间运行的任务,A2A 支持通过 SSE 进行流式状态更新。

// Client 端
const response = await fetch('https://agent.example.com/a2a', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'tasks/send',
    params: { id: 'task-001', message: { ... } }
  }),
});

const reader = response.body!.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const text = decoder.decode(value);
  const lines = text.split('\n').filter(l => l.startsWith('data: '));

  for (const line of lines) {
    const data = JSON.parse(line.slice(6));
    console.log('Status update:', data.result.status.state);
  }
}

任务委派

Agent 可以将子任务委派给其他 Agent:

class AgentWithDelegation {
  private a2aClient: A2AClient;

  async executeTask(task: string): Promise<string> {
    // 分析任务是否需要委派
    const analysis = await this.analyzeTask(task);

    if (analysis.needsDelegation) {
      // 查找合适的 Agent
      const agentCard = await this.a2aClient.discoverAgent(
        analysis.requiredSkill
      );

      // 委派子任务
      const subResult = await this.a2aClient.sendTask(agentCard.url, {
        message: {
          role: 'user',
          parts: [{ type: 'text', text: analysis.subtask }],
        },
      });

      // 整合结果
      return await this.integrateResults(task, subResult);
    }

    return await this.processDirectly(task);
  }
}

与 MCP 的关系

A2A 和 MCP 解决的是不同层面的问题:

  • MCP——Agent 与工具/服务之间的通信(Agent ↔ Tool)
  • A2A——Agent 与 Agent 之间的通信(Agent ↔ Agent)

两者互补:一个 Agent 可以通过 MCP 使用工具,同时通过 A2A 与其他 Agent 协作。

Agent A ──MCP──→ Tool Server

   └──A2A──→ Agent B ──MCP──→ Tool Server

常见问题(FAQ)

A2A 和 MCP 可以一起使用吗?

可以,而且应该。MCP 管理 Agent 的工具能力,A2A 管理 Agent 间的协作能力。

A2A 协议的成熟度如何?

A2A 是 Google 于 2025 年提出的协议,目前仍处于快速发展阶段。核心规范已经稳定,但生态工具还在完善中。

如何保证 A2A 通信的安全性?

使用 HTTPS 加密传输,配合 OAuth2 或 API Key 进行身份认证。Agent Card 中声明支持的认证方式。

总结

A2A 协议为 Agent 间通信提供了标准化的框架。通过 Agent Card 进行能力发现,通过任务协议进行协作,通过流式传输实现实时交互。与 MCP 配合使用,可以构建出完整的 Agent 生态系统。