Skip to content
Select themeDarkLightAuto
Documentation version0.5.0-alpha.1
Select languageEnglish
0.5.0-alpha.1 is superseded. Fresh dependency resolution can fail on Rust 1.85. Use the 1.0.0 guide.

Connect an agent to AG-UI

This page connects an existing agent to AG-UI. Your application or framework owns the model loop, executable tool registration, graph routing and subagent execution. The Greeter below is a minimal example of the protocol connection.

Build a server that streams text at http://127.0.0.1:3000/agent. Start with a small Agent, then add the components your application needs. Requires Rust 1.85 or newer.

Terminal window
cargo new agent-server
cd agent-server

Pin this guide’s prerelease in Cargo.toml:

Cargo.toml
[dependencies]
ag-ui = { version = "=0.5.0-alpha.1", features = ["axum"] }
axum = "0.8"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net"] }

Replace src/main.rs with the following code. Put application logic in Agent, write client output through RunContext, and connect it to HTTP with route_agui.

src/main.rs
use ag_ui::axum::RouterExt;
use ag_ui::RunOutcome;
use ag_ui::server::{Agent, Result, RunContext};
use axum::Router;
struct Greeter;
impl Agent for Greeter {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
// Streams as TEXT_MESSAGE_START / _CONTENT / _END.
let mut message = ctx.assistant_message()?;
message.delta("Hello from Rust.")?;
message.end()?;
Ok(RunOutcome::Success)
}
}
#[tokio::main]
async fn main() {
let app: Router = Router::new().route_agui("/agent", Greeter);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
println!("agent on http://127.0.0.1:3000/agent");
axum::serve(listener, app).await.unwrap();
}

Start the server with cargo run, then send a request from another terminal.

Terminal window
curl -N -X POST http://127.0.0.1:3000/agent \
-H 'content-type: application/json' \
-d '{"threadId":"thread-1","runId":"run-1","protocolVersion":"1.0","messages":[],"tools":[],"context":[]}'

Expect RUN_STARTED → TEXT_MESSAGE_START → TEXT_MESSAGE_CONTENT → TEXT_MESSAGE_END → RUN_FINISHED, with Hello from Rust. as the text content. The SDK generates the run’s opening and closing events.

What you need Component Guide
Connect a model or application logic Agent, RunContext, RunOutcome Agent and run context
Expose HTTP and handle requests RouterExt, AgentEndpoint, AgUiInput HTTP endpoint
Stream a response incrementally Message handle, delta, end Streaming text
Describe tool calls and results Tool call handle Tool calls
Update state shared with the UI State, snapshots, deltas Shared state
Wait for a decision and resume Interrupt, resume input Human in the loop
Attribute child agent output Subagent handle, invocation ID Subagents
Handle failures and disconnects Server errors, cancellation token Errors and cancellation

For example, a task-board server reads task data, emits state changes and returns an interrupt when it needs approval. The application performs database writes and authorization. Inject your services or model client into the Agent implementation and emit their output through RunContext.

Follow the task-board example for that flow. Use agent tests to check it without HTTP. Next, connect a Rust client to display the response.