多智能体协作的标准语言:A2A协议技术解析
最近在研究多智能体系统开发时,发现了A2A协议这个很有价值的技术文档。它系统性地介绍了多智能体通信的标准方案,结合我自己的项目经验,想和大家分享一下这个协议的核心价值和实际应用。
📌 1. A2A协议的核心技术价值
从技术角度看,A2A协议解决了多智能体系统中的几个关键问题。
标准化通信机制是第一个优势。A2A基于HTTP/HTTPS、JSON-RPC 2.0和SSE等成熟Web标准构建,这大大降低了集成成本。在我的过往项目中,不同团队开发的AI组件往往需要大量的「胶水代码」才能互通。A2A提供的统一协议可以显著减少这种定制化开发工作。
动态发现与能力协商是另一个亮点。Agent Card机制(/.well-known/agent.json)让智能体可以动态发现彼此的能力。这类似于微服务架构中的服务发现,但更适应AI智能体的特性。在我参与的一个供应链优化项目中,如果有这样的机制,就不需要硬编码各个智能体的能力描述。
👉 2. 从项目实践中看到的A2A价值
基于我参与过的多智能体系统开发经验,A2A在以下方面表现出显著优势。
解耦系统架构是最明显的好处。传统微服务集成需要预先知道对方的API细节,导致紧密耦合。A2A通过能力发现机制实现了松耦合,智能体只需发送任务,不需要了解对端的具体实现。这在我之前开发的客户服务系统中尤为需要,其中不同部门的AI助手需要协作但由不同团队维护。
长任务支持与状态管理也很实用。A2A明确定义了任务生命周期(submitted、working、input-required、completed等状态),这对于需要长时间运行的任务至关重要。在我参与的数据分析平台项目中,经常需要处理耗时数小时的分析任务。A2A的状态机模型可以提供更好的进度跟踪和用户体验。
多模态交互支持是个加分项。A2A原生支持文本、音频、视频等多种内容类型,这为构建丰富的多模态应用奠定了基础。相比之下,传统REST API通常局限于结构化数据交换,灵活性较低。
⚠️ 3. A2A与MCP的互补关系
文档中清晰区分了A2A和MCP的不同作用范围,这一点很关键。
MCP解决的是AI与外部工具/数据的连接问题(如数据库、API调用)。A2A解决的是AI智能体之间的协作问题。
在实际项目中,这两种协议可以结合使用。例如,一个智能体通过MCP获取数据,然后通过A2A将分析任务委托给另一个专业智能体。
🔒 4. 安全考虑与企业级应用
A2A的「默认安全」设计原则对企业应用很重要。它支持mTLS、OAuth 2.0等标准安全机制,并提供完整的审计日志。在我参与的金融领域AI项目中,安全性和合规性是首要考虑,A2A在这方面提供了良好基础。
💡 5. 实际开发中的学习要点
从实现角度看,有几个关键点值得关注。
异步处理模式需要特别注意。A2A支持多种交互模式:同步请求/响应、异步轮询、SSE流式更新和webhook推送。开发者需要根据任务特性选择适当模式。对于实时性要求高的场景,SSE比轮询更高效。对于长时间任务,webhook避免保持长连接。
错误处理与重试机制不可忽视。由于智能体协作涉及网络通信和不稳定因素,健壮的错误处理和重试策略必不可少。A2A的标准状态机有助于实现一致的错误处理逻辑。
性能与可扩展性是大型项目的关键。结合Kafka等消息队列可以进一步解耦智能体通信,这在大型分布式系统中尤为重要。基于主题的发布-订阅模式可以提高系统的可扩展性。
📝 写在最后
通过学习这篇文档,我认识到A2A协议为多智能体系统开发提供了急需的标准化框架。其核心价值在于降低集成复杂度(通过统一协议减少定制开发)、增强系统灵活性(支持动态发现和多种交互模式)以及提高可维护性(明确的状态机和标准化通信格式)。
对于技术人员而言,掌握A2A意味着能够设计更加模块化、可扩展的AI系统。特别是在当前多智能体架构日益普及的背景下,理解这一协议对开发复杂的AI协作应用至关重要。
从我过去的项目经验看,如果早有这样的标准,可以避免很多集成难题和重复工作。未来在设计和开发多智能体系统时,我会优先考虑采用A2A协议作为基础通信层。
你在开发多智能体系统时遇到过哪些通信难题?欢迎留言分享你的经验和看法!
Chapter 15: Inter-Agent Communication (A2A)
第15章:智能体间通信(A2A)
Individual AI agents often face limitations when tackling complex, multifaceted problems, even with advanced capabilities. To overcome this, Inter-Agent Communication (A2A) enables diverse AI agents, potentially built with different frameworks, to collaborate effectively. This collaboration involves seamless coordination, task delegation, and information exchange.
即使拥有先进功能,单个AI智能体在处理复杂、多方面的问题时也常常面临局限性。为了克服这一问题,智能体间通信(Inter-Agent Communication,简称A2A) 使各种可能基于不同框架构建的AI智能体能够有效协作。这种协作涉及无缝协调、任务委派和信息交换。
Google's A2A protocol is an open standard designed to facilitate this universal communication. This chapter will explore A2A, its practical applications, and its implementation within the Google ADK.
Google的A2A协议是一种开放标准,旨在促进这种通用通信。本章将探讨A2A、其实际应用以及在Google ADK中的实现。
Inter-Agent Communication Pattern Overview
智能体间通信模式概述
The Agent2Agent (A2A) protocol is an open standard designed to enable communication and collaboration between different AI agent frameworks. It ensures interoperability, allowing AI agents developed with technologies like LangGraph, CrewAI, or Google ADK to work together regardless of their origin or framework differences.
智能体到智能体(Agent2Agent,简称A2A)协议是一种开放标准,旨在实现不同AI智能体框架之间的通信和协作。它确保了互操作性,允许使用LangGraph、CrewAI或Google ADK等技术开发的AI智能体能够协同工作,无论其来源或框架差异如何。
A2A is supported by a range of technology companies and service providers, including Atlassian, Box, LangChain, MongoDB, Salesforce, SAP, and ServiceNow. Microsoft plans to integrate A2A into Azure AI Foundry and Copilot Studio, demonstrating its commitment to open protocols. Additionally, Auth0 and SAP are integrating A2A support into their platforms and agents.
A2A得到了多家技术公司和服务提供商的支持,包括Atlassian、Box、LangChain、MongoDB、Salesforce、SAP和ServiceNow。微软计划将A2A集成到Azure AI Foundry和Copilot Studio中,展示了其对开放协议的承诺。此外,Auth0和SAP正在将A2A支持集成到他们的平台和智能体中。
As an open-source protocol, A2A welcomes community contributions to facilitate its evolution and widespread adoption.
作为一个开源协议,A2A欢迎社区贡献,以促进其发展和广泛采用。
Core Concepts of A2A
A2A的核心概念
The A2A protocol provides a structured approach for agent interactions, built upon several core concepts. A thorough grasp of these concepts is crucial for anyone developing or integrating with A2A-compliant systems. The foundational pillars of A2A include Core Actors, Agent Card, Agent Discovery, Communication and Tasks, Interaction mechanisms, and Security, all of which will be reviewed in detail.
A2A协议为智能体交互提供了一种结构化方法,建立在几个核心概念之上。深入理解这些概念对于任何开发或集成A2A兼容系统的人来说都至关重要。A2A的基础支柱包括核心参与者(Core Actors) 、智能体卡片(Agent Card) 、智能体发现(Agent Discovery) 、通信与任务(Communication and Tasks) 、交互机制(Interaction mechanisms) 和安全性(Security) ,所有这些都将在下文详细介绍。
Core Actors: A2A involves three main entities:
核心参与者: A2A涉及三个主要实体:
- User: Initiates requests for agent assistance.
- A2A Client (Client Agent): An application or AI agent that acts on the user's behalf to request actions or information.
- A2A Server (Remote Agent): An AI agent or system that provides an HTTP endpoint to process client requests and return results. The remote agent operates as an "opaque" system, meaning the client does not need to understand its internal operational details.
- 用户(User): 发起对智能体协助的请求。
- A2A客户端(Client Agent): 代表用户请求操作或信息的应用程序或AI智能体。
- A2A服务器(Remote Agent): 提供HTTP端点来处理客户端请求并返回结果的AI智能体或系统。远程智能体作为一个"不透明"系统运行,这意味着客户端不需要了解其内部操作细节。
Agent Card: An agent's digital identity is defined by its Agent Card, usually a JSON file. This file contains key information for client interaction and automatic discovery, including the agent's identity, endpoint URL, and version. It also details supported capabilities like streaming or push notifications, specific skills, default input/output modes, and authentication requirements. Below is an example of an Agent Card for a WeatherBot.
智能体卡片(Agent Card): 智能体的数字身份由其智能体卡片定义,通常是一个JSON文件。该文件包含客户端交互和自动发现所需的关键信息,包括智能体的身份、端点URL和版本。它还详细说明了支持的功能,如流式传输或推送通知、特定技能、默认输入/输出模式和身份验证要求。以下是WeatherBot的智能体卡片示例。
{
"name": "WeatherBot",
"description": "Provides accurate weather forecasts and historical data.",
"url": "http://weather-service.example.com/a2a",
"version": "1.0.0",
"capabilities": {
"streaming": true,
"pushNotifications": false,
"stateTransitionHistory": true
},
"authentication": {
"schemes": [
"apiKey"
]
},
"defaultInputModes": [
"text"
],
"defaultOutputModes": [
"text"
],
"skills": [
{
"id": "get_current_weather",
"name": "Get Current Weather",
"description": "Retrieve real-time weather for any location.",
"inputModes": [
"text"
],
"outputModes": [
"text"
],
"examples": [
"What's the weather in Paris?",
"Current conditions in Tokyo"
],
"tags": [
"weather",
"current",
"real-time"
]
},
{
"id": "get_forecast",
"name": "Get Forecast",
"description": "Get 5-day weather predictions.",
"inputModes": [
"text"
],
"outputModes": [
"text"
],
"examples": [
"5-day forecast for New York",
"Will it rain in London this weekend?"
],
"tags": [
"weather",
"forecast",
"prediction"
]
}
]
}
Agent discovery: it allows clients to find Agent Cards, which describe the capabilities of available A2A Servers. Several strategies exist for this process:
智能体发现(Agent Discovery): 它允许客户端找到智能体卡片,这些卡片描述了可用A2A服务器的功能。这一过程有几种策略:
- Well-Known URI: Agents host their Agent Card at a standardized path (e.g., /.well-known/agent.json). This approach offers broad, often automated, accessibility for public or domain-specific use.
- Curated Registries**:** These provide a centralized catalog where Agent Cards are published and can be queried based on specific criteria. This is well-suited for enterprise environments needing centralized management and access control.
- Direct Configuration**:** Agent Card information is embedded or privately shared. This method is appropriate for closely coupled or private systems where dynamic discovery isn't crucial.
- 众所周知的URI(Well-Known URI): 智能体将其智能体卡片托管在标准化路径(例如,/.well-known/agent.json)。这种方法为公共或特定领域使用提供了广泛的、通常是自动化的可访问性。
- 精选注册表(Curated Registries): 提供一个集中的目录,智能体卡片在其中发布,并可以根据特定条件进行查询。这非常适合需要集中管理和访问控制的企业环境。
- 直接配置(Direct Configuration): 智能体卡片信息被嵌入或私下共享。这种方法适用于紧密耦合或私有系统,其中动态发现不是至关重要的。
Regardless of the chosen method, it is important to secure Agent Card endpoints. This can be achieved through access control, mutual TLS (mTLS), or network restrictions, especially if the card contains sensitive (though non-secret) information.
无论选择哪种方法,保护智能体卡片端点都很重要。这可以通过访问控制、相互TLS(mTLS)或网络限制来实现,特别是如果卡片包含敏感(但非机密)信息。
Communications and Tasks: In the A2A framework, communication is structured around asynchronous tasks, which represent the fundamental units of work for long-running processes. Each task is assigned a unique identifier and moves through a series of states—such as submitted, working, or completed—a design that supports parallel processing in complex operations. Communication between agents occurs through a Message.
通信与任务(Communications and Tasks): 在A2A框架中,通信围绕异步任务(asynchronous tasks) 构建,这些任务代表长期运行过程的基本工作单元。每个任务都被分配一个唯一标识符,并通过一系列状态——如已提交、处理中或已完成——进行移动,这种设计支持复杂操作中的并行处理。智能体之间的通信通过消息(Message) 进行。
This communication contains attributes, which are key-value metadata describing the message (like its priority or creation time), and one or more parts, which carry the actual content being delivered, such as plain text, files, or structured JSON data. The tangible outputs generated by an agent during a task are called artifacts. Like messages, artifacts are also composed of one or more parts and can be streamed incrementally as results become available. All communication within the A2A framework is conducted over HTTP(S) using the JSON-RPC 2.0 protocol for payloads. To maintain continuity across multiple interactions, a server-generated contextId is used to group related tasks and preserve context.
这种通信包含属性(attributes) ,即描述消息的键值元数据(如其优先级或创建时间),以及一个或多个部分(parts) ,这些部分承载实际传递的内容,如纯文本、文件或结构化JSON数据。智能体在任务期间生成的有形输出称为成果(artifacts) 。与消息一样,成果也由一个或多个部分组成,可以随着结果的可用而逐步流式传输。A2A框架内的所有通信都通过HTTP(S)进行,使用JSON-RPC 2.0协议作为有效负载。为了在多个交互中保持连续性,使用服务器生成的contextId来对相关任务进行分组并保留上下文。
Interaction Mechanisms: Request/Response (Polling) Server-Sent Events (SSE). A2A provides multiple interaction methods to suit a variety of AI application needs, each with a distinct mechanism:
交互机制(Interaction Mechanisms): 请求/响应(轮询)、服务器发送事件(SSE)。A2A提供多种交互方法,以满足各种AI应用需求,每种方法都有不同的机制:
- Synchronous Request/Response: For quick, immediate operations. In this model, the client sends a request and actively waits for the server to process it and return a complete response in a single, synchronous exchange.
- Asynchronous Polling: Suited for tasks that take longer to process. The client sends a request, and the server immediately acknowledges it with a "working" status and a task ID. The client is then free to perform other actions and can periodically poll the server by sending new requests to check the status of the task until it is marked as "completed" or "failed."
- Streaming Updates (Server-Sent Events - SSE): Ideal for receiving real-time, incremental results. This method establishes a persistent, one-way connection from the server to the client. It allows the remote agent to continuously push updates, such as status changes or partial results, without the client needing to make multiple requests.
- Push Notifications (Webhooks): Designed for very long-running or resource-intensive tasks where maintaining a constant connection or frequent polling is inefficient. The client can register a webhook URL, and the server will send an asynchronous notification (a "push") to that URL when the task's status changes significantly (e.g., upon completion).
- 同步请求/响应(Synchronous Request/Response): 适用于快速、即时的操作。在这种模型中,客户端发送请求并主动等待服务器处理它,并在单个同步交换中返回完整响应。
- 异步轮询(Asynchronous Polling): 适用于处理时间较长的任务。客户端发送请求,服务器立即以"处理中"状态和任务ID确认。然后,客户端可以自由执行其他操作,并可以通过发送新请求定期轮询服务器,以检查任务状态,直到任务标记为"已完成"或"失败"。
- 流式更新(Streaming Updates)(服务器发送事件 - SSE): 理想用于接收实时、增量结果。这种方法建立了从服务器到客户端的持久单向连接。它允许远程智能体持续推送更新,如状态更改或部分结果,而客户端无需发出多个请求。
- 推送通知(Push Notifications)(Webhooks): 专为长时间运行或资源密集型任务设计,其中维持恒定连接或频繁轮询效率低下。客户端可以注册webhook URL,当任务状态发生显著变化(例如,完成时),服务器将向该URL发送异步通知("推送")。
The Agent Card specifies whether an agent supports streaming or push notification capabilities. Furthermore, A2A is modality-agnostic, meaning it can facilitate these interaction patterns not just for text, but also for other data types like audio and video, enabling rich, multimodal AI applications. Both streaming and push notification capabilities are specified within the Agent Card.
智能体卡片指定智能体是否支持流式传输或推送通知功能。此外,A2A是模态无关的(modality-agnostic) ,这意味着它不仅可以为文本,还可以为音频和视频等其他数据类型促进这些交互模式,从而实现丰富的多模态AI应用程序。流式传输和推送通知功能都在智能体卡片中指定。
{
"jsonrpc": "2.0",
"id": "1",
"method": "sendTask",
"params": {
"id": "task-001",
"sessionId": "session-001",
"message": {
"role": "user",
"parts": [
{
"type": "text",
"text": "What is the exchange rate from USD to EUR?"
}
]
},
"acceptedOutputModes": ["text/plain"],
"historyLength": 5
}
}
The synchronous request uses the sendTask method, where the client asks for and expects a single, complete answer to its query. In contrast, the streaming request uses the sendTaskSubscribe method to establish a persistent connection, allowing the agent to send back multiple, incremental updates or partial results over time.
同步请求使用sendTask方法,客户端请求并期望其查询得到一个完整的答案。相比之下,流式请求使用sendTaskSubscribe方法建立持久连接,允许智能体随时间发送多个增量更新或部分结果。
{
"jsonrpc": "2.0",
"id": "2",
"method": "sendTaskSubscribe",
"params": {
"id": "task-002",
"sessionId": "session-001",
"message": {
"role": "user",
"parts": [
{
"type": "text",
"text": "What's the exchange rate for JPY to GBP today?"
}
]
},
"acceptedOutputModes": ["text/plain"],
"historyLength": 5
}
}
Security: Inter-Agent Communication (A2A): Inter-Agent Communication (A2A) is a vital component of system architecture, enabling secure and seamless data exchange among agents. It ensures robustness and integrity through several built-in mechanisms.
安全性(Security): 智能体间通信(A2A)是系统架构的重要组成部分,实现智能体之间安全无缝的数据交换。它通过几种内置机制确保健壮性和完整性。
Mutual Transport Layer Security (TLS): Encrypted and authenticated connections are established to prevent unauthorized access and data interception, ensuring secure communication.
相互传输层安全性(Mutual Transport Layer Security,简称mTLS): 建立加密和认证连接,防止未授权访问和数据拦截,确保安全通信。
Comprehensive Audit Logs: All inter-agent communications are meticulously recorded, detailing information flow, involved agents, and actions. This audit trail is crucial for accountability, troubleshooting, and security analysis.
全面审计日志(Comprehensive Audit Logs): 所有智能体间通信都被仔细记录,详细说明信息流、涉及的智能体和操作。这种审计跟踪对于问责制、故障排除和安全分析至关重要。
Agent Card Declaration: Authentication requirements are explicitly declared in the Agent Card, a configuration artifact outlining the agent's identity, capabilities, and security policies. This centralizes and simplifies authentication management.
智能体卡片声明(Agent Card Declaration): 身份验证要求在智能体卡片中明确声明,这是一种概述智能体身份、功能和安全策略的配置工件。这集中并简化了身份验证管理。
Credential Handling: Agents typically authenticate using secure credentials like OAuth 2.0 tokens or API keys, passed via HTTP headers. This method prevents credential exposure in URLs or message bodies, enhancing overall security.
凭证处理(Credential Handling): 智能体通常使用安全凭证进行身份验证,如OAuth 2.0令牌或API密钥,通过HTTP头传递。这种方法防止凭证在URL或消息体中暴露,增强了整体安全性。
A2A vs. MCP
A2A与MCP的比较
A2A is a protocol that complements Anthropic's Model Context Protocol (MCP) (see Fig. 1). While MCP focuses on structuring context for agents and their interaction with external data and tools, A2A facilitates coordination and communication among agents, enabling task delegation and collaboration.
A2A是一种补充Anthropic的模型上下文协议(Model Context Protocol,简称MCP) 的协议(见图1)。虽然MCP专注于为智能体及其与外部数据和工具的交互构建上下文,但A2A促进智能体之间的协调和通信,实现任务委派和协作。
Fig.1: Comparison A2A and MCP Protocols 图1:A2A和MCP协议的比较
The goal of A2A is to enhance efficiency, reduce integration costs, and foster innovation and interoperability in the development of complex, multi-agent AI systems. Therefore, a thorough understanding of A2A's core components and operational methods is essential for its effective design, implementation, and application in building collaborative and interoperable AI agent systems..
A2A的目标是提高效率,降低集成成本,并在复杂、多智能体AI系统的开发中促进创新和互操作性。因此,深入理解A2A的核心组件和操作方法对于其在构建协作和互操作的AI智能体系统中的有效设计、实现和应用至关重要。
Practical Applications & Use Cases
实际应用和用例
Inter-Agent Communication is indispensable for building sophisticated AI solutions across diverse domains, enabling modularity, scalability, and enhanced intelligence.
智能体间通信对于在不同领域构建复杂的AI解决方案是不可或缺的,它实现了模块化、可扩展性和增强的智能。
- Multi-Framework Collaboration: A2A's primary use case is enabling independent AI agents, regardless of their underlying frameworks (e.g., ADK, LangChain, CrewAI), to communicate and collaborate. This is fundamental for building complex multi-agent systems where different agents specialize in different aspects of a problem.
- Automated Workflow Orchestration: In enterprise settings, A2A can facilitate complex workflows by enabling agents to delegate and coordinate tasks. For instance, an agent might handle initial data collection, then delegate to another agent for analysis, and finally to a third for report generation, all communicating via the A2A protocol.
- Dynamic Information Retrieval: Agents can communicate to retrieve and exchange real-time information. A primary agent might request live market data from a specialized "data fetching agent," which then uses external APIs to gather the information and send it back.
- 多框架协作(Multi-Framework Collaboration): A2A的主要用例是使独立的AI智能体能够通信和协作,无论其底层框架如何(例如,ADK、LangChain、CrewAI)。这对于构建复杂的多智能体系统至关重要,其中不同的智能体专门处理问题的不同方面。
- 自动化工作流编排(Automated Workflow Orchestration): 在企业环境中,A2A可以通过使智能体能够委派和协调任务来促进复杂工作流。例如,一个智能体可能处理初始数据收集,然后委派给另一个智能体进行分析,最后委派给第三个智能体生成报告,所有这些都通过A2A协议进行通信。
- 动态信息检索(Dynamic Information Retrieval): 智能体可以通信以检索和交换实时信息。主智能体可能会向专门的"数据获取智能体"请求实时市场数据,后者然后使用外部API收集信息并发送回来。
Hands-On Code Example
实践代码示例
Let's examine the practical applications of the A2A protocol. The repository at github.com/google-a2a/… provides examples in Java, Go, and Python that illustrate how various agent frameworks, such as LangGraph, CrewAI, Azure AI Foundry, and AG2, can communicate using A2A. All code in this repository is released under the Apache 2.0 license. To further illustrate A2A's core concepts, we will review code excerpts focusing on setting up an A2A Server using an ADK-based agent with Google-authenticated tools. Looking at github.com/google-a2a/…
让我们研究A2A协议的实际应用。位于github.com/google-a2a/…的存储库提供了Java、Go和Python的示例,展示了LangGraph、CrewAI、Azure AI Foundry和AG2等各种智能体框架如何使用A2A进行通信。该存储库中的所有代码都在Apache 2.0许可下发布。为了进一步说明A2A的核心概念,我们将回顾代码摘录,重点关注如何使用基于ADK的智能体和Google认证工具设置A2A服务器。查看github.com/google-a2a/…
import datetime
from google.adk.agents import LlmAgent # type: ignore[import-untyped]
from google.adk.tools.google_api_tool import CalendarToolset # type: ignore[import-untyped]
async def create_agent(client_id, client_secret) -> LlmAgent:
"""Constructs the ADK agent."""
toolset = CalendarToolset(client_id=client_id, client_secret=client_secret)
return LlmAgent(
model='gemini-2.0-flash-001',
name='calendar_agent',
description="An agent that can help manage a user's calendar",
instruction=f"""
You are an agent that can help manage a user's calendar.
Users will request information about the state of their calendar
or to make changes to their calendar.
Use the provided tools for interacting with the calendar API.
If not specified, assume the calendar the user wants is the 'primary' calendar.
When using the Calendar API tools, use well-formed RFC3339 timestamps.
Today is {datetime.datetime.now()}.
""",
tools=await toolset.get_tools(),
)
This Python code defines an asynchronous function create_agent that constructs an ADK LlmAgent. It begins by initializing a CalendarToolset using the provided client credentials to access the Google Calendar API. Subsequently, an LlmAgent instance is created, configured with a specified Gemini model, a descriptive name, and instructions for managing a user's calendar. The agent is furnished with calendar tools from the CalendarToolset, enabling it to interact with the Calendar API and respond to user queries regarding calendar states or modifications. The agent's instructions dynamically incorporate the current date for temporal context. To illustrate how an agent is constructed, let's examine a key section from the calendar_agent found in the A2A samples on GitHub.
这段Python代码定义了一个异步函数create_agent,用于构建ADK LlmAgent。它首先使用提供的客户端凭据初始化CalendarToolset,以访问Google Calendar API。随后,创建了一个LlmAgent实例,配置了指定的Gemini模型、描述性名称和管理用户日历的指令。智能体配备了来自CalendarToolset的日历工具,使其能够与Calendar API交互并响应用户关于日历状态或修改的查询。智能体的指令动态地结合了当前日期作为时间上下文。为了说明智能体是如何构建的,让我们检查GitHub上A2A样本中calendar_agent的关键部分。
The code below shows how the agent is defined with its specific instructions and tools. Please note that only the code required to explain this functionality is shown; you can access the complete file here: github.com/a2aproject/…
下面的代码展示了智能体如何使用其特定指令和工具进行定义。请注意,这里只显示了解释此功能所需的代码;您可以在此处访问完整文件:github.com/a2aproject/…
def main(host: str, port: int):
# Verify an API key is set.
# Not required if using Vertex AI APIs.
if os.getenv('GOOGLE_GENAI_USE_VERTEXAI') != 'TRUE' and not os.getenv(
'GOOGLE_API_KEY'
):
raise ValueError(
'GOOGLE_API_KEY environment variable not set and '
'GOOGLE_GENAI_USE_VERTEXAI is not TRUE.'
)
skill = AgentSkill(
id='check_availability',
name='Check Availability',
description="Checks a user's availability for a time using their Google Calendar",
tags=['calendar'],
examples=['Am I free from 10am to 11am tomorrow?'],
)
agent_card = AgentCard(
name='Calendar Agent',
description="An agent that can manage a user's calendar",
url=f'http://{host}:{port}/',
version='1.0.0',
defaultInputModes=['text'],
defaultOutputModes=['text'],
capabilities=AgentCapabilities(streaming=True),
skills=[skill],
)
adk_agent = asyncio.run(create_agent(
client_id=os.getenv('GOOGLE_CLIENT_ID'),
client_secret=os.getenv('GOOGLE_CLIENT_SECRET'),
))
runner = Runner(
app_name=agent_card.name,
agent=adk_agent,
artifact_service=InMemoryArtifactService(),
session_service=InMemorySessionService(),
memory_service=InMemoryMemoryService(),
)
agent_executor = ADKAgentExecutor(runner, agent_card)
async def handle_auth(request: Request) -> PlainTextResponse:
await agent_executor.on_auth_callback(
str(request.query_params.get('state')), str(request.url)
)
return PlainTextResponse('Authentication successful.')
request_handler = DefaultRequestHandler(
agent_executor=agent_executor, task_store=InMemoryTaskStore()
)
a2a_app = A2AStarletteApplication(
agent_card=agent_card, http_handler=request_handler
)
routes = a2a_app.routes()
routes.append(
Route(
path='/authenticate',
methods=['GET'],
endpoint=handle_auth,
)
)
app = Starlette(routes=routes)
uvicorn.run(app, host=host, port=port)
if __name__ == '__main__':
main()
This Python code demonstrates setting up an A2A-compliant "Calendar Agent" for checking user availability using Google Calendar. It involves verifying API keys or Vertex AI configurations for authentication purposes. The agent's capabilities, including the "check_availability" skill, are defined within an AgentCard, which also specifies the agent's network address. Subsequently, an ADK agent is created, configured with in-memory services for managing artifacts, sessions, and memory. The code then initializes a Starlette web application, incorporates an authentication callback and the A2A protocol handler, and executes it using Uvicorn to expose the agent via HTTP.
这段Python代码演示了如何设置一个A2A兼容的"日历智能体(Calendar Agent)",用于使用Google日历检查用户可用性。它涉及验证API密钥或Vertex AI配置以进行身份验证。智能体的功能,包括"check_availability"技能,在智能体卡片中定义,该卡片还指定了智能体的网络地址。随后,创建一个ADK智能体,配置内存中的服务来管理成果、会话和内存。然后,代码初始化一个Starlette Web应用程序,整合身份验证回调和A2A协议处理器,并使用Uvicorn执行它,通过HTTP公开智能体。
These examples illustrate the process of building an A2A-compliant agent, from defining its capabilities to running it as a web service. By utilizing Agent Cards and ADK, developers can create interoperable AI agents capable of integrating with tools like Google Calendar. This practical approach demonstrates the application of A2A in establishing a multi-agent ecosystem.
这些示例说明了构建A2A兼容智能体的过程,从定义其功能到将其作为Web服务运行。通过利用智能体卡片和ADK,开发人员可以创建能够与Google日历等工具集成的互操作AI智能体。这种实用方法展示了A2A在建立多智能体生态系统中的应用。
Further exploration of A2A is recommended through the code demonstration at www.trickle.so/blog/how-to…. Resources available at this link include sample A2A clients and servers in Python and JavaScript, multi-agent web applications, command-line interfaces, and example implementations for various agent frameworks.
建议通过www.trickle.so/blog/how-to…的代码演示进一步探索A2A。此链接提供的资源包括Python和JavaScript中的示例A2A客户端和服务器、多智能体Web应用程序、命令行界面以及各种智能体框架的示例实现。
At a Glance
概览
What: Individual AI agents, especially those built on different frameworks, often struggle with complex, multi-faceted problems on their own. The primary challenge is the lack of a common language or protocol that allows them to communicate and collaborate effectively. This isolation prevents the creation of sophisticated systems where multiple specialized agents can combine their unique skills to solve larger tasks. Without a standardized approach, integrating these disparate agents is costly, time-consuming, and hinders the development of more powerful, cohesive AI solutions.
什么是A2A: 单个AI智能体,尤其是那些基于不同框架构建的智能体,在独立处理复杂、多方面的问题时常常遇到困难。主要挑战在于缺乏一种共同语言或协议,使它们能够有效通信和协作。这种隔离阻碍了创建复杂系统的可能性,在这些系统中,多个专业智能体可以结合各自独特的技能来解决更大的任务。没有标准化方法,集成这些不同的智能体成本高昂、耗时且阻碍了更强大、更协调的AI解决方案的开发。
Why: The Inter-Agent Communication (A2A) protocol provides an open, standardized solution for this problem. It is an HTTP-based protocol that enables interoperability, allowing distinct AI agents to coordinate, delegate tasks, and share information seamlessly, regardless of their underlying technology. A core component is the Agent Card, a digital identity file that describes an agent's capabilities, skills, and communication endpoints, facilitating discovery and interaction. A2A defines various interaction mechanisms, including synchronous and asynchronous communication, to support diverse use cases. By creating a universal standard for agent collaboration, A2A fosters a modular and scalable ecosystem for building complex, multi-agent Agentic systems.
为什么需要A2A: 智能体间通信(A2A)协议为这个问题提供了一个开放、标准化的解决方案。它是一种基于HTTP的协议,实现互操作性,允许不同的AI智能体无缝协调、委派任务和共享信息,无论它们的底层技术如何。核心组件是智能体卡片(Agent Card) ,这是一种数字身份文件,描述智能体的能力、技能和通信端点,促进发现和交互。A2A定义了各种交互机制,包括同步和异步通信,以支持多样化的用例。通过为智能体协作创建通用标准,A2A促进了模块化和可扩展的生态系统,用于构建复杂的多智能体智能系统。
Rule of thumb: Use this pattern when you need to orchestrate collaboration between two or more AI agents, especially if they are built using different frameworks (e.g., Google ADK, LangGraph, CrewAI). It is ideal for building complex, modular applications where specialized agents handle specific parts of a workflow, such as delegating data analysis to one agent and report generation to another. This pattern is also essential when an agent needs to dynamically discover and consume the capabilities of other agents to complete a task.
经验法则(Rule of thumb): 当您需要编排两个或多个AI智能体之间的协作时,特别是当它们使用不同框架(如Google ADK、LangGraph、CrewAI)构建时,使用此模式。它非常适合构建复杂的模块化应用程序,其中专业智能体处理工作流的特定部分,例如将数据分析委派给一个智能体,将报告生成委派给另一个智能体。当智能体需要动态发现并利用其他智能体的能力来完成任务时,此模式也至关重要。
Visual summary
Fig.2: A2A inter-agent communication pattern 图2:A2A智能体间通信模式
Key Takeaways
关键要点
Key Takeaways:
- The Google A2A protocol is an open, HTTP-based standard that facilitates communication and collaboration between AI agents built with different frameworks.
- An AgentCard serves as a digital identifier for an agent, allowing for automatic discovery and understanding of its capabilities by other agents.
- A2A offers both synchronous request-response interactions (using
tasks/send) and streaming updates (usingtasks/sendSubscribe) to accommodate varying communication needs. - The protocol supports multi-turn conversations, including an
input-requiredstate, which allows agents to request additional information and maintain context during interactions. - A2A encourages a modular architecture where specialized agents can operate independently on different ports, enabling system scalability and distribution.
- Tools such as Trickle AI aid in visualizing and tracking A2A communications, which helps developers monitor, debug, and optimize multi-agent systems.
- While A2A is a high-level protocol for managing tasks and workflows between different agents, the Model Context Protocol (MCP) provides a standardized interface for LLMs to interface with external resources
关键要点:
- Google A2A协议是一种开放的、基于HTTP的标准,促进使用不同框架构建的AI智能体之间的通信和协作。
- 智能体卡片(AgentCard) 作为智能体的数字标识符,允许其他智能体自动发现并理解其能力。
- A2A提供同步请求-响应交互(使用
tasks/send)和流式更新(使用tasks/sendSubscribe),以适应不同的通信需求。 - 该协议支持多轮对话,包括
input-required状态,允许智能体在交互期间请求额外信息并维护上下文。 - A2A鼓励模块化架构,其中专业智能体可以在不同端口上独立运行,实现系统可扩展性和分布式部署。
- 诸如Trickle AI之类的工具有助于可视化和跟踪A2A通信,帮助开发人员监控、调试和优化多智能体系统。
- 虽然A2A是用于管理不同智能体之间任务和工作流的高级协议,但模型上下文协议(Model Context Protocol,简称MCP) 为LLM与外部资源接口提供了标准化接口。
Conclusions
结论
The Inter-Agent Communication (A2A) protocol establishes a vital, open standard to overcome the inherent isolation of individual AI agents. By providing a common HTTP-based framework, it ensures seamless collaboration and interoperability between agents built on different platforms, such as Google ADK, LangGraph, or CrewAI. A core component is the Agent Card, which serves as a digital identity, clearly defining an agent's capabilities and enabling dynamic discovery by other agents. The protocol's flexibility supports various interaction patterns, including synchronous requests, asynchronous polling, and real-time streaming, catering to a wide range of application needs.
智能体间通信(A2A)协议建立了一个重要的开放标准,以克服单个AI智能体固有的隔离性。通过提供通用的基于HTTP的框架,它确保了基于不同平台(如Google ADK、LangGraph或CrewAI)构建的智能体之间的无缝协作和互操作性。核心组件是智能体卡片(Agent Card) ,它作为数字身份,清晰定义智能体的能力,并允许其他智能体动态发现。该协议的灵活性支持各种交互模式,包括同步请求、异步轮询和实时流式传输,满足广泛的应用需求。
This enables the creation of modular and scalable architectures where specialized agents can be combined to orchestrate complex automated workflows. Security is a fundamental aspect, with built-in mechanisms like mTLS and explicit authentication requirements to protect communications. While complementing other standards like MCP, A2A's unique focus is on the high-level coordination and task delegation between agents. The strong backing from major technology companies and the availability of practical implementations highlight its growing importance. This protocol paves the way for developers to build more sophisticated, distributed, and intelligent multi-agent systems. Ultimately, A2A is a foundational pillar for fostering an innovative and interoperable ecosystem of collaborative AI.
这使得创建模块化和可扩展架构成为可能,其中专业智能体可以组合在一起,编排复杂的自动化工作流。安全性是一个基本方面,内置机制如相互传输层安全性(mTLS) 和明确的身份验证要求来保护通信。虽然A2A补充了MCP等其他标准,但它独特的重点是智能体之间的高级协调和任务委派。来自主要技术公司的强大支持和实际实现的可用性凸显了其日益增长的重要性。该协议为开发人员构建更复杂、分布式和智能的多智能体系统铺平了道路。最终,A2A是培养创新和互操作的协作AI生态系统的基础支柱。
References
参考文献
- Chen, B. (2025, April 22). How to Build Your First Google A2A Project: A Step-by-Step Tutorial. Trickle.so Blog. www.trickle.so/blog/how-to…
- Google A2A GitHub Repository. github.com/google-a2a/…
- Google Agent Development Kit (ADK) google.github.io/adk-docs/
- Getting Started with Agent-to-Agent (A2A) Protocol: codelabs.developers.google.com/intro-a2a-p…
- Google AgentDiscovery - a2a-protocol.org/latest/
- Communication between different AI frameworks such as LangGraph, CrewAI, and Google ADK www.trickle.so/blog/how-to…
- Designing Collaborative Multi-Agent Systems with the A2A Protocol www.oreilly.com/radar/desig…