A2A Python SDK
The A2A Python SDK is an official library for building agentic applications that comply with the Agent2Agent (A2A) Protocol. It provides foundational building blocks for creating A2A servers and clients, supporting features like multi-turn conversations and streaming responses. The library is actively maintained with regular updates, and the 0.3 version of the protocol is intended to remain stable for a significant period with backward compatibility for SDKs starting at version 0.3. [1, 3, 12, 17, 19]
Warnings
- breaking The SDK is being upgraded to align with the A2A 1.0 protocol specification, introducing proto-based types. This will likely involve significant changes to existing classes and data structures. [10]
- breaking Class fields within the SDK are being refactored to be more Pythonic, adopting `snake_case` conventions. This affects how properties and fields are accessed. [12]
- breaking The standard path for hosting Agent Cards has been updated from `/.well-known/agent.json` to `/.well-known/agent-card.json` based on IANA feedback. [12]
- gotcha The SDK's `AgentExecutor` signature and `DefaultRequestHandler` often lead to agent logic being tightly coupled to A2A protocol details (e.g., `Task`, `TaskOutput`, `MessagePart`). This can hinder testability, reusability, and readability of core agent intelligence. [15, 16]
- gotcha If an `AgentCard` contains sensitive information, the endpoint serving it *must* be protected by appropriate access controls (e.g., mTLS, network restrictions, authentication). It is generally *not recommended* to include plaintext secrets (like static API keys) directly in an `AgentCard`. [20]
Install
-
pip install a2a-sdk
Imports
- AgentApp
from a2a.server.app import AgentApp
- AgentExecutor
from a2a.server.sdk import AgentExecutor
- AgentCard
from a2a.server.sdk import AgentCard
Quickstart
import os
from a2a.server.app import AgentApp
from a2a.server.sdk import AgentExecutor, AgentCard
from a2a.server.types import AgentTask, TaskOutput, MessagePart
from a2a.server.exceptions import AgentException
class EchoAgentExecutor(AgentExecutor):
"""A simple agent that echoes back the input message."""
async def invoke(self, task: AgentTask) -> TaskOutput:
input_message = ""
# Extract text from the first message part
for part in task.input.parts:
if part.text:
input_message += part.text.text
break # Only process the first text part for simplicity
if not input_message:
raise AgentException("No text message received.")
response_parts = [MessagePart(text=f"Echo: {input_message}")]
return TaskOutput(output=response_parts)
# Define the agent's capabilities
agent_card = AgentCard(
agent_id="echo-agent",
display_name="Echo Agent",
description="An agent that echoes back any message it receives.",
skills=[{"name": "echo", "description": "Echoes the input."}]
)
# Create the A2A application instance
app = AgentApp(
agent_card=agent_card,
agent_executor=EchoAgentExecutor()
)
# To run this application (e.g., using uvicorn):
# 1. Save the code above as `main.py`
# 2. Run from your terminal: `uvicorn main:app --host 0.0.0.0 --port 8000`
# (Install uvicorn first: `pip install uvicorn`)