pub struct ToolRegistry<Ctx> { /* private fields */ }Expand description
Implementations§
Source§impl<Ctx> ToolRegistry<Ctx>
impl<Ctx> ToolRegistry<Ctx>
pub fn new() -> ToolRegistry<Ctx>
Sourcepub fn register<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: Tool<Ctx> + 'static,
pub fn register<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: Tool<Ctx> + 'static,
Register a synchronous tool in the registry.
The tool’s name is converted to a string via serde serialization and used as the lookup key.
Sourcepub fn register_simple<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: SimpleTool<Ctx> + 'static,
pub fn register_simple<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: SimpleTool<Ctx> + 'static,
Register a SimpleTool — a tool whose name is a plain &str and
which needs no ToolName type.
The tool is wrapped in a SimpleToolAdapter (giving it
Name = DynamicToolName) and registered like any other Tool.
This is the lowest-ceremony way to add a first custom tool.
Sourcepub fn register_typed<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: TypedTool<Ctx> + 'static,
pub fn register_typed<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: TypedTool<Ctx> + 'static,
Register a TypedTool — a tool whose model-emitted arguments are
deserialized into a typed TypedTool::Input and validated before
execute runs.
The tool is wrapped in a TypedToolAdapter (giving it
Name = DynamicToolName) and registered like any other Tool. A
malformed tool call is turned into a structured validation-error
ToolResult at the dispatch boundary so the model can self-correct;
execute is never reached with invalid arguments.
Sourcepub fn register_async<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: AsyncTool<Ctx> + 'static,
pub fn register_async<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: AsyncTool<Ctx> + 'static,
Register an async tool in the registry.
Async tools have two phases: execute (lightweight, starts operation)
and check_status (streams progress until completion).
Sourcepub fn register_listen<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: ListenExecuteTool<Ctx> + 'static,
pub fn register_listen<T>(&mut self, tool: T) -> &mut ToolRegistry<Ctx>where
T: ListenExecuteTool<Ctx> + 'static,
Register a listen/execute tool in the registry.
Listen/execute tools start by streaming updates via listen(), then run
final execution with execute() once confirmed.
Sourcepub fn get(&self, name: &str) -> Option<&Arc<dyn ErasedTool<Ctx>>>
pub fn get(&self, name: &str) -> Option<&Arc<dyn ErasedTool<Ctx>>>
Get a synchronous tool by name.
Sourcepub fn get_async(&self, name: &str) -> Option<&Arc<dyn ErasedAsyncTool<Ctx>>>
pub fn get_async(&self, name: &str) -> Option<&Arc<dyn ErasedAsyncTool<Ctx>>>
Get an async tool by name.
Sourcepub fn get_listen(&self, name: &str) -> Option<&Arc<dyn ErasedListenTool<Ctx>>>
pub fn get_listen(&self, name: &str) -> Option<&Arc<dyn ErasedListenTool<Ctx>>>
Get a listen/execute tool by name.
Sourcepub fn is_listen(&self, name: &str) -> bool
pub fn is_listen(&self, name: &str) -> bool
Check if a tool name refers to a listen/execute tool.
Sourcepub fn all(&self) -> impl Iterator<Item = &Arc<dyn ErasedTool<Ctx>>>
pub fn all(&self) -> impl Iterator<Item = &Arc<dyn ErasedTool<Ctx>>>
Get all registered synchronous tools.
Sourcepub fn all_async(&self) -> impl Iterator<Item = &Arc<dyn ErasedAsyncTool<Ctx>>>
pub fn all_async(&self) -> impl Iterator<Item = &Arc<dyn ErasedAsyncTool<Ctx>>>
Get all registered async tools.
Sourcepub fn all_listen(
&self,
) -> impl Iterator<Item = &Arc<dyn ErasedListenTool<Ctx>>>
pub fn all_listen( &self, ) -> impl Iterator<Item = &Arc<dyn ErasedListenTool<Ctx>>>
Get all registered listen/execute tools.
Sourcepub fn to_llm_tools(&self) -> Vec<Tool>
pub fn to_llm_tools(&self) -> Vec<Tool>
Convert all tools (sync + async + listen) to LLM tool definitions. The output is sorted by tool name so the order is deterministic across builds and across calls.
Determinism matters for prompt caching. Anthropic’s
cache_control: ephemeral keys on the byte content of the
system + tool list. Anything that perturbs the order of the
tool list invalidates the cache. The three backing maps are
HashMaps, whose values() order is randomized (DoS-safe
RandomState by default), so two consecutive turns with the
same registered tool set were producing different orderings
and silently zeroing the cache hit rate.
Sorting by name is the cheapest fix that holds across insertion order, internal map type changes, and concurrent builds. The tool count is small (tens, not thousands) so the sort cost is negligible compared to a single LLM call.
Trait Implementations§
Source§impl<Ctx> Clone for ToolRegistry<Ctx>
impl<Ctx> Clone for ToolRegistry<Ctx>
Source§fn clone(&self) -> ToolRegistry<Ctx>
fn clone(&self) -> ToolRegistry<Ctx>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more