AgentVault Library Usage Example¶
This example demonstrates how to use the core agentvault
Python client library directly to interact with an A2A-compliant agent, without using the agentvault_cli
.
Concept¶
The agentvault
library provides the AgentVaultClient
class, which handles the complexities of the A2A protocol (JSON-RPC, SSE, authentication via KeyManager
). This script shows the basic workflow:
- Load the target agent's
AgentCard
. - Instantiate
KeyManager
to handle potential authentication credentials. - Instantiate
AgentVaultClient
(usingasync with
). - Call
client.initiate_task
to start the interaction. - Use
async for event in client.receive_messages(...)
to stream and process events (status updates, messages, artifacts) from the agent. - Handle potential exceptions.
Components¶
requirements.txt
: Lists theagentvault
library dependency.main.py
: The Python script demonstrating the library usage. It takes the agent reference and input text as command-line arguments.
Setup¶
- Navigate: Open your terminal in this directory (
examples/library_usage_example
). - Install Dependencies: Create and activate a virtual environment, then install the requirements.
Note: This installs the
# Create venv (optional, recommended) # python -m venv .venv # source .venv/bin/activate # On Linux/macOS # .venv\Scripts\activate # On Windows pip install -r requirements.txt
agentvault
library from your local source tree. - Target Agent: By default, the script targets the Basic A2A Server example agent card URL (
http://localhost:8000/agent-card.json
). Ensure that agent is running if you use the default. You can target other agents using the--agent-ref
argument. - Credentials (If Needed): If the target agent requires authentication (e.g.,
apiKey
oroauth2
), ensure the necessary credentials are configured using the AgentVault CLI (agentvault config set ...
) or environment variables, matching theservice_identifier
specified in the agent's card. TheKeyManager
within the script will automatically pick them up.
Running the Example¶
Execute the Python script from your terminal, providing the input text:
# Run against the default Basic Echo Agent (make sure it's running on port 8000)
python main.py --input "Hello from the library!"
# Run against a different agent (e.g., one requiring auth)
# Ensure 'my-api-key-service' is configured via `agentvault config` if needed
# python main.py --agent-ref "https://some-other-agent.com/card.json" --input "Process this data" --key-service "my-api-key-service"
Expected Output:
The script will print logs indicating the steps it's taking (loading card, initiating task) and then print details for each event received from the agent via the SSE stream (status changes, messages, artifacts).
INFO:root:Loading agent card: http://localhost:8000/agent-card.json
INFO:root:Loaded Agent: SDK Basic Echo Agent
INFO:root:Initiating task...
INFO:agentvault.client:Initiating task with agent: examples/simple-agent
INFO:agentvault.client:Task successfully initiated with agent examples/simple-agent. Task ID: simple-xxxxxx
INFO:root:Task initiated: simple-xxxxxx
INFO:root:Streaming events...
INFO:agentvault.client:Subscribing to events for task simple-xxxxxx on agent: examples/simple-agent
INFO:root: Status Update: WORKING (Msg: N/A)
INFO:root: Message Received (Role: assistant):
INFO:root: Text: Echo response for task simple-xxxxxx
INFO:root: Status Update: COMPLETED (Msg: N/A)
INFO:root: Terminal state reached.
INFO:root:
--- Final Aggregated Agent Response ---
Echo response for task simple-xxxxxx
---------------------------------------