Overview
A2A (Agent-to-Agent) is a protocol that enables AI agents to communicate with each other using JSON-RPC. The Nevermined SDK provides A2A integration to:- Build A2A servers with payment validation
- Automatically verify x402 tokens on incoming requests
- Handle credit redemption for agent tasks
Building Agent Cards
With a Single Plan
With Multiple Plans
When your agent supports multiple plans (e.g. a basic and a premium tier), useplanIds instead of planId:
Note: Provide eitherplanIdorplanIds, not both.planIdsmust be a non-empty list.
Agent Card Structure
The agent card includes a payment extension:Using the @a2a_requires_payment Decorator
The simplest way to create a payment-protected A2A agent:
- Payment middleware (verify/settle) automatically
- Publishing task status events with
creditsUsedmetadata - Credit burning on task completion
AgentResponse
| Field | Type | Description |
|---|---|---|
text | str | The agent’s text response |
credits_used | int | None | Credits consumed (falls back to default_credits) |
metadata | dict | None | Extra metadata for the final event |
Starting an A2A Server (Advanced)
For more control, usePaymentsA2AServer.start() directly:
Server Configuration Options
| Option | Type | Required | Description |
|---|---|---|---|
agent_card | AgentCard | Yes | A2A agent card with payment extension |
executor | AgentExecutor | Yes | Your agent implementation |
payments_service | Payments | Yes | Payments instance |
port | int | No | Server port (default: 8080) |
task_store | TaskStore | No | Task storage implementation |
base_path | str | No | Base URL path (default: ”/“) |
expose_agent_card | bool | No | Expose /.well-known/agent.json |
hooks | dict | No | Request lifecycle hooks |
async_execution | bool | No | Enable async task execution |
Request Validation
The A2A server automatically validates payments on every POST request:- Extracts Bearer token from Authorization header
- Reads
planIdorplanIdsfrom the agent card’s payment extension - Verifies permissions via
build_payment_required_for_plans() - Rejects requests with 402 if validation fails, including a base64-encoded
payment-requiredheader
accepts[], allowing the client to choose which plan to purchase.
Client Usage
Discovering Plans from the Agent Card
Consumers can fetch the agent card to discover available plans:Ordering a Plan and Sending Messages
Hooks
Add custom logic at request lifecycle points:Error Handling
| Error Code | HTTP Status | Description |
|---|---|---|
| -32001 | 402 | Missing Bearer token |
| -32001 | 402 | Payment validation failed |
| -32001 | 402 | Agent ID missing from card |
| -32001 | 402 | Plan ID missing from card |