用 A2A 启动器将 Go ADK 智能体暴露为远程服务

该快速入门面向已有 Go ADK 智能体的开发者,说明如何借助 A2A 启动器让其他智能体通过 A2A 协议调用该智能体,并指出启动器会动态生成智能体卡片,最后给出后续消费远程智能体的学习方向。

## 快速启动: 通过 A2A 曝光远程代理[¶](<https://adk.dev/a2a/quickstart-exposing-go/#quickstart-exposing-a-remote-agent-via-a2a>) 支持于 ADKGoExperimental 这个快速启动涵盖了任何开发者最常用的起点: **"我有一个特工,我该如何揭露它,以便其他特工可以通过A2A使用我的特工".**这一点对于建立复杂的多代理系统至关重要,因为不同的代理机构需要合作和互动。 ## 概览[¶](<https://adk.dev/a2a/quickstart-exposing-go/#overview>) 这个样本证明了你如何可以轻易地暴露出一种ADK剂,以便随后被另一种剂使用A2A协议消耗. 在Go中,你通过使用A2A发射器来曝光一个代理,它能动态地为您生成代理卡. ``` ┌─────────────────┐ ┌───────────────────────────────┐ │ Root Agent │ A2A Protocol │ A2A-Exposed Check Prime Agent │ │ │────────────────────────────▶│ (localhost: 8001) │ └─────────────────┘ └───────────────────────────────┘ ``` 样本包括: - **远程主代理** (`remote_a2a/check_prime_agent/main.go`: 这是您想要曝光的代理, 以便其他代理可以通过 A2A 使用它。 它是处理质数检查的代理。 它会使用 A2A 发射器进行曝光 。 - **根代理** (`main.go`:一个简单的代理,它只是呼叫远程主代理. ## 用 A2A 发射器曝光远程代理[¶](<https://adk.dev/a2a/quickstart-exposing-go/#exposing-the-remote-agent-with-the-a2a-launcher>) 您可以使用使用Go ADK构建的现有代理,并通过使用A2A发射器使其A2A兼容. ### 1. 获取样本代码[¶](<https://adk.dev/a2a/quickstart-exposing-go/#getting-the-sample-code>) 首先,确保您安装了Go,并且您的环境已经设置了. 您可以克隆并导航到 [**`a2a_basic`** 样本](<https://github.com/google/adk-docs/tree/main/examples/go/a2a_basic>) 此处: ``` cd examples/go/a2a_basic ``` 如你所见,文件夹结构如下: ``` a2a_basic/ ├── remote_a2a/ │ └── check_prime_agent/ │ └── main.go # Remote Prime Agent ├── go.mod ├── go.sum └── main.go # Root agent ``` #### 根代理( R)`a2a_basic/main.go`)[¶](<https://adk.dev/a2a/quickstart-exposing-go/#root-agent-a2a_basicmaingo>) - **`newRootAgent`**:一个连接远程A2A服务的本地代理. #### 远程主代理( R)`a2a_basic/remote_a2a/check_prime_agent/main.go`)[¶](<https://adk.dev/a2a/quickstart-exposing-go/#remote-prime-agent-a2a_basicremote_a2acheck_prime_agentmaingo>) - **`checkPrimeTool`**: 质数检查的函数 。 - **`main`**:创建代理并启动A2A服务器的主要功能. ### 2. 启动远程 A2A 代理服务器[¶](<https://adk.dev/a2a/quickstart-exposing-go/#start-the-remote-a2a-agent-server>) 您现在可以启动远程代理服务器, 它将主机 `check_prime_agent`: ``` # Start the remote agent 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. 检查您的远程代理正在运行[¶](<https://adk.dev/a2a/quickstart-exposing-go/#check-that-your-remote-agent-is-running>) 您可以通过访问由 A2A 发射器自动生成的代理卡来检查您的代理机是否正常运行 : http://localhost:8001/.well-known/agent-card.json 您应当查看代理卡的内容 。 ### 4. 运行主( 消费) 代理[¶](<https://adk.dev/a2a/quickstart-exposing-go/#run-the-main-consuming-agent>) 现在你的远程特工正在运行, 你可以运行主特工。 ``` # In a separate terminal, run the main agent go run main.go ``` #### 如何运作[¶](<https://adk.dev/a2a/quickstart-exposing-go/#how-it-works>) 该远程代理在其中使用 A2A 发射器曝光 `main` 函数。发射器负责启动服务器并生成代理卡。 remote_a2a/check_prime_agent/main.go ``` 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) } } ``` ## 示例互动[¶](<https://adk.dev/a2a/quickstart-exposing-go/#example-interactions>) 一旦两个服务运行,您就可以与根代理进行交互,看看它如何通过A2A来称呼远程代理: **首选项检查 :** 此交互通过 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. ... ``` ## 下一个步骤[¶](<https://adk.dev/a2a/quickstart-exposing-go/#next-steps>) 现在,你已经创建了一个代理,正在通过A2A服务器曝光一个远程代理,下一步是从另一个代理中学习如何消耗它. - [**A2A 快速启动( 消费)**](<https://adk.dev/a2a/quickstart-consuming-go/>):学习你的代理如何使用其他代理使用A2A协议.

查看原始来源