2025-05-12 14:29:47 -05:00
# 6. Interacting with the Server
With the Helloworld A2A server running, let's send some requests to it. The SDK includes a client (`A2AClient` ) that simplifies these interactions.
## The Helloworld Test Client
2025-05-29 10:13:10 -05:00
The `test_client.py` script demonstrates how to:
2025-05-12 14:29:47 -05:00
1. Fetch the Agent Card from the server.
2. Create an `A2AClient` instance.
2025-05-14 13:14:28 -05:00
3. Send both non-streaming (`message/send` ) and streaming (`message/stream` ) requests.
2025-05-12 14:29:47 -05:00
2025-05-27 16:31:32 -05:00
Open a **new terminal window** , activate your virtual environment, and navigate to the `a2a-samples` directory.
2025-05-12 14:29:47 -05:00
Activate virtual environment (Be sure to do this in the same directory where you created the virtual environment):
=== "Mac/Linux"
```sh
source .venv/bin/activate
```
=== "Windows"
```powershell
.venv\Scripts\activate
```
Run the test client:
```bash
2025-05-27 16:31:32 -05:00
# from the a2a-samples directory
2025-05-29 10:13:10 -05:00
python samples/python/agents/helloworld/test_client.py
2025-05-12 14:29:47 -05:00
```
## Understanding the Client Code
2025-05-29 10:13:10 -05:00
Let's look at key parts of `test_client.py` :
2025-05-12 14:29:47 -05:00
1. **Fetching the Agent Card & Initializing the Client** :
```python { .no-copy }
2025-05-29 10:13:10 -05:00
--8< -- " https: / / raw . githubusercontent . com / google-a2a / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:A2ACardResolver "
2025-05-12 14:29:47 -05:00
```
2025-05-29 10:13:10 -05:00
The `A2ACardResolver` class is a convenience. It first fetches the `AgentCard` from the server's `/.well-known/agent.json` endpoint (based on the provided base URL) and then initializes the client with it.
2025-05-12 14:29:47 -05:00
2. **Sending a Non-Streaming Message (`send_message`)** :
```python { .no-copy }
2025-05-29 10:13:10 -05:00
--8< -- " https: / / raw . githubusercontent . com / google-a2a / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:send_message "
2025-05-12 14:29:47 -05:00
```
2025-05-14 13:14:28 -05:00
- The `send_message_payload` constructs the data for `MessageSendParams` .
- This is wrapped in a `SendMessageRequest` .
2025-05-12 14:29:47 -05:00
- It includes a `message` object with the `role` set to "user" and the content in `parts` .
2025-05-14 13:14:28 -05:00
- The Helloworld agent's `execute` method will enqueue a single "Hello World" message. The `DefaultRequestHandler` will retrieve this and send it as the response.
2025-05-12 14:29:47 -05:00
- The `response` will be a `SendMessageResponse` object, which contains either a `SendMessageSuccessResponse` (with the agent's `Message` as the result) or a `JSONRPCErrorResponse` .
2025-05-14 13:14:28 -05:00
3. **Handling Task IDs (Illustrative Note for Helloworld)** :
2025-06-03 10:44:32 -05:00
2025-05-29 10:13:10 -05:00
The Helloworld client (`test_client.py` ) doesn't attempt `get_task` or `cancel_task` directly because the simple Helloworld agent's `execute` method, when called via `message/send` , results in the `DefaultRequestHandler` returning a direct `Message` response rather than a `Task` object. More complex agents that explicitly manage tasks (like the LangGraph example) would return a `Task` object from `message/send` , and its `id` could then be used for `get_task` or `cancel_task` .
2025-05-12 14:29:47 -05:00
4. **Sending a Streaming Message (`send_message_streaming`)** :
```python { .no-copy }
2025-05-29 10:13:10 -05:00
--8< -- " https: / / raw . githubusercontent . com / google-a2a / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:send_message_streaming "
2025-05-12 14:29:47 -05:00
```
2025-05-14 13:14:28 -05:00
- This method calls the agent's `message/stream` endpoint. The `DefaultRequestHandler` will invoke the `HelloWorldAgentExecutor.execute` method.
- The `execute` method enqueues one "Hello World" message, and then the event queue is closed.
- The client will receive this single message as one `SendStreamingMessageResponse` event, and then the stream will terminate.
- The `stream_response` is an `AsyncGenerator` .
2025-05-12 14:29:47 -05:00
## Expected Output
When you run `test_client.py` , you'll see JSON outputs for:
- The non-streaming response (a single "Hello World" message).
2025-05-14 13:14:28 -05:00
- The streaming response (a single "Hello World" message as one chunk, after which the stream ends).
The `id` fields in the output will vary with each run.
2025-05-12 14:29:47 -05:00
```console { .no-copy }
2025-05-14 13:14:28 -05:00
// Non-streaming response
{"jsonrpc":"2.0","id":"xxxxxxxx","result":{"type":"message","role":"agent","parts":[{"type":"text","text":"Hello World"}],"messageId":"yyyyyyyy"}}
// Streaming response (one chunk)
{"jsonrpc":"2.0","id":"zzzzzzzz","result":{"type":"message","role":"agent","parts":[{"type":"text","text":"Hello World"}],"messageId":"wwwwwwww","final":true}}
2025-05-12 14:29:47 -05:00
```
2025-06-03 10:44:32 -05:00
_(Actual IDs like `xxxxxxxx` , `yyyyyyyy` , `zzzzzzzz` , `wwwwwwww` will be different UUIDs/request IDs)_
2025-05-14 13:14:28 -05:00
This confirms your server is correctly handling basic A2A interactions with the updated SDK structure!
2025-05-12 14:29:47 -05:00
2025-05-14 13:14:28 -05:00
Now you can shut down the server by typing Ctrl+C in the terminal window where `__main__.py` is running.