Skip to main content

ToolRegistry

Struct ToolRegistry 

Source
pub struct ToolRegistry<Ctx> { /* private fields */ }
Expand description

Registry of available tools.

Tools are stored with their names erased to allow different Name types in the same registry. The registry uses string-based lookup for LLM compatibility.

Supports both synchronous Tools and asynchronous AsyncTools.

Implementations§

Source§

impl<Ctx> ToolRegistry<Ctx>
where Ctx: Send + Sync + 'static,

Source

pub fn new() -> ToolRegistry<Ctx>

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn get(&self, name: &str) -> Option<&Arc<dyn ErasedTool<Ctx>>>

Get a synchronous tool by name.

Source

pub fn get_async(&self, name: &str) -> Option<&Arc<dyn ErasedAsyncTool<Ctx>>>

Get an async tool by name.

Source

pub fn get_listen(&self, name: &str) -> Option<&Arc<dyn ErasedListenTool<Ctx>>>

Get a listen/execute tool by name.

Source

pub fn is_async(&self, name: &str) -> bool

Check if a tool name refers to an async tool.

Source

pub fn is_listen(&self, name: &str) -> bool

Check if a tool name refers to a listen/execute tool.

Source

pub fn all(&self) -> impl Iterator<Item = &Arc<dyn ErasedTool<Ctx>>>

Get all registered synchronous tools.

Source

pub fn all_async(&self) -> impl Iterator<Item = &Arc<dyn ErasedAsyncTool<Ctx>>>

Get all registered async tools.

Source

pub fn all_listen( &self, ) -> impl Iterator<Item = &Arc<dyn ErasedListenTool<Ctx>>>

Get all registered listen/execute tools.

Source

pub fn len(&self) -> usize

Get the number of registered tools (sync + async).

Source

pub fn is_empty(&self) -> bool

Check if the registry is empty.

Source

pub fn filter<F>(&mut self, predicate: F)
where F: Fn(&str) -> bool,

Filter tools by a predicate.

Removes tools for which the predicate returns false. The predicate receives the tool name. Applies to both sync and async tools.

§Example
registry.filter(|name| name != "bash");
Source

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>

Source§

fn clone(&self) -> ToolRegistry<Ctx>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Ctx> Default for ToolRegistry<Ctx>
where Ctx: Send + Sync + 'static,

Source§

fn default() -> ToolRegistry<Ctx>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Ctx> Freeze for ToolRegistry<Ctx>

§

impl<Ctx> !RefUnwindSafe for ToolRegistry<Ctx>

§

impl<Ctx> Send for ToolRegistry<Ctx>

§

impl<Ctx> Sync for ToolRegistry<Ctx>

§

impl<Ctx> Unpin for ToolRegistry<Ctx>

§

impl<Ctx> UnsafeUnpin for ToolRegistry<Ctx>

§

impl<Ctx> !UnwindSafe for ToolRegistry<Ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,