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.
1. Create the project
Section titled “1. Create the project”cargo new agent-servercd agent-server[dependencies]ag-ui = { version = "=0.4.5", features = ["axum"] }axum = "0.8"tokio = { version = "1", features = ["rt-multi-thread", "macros", "net"] }2. Implement an agent and connect HTTP
Section titled “2. Implement an agent and connect HTTP”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.
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();}3. Run and check the response
Section titled “3. Run and check the response”Start the server with cargo run, then send a request from another terminal.
curl -N -X POST http://127.0.0.1:3000/agent \ -H 'content-type: application/json' \ -d '{"threadId":"thread-1","runId":"run-1","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.
Component guides
Section titled “Component guides”| 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 |
Connect application logic
Section titled “Connect application logic”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.