llm-tool
Framework-agnostic Rust tool definitions for LLM agents.
Define strongly-typed tools with the #[llm_tool] attribute macro and register
them in a ToolRegistry. The registry produces framework-agnostic
ToolDefinitions (name, description, JSON Schema) — no coupling to any
specific SDK or agent runtime.
Quick start
Add llm-tool to your Cargo.toml:
[]
= "0.1"
Defining a tool with #[llm_tool]
The easiest way to create a tool is the #[llm_tool] attribute macro.
It generates a params struct and a RustTool implementation from a plain
function:
use ;
/// Adds two numbers together.
// The macro generates an `Add` struct (PascalCase of the fn name).
let registry = new.with_tool;
let defs = registry.definitions;
assert_eq!;
assert_eq!;
Rules for #[llm_tool] functions:
- Must have a doc comment (becomes the tool description),
#[llm_tool(description = "inline text")]for an inline override, or#[llm_tool(template = "path.tmpl.md")]with theprompt-templatesfeature. - Every parameter must have a doc comment (becomes the JSON Schema description for that field).
- Return type can be
Result<T, E>or a bareT(infallible tools):TisString(auto-wrapped intoToolOutput),ToolOutput(passed through), anyT: Serialize(auto-serialized to JSON), or anyT: Into<ToolOutput>.Eis anyE: Into<ToolError>— built-in conversions exist forToolError,String,std::io::Error,serde_json::Error, andBox<dyn Error + Send + Sync>.
- Can be
async fn— the generatedRustTool::callis always async. &strparameters are accepted — the generated struct storesStringand auto-borrows.Option<T>parameters automatically get#[serde(default)], so they are omitted from the JSON Schemarequiredarray.- A
&ToolContextparameter is recognized as the execution context and forwarded from the registry — it is not included in the params struct.
Returning ToolOutput with metadata
Tools can return a ToolOutput directly, attaching structured metadata
for hooks, policies, and logging pipelines. Metadata is never sent to
the model — only the content string is. ToolError supports metadata
for error diagnostics the same way:
use ;
/// Runs a shell command and returns its stdout.
The ? operator — zero-boilerplate error handling
ToolError implements From<std::io::Error>, From<serde_json::Error>,
and From<Box<dyn Error + Send + Sync>>, so the ? operator works
without manual .map_err():
use ;
/// Reads a file from disk.
async
# block_on;
Infallible tools — no Result needed
Tools that can never fail can return a bare type instead of Result:
use ;
/// Returns a friendly greeting.
let registry = new.with_tool;
# block_on;
Structured return types
There are several ways to return structured data from a tool:
| Approach | Return type | Use when |
|---|---|---|
| Auto-serialize | Result<T: Serialize, E> |
Fallible tool with a struct result |
ToolOutput::json() |
Result<ToolOutput, ToolError> |
Need metadata or explicit error handling |
Json<T> |
Json<T> |
Infallible tool with a struct result |
Auto-serialized — any T: Serialize returned from a tool is
automatically serialized to JSON by the macro:
use ;
use Serialize;
/// Returns metadata about a file.
Json<T> — for infallible tools returning a serializable struct:
use ;
use Serialize;
/// Computes statistics.
Custom From<T> for ToolOutput
Implement From<YourType> for ToolOutput for domain types that should
convert directly into tool output, then call .into() in the tool body:
use ;
;
/// Renders documentation as Markdown.
Async tools
Async functions work out of the box — the generated RustTool::call is
always async regardless. See the read_file example above.
Optional parameters
Option<T> fields are not required in the JSON the model sends:
use ;
/// Greets someone.
Accessing ToolContext
If your tool needs the conversation ID or shared state, accept a
&ToolContext parameter. It is automatically wired by the registry and
excluded from the generated params struct:
use ;
/// Returns the current conversation ID.
Template Descriptions (feature: prompt-templates)
Instead of writing tool descriptions as doc comments, you can write them in
.tmpl.md template files using the prompt-templates crate syntax.
Enable the feature in your Cargo.toml:
[]
= { = "0.1", = ["prompt-templates"] }
= "0.1"
Static descriptions
Store the description in a markdown template file:
tools/get_weather.tmpl.md:
name: get_weather
params: []
Fetch the current weather for any city worldwide.
Reference it with template = "...":
#[llm_tool(template = "tools/get_weather.tmpl.md")]
fn get_weather(
/// The city to look up.
city: String,
) -> Result<String, ToolError> {
Ok(format!("Weather for {city}"))
}
Doc comments are optional when using template = "..." or description = "...".
Compile-time params
Templates can declare parameters that are rendered at compile time — zero runtime cost:
tools/weather_api.tmpl.md:
name: weather_api
params:
- -
Weather lookup (API {{ api_version }}, {{ env_name }} environment).
#[llm_tool(
template = "tools/weather_api.tmpl.md",
params(api_version = "v3.1", env_name = "production")
)]
fn get_weather(
/// The city to look up.
city: String,
) -> Result<String, ToolError> {
Ok(format!("Weather for {city}"))
}
The macro validates that all declared template variables are provided and that no extra keys are passed — at compile time.
Dynamic descriptions
For values that aren't known until runtime, use a context function:
fn weather_context(_tool: &GetWeatherDynamic) -> prompt_templates::Context {
let mut ctx = prompt_templates::Context::new();
ctx.set("api_version", "v3.1");
ctx.set("env_name", "production");
ctx
}
#[llm_tool(
template = "tools/dynamic_desc.tmpl.md",
context = weather_context
)]
fn get_weather_dynamic(
/// The city to look up.
city: String,
) -> Result<String, ToolError> {
Ok(format!("Weather for {city}"))
}
Inline descriptions
For short descriptions, skip the template file entirely:
#[llm_tool(description = "Look up the current weather for a city.")]
fn get_weather(
/// The city name.
city: String,
) -> Result<String, ToolError> {
Ok(format!("Weather for {city}"))
}
| Attribute form | Cost | Feature |
|---|---|---|
#[llm_tool] + doc comment |
Zero | — |
description = "inline text" |
Zero | — |
template = "path.tmpl.md" |
Zero | prompt-templates |
template = "...", params(k = "v") |
Zero | prompt-templates |
template = "...", context = fn |
Runtime | prompt-templates |
| Behaviour | Detail |
|---|---|
| Context receiver | The context function receives &Self (the tool struct) and returns a prompt_templates::Context. |
| Rendering | description() renders the template at runtime with the provided context. |
| Caching | Templates are parsed once (via LazyLock) and cached for zero-cost repeated calls. |
| Missing params | If the template declares parameters but neither params(...) nor context = ... is provided, compile error. |
| Mutual exclusion | params(...) and context = ... are mutually exclusive. description and template are mutually exclusive. |
| Fallback const | The DESCRIPTION const still holds the raw template body (or rendered text) as a fallback. |
Why use template descriptions?
- Keep source clean — edit markdown, not Rust, for long descriptions.
- Share descriptions across tools or embed them in external documentation.
- Dynamic adaptation — descriptions can reflect runtime config (API versions, environments, feature flags).
- Compile-time validation — missing files, malformed templates, and
missing params are caught during
cargo build. - Auto-rebuild —
cargore-compiles when template files change.
Manual RustTool implementation
For full control, implement RustTool directly:
use ;
use Deserialize;
;
ToolRegistry
The registry stores tools and provides two operations:
definitions()— returnsVec<ToolDefinition>with the name, description, and JSON Schema for each tool. Feed these to your framework.dispatch(name, args, ctx)— deserializes JSON args, calls the tool, and returns aToolOutputor aToolError.
use ;
/// Echoes its input.
# block_on;
Plugging into any agent framework
llm-tool is deliberately framework-agnostic. To integrate with a new
framework:
- Register your tools in a
ToolRegistry. - Extract definitions via
registry.definitions()— eachToolDefinitionhas.name,.description, and.parameter_schema(aserde_json::Valuecontaining a JSON Schema object). - Convert
ToolDefinitions into whatever format your framework expects (e.g.OpenAIfunction-calling JSON, Anthropic tool-use blocks, GeminiFunctionDeclarations, etc.). Theparameter_schemais standard JSON Schema (draft 7, with nullable arrays already sanitized to scalar"type"strings for Go genai compatibility). - On tool call, extract the tool name and JSON arguments from your
framework's response, then call
registry.dispatch(name, args, &ctx). - Return the result (or error message) to the model as the tool response.
Minimal integration sketch:
use ;
async
# let registry = new;
# send_definitions_to_model;
# block_on;
Key types
| Type | Description |
|---|---|
RustTool |
Trait for implementing a tool with typed parameters. |
ToolRegistry |
Registry for storing and dispatching tools by name. |
ToolDefinition |
Serializable metadata (name, description, JSON Schema). |
ToolContext |
Execution context with conversation state and shared key-value store. |
ToolOutput |
Structured return value (content + metadata) from tool execution. |
ToolError |
Error type with From impls for io::Error, serde_json::Error. |
Json<T> |
Wrapper for infallible serialization of T: Serialize into output. |
EmptyParams |
Convenience struct for tools that take no parameters. |
#[llm_tool] |
Proc-macro attribute for defining tools from plain functions. |
| Feature | Description |
|---|---|
prompt-templates |
Enables .tmpl.md template descriptions via the prompt-templates crate. |
License
Dual-licensed under Apache-2.0 OR MIT.