Skip to main content

serve

Function serve 

Source
pub async fn serve(
    addr: impl ToSocketAddrs,
    dispatcher: impl Dispatcher,
) -> Result<(), Error>
Expand description

Starts an HTTP server that dispatches requests using the given dispatcher.

Binds a TCP listener on addr, accepts connections, and serves each one using a hyper auto-connection builder. This eliminates the ~25 lines of boilerplate that every A2A agent otherwise needs.

Each connection is served in a separate Tokio task.

Once the listener is bound this function does not return. accept() failures are transient by nature (a per-connection abort, or a momentarily full descriptor table), so the loop logs them and retries — backing off briefly on descriptor exhaustion — rather than tearing the server down. Callers wanting shutdown should race this future against their own signal (tokio::select!) instead of waiting for it to resolve.

§Errors

Returns std::io::Error if the TCP listener fails to bind. This is the only way the returned future completes.

§Example

use std::sync::Arc;
use a2a_protocol_server::serve::serve;
use a2a_protocol_server::dispatch::JsonRpcDispatcher;
use a2a_protocol_server::RequestHandlerBuilder;

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

let dispatcher = JsonRpcDispatcher::new(handler);
serve("127.0.0.1:3000", dispatcher).await?;