Tokitai Macros
Procedural macros for Tokitai - Zero-dependency macro for AI tool integration
This crate provides the #[tool] procedural macro that enables compile-time tool definitions
for AI/LLM tool calling systems. It generates all the boilerplate code needed to expose
your Rust functions as AI-callable tools.
🎯 Key Features
- Zero Runtime Dependencies - The macro itself has no runtime overhead
- Compile-time Generation - Tool definitions are generated at compile time
- Type Safety - Parameter validation happens at compile time
- Automatic Discovery - Mark an
implblock and allpubmethods become tools - Customizable - Override tool names and descriptions via attributes
- Vendor Neutral - Works with any AI/LLM provider (Ollama, OpenAI, Anthropic, etc.)
Quick Start
Add to your Cargo.toml:
[]
= "0.3"
Then use the #[tool] macro:
use tool;
;
// Usage
let calc = Calculator;
// Get tool definitions (compile-time generated)
let tools = tool_definitions;
println!;
// Call a tool
let result = calc.call_tool.unwrap;
println!; // 30
How It Works
The #[tool] macro automatically:
- Extracts doc comments as tool descriptions
- Generates JSON Schema for parameters from Rust types
- Creates
TOOL_DEFINITIONSconstant with all tool metadata - Implements
call_tooldispatcher for runtime invocation - Generates parameter parsing and validation code
Customization
You can customize tool names and descriptions using the attribute syntax:
Generated Code
For each #[tool] impl block, the macro generates:
// 1. Tool definitions constant
// 2. ToolProvider trait implementation
// 3. call_tool dispatcher
Type Mapping
Rust types are automatically mapped to JSON Schema types:
| Rust Type | JSON Schema Type |
|---|---|
String, str |
string |
i8, i16, i32, i64, u8, u16, u32, u64 |
integer |
f32, f64 |
number |
bool |
boolean |
Vec<T> |
array |
| Custom structs | object |
Requirements
- Rust Version: 1.70+
- Edition: 2021
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Contributing
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See Also
tokitai- Main crate with runtime supporttokitai-core- Core types and traits