Skip to content

Commit 0544f18

Browse files
authored
feat: Add a multi-transport sample for A2A Java SDK (#353)
# Description This PR adds a multi-transport sample using the A2A Java SDK. The server agent supports both gRPC and JSON-RPC, with gRPC being the preferred transport. The client supports both as well so the gRPC will be the selected transport. - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-samples/blob/main/CONTRIBUTING.md).
1 parent 037abfa commit 0544f18

16 files changed

Lines changed: 936 additions & 9 deletions

File tree

samples/java/agents/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Each agent can be run as its own A2A server with the instructions in its README.
1515
* [**Weather Agent**](weather_mcp/README.md)
1616
Sample agent to provide weather information. To make use of this agent in a multi-language, multi-agent system, check out the [weather_and_airbnb_planner](../../python/hosts/weather_and_airbnb_planner/README.md) sample.
1717

18+
* [**Dice Agent (Multi-Transport)**](dice_agent_multi_transport/README.md)
19+
Sample agent that can roll dice of different sizes and check if numbers are prime. This agent demonstrates
20+
multi-transport capabilities.
21+
1822
## Disclaimer
1923

2024
Important: The sample code provided is for demonstration purposes and illustrates the
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Dice Agent (Multi-Transport)
2+
3+
This sample agent can roll dice of different sizes and check if numbers are prime. This agent demonstrates
4+
multi-transport capabilities, supporting both gRPC and JSON-RPC transport protocols. The agent is written
5+
using Quarkus LangChain4j and makes use of the [A2A Java](https://github.com/a2aproject/a2a-java) SDK.
6+
7+
## Prerequisites
8+
9+
- Java 17 or higher
10+
- Access to an LLM and API Key
11+
12+
## Running the Sample
13+
14+
This sample consists of an A2A server agent, which is in the `server` directory, and an A2A client,
15+
which is in the `client` directory.
16+
17+
### Running the A2A Server Agent
18+
19+
1. Navigate to the `dice_agent_multi_transport` sample directory:
20+
21+
```bash
22+
cd samples/java/agents/dice_agent_multi_transport/server
23+
```
24+
25+
2. Set your Google AI Studio API Key as an environment variable:
26+
27+
```bash
28+
export QUARKUS_LANGCHAIN4J_AI_GEMINI_API_KEY=your_api_key_here
29+
```
30+
31+
Alternatively, you can create a `.env` file in the `dice_agent_multi_transport` directory:
32+
33+
```bash
34+
QUARKUS_LANGCHAIN4J_AI_GEMINI_API_KEY=your_api_key_here
35+
```
36+
37+
3. Start the A2A server agent
38+
39+
**NOTE:**
40+
By default, the agent will start on port 11000. To override this, add the `-Dquarkus.http.port=YOUR_PORT`
41+
option at the end of the command below.
42+
43+
```bash
44+
mvn quarkus:dev
45+
```
46+
47+
### Running the A2A Java Client
48+
49+
The Java `TestClient` communicates with the Dice Agent using the A2A Java SDK.
50+
51+
Since the A2A server agent's [preferred transport](server/src/main/java/com/samples/a2a/DiceAgentCardProducer.java) is gRPC and since our client
52+
also [supports](client/src/main/java/com/samples/a2a/TestClient.java) gRPC, the gRPC transport will be used.
53+
54+
1. Make sure you have [JBang installed](https://www.jbang.dev/documentation/guide/latest/installation.html)
55+
56+
2. Run the client using the JBang script:
57+
```bash
58+
cd samples/java/agents/dice_agent_multi_transport/client/src/main/java/com/samples/a2a/client
59+
jbang TestClientRunner.java
60+
```
61+
62+
Or specify a custom server URL:
63+
```bash
64+
jbang TestClientRunner.java --server-url http://localhost:11001
65+
```
66+
67+
Or specify a custom message:
68+
```bash
69+
jbang TestClientRunner.java --message "Can you roll a 12-sided die and check if the result is prime?"
70+
```
71+
72+
### Running the A2A Python Client
73+
74+
You can also use a Python client to communicate with the Dice Agent using the A2A
75+
Python SDK.
76+
77+
Since the A2A server agent's [preferred transport](server/src/main/java/com/samples/a2a/DiceAgentCardProducer.java) is gRPC and since our [client](client/src/main/java/com/samples/a2a/TestClient.java) also supports gRPC, the gRPC
78+
transport will be used.
79+
80+
1. In a separate terminal, run the A2A client and use it to send a message to the
81+
agent:
82+
83+
```bash
84+
cd samples/python/agents/dice_agent_grpc
85+
uv run test_client.py
86+
```
87+
88+
## Expected Client Output
89+
90+
Both the Java and Python A2A clients will:
91+
1. Connect to the dice agent
92+
2. Fetch the agent card
93+
3. Automatically select gRPC as the transport to be used
94+
4. Send the message "Can you roll a 5 sided die?"
95+
5. Display the dice roll result from the agent
96+
97+
## Multi-Transport Support
98+
99+
This sample demonstrates multi-transport capabilities by supporting both gRPC and
100+
JSON-RPC protocols. The A2A server agent is configured to use a unified port
101+
(11000 by default) for both transport protocols, as specified in the
102+
`application.properties` file with `quarkus.grpc.server.use-separate-server=false`.
103+
104+
You can tweak the transports supported by the server or the client to experiment
105+
with different transport protocols.
106+
107+
## Disclaimer
108+
Important: The sample code provided is for demonstration purposes and illustrates the
109+
mechanics of the Agent-to-Agent (A2A) protocol. When building production applications,
110+
it is critical to treat any agent operating outside of your direct control as a
111+
potentially untrusted entity.
112+
113+
All data received from an external agent—including but not limited to its AgentCard,
114+
messages, artifacts, and task statuses—should be handled as untrusted input. For
115+
example, a malicious agent could provide an AgentCard containing crafted data in its
116+
fields (e.g., description, name, skills.description). If this data is used without
117+
sanitization to construct prompts for a Large Language Model (LLM), it could expose
118+
your application to prompt injection attacks. Failure to properly validate and
119+
sanitize this data before use can introduce security vulnerabilities into your
120+
application.
121+
122+
Developers are responsible for implementing appropriate security measures, such as
123+
input validation and secure handling of credentials to protect their systems and users.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.samples.a2a</groupId>
9+
<artifactId>dice-agent-multi-transport</artifactId>
10+
<version>0.1.0</version>
11+
</parent>
12+
13+
<artifactId>dice-agent-client</artifactId>
14+
<name>Dice Agent Client</name>
15+
<description>A2A Dice Agent Test Client</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.github.a2asdk</groupId>
20+
<artifactId>a2a-java-sdk-client</artifactId>
21+
<version>${io.a2a.sdk.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.github.a2asdk</groupId>
25+
<artifactId>a2a-java-sdk-client-transport-grpc</artifactId>
26+
<version>${io.a2a.sdk.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>io.grpc</groupId>
34+
<artifactId>grpc-netty-shaded</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.codehaus.mojo</groupId>
47+
<artifactId>exec-maven-plugin</artifactId>
48+
<configuration>
49+
<mainClass>com.samples.a2a.TestClient</mainClass>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>

0 commit comments

Comments
 (0)