a2a-sdk is the official Python SDK for building A2A clients and servers. It handles low-level protocol communication, letting client code prepare A2A Messages and handle Tasks, or server code wrap an agent in an executor. section 10.2 section 10.3.1
A client-server integration uses the SDK in two complementary ways:
- On the client, an async dispatch tool sends a Message to a registered remote agent and converts the A2A response into a ToolCallResult. section 10.2.2
- On the server, an AgentExecutor handles requests, delegates execution to an LLMAgent, and uses TaskUpdater to publish progress and the final result. section 10.3.1
- A FastAPI server can wrap the agent with an executor, create an AgentCard and request handler, add SDK routes, and launch the application; Starlette is another supported ASGI option. section 10.3.2
Remote Hailstone task
A coordinator can register a remote Hailstone agent and ask it for the sequence starting at 8; the returned sequence is 8, 4, 2, 1. section 10.3.2
Listing 10.6 Implementing UseA2AAgentTool: __call__()
# llm_agents_from_scratch/a2a/client/tools.py
… #A
import httpx
from a2a.client import ClientConfig, create_client
from a2a.helpers import new_text_message
from a2a.types import Role as A2ARole
from a2a.types import SendMessageRequest, StreamResponse
class UseA2AAgentTool(AsyncBaseTool):
… #B
async def __call__(
self, tool_call: ToolCall, *args: Any, **kwargs: Any,
) -> ToolCallResult:
"""Dispatch a task to the named A2A peer and return its result.""" #C
… #D
agent_name: str = tool_call.arguments["name"]
task_instruction: str = tool_call.arguments["task"]
task_id = tool_call.arguments.get("task_id")
spec = self._a2a_agents_registry[agent_name]
async with httpx.AsyncClient(
headers=spec.headers, timeout=spec.timeout,
) as httpx_client:
client = None
try:
client = await create_client( #E
agent=spec.agent_card,
client_config=ClientConfig(
streaming=False, httpx_client=httpx_client,
),
)
message = new_text_message(
text=task_instruction, task_id=task_id, role=A2ARole.ROLE_USER,
)
request = SendMessageRequest(message=message) #F
response: StreamResponse | None = None
async for chunk in client.send_message(request): #G
response = chunk
except Exception as e:
return ToolCallResult(
tool_call_id=tool_call.id_, error=True,
content=json.dumps({…},), #H
)
finally:
… #I
return a2a_response_to_tool_call_result( #J
response, agent_name, tool_call.id_,
)Inspect the async client call that dispatches a task to a named A2A peer. section 10.2.2
