pub struct A2aServer<S = InMemoryTaskStore> { /* private fields */ }target_family=wasm only.Expand description
A Rig agent served over A2A: its agent card, its task store, and the
router into_router builds from them.
The router serves the card at /.well-known/agent-card.json, the JSON-RPC
binding at JSONRPC_PATH and the HTTP+JSON binding at REST_PATH.
Streaming is on. Push notifications are off unless
push_notifications turns them on.
For peers still on A2A v0.3, the same agent is served under
V03_PATH: its v0.3 card at
/v03/.well-known/agent-card.json and v0.3 JSON-RPC at /v03. Point a
v0.3 client at {url}/v03.
Implementations§
Source§impl A2aServer
impl A2aServer
Sourcepub fn new(agent: Agent, url: impl AsRef<str>) -> Self
pub fn new(agent: Agent, url: impl AsRef<str>) -> Self
Serve agent at url, the base URL clients reach this server at.
The card takes the agent’s name and description, lists both bindings
under url, accepts text/plain and application/json parts, and
declares one chat skill. Tasks live in an InMemoryTaskStore,
which keeps finished tasks without limit unless
terminal_task_limit is set.
Sourcepub fn terminal_task_limit(self, max: usize) -> Self
pub fn terminal_task_limit(self, max: usize) -> Self
Keep at most max finished tasks in memory, purging the oldest
first. A purged task answers GetTask with TaskNotFound. Tasks
still running are never purged. A max of 0 is treated as 1.
Off by default: a client may fetch a finished task long after it ended, and a limit would change that answer. Set it for a server that runs for long and takes many tasks.
Caveats:
- A late status update for a purged task can bring it back. The
executor’s save path falls back to
createwhenupdatefinds no task (ina2a-server-lf, not in this crate), so a cancel or event that arrives after the purge re-creates the task as a finished one, where it can be purged again. Set the limit well above the number of tasks in flight. - Only the default in-memory store has this limit. Call it before
task_storeor the limit is dropped; for a custom store, set its own limit instead.
Source§impl<S: TaskStore> A2aServer<S>
impl<S: TaskStore> A2aServer<S>
Sourcepub fn edit_card(self, edit: impl FnOnce(&mut AgentCard)) -> Self
pub fn edit_card(self, edit: impl FnOnce(&mut AgentCard)) -> Self
Change the agent card, for example its skills, version or provider.
The router serves the card as edited and refuses a part that declares
a media type outside its default_input_modes. The routes stay at
JSONRPC_PATH and REST_PATH whatever the card’s interfaces say.
Sourcepub fn max_contexts(self, max: usize) -> Self
pub fn max_contexts(self, max: usize) -> Self
Remember at most max conversations; see A2aExecutor::max_contexts.
Sourcepub fn typed<T>(self) -> Self
pub fn typed<T>(self) -> Self
Answer every task with a T as JSON; see A2aExecutor::typed. The
card then declares application/json output and, in each skill’s
description, the JSON schema of T, for callers to know what comes back.
Sourcepub fn history_store<T>(self, store: T) -> Self
pub fn history_store<T>(self, store: T) -> Self
Keep conversations in a rig_memory store; see
A2aExecutor::history_store.
Sourcepub fn history_window(self, window: impl Recent + 'static) -> Self
pub fn history_window(self, window: impl Recent + 'static) -> Self
How much of a conversation each request sends; see
A2aExecutor::history_window.
Sourcepub fn max_concurrent_tasks(self, max: usize) -> Self
pub fn max_concurrent_tasks(self, max: usize) -> Self
Run at most max tasks at once; see A2aExecutor::max_concurrent_tasks.
Sourcepub fn max_run_duration(self, max: Duration) -> Self
pub fn max_run_duration(self, max: Duration) -> Self
Stop runs longer than max; see A2aExecutor::max_run_duration.
Sourcepub fn allowed_hosts<I, H>(self, hosts: I) -> Self
pub fn allowed_hosts<I, H>(self, hosts: I) -> Self
Answer only requests whose Host header names one of hosts, and
refuse the rest with HTTP 403 before any agent work. This guards a
local server against DNS rebinding, where a web page on another site
is pointed at localhost. Cards are checked too.
An entry is a host name such as agent.example.com, which matches
any port, or host:port, which matches only that port. Host names
compare without case. An empty list refuses every request.
Off by default: without this call the Host header is not checked.
Sourcepub fn max_request_bytes(self, bytes: usize) -> Self
pub fn max_request_bytes(self, bytes: usize) -> Self
Refuse request bodies over bytes with HTTP 413. Defaults to
DEFAULT_MAX_REQUEST_BYTES.
Sourcepub fn bearer_token(self, token: impl Into<String>) -> Self
pub fn bearer_token(self, token: impl Into<String>) -> Self
Require Authorization: Bearer <token> on every call. The agent
cards stay public, as discovery needs them, and declare the scheme;
calls without the token get HTTP 401.
Sourcepub fn extended_card(self, card: AgentCard) -> Self
pub fn extended_card(self, card: AgentCard) -> Self
Serve card as the authenticated extended agent card. Callers get it
with the bearer token, from GetExtendedAgentCard on the JSON-RPC
binding and from /extendedAgentCard on the HTTP+JSON binding. Without
the token they get HTTP 401, as for any other call. Off by default.
The card is served as given, to every caller with the token. Put only what that caller may see here. The public card stays as it is.
Needs bearer_token, since the token is the only
check on who gets the card. Without one, the server logs an error when
it is built and keeps the extended card off: the public card omits
capabilities.extendedAgentCard, and the methods refuse the call. The
card is never served unauthenticated.
The v0.3 surface has no extended card.
The extended card is not exempt from extension:
a required extension must be activated to read it, even with the
token. The SDK checks it on every method, and the card method cannot
be told apart without reading the JSON-RPC body.
Sourcepub fn extension(self, extension: AgentExtension) -> Self
pub fn extension(self, extension: AgentExtension) -> Self
Declare an A2A extension in the card’s capabilities.extensions.
A client activates an extension with the A2A-Extensions header. The
SDK refuses a request that does not activate an extension the card
marks required, with ExtensionSupportRequiredError. The server
echoes the activated extensions it declares in the response’s
A2A-Extensions header. A tool or hook in the agent run reads the
activated URIs with activated_extensions.
Adding a URI that is already declared is ignored. The first declaration is kept, and the card keeps the order of the first declarations.
Unset, the card omits extensions.
Sourcepub fn push_notifications(self, enabled: bool) -> Self
pub fn push_notifications(self, enabled: bool) -> Self
Accept push notification configs, so a client can ask the server to POST task updates to a webhook. Off by default.
Push needs bearer_token: without one, the
server logs an error when it is built and keeps push off, so the card
says pushNotifications: false and a config set is refused with
PushNotificationNotSupported. With push on, the card declares
pushNotifications: true.
Configs are kept in memory unless push_config_store
is set. At most 10 configs per task are accepted, and 10,000 in all
counted since the server started. When the total is reached, configs
of tasks that terminal_task_limit has
purged are dropped, so the total counts live tasks.
Each config is checked when it is saved: its URL must be https, on
the list from push_allowed_hosts if one
is set, and not a literal IP in 100.64.0.0/10, 64:ff9b::/96, 2002::/16
or ::a.b.c.d/96.
The SDK’s address guard runs when a webhook is called. It refuses
127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16,
224.0.0.0/4, 0.0.0.0, ::1, ::, fc00::/7, fe80::/10, ff00::/8, the
IPv4-mapped forms of those IPv4 ranges, and the hosts localhost,
metadata.google.internal and metadata.azure.com. It checks the
address a name resolves to, and each redirect. It does not cover the
ranges listed above, so a name resolving to them is not refused.
Redirects are followed only to hosts that pass the same check.
§Security
- A webhook URL comes from the client. An open server with push on lets any caller make it POST to any host the guard allows. Set an allowlist when clients are not trusted.
- The server signs nothing: there is no JWT or JWKS. It forwards the
client’s
tokenas theA2A-Notification-Tokenheader and itsauthenticationcredentials asAuthorization. A receiver must check both itself. - Each POST carries the full task update, including the agent’s output and any artifacts.
- Anyone who holds a task id can attach a webhook to it, and so receive that task’s updates. Push requires a bearer token for this reason, and the bearer token is the only check on who may do so.
- A slow webhook delays the task. Delivery runs in the task’s event loop, one config at a time, and each POST has a 5 second timeout. A task with 10 configs whose receivers all stall can take up to 50 seconds per update, and the client’s stream waits for it.
- A finished task’s configs stay until the total cap is reached. Set
terminal_task_limittoo: then the oldest finished tasks are purged, and their configs are dropped at the next save that reaches the total cap.
let server = A2aServer::new(agent, "https://agent.example.com")
.bearer_token(token)
.push_notifications(true)
.push_allowed_hosts(["hooks.example.com"])
.terminal_task_limit(1_000);Sourcepub fn push_config_store<P: PushConfigStore>(self, store: P) -> Self
pub fn push_config_store<P: PushConfigStore>(self, store: P) -> Self
Keep push configs in store instead of in memory. Also turns push
notifications on, as push_notifications(true)
does. Every config still goes through the URL checks and the caps.
Sourcepub fn push_allowed_hosts<I, H>(self, hosts: I) -> Self
pub fn push_allowed_hosts<I, H>(self, hosts: I) -> Self
Accept webhook URLs only on these hosts, compared without regard to case. A config naming another host is refused when it is saved. Takes effect only while push notifications are on.
Sourcepub fn allow_unguarded_push_urls(self) -> Self
pub fn allow_unguarded_push_urls(self) -> Self
Drop the checks that keep a webhook URL from reaching a private
target: the https requirement, the refusal on save of the literal
ranges 100.64.0.0/10, 64:ff9b::/96, 2002::/16 and ::a.b.c.d/96, and
the SDK’s address guard (loopback, private and link-local addresses,
DNS and redirects). Also lets the SDK send webhooks through the
proxies named in the environment. For tests against a webhook on this
machine only. The host allowlist still applies.
Sourcepub fn task_store<T: TaskStore>(self, store: T) -> A2aServer<T>
pub fn task_store<T: TaskStore>(self, store: T) -> A2aServer<T>
Keep tasks in store instead of in memory.
This replaces the default store, and with it any limit set by
terminal_task_limit called before
it: that limit is dropped. Set the limit on store itself, for
example with InMemoryTaskStore::with_terminal_task_limit.
Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
The Axum router serving the card and both bindings.
Auto Trait Implementations§
impl<S = InMemoryTaskStore> !RefUnwindSafe for A2aServer<S>
impl<S = InMemoryTaskStore> !UnwindSafe for A2aServer<S>
impl<S> Forget for A2aServer<S>
impl<S> Freeze for A2aServer<S>where
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
*const ArcInner<Shared> is !null: Freeze,
PhantomData<ArcInner<Shared>>: Freeze,
*const ArcInner<str> is !null: Freeze,
PhantomData<ArcInner<str>>: Freeze,
*const ArcInner<Atomic<usize>> is !null: Freeze,
PhantomData<ArcInner<Atomic<usize>>>: Freeze,
PhantomData<fn() -> Completion>: Freeze,
*const ArcInner<RunSpec> is !null: Freeze,
PhantomData<ArcInner<RunSpec>>: Freeze,
PhantomData<Arc<dyn DynAgentHook>>: Freeze,
PhantomData<String>: Freeze,
PhantomData<Key<Completion>>: Freeze,
PhantomData<Key<Retrieve>>: Freeze,
*const ArcInner<AnonymousModel> is !null: Freeze,
PhantomData<ArcInner<AnonymousModel>>: Freeze,
*const ArcInner<dyn Persist> is !null: Freeze,
PhantomData<ArcInner<dyn Persist>>: Freeze,
*const ArcInner<RwLock<ToolServerState>> is !null: Freeze,
PhantomData<ArcInner<RwLock<ToolServerState>>>: Freeze,
*const ArcInner<dyn HistoryStore> is !null: Freeze,
PhantomData<ArcInner<dyn HistoryStore>>: Freeze,
*const ArcInner<dyn Recent> is !null: Freeze,
PhantomData<ArcInner<dyn Recent>>: Freeze,
*const ArcInner<Mutex<HashMap<String, AbortHandle>>> is !null: Freeze,
PhantomData<ArcInner<Mutex<HashMap<String, AbortHandle>>>>: Freeze,
*const ArcInner<Semaphore> is !null: Freeze,
PhantomData<ArcInner<Semaphore>>: Freeze,
*const ArcInner<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync> is !null: Freeze,
PhantomData<ArcInner<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync>>: Freeze,
*const ArcInner<[String]> is !null: Freeze,
PhantomData<ArcInner<[String]>>: Freeze,
PhantomData<AgentInterface>: Freeze,
PhantomData<AgentExtension>: Freeze,
PhantomData<AgentSkill>: Freeze,
PhantomData<(String, SecurityScheme)>: Freeze,
PhantomData<HashMap<String, Vec<String>>>: Freeze,
PhantomData<AgentCardSignature>: Freeze,
S: Freeze,
*const ArcInner<dyn PushConfigStore> is !null: Freeze,
PhantomData<ArcInner<dyn PushConfigStore>>: Freeze,
impl<S> Move for A2aServer<S>
impl<S> Send for A2aServer<S>where
S: Send,
RawVecInner: Send,
Arc<Shared>: Send,
Arc<str>: Send,
Arc<Atomic<usize>>: Send,
Arc<RunSpec>: Send,
Arc<dyn DynAgentHook>: Send,
Arc<AnonymousModel>: Send,
Arc<dyn Persist>: Send,
Arc<RwLock<ToolServerState>>: Send,
Arc<dyn HistoryStore>: Send,
Arc<dyn Recent>: Send,
Arc<Mutex<HashMap<String, AbortHandle>>>: Send,
Arc<Semaphore>: Send,
Arc<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync>: Send,
Arc<[String]>: Send,
RawTable<(String, Value)>: Send,
RawTable<(String, Vec<String>)>: Send,
RawTable<(String, SecurityScheme)>: Send,
Arc<dyn PushConfigStore>: Send,
impl<S> Sync for A2aServer<S>where
S: Sync,
RawVecInner: Sync,
Arc<Shared>: Sync,
Arc<str>: Sync,
Arc<Atomic<usize>>: Sync,
Arc<RunSpec>: Sync,
Arc<dyn DynAgentHook>: Sync,
Arc<AnonymousModel>: Sync,
Arc<dyn Persist>: Sync,
Arc<RwLock<ToolServerState>>: Sync,
Arc<dyn HistoryStore>: Sync,
Arc<dyn Recent>: Sync,
Arc<Mutex<HashMap<String, AbortHandle>>>: Sync,
Arc<Semaphore>: Sync,
Arc<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync>: Sync,
Arc<[String]>: Sync,
RawTable<(String, Value)>: Sync,
RawTable<(String, Vec<String>)>: Sync,
RawTable<(String, SecurityScheme)>: Sync,
Arc<dyn PushConfigStore>: Sync,
impl<S> Unpin for A2aServer<S>where
S: Unpin,
*const u8 is !null: Unpin,
Arc<Shared>: Unpin,
Arc<str>: Unpin,
Arc<Atomic<usize>>: Unpin,
Arc<RunSpec>: Unpin,
Arc<dyn DynAgentHook>: Unpin,
Arc<AnonymousModel>: Unpin,
Arc<dyn Persist>: Unpin,
Arc<RwLock<ToolServerState>>: Unpin,
Arc<dyn HistoryStore>: Unpin,
Arc<dyn Recent>: Unpin,
Arc<Mutex<HashMap<String, AbortHandle>>>: Unpin,
Arc<Semaphore>: Unpin,
Arc<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync>: Unpin,
Arc<[String]>: Unpin,
*const LeafNode<String, Value> is !null: Unpin,
Box<(String, Value)>: Unpin,
Arc<dyn PushConfigStore>: Unpin,
impl<S> UnsafeUnpin for A2aServer<S>where
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const ArcInner<Shared> is !null: UnsafeUnpin,
PhantomData<ArcInner<Shared>>: UnsafeUnpin,
*const ArcInner<str> is !null: UnsafeUnpin,
PhantomData<ArcInner<str>>: UnsafeUnpin,
*const ArcInner<Atomic<usize>> is !null: UnsafeUnpin,
PhantomData<ArcInner<Atomic<usize>>>: UnsafeUnpin,
PhantomData<fn() -> Completion>: UnsafeUnpin,
*const ArcInner<RunSpec> is !null: UnsafeUnpin,
PhantomData<ArcInner<RunSpec>>: UnsafeUnpin,
PhantomData<Arc<dyn DynAgentHook>>: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
PhantomData<Key<Completion>>: UnsafeUnpin,
PhantomData<Key<Retrieve>>: UnsafeUnpin,
*const ArcInner<AnonymousModel> is !null: UnsafeUnpin,
PhantomData<ArcInner<AnonymousModel>>: UnsafeUnpin,
*const ArcInner<dyn Persist> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Persist>>: UnsafeUnpin,
*const ArcInner<RwLock<ToolServerState>> is !null: UnsafeUnpin,
PhantomData<ArcInner<RwLock<ToolServerState>>>: UnsafeUnpin,
*const ArcInner<dyn HistoryStore> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn HistoryStore>>: UnsafeUnpin,
*const ArcInner<dyn Recent> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Recent>>: UnsafeUnpin,
*const ArcInner<Mutex<HashMap<String, AbortHandle>>> is !null: UnsafeUnpin,
PhantomData<ArcInner<Mutex<HashMap<String, AbortHandle>>>>: UnsafeUnpin,
*const ArcInner<Semaphore> is !null: UnsafeUnpin,
PhantomData<ArcInner<Semaphore>>: UnsafeUnpin,
*const ArcInner<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Fn(Agent, Message, Vec<Message>) -> Pin<Box<dyn Future<Output = Result<(Value, Vec<Message>), String>> + Send>> + Send + Sync>>: UnsafeUnpin,
*const ArcInner<[String]> is !null: UnsafeUnpin,
PhantomData<ArcInner<[String]>>: UnsafeUnpin,
PhantomData<AgentInterface>: UnsafeUnpin,
PhantomData<AgentExtension>: UnsafeUnpin,
PhantomData<AgentSkill>: UnsafeUnpin,
PhantomData<(String, SecurityScheme)>: UnsafeUnpin,
PhantomData<HashMap<String, Vec<String>>>: UnsafeUnpin,
PhantomData<AgentCardSignature>: UnsafeUnpin,
S: UnsafeUnpin,
*const ArcInner<dyn PushConfigStore> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn PushConfigStore>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request