-
Notifications
You must be signed in to change notification settings - Fork 423
feat: add a TextClient class for a simplified text-based communication
#963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb30ac3
feat: add a `TextClient` class for a simplified text-based communication
sokoliva 95b224e
Add README.md, task_id persistence
sokoliva 03366d9
fix
sokoliva f75354a
Merge branch '1.0-dev' of https://github.com/a2aproject/a2a-python in…
sokoliva 82ac284
few small fixes
sokoliva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # A2A Python SDK — Samples | ||
|
|
||
| This directory contains runnable examples demonstrating how to build and interact with an A2A-compliant agent using the Python SDK. | ||
|
|
||
| ## Contents | ||
|
|
||
| | File | Role | Description | | ||
| |---|---|---| | ||
| | `hello_world_agent.py` | **Server** | A2A agent server | | ||
| | `cli.py` | **Client** | Interactive terminal client | | ||
| | `text_client_cli.py` | **Client** | Simplified text-only interactive terminal client | | ||
|
|
||
| All three samples are designed to work together out of the box: the agent listens on `http://127.0.0.1:41241`, which is the default URL used by both clients. | ||
| --- | ||
|
|
||
| ## `hello_world_agent.py` — Agent Server | ||
|
|
||
| Implements an A2A agent that responds to simple greeting messages (e.g., "hello", "how are you", "bye") with text replies, simulating a 1-second processing delay. | ||
|
|
||
| Demonstrates: | ||
| - Subclassing `AgentExecutor` and implementing `execute()` / `cancel()` | ||
| - Publishing streaming status updates and artifacts via `TaskUpdater` | ||
| - Exposing all three transports in both protocol versions (v1.0 and v0.3 compat) simultaneously: | ||
| - **JSON-RPC** (v1.0 and v0.3) at `http://127.0.0.1:41241/a2a/jsonrpc` | ||
| - **HTTP+JSON (REST)** (v1.0 and v0.3) at `http://127.0.0.1:41241/a2a/rest` | ||
| - **gRPC v1.0** on port `50051` | ||
| - **gRPC v0.3 (compat)** on port `50052` | ||
| - Serving the agent card at `http://127.0.0.1:41241/.well-known/agent-card.json` | ||
|
|
||
| **Run:** | ||
|
|
||
| ```bash | ||
| uv run python samples/hello_world_agent.py | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## `cli.py` — Client | ||
|
|
||
| An interactive terminal client with full visibility into the streaming event flow. Each `TaskStatusUpdate` and `TaskArtifactUpdate` event is printed as it arrives. | ||
|
|
||
| Features: | ||
| - Transport selection via `--transport` flag (`JSONRPC`, `HTTP+JSON`, `GRPC`) | ||
| - Session management (`context_id` persisted across messages, `task_id` per task) | ||
| - Graceful error handling for HTTP and gRPC failures | ||
|
|
||
| **Run:** | ||
|
|
||
| ```bash | ||
| # Connect to the local hello_world_agent (default): | ||
| uv run python samples/cli.py | ||
|
|
||
| # Connect to a different URL, using gRPC: | ||
| uv run python samples/cli.py --url http://192.168.1.10:41241 --transport GRPC | ||
| ``` | ||
|
|
||
| Type `/quit` or `/exit` to stop, or press `Ctrl+C`. | ||
|
|
||
| --- | ||
|
|
||
| ## `text_client_cli.py` — Simple Text Client | ||
|
|
||
| A stripped-down interactive client using the high-level `TextClient` abstraction. It hides all streaming and event mechanics, presenting a simple request/response interface. | ||
|
|
||
| Ideal for understanding the **minimum code required** to call an A2A agent. | ||
|
|
||
| **Run:** | ||
|
|
||
| ```bash | ||
| # Connect to the local hello_world_agent (default): | ||
| uv run python samples/text_client_cli.py | ||
|
|
||
| # Connect to a different URL: | ||
| uv run python samples/text_client_cli.py --url http://192.168.1.10:41241 | ||
|
|
||
| # Use a specific transport: | ||
| uv run python samples/text_client_cli.py --transport GRPC | ||
| ``` | ||
|
|
||
| Type `/quit` or `/exit` to stop, or press `Ctrl+C`. | ||
|
|
||
| --- | ||
|
|
||
|
|
||
| ## Quick Start | ||
|
|
||
| In two separate terminals: | ||
|
|
||
| ```bash | ||
| # Terminal 1 — start the agent | ||
| uv run python samples/hello_world_agent.py | ||
|
|
||
| # Terminal 2 — start the client | ||
| uv run python samples/cli.py | ||
| ``` | ||
|
|
||
| Then type a message like `hello` and press Enter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import argparse | ||
| import asyncio | ||
|
|
||
| import grpc | ||
| import httpx | ||
|
|
||
| from a2a.client import A2ACardResolver, ClientConfig, create_text_client | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Run the simple A2A terminal client using TextClient.""" | ||
| parser = argparse.ArgumentParser(description='A2A Simple Text Client') | ||
| parser.add_argument( | ||
| '--url', default='http://127.0.0.1:41241', help='Agent base URL' | ||
| ) | ||
| parser.add_argument( | ||
| '--transport', | ||
| default=None, | ||
| help='Preferred transport (JSONRPC, HTTP+JSON, GRPC)', | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| config = ClientConfig() | ||
| if args.transport: | ||
| config.supported_protocol_bindings = [args.transport] | ||
| if args.transport == 'GRPC': | ||
| config.grpc_channel_factory = grpc.aio.insecure_channel | ||
|
|
||
| print( | ||
| f'Connecting to {args.url} (preferred transport: {args.transport or "Any"})' | ||
| ) | ||
|
|
||
| async with httpx.AsyncClient() as httpx_client: | ||
| resolver = A2ACardResolver(httpx_client, args.url) | ||
| card = await resolver.get_agent_card() | ||
| print('\n✓ Agent Card Found:') | ||
| print(f' Name: {card.name}') | ||
|
|
||
| text_client = await create_text_client(card, client_config=config) | ||
|
|
||
| actual_transport = getattr( | ||
| text_client.client, '_transport', text_client.client | ||
| ) | ||
| print(f' Picked Transport: {actual_transport.__class__.__name__}') | ||
|
|
||
| print('\nConnected! Send a message or type /quit to exit.') | ||
|
|
||
| while True: | ||
| try: | ||
| loop = asyncio.get_running_loop() | ||
| user_input = await loop.run_in_executor(None, input, 'You: ') | ||
| except KeyboardInterrupt: | ||
| break | ||
|
|
||
| if user_input.lower() in ('/quit', '/exit'): | ||
| break | ||
| if not user_input.strip(): | ||
| continue | ||
|
|
||
| try: | ||
| response = await text_client.send_text_message(user_input) | ||
| print(f'Agent: {response}') | ||
| except (httpx.RequestError, grpc.RpcError) as e: | ||
| print(f'Error communicating with agent: {e}') | ||
|
|
||
| await text_client.close() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agentword is too broad. Can we do ?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agentcan also be the base URL of the agent so renaming it toagent_cardcould be misleading. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I just noticed optional
str. My bad! Let's keep it as it but add 2 example in doc-string.