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
2026-03-26 13:50:49 +01:00
1. **Fetching the Agent Card** :
2025-05-12 14:29:47 -05:00
```python { .no-copy }
2025-06-23 21:34:48 +02:00
--8< -- " https: / / raw . githubusercontent . com / a2aproject / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:A2ACardResolver "
2025-05-12 14:29:47 -05:00
```
2026-03-26 13:50:49 +01:00
The `A2ACardResolver` class is a convenience. It first fetches the `AgentCard` from the server's `/.well-known/agent-card.json` endpoint (based on the provided base URL) which is then used to initialize the client.
2025-05-12 14:29:47 -05:00
2026-03-26 13:50:49 +01:00
2. **Initializing the Client & Sending a Non-Streaming Message** :
2025-05-12 14:29:47 -05:00
```python { .no-copy }
2026-04-09 09:55:38 +02:00
--8< -- " https: / / raw . githubusercontent . com / a2aproject / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:message_send "
2025-05-12 14:29:47 -05:00
```
2026-03-26 13:50:49 +01:00
- A `ClientFactory` creates a non-streaming client based on the fetched card.
- We construct a `Message` object using `Role.ROLE_USER` and `Part` for the content.
2025-05-14 13:14:28 -05:00
- This is wrapped in a `SendMessageRequest` .
2026-03-26 13:50:49 +01:00
- The client's `send_message` method returns an async generator that yields a sequence of `Task` events from the agent.
2025-05-12 14:29:47 -05:00
2026-03-26 13:50:49 +01:00
3. **Initializing the Client & Sending a Streaming Message** :
2025-05-12 14:29:47 -05:00
```python { .no-copy }
2026-04-09 09:55:38 +02:00
--8< -- " https: / / raw . githubusercontent . com / a2aproject / a2a-samples / refs / heads / main / samples / python / agents / helloworld / test_client . py:message_stream "
2025-05-12 14:29:47 -05:00
```
2026-03-26 13:50:49 +01:00
- A new streaming client is created via `ClientFactory` configured with `streaming=True` .
- We again call `send_message` (which now handles both streaming and non-streaming under the same method name, based on the `ClientConfig` and agent `capabilities` ).
- The response dynamically yields `Task` events as they are streamed over the network.
2025-05-12 14:29:47 -05:00
## Expected Output
2026-03-26 13:50:49 +01:00
When you run `test_client.py` , you'll see in protobuf text format outputs for:
2025-05-12 14:29:47 -05:00
2026-03-26 13:50:49 +01:00
- The non-streaming response (a single final `task` log detailing the history, status, and the generated artifact containing the "Hello, World!" text).
- The streaming response (multiple discrete events including the initial `task` , a `status_update` , and a final `artifact_update` containing the "Hello, World!" text).
2025-05-14 13:14:28 -05:00
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
2026-03-26 13:50:49 +01:00
task {
id: "xxxxxxxx"
context_id: "yyyyyyyy"
status {
state: TASK_STATE_COMPLETED
}
artifacts {
artifact_id: "zzzzzzzz"
name: "result"
parts {
text: "Hello, World!"
}
}
history {
message_id: "vvvvvvvv"
context_id: "yyyyyyyy"
task_id: "xxxxxxxx"
role: ROLE_USER
parts {
text: "Say hello."
}
}
history {
message_id: "wwwwwwww"
role: ROLE_AGENT
parts {
text: "Processing request..."
}
}
}
// Streaming response
task {
id: "xxxxxxxx-s"
context_id: "yyyyyyyy-s"
status {
state: TASK_STATE_SUBMITTED
}
history {
message_id: "vvvvvvvv"
context_id: "yyyyyyyy-s"
task_id: "xxxxxxxx-s"
role: ROLE_USER
parts {
text: "Say hello."
}
}
}
Response chunk:
status_update {
task_id: "xxxxxxxx-s"
context_id: "yyyyyyyy-s"
status {
state: TASK_STATE_WORKING
message {
message_id: "zzzzzzzz-s"
role: ROLE_AGENT
parts {
text: "Processing request..."
}
}
}
}
Response chunk:
artifact_update {
task_id: "xxxxxxxx-s"
context_id: "yyyyyyyy-s"
artifact {
artifact_id: "wwwwwwww-s"
name: "result"
parts {
text: "Hello, World!"
}
}
}
Response chunk:
status_update {
task_id: "xxxxxxxx-s"
context_id: "yyyyyyyy-s"
status {
state: TASK_STATE_COMPLETED
}
}
2025-05-12 14:29:47 -05:00
```
2026-03-26 13:50:49 +01:00
_(Actual IDs like `xxxxxxxx` , `yyyyyyyy` , `zzzzzzzz` , `vvvvvvvv` , `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.