A2A Rust Libraries
Rust libraries for building Agent-to-Agent (A2A) applications following the A2A RC 1.0 specification. Build interoperable AI agents that can discover, communicate, and collaborate with each other.
What is A2A?
The Agent-to-Agent (A2A) protocol is an open standard for machine-to-machine communication between AI agents. It enables:
- Agent Discovery - Agents publish their capabilities via Agent Cards
- Standardized Messaging - JSON-RPC 2.0 over HTTP for reliable communication
- Task Management - Track asynchronous operations with state machines
- Security - Built-in support for OAuth2, API keys, and mutual TLS
Crates
| Crate | Description |
|---|---|
a2a-rs-core |
Shared A2A RC 1.0 types, JSON-RPC definitions, and utilities |
a2a-rs-server |
Generic server framework with pluggable MessageHandler trait |
a2a-rs-client |
Client library for agent discovery and message sending |
Quick Start
Installation
Add the crates you need to your Cargo.toml:
[]
# For building agent servers
= "1.0"
= "1.0"
# For building clients
= "1.0"
= "1.0"
# Required async runtime
= { = "1", = ["full"] }
Build an Echo Server (5 lines)
use A2aServer;
async
Build a Custom Agent
Implement the MessageHandler trait to create your own AI agent:
use ;
use ;
use async_trait;
;
async
Build a Client
use A2aClient;
use ;
async
Architecture
+---------------------------------------------------------------+
| Your Application |
+---------------------------------------------------------------+
| |
v v
+-----------------------------+ +-----------------------------+
| a2a-rs-client | | a2a-rs-server |
| - Agent discovery | | - MessageHandler trait |
| - Message sending | | - A2aServer builder |
| - Task polling | | - TaskStore |
| - OAuth PKCE support | | - Auth extractors |
+-----------------------------+ +-----------------------------+
| |
+-------------+-------------+
v
+-----------------------------+
| a2a-rs-core |
| - A2A RC 1.0 types |
| - Security schemes |
| - JSON-RPC definitions |
| - Helper functions |
+-----------------------------+
Core Concepts
Agent Card
Every A2A agent publishes an Agent Card that describes its capabilities:
use ;
// Agents expose their card at /.well-known/agent-card.json
let card = AgentCard ;
Messages and Parts
Messages contain multimodal content via flat Part structs:
use ;
// Text message
let msg = Message ;
// URL reference part
let url_part = url;
// Structured data part
let data_part = data;
// Raw bytes part (base64-encoded)
let raw_part = raw;
Tasks and States
Tasks track the lifecycle of agent operations:
use ;
// Task states follow this lifecycle:
//
// SUBMITTED -> WORKING -> COMPLETED
// \-> FAILED
// \-> CANCELED
// \-> INPUT_REQUIRED -> WORKING -> ...
// Check if task is done
if task.status.state.is_terminal
Server Features
Authentication
Add authentication to your server:
use ;
use HeaderMap;
new
.bind
.auth_extractor
.run
.await?;
Custom Routes
Add additional HTTP endpoints:
use ;
use ;
async
let custom_routes = new
.route;
new
.additional_routes
.run
.await?;
Client Features
Agent Card Caching
Agent cards are automatically cached for 5 minutes:
let client = with_server?;
let card = client.fetch_agent_card.await?; // Fetches from server
let card = client.fetch_agent_card.await?; // Uses cache
client.invalidate_card_cache.await; // Force refresh
let card = client.fetch_agent_card.await?; // Fetches fresh
Configurable Polling
use ;
let config = ClientConfig ;
let client = new?;
let task = client.poll_until_complete.await?;
OAuth PKCE Support
use ;
let config = ClientConfig ;
let client = new?;
let token = client.perform_oauth_interactive.await?;
// Or programmatic flow
let = client.start_oauth_flow.await?;
JSON-RPC Methods
The A2A protocol defines these JSON-RPC methods:
| Method | Description |
|---|---|
message/send |
Send a message to the agent |
message/sendStreaming |
Send with streaming response |
tasks/get |
Query a task by ID |
tasks/cancel |
Cancel a running task |
tasks/list |
List tasks |
tasks/subscribe |
Subscribe to task updates |
agentCard/getExtended |
Get extended agent card |
Example: Raw JSON-RPC
# Discover agent
# Send message
# Get task status
Error Handling
JSON-RPC Error Codes
use errors;
// Standard JSON-RPC errors
PARSE_ERROR; // -32700
INVALID_REQUEST; // -32600
METHOD_NOT_FOUND; // -32601
INVALID_PARAMS; // -32602
INTERNAL_ERROR; // -32603
// A2A-specific errors
TASK_NOT_FOUND; // -32001
TASK_NOT_CANCELABLE; // -32002
UNSUPPORTED_OPERATION; // -32004
VERSION_NOT_SUPPORTED; // -32007
EXTENSION_SUPPORT_REQUIRED; // -32009
Security Schemes
The A2A protocol supports multiple authentication methods:
use ;
use HashMap;
// API Key authentication
let api_key = ApiKeySecurityScheme;
// Bearer token
let bearer = HttpAuthSecurityScheme;
// OAuth 2.0
let oauth = Oauth2SecurityScheme;
// Mutual TLS
let mtls = MtlsSecurityScheme;
Documentation
- Architecture Guide - Detailed crate structure and data flows
- Getting Started - Step-by-step tutorial
- A2A Specification - Official protocol spec
Requirements
- Rust 1.75 or later
- Tokio async runtime
Dependencies
These libraries use minimal, well-maintained dependencies:
| Dependency | Purpose |
|---|---|
serde |
JSON serialization |
tokio |
Async runtime |
axum |
HTTP server (a2a-rs-server) |
reqwest |
HTTP client (a2a-rs-client) |
uuid |
UUID generation |
async-trait |
Async trait support |
Contributing
Contributions are welcome! Please see our Contributing Guide for details.
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Acknowledgments
This implementation follows the A2A Protocol Specification developed by Google.