Skip to content

快速入门:通过 A2A 公开远程智能体

Supported in ADKJavaExperimental

本快速入门涵盖了任何开发者最常见的起点:“我有一个智能体。如何公开它,以便其他智能体可以通过 A2A 使用我的智能体?”。这对于构建复杂的、需要不同智能体协同和交互的多智能体系统至关重要。

概览

本示例演示了如何使用 Quarkus 公开 ADK 智能体,以便另一个智能体可以使用 A2A 协议进行消费。

在 Java 中,你可以通过依赖 ADK A2A 扩展来原生构建 A2A 服务器。它使用 Quarkus 框架,这意味着你只需在标准的 Quarkus @ApplicationScoped 绑定中直接配置你的智能体。

┌─────────────────┐                             ┌───────────────────────────────┐
│   Root Agent    │       A2A Protocol          │ A2A-Exposed Check Prime Agent │
│                 │────────────────────────────▶│       (localhost:9090)        │
└─────────────────┘                             └───────────────────────────────┘

使用 Quarkus 公开远程智能体

使用 Quarkus,你可以将智能体映射到 A2A 执行端点,而无需手动处理传入的 HTTP JSON-RPC 负载或会话。

1. 获取示例代码

最快的入门方法是查看 adk-java 仓库contrib/samples/a2a_server 文件夹下的独立 Quarkus 应用。

cd contrib/samples/a2a_server

2. 工作原理

核心运行时使用提供的 AgentExecutor,它要求你构建一个 CDI @Produces bean 来配置你的原生 BaseAgent。Quarkus A2A 扩展会发现此配置并自动连接端点。

A2aExposingSnippet.java
import com.google.adk.a2a.executor.AgentExecutorConfig;
import com.google.adk.core.BaseAgent;
import com.google.adk.core.LlmAgent;
import com.google.adk.sessions.InMemorySessionService;
import io.a2a.server.agentexecution.AgentExecutor;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import com.google.adk.artifacts.InMemoryArtifactService;

/**
 * Exposing an agent to the A2A network using ADK's Quarkus module.
 * By defining an AgentExecutor as a CDI @Produces, the framework
 * automatically binds your agent to the A2A endpoint.
 */
@ApplicationScoped
public class A2aExposingSnippet {

    @Produces
    public AgentExecutor agentExecutor() {
        BaseAgent myRemoteAgent = LlmAgent.builder()
            .name("prime_agent")
            .model("gemini-2.5-flash")
            .instruction("You are an expert in mathematics. Check if numbers are prime.")
            .build();

        return new com.google.adk.a2a.executor.AgentExecutor.Builder()
            .agent(myRemoteAgent)
            .appName("my-adk-a2a-server")
            .sessionService(new InMemorySessionService())
            .artifactService(new InMemoryArtifactService())
            .agentExecutorConfig(AgentExecutorConfig.builder().build())
            .build();
    }
}

该应用会自动处理挂载在 /a2a/remote/v1/message:send 上的传入 HTTP JSON-RPC 调用,将内容、历史记录和上下文直接转发到你的 BaseAgent 流程中。

3. 启动远程 A2A 智能体服务器

在原生 ADK 结构中,你可以运行 Quarkus 开发模式任务:

./mvnw -f contrib/samples/a2a_server/pom.xml quarkus:dev

执行后,Quarkus 会自动托管符合 A2A 规范的 REST 路径。通过手动执行 curl,你可以使用原生 A2A 规范立即对负载进行冒烟测试:

curl -X POST http://localhost:9090 \
  -H "Content-Type: application/json" \
  -d '{
        "jsonrpc": "2.0",
        "id": "cli-check",
        "method": "message/send",
        "params": {
          "message": {
            "kind": "message",
            "contextId": "cli-demo-context",
            "messageId": "cli-check-id",
            "role": "user",
            "parts": [
              { "kind": "text", "text": "Is 3 prime?" }
            ]
          }
        }
      }'

4. 检查你的远程智能体是否正在运行

一个合适的智能体卡片会自动公开在代表你实例的标准路径上: http://localhost:9090/.well-known/agent-card.json

你应该能够在响应 JSON 中看到从智能体配置动态镜像而来的名称。

下一步

现在你已经通过 A2A 公开了你的智能体,下一步是学习如何从另一个智能体编排器原生消费它。