|
| 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. |
0 commit comments