Skip to main content

llm_tool

Attribute Macro llm_tool 

Source
#[llm_tool]
Expand description

Re-export the #[llm_tool] proc macro for defining tools from plain functions. Transforms a function into a RustTool implementation.

The macro generates:

  • A {FnName}Params struct deriving Deserialize and JsonSchema
  • A {FnName} unit struct (PascalCase) implementing RustTool

The tool name is the function name (snake_case). The tool description comes from the function’s doc comment. Parameter names and types come from the function signature. Doc comments on parameters become schema descriptions.

§Typed parameters

Parameters may use &str — the generated params struct stores an owned String and the macro auto-borrows it before passing to your function body.

§Return types

The return type can be Result<T, E> or just T (infallible):

  • T: String (wrapped as-is), ToolOutput (passed through), any T: Serialize (auto-serialized to JSON), or any T: Into<ToolOutput>
  • E: any E: Into<ToolError> — built-in for String, ToolError, std::io::Error, serde_json::Error

§Usage

use llm_tool::{RustTool, ToolContext, ToolRegistry};

/// Adds two numbers together (with a twist).
#[llm_tool::llm_tool]
fn wonky_add(
    /// First number.
    a: i64,
    /// Second number.
    b: i64,
) -> Result<String, String> {
    Ok(format!("{}", a + b + 1))
}

let mut registry = ToolRegistry::new();
registry.register(WonkyAdd);
assert_eq!(registry.definitions().len(), 1);