Skip to main content

zeph_a2a/
lib.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! A2A (Agent-to-Agent) protocol client, server, and agent discovery for Zeph.
5//!
6//! This crate implements the [A2A protocol](https://google.github.io/A2A/) — a JSON-RPC 2.0
7//! based specification for communication between AI agents. It provides:
8//!
9//! - **Client** ([`A2aClient`]): sends messages and streams responses to remote A2A agents.
10//! - **Server** (`A2aServer`, feature `server`): exposes an HTTP endpoint that accepts
11//!   A2A JSON-RPC requests and streams Server-Sent Events (SSE) for real-time output.
12//! - **Discovery** ([`AgentRegistry`]): fetches and caches agent capability cards from
13//!   `/.well-known/agent.json` with configurable TTL.
14//! - **Capability cards** ([`AgentCardBuilder`]): builds [`AgentCard`] metadata describing
15//!   the agent's skills, I/O modes, and protocol version.
16//! - **IBCT** ([`Ibct`], feature `ibct`): Invocation-Bound Capability Tokens for scoped
17//!   delegation — HMAC-SHA256 signed tokens bound to a specific task and endpoint.
18//! - **JSON-RPC 2.0 types** ([`jsonrpc`]): request/response envelope types and the A2A
19//!   method name constants.
20//! - **Protocol types** ([`types`]): shared wire-format types re-exported at the crate root.
21//!
22//! # Architecture
23//!
24//! `zeph-a2a` is an optional feature-gated dependency of the main `zeph` binary. The
25//! `A2aServer` is started as a background service when `[a2a]` is enabled in config. The
26//! [`AgentRegistry`] verifies a peer's [`AgentCard`] (signature + URL-origin trust policy,
27//! A2A 1.0.0 §8.4) before `zeph --connect <URL>` establishes a session via [`A2aClient`]
28//! (#6200); see `src/tui_remote.rs` in the `zeph` binary crate for the wiring.
29//!
30//! # Features
31//!
32//! | Feature | Description |
33//! |---------|-------------|
34//! | `server` | Enables `A2aServer`, `TaskManager`, and `TaskProcessor` |
35//! | `ibct`   | Enables [`Ibct`] token issuance and verification (HMAC-SHA256) |
36//! | `card-signing` | Enables [`card_signing::verify_card_signatures`] and [`card_signing::sign_card`] (JWS/ES256 over RFC 8785 JCS). Without it, `AgentCardSignature`/`signatures` still (de)serialize, but verification always returns `SignatureVerification::FeatureDisabled`. |
37//!
38//! # Examples
39//!
40//! ```rust,no_run
41//! use zeph_a2a::{A2aClient, AgentCardBuilder, AgentRegistry, SendMessageParams, Message};
42//! use std::time::Duration;
43//!
44//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
45//! // Build an agent card for this agent.
46//! let card = AgentCardBuilder::new("my-agent", "http://localhost:8080", "0.1.0")
47//!     .description("A helpful AI agent")
48//!     .streaming(true)
49//!     .build();
50//!
51//! // Discover a peer agent's capabilities.
52//! let registry = AgentRegistry::new(reqwest::Client::new(), Duration::from_secs(300));
53//! let peer_card = registry.discover("http://peer-agent.example.com").await?;
54//!
55//! // Send a message to the peer agent.
56//! let client = A2aClient::new(reqwest::Client::new());
57//! let params = SendMessageParams {
58//!     message: Message::user_text("Hello, peer agent!"),
59//!     configuration: None,
60//! };
61//! let task = client.send_message(&peer_card.url, params, None).await?;
62//! println!("Task {} in state {:?}", task.id, task.status.state);
63//! # Ok(())
64//! # }
65//! ```
66
67#![forbid(unsafe_code)]
68
69pub mod card;
70pub mod card_signing;
71pub mod client;
72pub mod discovery;
73pub mod error;
74pub mod ibct;
75pub mod jsonrpc;
76#[cfg(feature = "server")]
77#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
78pub mod server;
79pub mod types;
80
81#[cfg(test)]
82mod testing;
83
84/// A2A protocol version implemented by this crate.
85///
86/// This crate implements A2A **0.2.1** for wire compatibility (method names, well-known
87/// discovery path `/.well-known/agent.json`, field shapes), plus one additive 1.0.0
88/// feature: [`AgentCard::signatures`](crate::AgentCard::signatures) / [`card_signing`]
89/// (A2A 1.0.0 §8.4). This constant is intentionally **not** bumped to `"1.0"` — doing so
90/// would over-claim conformance the Key Invariant "`AgentCard` must accurately reflect
91/// supported capabilities" forbids. Deferred 1.0.0 items, tracked as follow-ups to #5928:
92///
93/// - Well-known path rename to `/.well-known/agent-card.json` (see `discovery.rs`).
94/// - gRPC / HTTP-REST transport bindings (JSON-RPC only today).
95/// - Signing our own served card (`server`/`card.rs` emitting `signatures`).
96/// - `jku`/JWKS key retrieval and `x5c` certificate-chain trust anchoring.
97pub const A2A_PROTOCOL_VERSION: &str = "0.2.1";
98
99pub use card::AgentCardBuilder;
100pub use card_signing::{SigAlg, SignatureVerification, TrustedKey};
101pub use client::{A2aClient, SecurityPolicy, TaskEvent, TaskEventStream};
102pub use discovery::{AgentRegistry, CardTrustPolicy};
103pub use error::A2aError;
104pub use ibct::{Ibct, IbctError, IbctKey};
105pub use jsonrpc::SendMessageParams;
106#[cfg(feature = "server")]
107#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
108pub use server::{A2aServer, ProcessorEvent, TaskManager, TaskProcessor};
109pub use types::*;