Quickstart: Exposing a remote agent via A2A¶
快速入门:通过 A2A 暴露远程智能体¶
This quickstart covers the most common starting point for any developer: "I have an agent. How do I expose it so that other agents can use my agent via A2A?". This is crucial for building complex multi-agent systems where different agents need to collaborate and interact.
本快速入门涵盖了任何开发者最常见的起点:"我有一个智能体。如何暴露它,以便其他智能体可以通过 A2A 使用我的智能体?"。这对于构建需要不同智能体协作和交互的复杂多智能体系统至关重要。
Overview¶
概述¶
This sample demonstrates how you can easily expose an ADK agent so that it can be then consumed by another agent using A2A Protocol.
此示例演示了如何轻松暴露 ADK 智能体,以便另一个智能体可以使用 A2A 协议消费它。
In Go, you expose an agent by using the A2A launcher, which dynamically generates an agent card for you.
在 Go 中,您使用 A2A 启动器来暴露智能体,它会为您动态生成智能体卡。
┌─────────────────┐ ┌───────────────────────────────┐
│ Root Agent │ A2A Protocol │ A2A-Exposed Check Prime Agent │
│ │────────────────────────────▶│ (localhost: 8001) │
└─────────────────┘ └───────────────────────────────┘
The sample consists of :
该示例由以下部分组成:
- Remote Prime Agent (
remote_a2a/check_prime_agent/main.go): This is the agent that you want to expose so that other agents can use it via A2A. It is an agent that handles prime number checking. It becomes exposed using the A2A launcher. - 远程质数检查智能体 (
remote_a2a/check_prime_agent/main.go): 这是您想要暴露的智能体,以便其他智能体可以通过 A2A 使用它。它是处理质数检查的智能体。它使用 A2A 启动器暴露。 - Root Agent (
main.go): A simple agent that is just calling the remote prime agent. - 根智能体 (
main.go): 一个简单的智能体,只是调用远程质数检查智能体。
Exposing the Remote Agent with A2A Launcher¶
使用 A2A 启动器暴露远程智能体¶
You can take an existing agent built using the Go ADK and make it A2A-compatible by using the A2A launcher.
您可以使用 A2A 启动器使使用 Go ADK 构建的现有智能体变得 A2A 兼容。
1. Getting the Sample Code¶
1. 获取示例代码¶
First, make sure you have Go installed and your environment is set up.
首先,确保您已安装 Go 并设置了环境。
You can clone and navigate to the a2a_basic sample here:
您可以在此处克隆并导航到 a2a_basic 示例:
As you'll see, the folder structure is as follows:
如您所见,文件夹结构如下:
a2a_basic/
├── remote_a2a/
│ └── check_prime_agent/
│ └── main.go # Remote Prime Agent
├── go.mod
├── go.sum
└── main.go # Root agent
Root Agent (a2a_basic/main.go)¶
根智能体 (a2a_basic/main.go)¶
newRootAgent: A local agent that connects to the remote A2A service.newRootAgent: 连接到远程 A2A 服务的本地智能体。
Remote Prime Agent (a2a_basic/remote_a2a/check_prime_agent/main.go)¶
远程质数检查智能体 (a2a_basic/remote_a2a/check_prime_agent/main.go)¶
checkPrimeTool: Function for prime number checking.checkPrimeTool: 用于质数检查的函数。main: The main function that creates the agent and starts the A2A server.main: 创建智能体并启动 A2A 服务器的 main 函数。
2. Start the Remote A2A Agent server¶
2. 启动远程 A2A 智能体服务器¶
You can now start the remote agent server, which will host the check_prime_agent:
您现在可以启动远程智能体服务器,该服务器将托管 check_prime_agent:
Once executed, you should see something like:
执行后,您应该会看到类似以下内容:
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. Check that your remote agent is running¶
3. 检查您的远程智能体是否正在运行¶
You can check that your agent is up and running by visiting the agent card that was auto-generated by the A2A launcher:
您可以通过访问由 A2A 启动器自动生成的智能体卡来检查您的智能体是否已启动并正在运行:
http://localhost:8001/.well-known/agent-card.json
You should see the contents of the agent card.
您应该会看到智能体卡的内容。
4. Run the Main (Consuming) Agent¶
4. 运行主(消费)智能体¶
Now that your remote agent is running, you can run the main agent.
既然您的远程智能体正在运行,您就可以运行主智能体了。
How it works¶
工作原理¶
The remote agent is exposed using the A2A launcher in the main function. The launcher takes care of starting the server and generating the agent card.
远程智能体在 main 函数中使用 A2A 启动器暴露。启动器负责启动服务器和生成智能体卡。
func main() {
ctx := context.Background()
primeTool, err := functiontool.New(functiontool.Config{
Name: "prime_checking",
Description: "Check if numbers in a list are prime using efficient mathematical algorithms",
}, checkPrimeTool)
if err != nil {
log.Fatalf("Failed to create prime_checking tool: %v", err)
}
model, err := gemini.NewModel(ctx, "gemini-2.0-flash", &genai.ClientConfig{})
if err != nil {
log.Fatalf("Failed to create model: %v", err)
}
primeAgent, err := llmagent.New(llmagent.Config{
Name: "check_prime_agent",
Description: "check prime agent that can check whether numbers are prime.",
Instruction: `
You check whether numbers are prime.
When checking prime numbers, call the check_prime tool with a list of integers. Be sure to pass in a list of integers. You should never pass in a string.
You should not rely on the previous history on prime results.
`,
Model: model,
Tools: []tool.Tool{primeTool},
})
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}
// Create launcher. The a2a.NewLauncher() will dynamically generate the agent card.
port := 8001
webLauncher := web.NewLauncher(a2a.NewLauncher())
_, err = webLauncher.Parse([]string{
"--port", strconv.Itoa(port),
"a2a", "--a2a_agent_url", "http://localhost:" + strconv.Itoa(port),
})
if err != nil {
log.Fatalf("launcher.Parse() error = %v", err)
}
// Create ADK config
config := &launcher.Config{
AgentLoader: agent.NewSingleLoader(primeAgent),
SessionService: session.InMemoryService(),
}
log.Printf("Starting A2A prime checker server on port %d\n", port)
// Run launcher
if err := webLauncher.Run(context.Background(), config); err != nil {
log.Fatalf("webLauncher.Run() error = %v", err)
}
}
Example Interactions¶
示例交互¶
Once both services are running, you can interact with the root agent to see how it calls the remote agent via A2A:
一旦两个服务都在运行,您就可以与根智能体交互,以查看它如何通过 A2A 调用远程智能体:
Prime Number Checking: 质数检查:
This interaction uses a remote agent via A2A, Prime Agent:
此交互使用通过 A2A 的远程智能体 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.
...
Next Steps¶
下一步¶
Now that you have created an agent that's exposing a remote agent via an A2A server, the next step is to learn how to consume it from another agent.
既然您已经创建了一个通过 A2A 服务器暴露远程智能体的智能体,下一步是学习如何从另一个智能体消费它。
- A2A Quickstart (Consuming): Learn how your agent can use other agents using the A2A Protocol.
- A2A 快速入门(消费): 学习您的智能体如何使用 A2A 协议使用其他智能体。