Dice Agent Multi Transport (Official Sample)
56by A2A Project
Official A2A java sample agent: Dice Agent Multi Transport
Getting Started
README
Dice Agent (Multi-Transport)
This sample agent can roll dice of different sizes and check if numbers are prime. This agent demonstrates multi-transport capabilities, supporting both gRPC and JSON-RPC transport protocols. The agent is written using Quarkus LangChain4j and makes use of the A2A Java SDK.
Prerequisites
- Java 17 or higher
- Access to an LLM and API Key
Running the Sample
This sample consists of an A2A server agent, which is in the server directory, and an A2A client,
which is in the client directory.
Running the A2A Server Agent
-
Navigate to the
dice_agent_multi_transportsample directory:cd samples/java/agents/dice_agent_multi_transport/server -
Set your Google AI Studio API Key as an environment variable:
export QUARKUS_LANGCHAIN4J_AI_GEMINI_API_KEY=your_api_key_hereAlternatively, you can create a
.envfile in thedice_agent_multi_transportdirectory:QUARKUS_LANGCHAIN4J_AI_GEMINI_API_KEY=your_api_key_here -
Start the A2A server agent
NOTE: By default, the agent will start on port 11000. To override this, add the
-Dquarkus.http.port=YOUR_PORToption at the end of the command below.mvn quarkus:dev
Running the A2A Java Client
The Java TestClient communicates with the Dice Agent using the A2A Java SDK.
Since the A2A server agent's preferred transport is gRPC and since our client also supports gRPC, the gRPC transport will be used.
-
Make sure you have JBang installed
-
Run the client using the JBang script:
cd samples/java/agents/dice_agent_multi_transport/client/src/main/java/com/samples/a2a/client jbang TestClientRunner.javaOr specify a custom server URL:
jbang TestClientRunner.java --server-url http://localhost:11001Or specify a custom message:
jbang TestClientRunner.java --message "Can you roll a 12-sided die and check if the result is prime?"
Running the A2A Python Client
You can also use a Python client to communicate with the Dice Agent using the A2A Python SDK.
Since the A2A server agent's preferred transport is gRPC and since our client also supports gRPC, the gRPC transport will be used.
-
In a separate terminal, run the A2A client and use it to send a message to the agent:
cd samples/python/agents/dice_agent_grpc uv run test_client.py
Expected Client Output
Both the Java and Python A2A clients will:
- Connect to the dice agent
- Fetch the agent card
- Automatically select gRPC as the transport to be used
- Send the message "Can you roll a 5 sided die?"
- Display the dice roll result from the agent
Multi-Transport Support
This sample demonstrates multi-transport capabilities by supporting both gRPC and
JSON-RPC protocols. The A2A server agent is configured to use a unified port
(11000 by default) for both transport protocols, as specified in the
application.properties file with quarkus.grpc.server.use-separate-server=false.
You can tweak the transports supported by the server or the client to experiment with different transport protocols.
Disclaimer
Important: The sample code provided is for demonstration purposes and illustrates the mechanics of the Agent-to-Agent (A2A) protocol. When building production applications, it is critical to treat any agent operating outside of your direct control as a potentially untrusted entity.
All data received from an external agent—including but not limited to its AgentCard, messages, artifacts, and task statuses—should be handled as untrusted input. For example, a malicious agent could provide an AgentCard containing crafted data in its fields (e.g., description, name, skills.description). If this data is used without sanitization to construct prompts for a Large Language Model (LLM), it could expose your application to prompt injection attacks. Failure to properly validate and sanitize this data before use can introduce security vulnerabilities into your application.
Developers are responsible for implementing appropriate security measures, such as input validation and secure handling of credentials to protect their systems and users.