A2A API Reference¶
Auto-generated API documentation for the A2A module.
a2a
¶
A2A (Agent-to-Agent Protocol) - Zero-dependency Python implementation.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
A pure-stdlib implementation of Google's A2A protocol (v1.0) for agent-to-agent communication. Covers the JSON-RPC 2.0 binding with SSE streaming, an HTTP client, an HTTP server, and an in-memory task store.
Protocol reference
https://github.com/a2aproject/A2A https://a2a-protocol.org/specification
Requires
Python >= 3.10, no external packages.
Sections
- Protocol Data Types - dataclass models for the canonical A2A data model
- JSON-RPC 2.0 Layer - request / response / error / dispatcher
- SSE Utilities - server-sent events writer and parser
- A2A Client - urllib-based client with SSE streaming
- A2A Server - http.server-based server with SSE support
- Task Management - in-memory TaskStore and TaskManager
Example usage is provided in the if __name__ == "__main__" block at the
bottom of this file.
JSONRPCDispatcher
¶
Routes JSON-RPC method calls to registered handler functions.
Example::
dispatcher = JSONRPCDispatcher()
@dispatcher.register("SendMessage")
def handle_send(params):
...
return result_dict
Source code in jsonrpc/jsonrpc.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
register(method)
¶
Decorator to register a handler for method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
JSON-RPC method name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[MethodHandler], MethodHandler]
|
The original handler function, unmodified. |
Source code in jsonrpc/jsonrpc.py
dispatch(request)
¶
Dispatch a parsed JSON-RPC request to the appropriate handler.
Catches JSONRPCException (and subclasses) raised by handlers and
converts them to error responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
JSONRPCRequest
|
The parsed JSON-RPC request. |
required |
Returns:
| Type | Description |
|---|---|
Union[JSONRPCResponse, Iterator[JSONRPCResponse]]
|
A single |
Union[JSONRPCResponse, Iterator[JSONRPCResponse]]
|
generator yielding |
Source code in jsonrpc/jsonrpc.py
JSONRPCError
dataclass
¶
A JSON-RPC 2.0 error object.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
int
|
Numeric error code. |
message |
str
|
Human-readable error message. |
data |
Any
|
Optional additional error data. |
Source code in jsonrpc/jsonrpc.py
JSONRPCRequest
dataclass
¶
A JSON-RPC 2.0 request object.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
The RPC method name. |
params |
dict[str, Any] | None
|
Method parameters. |
id |
Union[str, int, None]
|
Request identifier ( |
jsonrpc |
str
|
Protocol version (always |
Source code in jsonrpc/jsonrpc.py
JSONRPCResponse
dataclass
¶
A JSON-RPC 2.0 response object.
Exactly one of result or error should be set.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
Union[str, int, None]
|
Matching request identifier. |
result |
Any
|
Successful result payload. |
error |
JSONRPCError | None
|
Error details on failure. |
jsonrpc |
str
|
Protocol version (always |
Source code in jsonrpc/jsonrpc.py
to_dict()
¶
Serialize to a dictionary.
from_dict(d)
classmethod
¶
Deserialize from a dictionary.
Source code in jsonrpc/jsonrpc.py
success(request_id, result)
classmethod
¶
from_error(request_id, error)
classmethod
¶
Create an error response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
Any
|
The id of the original request. |
required |
error
|
Any
|
A |
required |
Source code in jsonrpc/jsonrpc.py
TaskState
¶
Bases: str, Enum
Lifecycle states of a Task (mirrors TaskState proto enum).
Source code in a2a/a2a.py
is_terminal()
¶
Return True if the state is terminal (no further transitions).
Role
¶
Part
dataclass
¶
The smallest unit of content inside a Message or Artifact.
Exactly one of text, raw, url, or data should be set,
corresponding to the oneof content in the proto definition.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str | None
|
Plain-text content. |
raw |
str | None
|
Base64-encoded binary content. |
url |
str | None
|
URL pointing to file content. |
data |
Any | None
|
Arbitrary structured data (JSON value). |
metadata |
dict[str, Any] | None
|
Optional key-value metadata. |
filename |
str | None
|
Optional filename hint. |
media_type |
str | None
|
MIME type of the content. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
Message
dataclass
¶
A single communication turn between client and agent.
Attributes:
| Name | Type | Description |
|---|---|---|
message_id |
str
|
Unique identifier for this message. |
role |
Role
|
The sender role (user or agent). |
parts |
list[Part]
|
Content parts of the message. |
context_id |
str | None
|
Optional context grouping identifier. |
task_id |
str | None
|
Optional associated task identifier. |
metadata |
dict[str, Any] | None
|
Optional key-value metadata. |
extensions |
list[str] | None
|
Extension URIs active for this message. |
reference_task_ids |
list[str] | None
|
Task IDs referenced for additional context. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
Artifact
dataclass
¶
An output produced by the agent as a result of task processing.
Attributes:
| Name | Type | Description |
|---|---|---|
artifact_id |
str
|
Unique identifier within a task. |
parts |
list[Part]
|
Content parts of the artifact. |
name |
str | None
|
Human-readable name. |
description |
str | None
|
Human-readable description. |
metadata |
dict[str, Any] | None
|
Optional key-value metadata. |
extensions |
list[str] | None
|
Extension URIs relevant to this artifact. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
TaskStatus
dataclass
¶
Current status of a Task.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
TaskState
|
The lifecycle state. |
message |
Message | None
|
An optional message associated with the status. |
timestamp |
str | None
|
ISO 8601 timestamp when the status was recorded. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
Task
dataclass
¶
The fundamental unit of work managed by A2A.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Server-generated unique identifier. |
status |
TaskStatus
|
Current task status. |
context_id |
str | None
|
Optional context grouping identifier. |
artifacts |
list[Artifact] | None
|
Output artifacts produced so far. |
history |
list[Message] | None
|
Message history for the task. |
metadata |
dict[str, Any] | None
|
Optional key-value metadata. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
TaskStatusUpdateEvent
dataclass
¶
Event indicating a change in task status.
Attributes:
| Name | Type | Description |
|---|---|---|
task_id |
str
|
The task that changed. |
context_id |
str
|
The context the task belongs to. |
status |
TaskStatus
|
The new status. |
metadata |
dict[str, Any] | None
|
Optional metadata. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
TaskArtifactUpdateEvent
dataclass
¶
Event indicating an artifact update on a task.
Attributes:
| Name | Type | Description |
|---|---|---|
task_id |
str
|
The task for this artifact. |
context_id |
str
|
The context the task belongs to. |
artifact |
Artifact
|
The artifact that was generated or updated. |
append |
bool
|
If True, content appends to a previous artifact with the same ID. |
last_chunk |
bool
|
If True, this is the final chunk of the artifact. |
metadata |
dict[str, Any] | None
|
Optional metadata. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
StreamResponse
dataclass
¶
Wrapper for streaming responses (oneof semantics).
Exactly one of the four fields should be set.
Attributes:
| Name | Type | Description |
|---|---|---|
task |
Task | None
|
A Task object with current state. |
message |
Message | None
|
A Message object. |
status_update |
TaskStatusUpdateEvent | None
|
A TaskStatusUpdateEvent. |
artifact_update |
TaskArtifactUpdateEvent | None
|
A TaskArtifactUpdateEvent. |
Source code in a2a/a2a.py
to_dict()
¶
Serialize to a camelCase dictionary.
Source code in a2a/a2a.py
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
AuthenticationInfo
dataclass
¶
Authentication details for push notifications.
Attributes:
| Name | Type | Description |
|---|---|---|
scheme |
str
|
HTTP authentication scheme (e.g. "Bearer"). |
credentials |
str | None
|
The credential string. |
Source code in a2a/a2a.py
PushNotificationConfig
dataclass
¶
Configuration for push notification delivery.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
Webhook URL where notifications are sent. |
id |
str | None
|
Unique configuration identifier. |
task_id |
str | None
|
The associated task ID. |
token |
str | None
|
A client-provided token for verification. |
authentication |
AuthenticationInfo | None
|
Authentication info for the webhook. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
SendMessageConfiguration
dataclass
¶
Configuration accompanying a SendMessage request.
Attributes:
| Name | Type | Description |
|---|---|---|
accepted_output_modes |
list[str] | None
|
Media types the client can accept. |
push_notification_config |
PushNotificationConfig | None
|
Optional push notification setup. |
history_length |
int | None
|
Max number of history messages to return. |
return_immediately |
bool
|
If True, return without waiting for completion. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
SendMessageRequest
dataclass
¶
Request object for the SendMessage / SendStreamingMessage operations.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Message
|
The message to send. |
configuration |
SendMessageConfiguration | None
|
Optional send configuration. |
metadata |
dict[str, Any] | None
|
Optional key-value metadata. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
SendMessageResponse
dataclass
¶
Response for SendMessage (oneof task | message).
Attributes:
| Name | Type | Description |
|---|---|---|
task |
Task | None
|
The task created or updated. |
message |
Message | None
|
A direct response message. |
Source code in a2a/a2a.py
to_dict()
¶
Serialize to a camelCase dictionary.
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
AgentProvider
dataclass
¶
Service provider information for an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
organization |
str
|
Provider organization name. |
url |
str
|
Provider URL. |
Source code in a2a/a2a.py
AgentExtension
dataclass
¶
Declaration of a protocol extension supported by an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
uri |
str
|
Unique URI identifying the extension. |
description |
str | None
|
Human-readable description. |
required |
bool
|
Whether the client must support this extension. |
params |
dict[str, Any] | None
|
Extension-specific configuration. |
Source code in a2a/a2a.py
AgentCapabilities
dataclass
¶
Optional capabilities supported by an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
streaming |
bool | None
|
Whether the agent supports streaming. |
push_notifications |
bool | None
|
Whether the agent supports push notifications. |
extensions |
list[AgentExtension] | None
|
List of supported protocol extensions. |
extended_agent_card |
bool | None
|
Whether an authenticated extended card is available. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
AgentSkill
dataclass
¶
A distinct capability that an agent can perform.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique skill identifier. |
name |
str
|
Human-readable name. |
description |
str
|
Detailed description. |
tags |
list[str]
|
Keywords describing the skill. |
examples |
list[str] | None
|
Example prompts or scenarios. |
input_modes |
list[str] | None
|
Supported input media types (overrides agent defaults). |
output_modes |
list[str] | None
|
Supported output media types (overrides agent defaults). |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
AgentInterface
dataclass
¶
A supported protocol interface for an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
The URL where the interface is available. |
protocol_binding |
str
|
Protocol binding type (JSONRPC, GRPC, HTTP+JSON). |
protocol_version |
str
|
A2A protocol version (e.g. "1.0"). |
tenant |
str | None
|
Optional tenant identifier. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
AgentCard
dataclass
¶
Self-describing manifest for an A2A agent.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable agent name. |
description |
str
|
Agent purpose description. |
version |
str
|
Agent version string. |
supported_interfaces |
list[AgentInterface]
|
Ordered list of supported protocol interfaces. |
default_input_modes |
list[str]
|
Supported input media types. |
default_output_modes |
list[str]
|
Supported output media types. |
skills |
list[AgentSkill]
|
Agent capabilities / skills. |
capabilities |
AgentCapabilities | None
|
Optional capability flags. |
provider |
AgentProvider | None
|
Service provider info. |
documentation_url |
str | None
|
Link to additional docs. |
security_schemes |
dict[str, Any] | None
|
Security scheme definitions. |
security_requirements |
list[dict[str, Any]] | None
|
Security requirements. |
icon_url |
str | None
|
URL to an agent icon. |
Source code in a2a/a2a.py
to_dict()
¶
from_dict(d)
classmethod
¶
Deserialize from a camelCase dictionary.
Source code in a2a/a2a.py
A2AError
¶
Bases: JSONRPCException
Base class for A2A protocol errors.
Source code in a2a/a2a.py
TaskNotFoundError
¶
TaskNotCancelableError
¶
PushNotificationNotSupportedError
¶
Bases: A2AError
Push notification features are not supported by this agent.
Source code in a2a/a2a.py
UnsupportedOperationError
¶
ContentTypeNotSupportedError
¶
InvalidAgentResponseError
¶
Bases: A2AError
The agent returned a response that does not conform to the spec.
Source code in a2a/a2a.py
A2AClient
¶
HTTP client for the A2A JSON-RPC protocol binding.
Uses only urllib.request and http.client from the standard library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_card_url
|
str | None
|
Full URL to the agent card JSON endpoint. If not provided, it is derived from base_url. |
None
|
base_url
|
str | None
|
The base JSON-RPC endpoint URL. If not provided, it is derived from the agent card once fetched. |
None
|
headers
|
dict[str, str] | None
|
Extra HTTP headers to include in every request. |
None
|
Example::
client = A2AClient(base_url="http://localhost:8000")
card = client.get_agent_card()
resp = client.send_message("Hello, agent!")
Source code in a2a/a2a.py
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 | |
get_agent_card()
¶
Fetch and cache the agent card.
Returns:
| Type | Description |
|---|---|
AgentCard
|
The |
Raises:
| Type | Description |
|---|---|
URLError
|
On network failure. |
Source code in a2a/a2a.py
send_message(text, *, task_id=None, context_id=None, configuration=None, metadata=None)
¶
Send a text message to the agent (blocking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The message text. |
required |
task_id
|
str | None
|
Optional task to continue. |
None
|
context_id
|
str | None
|
Optional context to associate with. |
None
|
configuration
|
SendMessageConfiguration | None
|
Optional send configuration. |
None
|
metadata
|
dict[str, Any] | None
|
Optional request metadata. |
None
|
Returns:
| Type | Description |
|---|---|
SendMessageResponse
|
A |
Source code in a2a/a2a.py
send_message_streaming(text, *, task_id=None, context_id=None, configuration=None, metadata=None)
¶
Send a text message and stream responses via SSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The message text. |
required |
task_id
|
str | None
|
Optional task to continue. |
None
|
context_id
|
str | None
|
Optional context to associate with. |
None
|
configuration
|
SendMessageConfiguration | None
|
Optional send configuration. |
None
|
metadata
|
dict[str, Any] | None
|
Optional request metadata. |
None
|
Yields:
| Type | Description |
|---|---|
StreamResponse
|
|
Source code in a2a/a2a.py
get_task(task_id, *, history_length=None)
¶
Retrieve the current state of a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
history_length
|
int | None
|
Optional max number of history messages to return. |
None
|
Returns:
| Type | Description |
|---|---|
Task
|
The current |
Source code in a2a/a2a.py
list_tasks(*, context_id=None, status=None, page_size=None, page_token=None)
¶
List tasks with optional filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id
|
str | None
|
Filter by context. |
None
|
status
|
TaskState | None
|
Filter by task state. |
None
|
page_size
|
int | None
|
Maximum results per page. |
None
|
page_token
|
str | None
|
Cursor for pagination. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Raw result dictionary with |
Source code in a2a/a2a.py
cancel_task(task_id)
¶
Request cancellation of a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
Task
|
The updated |
Source code in a2a/a2a.py
A2ARequestHandler
¶
Bases: BaseHTTPRequestHandler
HTTP request handler for the A2A JSON-RPC protocol binding.
Subclass this and assign dispatcher and agent_card on the server
to customise behavior, or use A2AServer which wires everything up.
Source code in a2a/a2a.py
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 | |
do_GET()
¶
Handle GET requests (agent card endpoint).
do_POST()
¶
Handle POST requests (JSON-RPC endpoint).
Source code in a2a/a2a.py
do_OPTIONS()
¶
Handle CORS preflight requests.
Source code in a2a/a2a.py
A2AServer
¶
A2A protocol server using the JSON-RPC binding over HTTP.
This wraps Python's http.server.HTTPServer with threading support
and provides a JSONRPCDispatcher for registering method handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Bind address (default |
'0.0.0.0'
|
port
|
int
|
Bind port (default |
8000
|
agent_card
|
AgentCard | None
|
The |
None
|
Example::
card = AgentCard(name="Echo Agent", description="Echoes messages")
server = A2AServer(port=9000, agent_card=card)
@server.dispatcher.register("SendMessage")
def handle_send(params):
req = SendMessageRequest.from_dict(params)
text = req.message.parts[0].text or ""
task = Task(
status=TaskStatus(state=TaskState.COMPLETED),
artifacts=[Artifact(parts=[Part(text=f"Echo: {text}")])],
)
return SendMessageResponse(task=task).to_dict()
server.start()
Source code in a2a/a2a.py
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 | |
url
property
¶
Return the base URL of the running server.
start(blocking=True)
¶
Start serving requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocking
|
bool
|
If True, block the calling thread. If False, serve in a background daemon thread. |
True
|
Source code in a2a/a2a.py
TaskStore
¶
Thread-safe in-memory task store backed by a dictionary.
Provides CRUD operations for Task objects keyed by their id.
Example::
store = TaskStore()
task = Task(status=TaskStatus(state=TaskState.SUBMITTED))
store.save(task)
retrieved = store.get(task.id)
Source code in a2a/a2a.py
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 | |
save(task)
¶
Save or update a task in the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
Task
|
The task to save. |
required |
get(task_id)
¶
Retrieve a task by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
Task | None
|
A deep copy of the task, or |
Source code in a2a/a2a.py
delete(task_id)
¶
Remove a task from the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the task was deleted, False if it was not found. |
Source code in a2a/a2a.py
list_tasks(*, context_id=None, status=None, page_size=50, page_token=None)
¶
List tasks with optional filtering and pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id
|
str | None
|
Filter by context ID. |
None
|
status
|
TaskState | None
|
Filter by task state. |
None
|
page_size
|
int
|
Maximum number of tasks to return. |
50
|
page_token
|
str | None
|
Opaque cursor (task ID) for pagination. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[Task], str, int]
|
Tuple of (tasks, next_page_token, total_count). |
Source code in a2a/a2a.py
TaskManager
¶
High-level task lifecycle manager built on top of TaskStore.
Manages task creation, state transitions, artifact generation, and provides event callbacks for streaming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
TaskStore | None
|
The |
None
|
Example::
manager = TaskManager()
task = manager.create_task(message)
manager.update_status(task.id, TaskState.WORKING)
manager.add_artifact(task.id, Artifact(parts=[Part(text="result")]))
manager.update_status(task.id, TaskState.COMPLETED)
Source code in a2a/a2a.py
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 | |
create_task(message, *, context_id=None)
¶
Create a new task from an incoming message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
The initiating message. |
required |
context_id
|
str | None
|
Optional context to associate the task with. |
None
|
Returns:
| Type | Description |
|---|---|
Task
|
The newly created |
Source code in a2a/a2a.py
get_task(task_id)
¶
Retrieve a task, raising TaskNotFoundError if absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
Task
|
The |
Raises:
| Type | Description |
|---|---|
TaskNotFoundError
|
If the task does not exist. |
Source code in a2a/a2a.py
update_status(task_id, state, *, status_message=None)
¶
Transition a task to a new state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
state
|
TaskState
|
The target state. |
required |
status_message
|
Message | None
|
Optional message to attach to the status. |
None
|
Returns:
| Type | Description |
|---|---|
Task
|
The updated |
Raises:
| Type | Description |
|---|---|
TaskNotFoundError
|
If the task does not exist. |
ValueError
|
If the state transition is not valid. |
Source code in a2a/a2a.py
add_artifact(task_id, artifact, *, append=False, last_chunk=True)
¶
Add an artifact to a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
artifact
|
Artifact
|
The artifact to add. |
required |
append
|
bool
|
If True, append to an existing artifact with the same ID. |
False
|
last_chunk
|
bool
|
If True, this is the final chunk of the artifact. |
True
|
Returns:
| Type | Description |
|---|---|
Task
|
The updated |
Raises:
| Type | Description |
|---|---|
TaskNotFoundError
|
If the task does not exist. |
Source code in a2a/a2a.py
cancel_task(task_id)
¶
Cancel a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
Returns:
| Type | Description |
|---|---|
Task
|
The updated |
Raises:
| Type | Description |
|---|---|
TaskNotFoundError
|
If the task does not exist. |
TaskNotCancelableError
|
If the task is in a terminal state. |
Source code in a2a/a2a.py
subscribe(task_id, callback)
¶
Subscribe to streaming events for a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_id
|
str
|
The task identifier. |
required |
callback
|
Callable[[StreamResponse], None]
|
Function called with each |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], None]
|
An unsubscribe function that removes the callback. |
Source code in a2a/a2a.py
sse_encode(data)
¶
Encode data as a single SSE data: frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
JSON-serializable object. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
UTF-8 encoded SSE frame bytes ( |
Source code in a2a/a2a.py
sse_decode_stream(response)
¶
Parse an SSE stream from an http.client.HTTPResponse.
Reads lines from the response, extracts data: fields, and yields
parsed JSON objects. Handles chunked transfer encoding transparently
because http.client decodes it for us.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
HTTPResponse
|
An open HTTP response with |
required |
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed JSON dictionaries for each SSE |
Source code in a2a/a2a.py
handle_send_message(params)
¶
Handle a SendMessage request by echoing the input.
Source code in a2a/a2a.py
handle_stream_message(params)
¶
Handle a SendStreamingMessage by streaming token-by-token.
Source code in a2a/a2a.py
handle_get_task(params)
¶
Handle a GetTask request.
handle_cancel_task(params)
¶
Handle a CancelTask request.