Tools
Tools give LLMs the ability to take actions in the world -- calling APIs, querying databases, performing calculations, or any other side effect. Synaptic provides a complete tool system built around the Tool trait defined in synaptic-core.
Key Components
| Component | Crate | Description |
|---|---|---|
Tool trait | synaptic-core | The interface every tool must implement: name(), description(), and call() |
ToolRegistry | synaptic-tools | Thread-safe collection of registered tools (Arc<RwLock<HashMap>>) |
SerialToolExecutor | synaptic-tools | Dispatches tool calls by name through the registry |
ToolNode | synaptic-graph | Graph node that executes tool calls from AI messages in a state machine workflow |
ToolDefinition | synaptic-core | Schema description sent to the model so it knows what tools are available |
ToolChoice | synaptic-core | Controls whether and how the model selects tools |
How It Works
- You define tools using the
#[tool]macro (or by implementing theTooltrait manually). - Register them in a
ToolRegistry. - Convert them to
ToolDefinitionvalues and attach them to aChatRequestso the model knows what tools are available. - When the model responds with
ToolCallentries, dispatch them throughSerialToolExecutorto get results. - Send the results back to the model as
Message::tool(...)messages to continue the conversation.
Quick Example
use serde_json::{json, Value};
use synaptic::macros::tool;
use synaptic::core::SynapticError;
use synaptic::tools::{ToolRegistry, SerialToolExecutor};
/// Add two numbers.
#[tool]
async fn add(
/// First number
a: f64,
/// Second number
b: f64,
) -> Result<Value, SynapticError> {
Ok(json!({"result": a + b}))
}
let registry = ToolRegistry::new();
registry.register(add())?; // add() returns Arc<dyn Tool>
let executor = SerialToolExecutor::new(registry);
let result = executor.execute("add", json!({"a": 3, "b": 4})).await?;
assert_eq!(result, json!({"result": 7.0}));
Sub-Pages
- Custom Tools -- implement the
Tooltrait for your own tools - Tool Registry -- register, look up, and execute tools
- Tool Choice -- control how the model selects tools with
ToolChoice - Tool Definition Extras -- attach provider-specific parameters to tool definitions
- Runtime-Aware Tools -- tools that access graph state, store, and runtime context