跳转到内容

快速入门:通过 A2A 模式使用远程代理

在 ADK 中得到支持。Go实验性的(Experimental)

这个快速入门指南涵盖了所有开发者最常见的起点。 “有一个远程代理,我该如何让我的 ADK 代理通过 A2A 协议来使用它呢?”这对于构建复杂的多智能体系统至关重要;在这些系统中,不同的智能体需要相互协作和交流。

概述

这个示例展示了…… Agent2Agent(A2A) 该示例展示了代理开发工具包(Agent Development Kit, ADK)中的架构设计,说明了多个代理如何协同工作以处理复杂任务。具体来说,该示例实现了一个能够掷骰子并判断数字是否为质数的代理程序。

┌─────────────────┐    ┌──────────────────┐    ┌────────────────────┐
│   Root Agent    │───▶│   Roll Agent     │    │   Remote Prime     │
│  (Local)        │    │   (Local)        │    │   Agent            │
│                 │    │                  │    │  (localhost:8001)  │
│                 │───▶│                  │◀───│                    │
└─────────────────┘    └──────────────────┘    └────────────────────┘

A2A Basic 示例包括以下内容:

  • 根代理(Root Agent) (root_agent):这个主要的协调者负责将任务分配给各个专门的子代理(sub-agents)。
  • Roll Agent (roll_agent一个负责处理骰子投掷操作的本地子代理。
  • Prime Agent (prime_agent这是一个远程的 A2A(点对点)代理程序,用于判断数字是否为质数;该代理程序运行在另一台独立的 A2A 服务器上。

通过 ADK 服务器公开您的代理程序

在…… a2a_basic 例如,您首先需要公开(或暴露)某些资源或功能。 check_prime_agent 通过一个 A2A(点对点)服务器来传输数据,这样本地的根代理(root agent)就可以使用这些数据了。

1. 获取示例代码

首先,请确保您已经安装了 Go 语言,并且环境配置正确。

您可以克隆该文件,并导航到其中。 a2a_basic sample `here:`

cd examples/go/a2a_basic

如您所见,文件夹结构如下:

a2a_basic/
├── remote_a2a/
│   └── check_prime_agent/
│       └── main.go
├── go.mod
├── go.sum
└── main.go # local root agent

**主代理(Main Agent)**a2a_basic/main.go)

  • rollDieTool:用于掷骰子的功能工具
  • newRollAgent:一位专门负责掷骰子的本地代理。
  • newPrimeAgent:远程 A2A 代理配置
  • newRootAgent:具有委托逻辑(delegation logic)的主要协调器(main orchestrator)。

Remote Prime Agent(远程Prime代理)a2a_basic/remote_a2a/check_prime_agent/main.go)

  • checkPrimeTool:素数检测算法
  • main:实现了素数检测服务以及点对点(A2A)服务器的功能。

2. 启动 Remote Prime Agent 服务器

为了演示您的 ADK 代理如何通过 A2A(点对点)方式与远程代理进行通信,您首先需要启动一个远程代理服务器。该服务器将用于托管主代理程序(即负责处理通信请求的代理程序)。 check_prime_agent).

# Start the remote a2a server that serves the check_prime_agent on port 8001
go run remote_a2a/check_prime_agent/main.go

执行完成后,你应该会看到类似以下的内容:

2025/11/06 11:00:19 Starting A2A prime checker server on port 8001
2025/11/06 11:00:19 Starting the web server: &{port:8001}
2025/11/06 11:00:19 
2025/11/06 11:00:19 Web servers starts on http://localhost:8001
2025/11/06 11:00:19        a2a:  you can access A2A using jsonrpc protocol: http://localhost:8001

3. 请注意远程代理所需的代理卡(agent card)。

A2A 协议要求每个代理都必须拥有一张“代理卡”,该卡片用于描述该代理的具体功能或作用。

在 Go ADK 中,当您使用 A2A 启动器暴露某个代理时,代理卡片会动态生成。您可以访问…… http://localhost:8001/.well-known/agent-card.json 要查看生成的卡片,请点击相应的链接。

4. 运行主代理(即负责数据消费的代理)。

# In a separate terminal, run the main agent
go run main.go

它是如何工作的?

