Skip to content
Select themeDarkLightAuto
Documentation version0.5.0-alpha.2
Select languageEnglish

Call an agent from Rust

Build a Rust client that connects to an AG-UI server, prints streamed output and retains conversation history. Start the server from Build an agent server first, or replace the URL with an existing AG-UI endpoint. Requires Rust 1.85 or newer and a C toolchain for the HTTP client’s TLS dependencies.

Terminal window
cargo new agent-client
cd agent-client

Pin this guide’s prerelease in Cargo.toml:

Cargo.toml
[dependencies]
ag-ui = { version = "=0.5.0-alpha.2", features = ["http"] }
futures-util = "0.3"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

Replace src/main.rs with this code. HttpAgent owns connection settings, Thread owns conversation history and state, and Update describes changes for the view.

use ag_ui::client::{HttpAgent, MessageChangeKind, RunEnd, Update};
use futures_util::StreamExt;
use std::io::Write;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let agent = HttpAgent::new("http://127.0.0.1:3000/agent")?;
let mut thread = agent.thread("thread-1");
{
let mut run = thread.send("Hello")?;
while let Some(update) = run.next().await {
match update {
Update::Message(message) => {
if let MessageChangeKind::Content { delta } = message.change {
print!("{delta}");
std::io::stdout().flush()?;
}
}
Update::Error(error) => eprintln!("{error}"),
Update::Done(RunEnd::Success { .. }) => println!(),
Update::Done(end) => eprintln!("{end:?}"),
_ => {}
}
}
}
println!("{} messages in the thread", thread.messages().len());
Ok(())
}

With the server running, use cargo run. Expect Hello from Rust. and 2 messages in the thread. Call send() on the same Thread again to include the conversation in the next request.

What you need Component Guide
Connect and retain a conversation HttpAgent, Thread Connect and manage threads
Handle changes, diagnostics and completion RunStream, Update, RunEnd The update stream
Display messages, tools and child agents Message ID, tool call ID, subagent registry Rendering
Read shared state in the UI Update::State, state, raw_state Read shared state
Approve, decline and resume resume_many, decline, snapshot Approvals and recovery
Replace HTTP or test locally Transport, ReplayTransport Transports

Connect existing client functions using Client tools and results.

The SDK assembles events into messages and state. Your application connects Update to UI components, configures authentication and chooses where to store snapshots. Creating a new object with the same thread ID does not fetch server history.

This guide covers clients written in Rust. For a TypeScript browser client, use the official TypeScript SDK and the review-desk example. See board-watch for a Rust CLI implementation.