Skip to main content

rig_tool

Attribute Macro rig_tool 

Source
#[rig_tool]
Available on crate feature derive only.
Expand description

Generates a tool type, parameter struct, and uppercase static from a function returning Result<T, E>, implementing rig_core::tool::Tool. The function may take the call’s &mut ToolContext. Imported context names need #[rig(context)]; qualified Rig paths are recognized directly. The context is excluded from model arguments.

Accepts name, description, params(name = "description"), and required(name, ...). Explicit names must start with an ASCII letter or _, contain only ASCII letters, digits, _, or -, and be at most 64 bytes. Description attributes override doc comments. Unknown or duplicate options and parameter names are rejected.

Non-Option arguments are required by default. An explicit required list makes omitted arguments default through serde, requiring Default. Listing an Option argument as required is rejected.

A generated tool runs on JSON arguments in one call through rig_core::serve::adapters::ToolAdapter::run (or run_with to pass a context and read what the tool published).

use rig_core::serve::adapters::ToolAdapter;
use rig_derive::rig_tool;

#[rig_tool(description = "Add integers", params(a = "First operand"), required(a))]
fn add(a: i32, b: i32) -> Result<i32, rig_core::tool::ToolExecutionError> {
    Ok(a + b)
}

let runtime = tokio::runtime::Builder::new_current_thread().build()?;
let output = runtime.block_on(ToolAdapter::new(Add).run(serde_json::json!({ "a": 2, "b": 3 })))?;
assert_eq!(output.as_json(), Some(&serde_json::json!(5)));