|
| 1 | +# Quickstart: Consuming a remote agent via A2A |
| 2 | + |
| 3 | +<div class="language-support-tag"> |
| 4 | + <span class="lst-supported">Supported in ADK</span><span class="lst-java">Java</span><span class="lst-preview">Experimental</span> |
| 5 | +</div> |
| 6 | + |
| 7 | +This quickstart covers the most common starting point for any developer: **"There is a remote agent, how do I let my ADK agent use it via A2A?"**. This is crucial for building complex multi-agent systems where different agents need to collaborate and interact. |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +This sample demonstrates the **Agent2Agent (A2A)** architecture in the Agent Development Kit (ADK) for Java, showcasing how multiple agents can work together to handle complex tasks. |
| 12 | + |
| 13 | +```text |
| 14 | +┌─────────────────┐ ┌─────────────────┐ ┌────────────────────────┐ |
| 15 | +│ Root Agent │───▶│ Roll Agent │ │ Remote Prime Agent │ |
| 16 | +│ (Local) │ │ (Local) │ │ (localhost:8001) │ |
| 17 | +│ │───▶│ │◀───│ │ |
| 18 | +└─────────────────┘ └─────────────────┘ └────────────────────────┘ |
| 19 | +``` |
| 20 | + |
| 21 | +The A2A Basic sample consists of: |
| 22 | + |
| 23 | +- **Root Agent** (`root_agent`): The main orchestrator that delegates tasks to specialized sub-agents |
| 24 | +- **Roll Agent** (`roll_agent`): A local sub-agent that handles dice rolling operations |
| 25 | +- **Prime Agent** (`prime_agent`): A remote A2A agent that checks if numbers are prime, this agent is running on a separate A2A server |
| 26 | + |
| 27 | +## Consuming Your Agent Using the ADK Java SDK |
| 28 | + |
| 29 | +In Java, rather than manually generating requests, ADK relies on the official A2A SDK `Client` wrapped precisely over a `RemoteA2AAgent` entity. Note that the Java SDK currently uses A2A Protocol 0.3. |
| 30 | + |
| 31 | +### 1. Getting the Sample Code { #getting-the-sample-code } |
| 32 | + |
| 33 | +The native sample matching this quickstart workflow in Java can be found in the `adk-java` source code under `contrib/samples/a2a_basic`. |
| 34 | + |
| 35 | +You can navigate to the [**`a2a_basic`** sample](https://github.com/google/adk-java/tree/main/contrib/samples/a2a_basic): |
| 36 | + |
| 37 | +```bash |
| 38 | +cd contrib/samples/a2a_basic |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. Start the Remote Prime Agent server { #start-the-remote-prime-agent-server } |
| 42 | + |
| 43 | +To show how your ADK agent can consume a remote agent via A2A, you'll first need a remote agent server running. While you can write your own custom A2A server in any language, ADK provides the `a2a_server` sample which starts a Quarkus service hosting the agent on `:9090`. |
| 44 | + |
| 45 | +```bash |
| 46 | +# In adk-java root, start the pre-configured Quarkus remote service on port 9090 |
| 47 | +./mvnw -f contrib/samples/a2a_server/pom.xml quarkus:dev |
| 48 | +``` |
| 49 | + |
| 50 | +Once running successfully, the agent will be accessible via HTTP endpoints locally. |
| 51 | + |
| 52 | +### 3. Look out for the required agent card of the remote agent { #look-out-for-the-required-agent-card-of-the-remote-agent } |
| 53 | + |
| 54 | +A2A Protocol requires that each agent have an agent card that describes what it does to other nodes over the network. In an A2A server, the agent card is generated dynamically on boot and hosted statically. |
| 55 | + |
| 56 | +For an ADK Java webservice, the agent card is generally accessible dynamically using the [`.well-known/agent-card.json`](http://localhost:9090/.well-known/agent-card.json) standard endpoint format relative to its base URL. |
| 57 | + |
| 58 | +### 4. Run the Main (Consuming) Agent { #run-the-main-consuming-agent } |
| 59 | + |
| 60 | +In another terminal, you can run the client agent: |
| 61 | + |
| 62 | +```bash |
| 63 | +./mvnw -f contrib/samples/a2a_basic/pom.xml exec:java -Dexec.args="http://localhost:9090" |
| 64 | +``` |
| 65 | + |
| 66 | +#### How it works |
| 67 | + |
| 68 | +The main agent accesses the required A2A JSON-RPC transport wrapper to consume the remote agent (`prime_agent` in our example). As you can see below, it queries for the `AgentCard` from the target host and registers it locally inside an official A2A `Client`. |
| 69 | + |
| 70 | +```java title="A2aConsumerSnippet.java" |
| 71 | +--8<-- "examples/java/snippets/src/main/java/a2a/A2aConsumerSnippet.java:new-prime-agent" |
| 72 | +``` |
| 73 | + |
| 74 | +You can then pass the remote agent instance naturally to your agent builder, simply acting as just another standard ADK sub-agent. The ADK takes over internally for all translation logic over the wire. |
| 75 | + |
| 76 | +```java title="A2aConsumerSnippet.java" |
| 77 | +--8<-- "examples/java/snippets/src/main/java/a2a/A2aConsumerSnippet.java:new-root-agent" |
| 78 | +``` |
| 79 | + |
| 80 | +## Next Steps |
| 81 | + |
| 82 | +Now that you have created an agent that's using a remote agent via an A2A server, the next step is to learn how to expose your own Java agent. |
| 83 | + |
| 84 | +- [**A2A Quickstart (Exposing) for Java**](./quickstart-exposing-java.md): Learn how to expose your existing agent so that other agents can use it via the A2A Protocol. |
0 commit comments