Start the server¶
DefaultRequestHandler does the protocol work: it runs the executor, keeps the task up to date from the events and builds the responses. The server mounts it on routes.
examples/src/quickstart/server.rs
use std::sync::Arc;
use a2a_server::agent_card::agent_card_router;
use a2a_server::jsonrpc::jsonrpc_router;
use a2a_server::rest::rest_router;
use a2a_server::{DefaultRequestHandler, InMemoryTaskStore, StaticAgentCard};
use examples_lib::quickstart::{Greeter, agent_card};
#[tokio::main]
async fn main() {
// The handler runs the executor and keeps the tasks in the store.
let handler = Arc::new(DefaultRequestHandler::new(
Greeter,
InMemoryTaskStore::new(),
));
let card = Arc::new(StaticAgentCard::new(agent_card("http://localhost:9999")));
// One route per binding, and the card under /.well-known/.
let app = axum::Router::new()
.nest("/jsonrpc", jsonrpc_router(handler.clone()))
.nest("/rest", rest_router(handler))
.merge(agent_card_router(card));
let listener = tokio::net::TcpListener::bind("127.0.0.1:9999")
.await
.unwrap();
println!("Greeter listening on http://localhost:9999");
axum::serve(listener, app).await.unwrap();
}
InMemoryTaskStorekeeps the tasks in memory, so they are lost when the server stops. Any implementation of theTaskStoretrait can replace it.jsonrpc_routerandrest_routereach serve one binding of the same handler, under their own prefix. The prefixes match the URLs in the agent card.agent_card_routerserves the card at/.well-known/agent-card.json.- The server listens on
127.0.0.1, so only your machine can reach it. The base URL in the card is written inmain; pass the real one toagent_cardwhen you serve the agent elsewhere.
Run it¶
Wait for this line:
Leave the server running and open a second terminal. Fetch the card:
{
"name": "Greeter",
"description": "Greets you by name. Asks for the name first if it was not given.",
"version": "0.1.0",
"supportedInterfaces": [
{
"url": "http://localhost:9999/jsonrpc",
"protocolBinding": "JSONRPC",
"protocolVersion": "1.0"
},
{
"url": "http://localhost:9999/rest",
"protocolBinding": "HTTP+JSON",
"protocolVersion": "1.0"
}
],
"capabilities": {
"streaming": true,
"pushNotifications": false
},
"defaultInputModes": [
"text/plain"
],
"defaultOutputModes": [
"text/plain"
],
"skills": [
{
"id": "greet",
"name": "Greet",
"description": "Greets the name in the message.",
"tags": [
"greeting"
],
"examples": [
"Ada"
]
}
]
}
The card is the one agent_card built, in the JSON the protocol defines: the Rust field names become camelCase, and the fields left None are omitted.
Next, call the agent.