pub struct ToolRegistry { /* private fields */ }Implementations§
Source§impl ToolRegistry
impl ToolRegistry
Sourcepub fn register<T: RustTool + 'static>(&mut self, tool: T) -> &mut Self
pub fn register<T: RustTool + 'static>(&mut self, tool: T) -> &mut Self
Register a RustTool. Returns &mut Self for chaining.
If a tool with the same name was already registered, it is replaced.
Sourcepub fn with_tool<T: RustTool + 'static>(self, tool: T) -> Self
pub fn with_tool<T: RustTool + 'static>(self, tool: T) -> Self
Register a RustTool, consuming and returning Self for owned chaining.
This is the owned counterpart of register, enabling
patterns like:
use llm_tool::{RustTool, ToolContext, ToolError, ToolOutput, ToolRegistry};
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Deserialize, JsonSchema)]
struct NoParams {}
struct ToolA;
impl RustTool for ToolA {
type Params = NoParams;
const NAME: &'static str = "tool_a";
const DESCRIPTION: &'static str = "Tool A";
async fn call(&self, _: NoParams, _: &ToolContext) -> Result<ToolOutput, ToolError> {
Ok("a".into())
}
}
struct ToolB;
impl RustTool for ToolB {
type Params = NoParams;
const NAME: &'static str = "tool_b";
const DESCRIPTION: &'static str = "Tool B";
async fn call(&self, _: NoParams, _: &ToolContext) -> Result<ToolOutput, ToolError> {
Ok("b".into())
}
}
let registry = ToolRegistry::new().with_tool(ToolA).with_tool(ToolB);
assert_eq!(registry.definitions().len(), 2);Sourcepub fn definitions(&self) -> Vec<ToolDefinition>
pub fn definitions(&self) -> Vec<ToolDefinition>
Collect ToolDefinitions for all registered tools.
Logs a warning and skips any tool whose schema serialization fails.
Sourcepub async fn dispatch(
&self,
name: &str,
args: Value,
ctx: &ToolContext,
) -> Result<ToolOutput, ToolError>
pub async fn dispatch( &self, name: &str, args: Value, ctx: &ToolContext, ) -> Result<ToolOutput, ToolError>
Dispatch a tool call by name with raw JSON arguments and a context.
§Errors
Returns Err if the tool name is unknown or the handler returns an error.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&String, ToolDefinition)> + '_
pub fn iter(&self) -> impl Iterator<Item = (&String, ToolDefinition)> + '_
Iterate over (name, definition) pairs for every registered tool.
Logs a warning and skips tools whose schema serialization fails.
Trait Implementations§
Source§impl Debug for ToolRegistry
impl Debug for ToolRegistry
Source§impl Default for ToolRegistry
impl Default for ToolRegistry
Source§impl<'a> IntoIterator for &'a ToolRegistry
Iterate over (name, definition) pairs for every registered tool.
impl<'a> IntoIterator for &'a ToolRegistry
Iterate over (name, definition) pairs for every registered tool.
Yields (&String, ToolDefinition) for each tool in the registry.