Tools
Tools are how an agent acts on the world: it can add numbers, fetch a page, search the web or call your own API. The model asks for a tool by name with JSON arguments, troupekit runs it, and the result goes back to the model.
In code, a tool is anything that implements the Tool trait. An agent or graph accepts a #[tool] fn, a tool(..) value or an Arc<dyn Tool>. Before a tool runs, the registry checks the call's arguments against the tool's JSON Schema, so your fn only ever sees arguments of the right shape.
#[tool] fns
The quickest way to write a tool is a plain fn with #[tool]. The macro turns the fn into a unit struct that implements Tool: the doc comment becomes the description the model reads, and the arguments become the schema.
#![allow(unused)]
fn main() {
use troupekit::prelude::*;
/// Adds two numbers.
#[tool]
pub async fn add(a: i64, b: i64) -> i64 {
a + b
}
}
The fn can be sync or async, and you can still call its body yourself as add::invoke(..). The macro takes three optional keys: name, description and crate.
The tool(..) form
tool(name, description, f) builds a tool without a macro, which suits a closure or a tool you create at run time. f takes the parsed arguments, of any Deserialize + JsonSchema type, and returns any IntoToolOutput value. We define the argument type first:
#![allow(unused)]
fn main() {
#[derive(Deserialize, schemars::JsonSchema)]
struct AddArgs {
a: i64,
b: i64,
}
}
A tool can fail. When f returns an Err, the model receives it as error output and can try something else; the run itself does not fail. This test divides by zero and checks what comes back:
#![allow(unused)]
fn main() {
#[tokio::test]
async fn tool_fn_form_err_reaches_model_as_error_output() {
let mut registry = ToolRegistry::new();
let div = tool("div", "Divides a by b", |x: AddArgs| async move {
if x.b == 0 {
Err("division by zero")
} else {
Ok(x.a / x.b)
}
});
registry.register(Arc::new(div)).unwrap();
let out = registry
.call("div", json!({"a": 1, "b": 0}), &ToolContext::default())
.await
.unwrap();
assert_eq!(out, ToolOutput::error("division by zero"));
}
}
Adding tools to an agent
.tool(t) adds one #[tool] fn or tool(..) value, and .tool_arc(t) adds an Arc<dyn Tool>. To add many at once, in order, use .tools(iter) or .toolkit(kit). Two tools with the same name make build fail, because the model could not tell them apart.
When a model asks for several tools in one turn, the agent runs those calls concurrently.
Tool context
Every call gets a ToolContext. run_id names the run that made the call. idempotency_key matters for tools with side effects, such as sending an email or charging a card. After a crash, a resumed run may replay a call that already happened. The key is the same for a replayed call and new for every other call, so your tool can recognise the replay and skip the side effect.
Web fetch
web_fetch lets an agent read one web page. It sends a GET through a GuardedHttp client, keeps the visible text of HTML, caps it at max_chars characters and returns it fenced as untrusted, with the final URL as its source. Only text/html and text/plain responses are accepted.
The guarded client refuses private addresses. A model that reads a hostile page could be talked into fetching your router's admin page, and the guard is what stops it from reaching your network.
Web search
web_search lets an agent search the web. It tries the configured search instances in order and moves to the next one only when one fails. Two adapters ship, Brave Search and SearXNG, and the default instances live in crates/troupekit-tools/catalog/search.toml:
[[instance]]
name = "brave"
adapter = "brave"
base_url = "https://api.search.brave.com"
key_env = "BRAVE_SEARCH_API_KEY"
[[instance]]
name = "searxng-local"
adapter = "searxng"
base_url = "http://127.0.0.1:8888"
allow_private = true
An instance whose key is missing is skipped at load, which means a missing Brave key leaves you with SearXNG instead of an error. Results are third-party text, so they are fenced as untrusted before the model sees them.
Declarative HTTP tools
If the tool you need is an HTTP API, you can describe it in TOML instead of writing Rust. http_toolkit_from_toml loads one tool per [[tool]] table, with these keys:
| Key | Meaning |
|---|---|
name, description | what the model sees |
method | the HTTP method |
url | the endpoint, with {arg} placeholders for path arguments |
headers | optional; values may hold ${secret:NAME}, resolved when the file loads |
schema | the JSON Schema of the arguments |
Here is one:
[[tool]]
name = "forecast"
description = "Weather forecast for a city"
method = "GET"
url = "https://api.example.com/v1/{city}/forecast"
headers = { Authorization = "Bearer ${secret:WEATHER_KEY}", Accept = "application/json" }
schema = { type = "object", properties = { city = { type = "string" }, days = { type = "integer" } }, required = ["city"] }
Arguments named in the URL fill its placeholders, and the rest go to the query string or a JSON body.
Warning
Pass
http_toolkit_from_tomlaGuardedHttp, never an unguarded transport. The guard is what keeps a model from pointing your HTTP tools at a private address.