Skip to main content

Module axum_adapter

Module axum_adapter 

Source
Available on crate feature axum only.
Expand description

Axum framework integration for A2A servers.

Provides A2aRouter (also named RestRouter), which builds an [axum::Router] that handles all A2A v1.0 methods using the existing RequestHandler, and is itself a Dispatcher that Server serves (ADR 0052).

§Quick start

use std::sync::Arc;
use a2a_protocol_server::dispatch::axum_adapter::RestRouter;
use a2a_protocol_server::serve::{Server, shutdown_signal};
use a2a_protocol_server::RequestHandlerBuilder;

let handler = Arc::new(
    RequestHandlerBuilder::new(MyExecutor)
        .with_agent_card(card())
        .build()
        .expect("build handler"),
);

// `Server` gives the router its header-read and idle timeouts, connection
// cap, loopback `Host` check, production refusal and a shutdown that ends
// the tasks in flight before it drains.
let report = Server::bind("0.0.0.0:3000")
    .await
    .unwrap()
    .serve_with_shutdown(RestRouter::new(handler), shutdown_signal())
    .await;
assert_eq!(report.refused, None);

§Composability

into_router returns the router, to be merged with other Axum routes, middleware, and layers, and served with axum::serve, which then owns the timeouts and the shutdown:

// Any `tower` layer goes where `log_requests` is, such as
// `tower_http::cors::CorsLayer::permissive()` from the `tower-http` crate.
let app = axum::Router::new()
    .merge(A2aRouter::new(handler).into_router())
    .layer(middleware::from_fn(log_requests))
    .route("/custom", get(custom_handler));

§Multi-tenancy: use a resolver, not the URL prefix

This router registers no /tenants/{tenant}/… routes, unlike the built-in REST dispatcher (crate::dispatch::rest), which strips that prefix and threads the tenant through. Requests to a tenant-prefixed path therefore 404 here — fail-safe, but surprising if you are porting from RestDispatcher, where the same URL works.

Tenancy itself is not lost. This router forwards request headers to the handler, so a configured TenantResolver — for example HeaderTenantResolver — resolves tenants normally, and the resolver is authoritative over any client-supplied value. Pair it with require_resolved_tenant so a request that carries no tenant is rejected rather than served from the shared default partition.

With no resolver configured, a request is served from the tenant its own tenant field names, and from the default ("") tenant when it names none. A GET or DELETE carries the field in its query (?tenant=), as on the REST dispatcher; a POST in its body. The URL prefix is never read, which is why the prefixed routes are absent rather than silently mis-parsed; it is called out here so it is a choice rather than a discovery.

Structs§

A2aRouter
Builder for an Axum [Router] that serves all A2A v1.0 protocol methods.

Type Aliases§

RestRouter
A2aRouter under a name that says which binding it serves.