Lesson 38.7 · 7 of 7 · 5 min read · Reviewed 24 Nov 2026
Worked example: exposing an agent over A2A
By the end, you can
- Sketch how to expose a Claude-based agent over A2A.
- Map A2A concepts onto an Agent SDK agent.
Say you’ve built a contract-review agent with the Claude Agent SDKClaude Agent SDK: Anthropic’s library for building agents on the same loop that powers Claude Code, with tools, hooks, subagents and MCP. and other teams want to call it from their own agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done., whatever they’re built with.
The wrapper
A2A client --JSON-RPC over HTTPS--> your A2A server --> Agent SDK query() --> Claude + your tools (MCP)
- Publish an agent card with a skillSkill: A folder of instructions, scripts and resources that an agent loads when relevant to a task. such as “Review a supplier contract and flag risky clauses”, plus auth requirements.
- On a new message, create a task and start an Agent SDK run with the message as the prompt. Use your usual
allowedTools, hooksHook: Code that runs automatically at a lifecycle event, such as before or after a tool call, to check, block or log it. and limits. - Report progress: state
working, and streamStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply. updates if the client asked for them. - When the agent needs input, set
input-requiredand send its question. When the reply arrives, resumeSession resumption: Continuing an earlier session by reloading its saved history or state. the same session (42.4). - When done, return the review as an artifact, for example JSON with a list of flagged clauses, and set
completed. - On errors set
failedwith a useful message, not a stack traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps..
Keep your guardrails
Everything from Part 8 still applies: least privilege, hooks, budgets, escalation. A2AAgent-to-agent protocol (A2A): An open protocol for agents built by different vendors to discover each other and exchange tasks. only changes who’s asking.
Calling out
The reverse works too: give your Claude agent a tool such as delegate_to_agent(card_url, message) that speaks A2A, so Claude can hand tasks to other agents and get their artifacts back.
Key points
- There's no special switch: you wrap your Claude agent in an A2A server.
- The agent card advertises the agent's skills; incoming messages start or continue an Agent SDK run.
- Map run progress to task states, questions to input-required, and final outputs to artifacts.
Map an agent run onto A2A task states
Write the adapter at the heart of an A2A wrapper: turn an agent run's events into task states, questions and artifacts.
You will have: An adapter function and a printed A2A view of two simulated agent runs, one that asks a question and one that fails.
Steps
- Read the two simulated Agent SDK style event streams: init, progress, question, result and error events.
- Write to_a2a(events) that yields task updates: working on progress, input-required on a question, completed with an artifact on a successful result.
- On an error event, set failed with a short, useful message and no stack trace.
- Keep the session id from init so a reply to a question can resume the same session.
- Print the A2A updates for both runs.
Done when
- The contract run ends completed with a JSON artifact listing flagged clauses.
- Its question appears as input-required, carrying the session id to resume.
- The failing run ends failed with a one-line message, with no traceback text.
Open in the Playground No API key needed.
Starter code (Python)
run_ok = [
{"type": "init", "session_id": "sess-abc"},
{"type": "progress", "text": "Reading the supplier contract"},
{"type": "question", "text": "Which country's law applies?"},
{"type": "progress", "text": "Checking liability clauses"},
{"type": "result", "subtype": "success", "output": {"flagged": ["7.2 unlimited liability", "11.1 auto-renewal"]}},
]
run_bad = [
{"type": "init", "session_id": "sess-def"},
{"type": "progress", "text": "Reading the supplier contract"},
{"type": "error", "text": "Traceback (most recent call last): ... PdfReadError: file is encrypted"},
]
def to_a2a(events):
updates, session = [], None
for e in events:
# TODO: init -> remember session; progress -> working; question -> input-required (+session)
# TODO: result success -> completed + artifact; error -> failed with a short message
pass
return updates
for name, run in [("ok", run_ok), ("bad", run_bad)]:
print(name, to_a2a(run))
Show a solution
run_ok = [
{"type": "init", "session_id": "sess-abc"},
{"type": "progress", "text": "Reading the supplier contract"},
{"type": "question", "text": "Which country's law applies?"},
{"type": "progress", "text": "Checking liability clauses"},
{"type": "result", "subtype": "success", "output": {"flagged": ["7.2 unlimited liability", "11.1 auto-renewal"]}},
]
run_bad = [
{"type": "init", "session_id": "sess-def"},
{"type": "progress", "text": "Reading the supplier contract"},
{"type": "error", "text": "Traceback (most recent call last): ... PdfReadError: file is encrypted"},
]
def short_error(text):
return text.split(":")[-1].strip() if "Traceback" in text else text
def to_a2a(events):
updates, session = [], None
for e in events:
if e["type"] == "init":
session = e["session_id"]
elif e["type"] == "progress":
updates.append({"state": "working", "message": e["text"]})
elif e["type"] == "question":
updates.append({"state": "input-required", "message": e["text"], "resume": session})
elif e["type"] == "result" and e["subtype"] == "success":
updates.append({"state": "completed", "artifact": {"parts": [{"kind": "data", "data": e["output"]}]}})
elif e["type"] in ("error", "result"):
updates.append({"state": "failed", "message": "Review failed: " + short_error(e.get("text", "unknown error"))})
return updates
for name, run in [("ok", run_ok), ("bad", run_bad)]:
print(f"run {name}:")
for u in to_a2a(run):
print(" ", u)
Finished the chapter? Take the chapter check.