6. Interacting with the Server
6. Interacting with the Server
Section titled “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
Section titled “The Helloworld Test Client”The test_client.py script demonstrates how to:
- Fetch the Agent Card from the server.
- Create an
A2AClientinstance. - Send both non-streaming (
message/send) and streaming (message/stream) requests.
Open a new terminal window, activate your virtual environment, and navigate to the a2a-samples directory.
Activate virtual environment (Be sure to do this in the same directory where you created the virtual environment):
=== “Mac/Linux”
```shsource .venv/bin/activate```=== “Windows”
```powershell.venv\Scripts\activate```Run the test client:
# from the a2a-samples directorypython samples/python/agents/helloworld/test_client.pyUnderstanding the Client Code
Section titled “Understanding the Client Code”Let’s look at key parts of test_client.py:
-
Fetching the Agent Card & Initializing the Client:
--8<-- "https://raw.githubusercontent.com/a2aproject/a2a-samples/refs/heads/main/samples/python/agents/helloworld/test_client.py:A2ACardResolver"The
A2ACardResolverclass is a convenience. It first fetches theAgentCardfrom the server’s/.well-known/agent-card.jsonendpoint (based on the provided base URL) and then initializes the client with it. -
Sending a Non-Streaming Message (
send_message):--8<-- "https://raw.githubusercontent.com/a2aproject/a2a-samples/refs/heads/main/samples/python/agents/helloworld/test_client.py:send_message"- The
send_message_payloadconstructs the data forMessageSendParams. - This is wrapped in a
SendMessageRequest. - It includes a
messageobject with theroleset to “user” and the content inparts. - The Helloworld agent’s
executemethod will enqueue a single “Hello World” message. TheDefaultRequestHandlerwill retrieve this and send it as the response. - The
responsewill be aSendMessageResponseobject, which contains either aSendMessageSuccessResponse(with the agent’sMessageas the result) or aJSONRPCErrorResponse.
- The
-
Handling Task IDs (Illustrative Note for Helloworld):
The Helloworld client (
test_client.py) doesn’t attemptget_taskorcancel_taskdirectly because the simple Helloworld agent’sexecutemethod, when called viamessage/send, results in theDefaultRequestHandlerreturning a directMessageresponse rather than aTaskobject. More complex agents that explicitly manage tasks (like the LangGraph example) would return aTaskobject frommessage/send, and itsidcould then be used forget_taskorcancel_task. -
Sending a Streaming Message (
send_message_streaming):--8<-- "https://raw.githubusercontent.com/a2aproject/a2a-samples/refs/heads/main/samples/python/agents/helloworld/test_client.py:send_message_streaming"- This method calls the agent’s
message/streamendpoint. TheDefaultRequestHandlerwill invoke theHelloWorldAgentExecutor.executemethod. - The
executemethod enqueues one “Hello World” message, and then the event queue is closed. - The client will receive this single message as one
SendStreamingMessageResponseevent, and then the stream will terminate. - The
stream_responseis anAsyncGenerator.
- This method calls the agent’s
Expected Output
Section titled “Expected Output”When you run test_client.py, you’ll see JSON outputs for:
- The non-streaming response (a single “Hello World” message).
- 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.
// Non-streaming response{"jsonrpc":"2.0","id":"xxxxxxxx","result":{"message":{"role":"ROLE_AGENT","parts":[{"text":"Hello World"}],"messageId":"yyyyyyyy"}}}// Streaming response (one chunk){"jsonrpc":"2.0","id":"zzzzzzzz","result":{"message":{"role":"ROLE_AGENT","parts":[{"text":"Hello World"}],"messageId":"wwwwwwww"}}}(Actual IDs like xxxxxxxx, yyyyyyyy, zzzzzzzz, wwwwwwww will be different UUIDs/request IDs)
This confirms your server is correctly handling basic A2A interactions with the updated SDK structure!
Now you can shut down the server by typing Ctrl+C in the terminal window where __main__.py is running.