主要代理程序使用的是…… remoteagent.NewA2A from the google.golang.org/adk/v2/agent/remoteagent/v2 用于消费远程代理的软件包:)prime_agent (在我们的例子中)。如下所示,它设置了…… Name这是一个可选的元素。 Description,以及一个 AgentCardProvider. NewA2A 需要以下其中之一: 1. 一个静态值(static value); 2. ……(此处应补充具体所需的内容)。 AgentCard 或者一个 AgentCardProvider,以及 remoteagent.NewAgentCardProvider 该函数可以根据 URL 或本地文件路径创建一个提供者(provider)。需要注意的是,这个函数属于较旧版本的软件/库。 google.golang.org/adk/v2/agent/remoteagent `package`(软件包)……(后面的内容被省略了。) /v2 `suffix` 这个属性已经被弃用了(即不再被支持或推荐使用)。

a2a_basic/main.go
func newPrimeAgent() (agent.Agent, error) {
    remoteAgent, err := remoteagent.NewA2A(remoteagent.A2AConfig{
        Name:              "prime_agent",
        Description:       "Agent that handles checking if numbers are prime.",
        AgentCardProvider: remoteagent.NewAgentCardProvider("http://localhost:8001"),
    })
    if err != nil {
        return nil, fmt.Errorf("failed to create remote prime agent: %w", err)
    }
    return remoteAgent, nil
}

然后,你就可以在根代理(root agent)中直接使用远程代理(remote agent)了。在这种情况下…… primeAgent 被用作其中一个子代理(sub-agent)。 root_agent below:

a2a_basic/main.go
func newRootAgent(ctx context.Context, rollAgent, primeAgent agent.Agent) (agent.Agent, error) {
    model, err := gemini.NewModel(ctx, "gemini-2.0-flash", &genai.ClientConfig{})
    if err != nil {
        return nil, err
    }
    return llmagent.New(llmagent.Config{
        Name:  "root_agent",
        Model: model,
        Instruction: `
      You are a helpful assistant that can roll dice and check if numbers are prime.
      You delegate rolling dice tasks to the roll_agent and prime checking tasks to the prime_agent.
      Follow these steps:
      1. If the user asks to roll a die, delegate to the roll_agent.
      2. If the user asks to check primes, delegate to the prime_agent.
      3. If the user asks to roll a die and then check if the result is prime, call roll_agent first, then pass the result to prime_agent.
      Always clarify the results before proceeding.
    `,
        SubAgents: []agent.Agent{rollAgent, primeAgent},
        Tools:     []tool.Tool{},
    })
}

示例交互场景

一旦你的主代理(main agent)和远程代理(remote agent)都运行起来,你就可以与根代理(root agent)进行交互,查看它是如何通过 A2A(Agent-to-Agent)机制来调用远程代理的。

简单的骰子投掷游戏: 这种交互使用了一个本地代理程序,即 Roll Agent。

User: Roll a 6-sided die
Bot calls tool: transfer_to_agent with args: map[agent_name:roll_agent]
Bot calls tool: roll_die with args: map[sides:6]
Bot: I rolled a 6-sided die and the result is 6.

质数检测:

这种交互方式通过 A2A(即点对点)技术使用了一个远程代理——即“Prime Agent”(主要代理)。

User: Is 7 a prime number?
Bot calls tool: transfer_to_agent with args: map[agent_name:prime_agent]
Bot calls tool: prime_checking with args: map[nums:[7]]
Bot: Yes, 7 is a prime number.

联合行动:

这次交互同时使用了本地的 Roll Agent 和远程的 Prime Agent:

User: roll a die and check if it's a prime
Bot: Okay, I will first roll a die and then check if the result is a prime number.

Bot calls tool: transfer_to_agent with args: map[agent_name:roll_agent]
Bot calls tool: roll_die with args: map[sides:6]
Bot calls tool: transfer_to_agent with args: map[agent_name:prime_agent]
Bot calls tool: prime_checking with args: map[nums:[3]]
Bot: 3 is a prime number.

下一步行动

既然您已经创建了一个通过 A2A 服务器使用远程代理的代理程序,下一步就是学习如何公开(即让其他系统能够访问)您自己开发的代理程序。