Skip to main content

ToolRegistry

Struct ToolRegistry 

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

Registry of available tools for the agent loop.

Implementations§

Source§

impl ToolRegistry

Source

pub fn new() -> Self

Create a new empty registry.

Source

pub fn with_cache(self, cache: Arc<dyn ToolCache>) -> Self

Attach a tool result cache.

Source

pub fn register(&mut self, spec: ToolSpec)

Register a tool. Overwrites any existing tool with the same name.

Source

pub fn register_tools(&mut self, specs: impl IntoIterator<Item = ToolSpec>)

Register multiple tools at once.

Equivalent to calling register for each spec in order. Duplicate names overwrite earlier entries.

Source

pub fn with_tool(self, spec: ToolSpec) -> Self

Fluent builder: register a tool and return self.

Allows chaining multiple registrations:

use llm_agent_runtime::agent::{ToolRegistry, ToolSpec};

let registry = ToolRegistry::new()
    .with_tool(ToolSpec::new("search", "Search", |args| args.clone()))
    .with_tool(ToolSpec::new("calc", "Calculate", |args| args.clone()));
Source

pub async fn call( &self, name: &str, args: Value, ) -> Result<Value, AgentRuntimeError>

Call a tool by name.

§Errors
  • AgentRuntimeError::AgentLoop — tool not found, required field missing, or custom validator rejected the arguments
  • AgentRuntimeError::CircuitOpen — the tool’s circuit breaker is open (only possible when the orchestrator feature is enabled)
Source

pub fn get(&self, name: &str) -> Option<&ToolSpec>

Look up a registered tool by name. Returns None if not registered.

Source

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

Return true if a tool with the given name is registered.

Source

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

Remove a tool by name. Returns true if the tool was registered.

Source

pub fn tool_names(&self) -> Vec<&str>

Return the list of registered tool names.

Source

pub fn tool_names_owned(&self) -> Vec<String>

Return all registered tool names as owned Strings.

Unlike tool_names this does not borrow self, making the result usable after the registry is moved or mutated.

Source

pub fn all_tool_names(&self) -> Vec<String>

Return all registered tool names sorted alphabetically.

Useful for deterministic output in help text, diagnostics, or tests.

Source

pub fn tool_specs(&self) -> Vec<&ToolSpec>

Return references to all registered ToolSpecs.

Source

pub fn filter_tools<F: Fn(&ToolSpec) -> bool>(&self, pred: F) -> Vec<&ToolSpec>

Return references to all ToolSpecs that satisfy the given predicate.

§Example
let registry = ToolRegistry::new();
let long_desc: Vec<_> = registry.filter_tools(|s| s.description.len() > 20);
Source

pub fn rename_tool( &mut self, old_name: &str, new_name: impl Into<String>, ) -> bool

Rename a registered tool from old_name to new_name.

The tool’s name field and its registry key are both updated. Returns true if the tool was found and renamed, false if old_name is not registered. If new_name is already registered, it is overwritten.

Source

pub fn tool_count(&self) -> usize

Return the number of registered tools.

Source

pub fn is_empty(&self) -> bool

Return true if no tools are registered.

Source

pub fn clear(&mut self)

Remove all registered tools.

Useful for resetting the registry between test runs or for dynamic agent reconfiguration.

Source

pub fn remove(&mut self, name: &str) -> Option<ToolSpec>

Remove a tool from the registry by name.

Returns Some(spec) if the tool was registered, None if not found.

Source

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

Return true if a tool with the given name is registered.

Source

pub fn descriptions(&self) -> Vec<(&str, &str)>

Return (name, description) pairs for all registered tools, sorted by name.

Useful for generating help text or logging the tool set at startup.

Source

pub fn find_by_description_keyword(&self, keyword: &str) -> Vec<&ToolSpec>

Return references to all tool specs whose description contains keyword (case-insensitive).

Source

pub fn tool_count_with_required_fields(&self) -> usize

Return the number of tools that have at least one required field.

Source

pub fn tool_count_with_validators(&self) -> usize

Return the number of registered tools that have at least one attached validator.

Complements tool_count_with_required_fields; a tool can have validators without any required field declarations (for cross-field or range checks).

Source

pub fn names(&self) -> Vec<&str>

Return the names of all registered tools, sorted alphabetically.

Source

pub fn tool_names_starting_with(&self, prefix: &str) -> Vec<&str>

Return the names of all registered tools whose name starts with prefix, sorted alphabetically.

Source

pub fn description_for(&self, name: &str) -> Option<&str>

Return the description of the tool with the given name, or None if no such tool is registered.

Source

pub fn count_with_description_containing(&self, keyword: &str) -> usize

Return the count of tools whose description contains keyword (case-insensitive).

Source

pub fn unregister_all(&mut self)

Remove all registered tools.

Source

pub fn names_containing(&self, substring: &str) -> Vec<&str>

Return a sorted list of tool names that contain substring (case-insensitive).

Source

pub fn shortest_description(&self) -> Option<&str>

Return the description of the tool with the shortest description string.

Returns None if the registry is empty.

Source

pub fn longest_description(&self) -> Option<&str>

Return the description of the tool with the longest description string.

Returns None if the registry is empty.

Source

pub fn all_descriptions(&self) -> Vec<&str>

Return a sorted list of all tool descriptions.

Source

pub fn tool_names_with_keyword(&self, keyword: &str) -> Vec<&str>

Return the names of tools whose description contains keyword (case-insensitive).

Source

pub fn avg_description_length(&self) -> f64

Return the mean byte length of all tool descriptions.

Returns 0.0 if the registry is empty.

Source

pub fn tool_names_sorted(&self) -> Vec<&str>

Return all registered tool names in ascending sorted order.

Source

pub fn description_contains_count(&self, keyword: &str) -> usize

Return the count of tools whose description contains keyword (case-insensitive).

Source

pub fn total_description_bytes(&self) -> usize

Return the total byte length of all tool description strings combined.

Useful as a rough measure of how much context the tool registry will contribute to a prompt when all descriptions are serialized together.

Source

pub fn shortest_description_length(&self) -> usize

Return the byte length of the shortest tool description, or 0 if the registry is empty.

Source

pub fn longest_description_length(&self) -> usize

Return the byte length of the longest tool description, or 0 if the registry is empty.

Source

pub fn tool_count_above_desc_bytes(&self, min_bytes: usize) -> usize

Return the count of tools whose description byte length is strictly greater than min_bytes.

Returns 0 for an empty registry or when no description exceeds min_bytes.

Source

pub fn tools_with_required_field(&self, field: &str) -> Vec<&ToolSpec>

Return references to all ToolSpecs that list field in their required_fields.

Returns an empty Vec when no tools declare field as required.

Source

pub fn tools_without_required_fields(&self) -> Vec<&ToolSpec>

Return references to all ToolSpecs that have no required fields.

Returns an empty Vec when every registered tool declares at least one required field, or when the registry is empty.

Source

pub fn avg_required_fields_count(&self) -> f64

Return the average number of required fields per registered tool.

Returns 0.0 for an empty registry.

Source

pub fn tool_descriptions_total_words(&self) -> usize

Return the total word count across all tool descriptions.

Words are split on ASCII whitespace. Returns 0 for an empty registry or when all descriptions are blank.

Source

pub fn has_tools_with_empty_descriptions(&self) -> bool

Return true if any registered tool has a blank description.

A blank description is one that is empty or contains only whitespace. Returns false for an empty registry (vacuously no blank descriptions).

Source

pub fn total_required_fields(&self) -> usize

Return the total number of required fields across all registered tools.

Sums the required_fields lengths of every ToolSpec. Returns 0 for an empty registry or when no tool has required fields.

Source

pub fn has_tool_with_description_containing(&self, keyword: &str) -> bool

Return true if at least one registered tool’s description contains keyword (case-sensitive substring search).

Source

pub fn tools_with_description_longer_than(&self, min_bytes: usize) -> Vec<&str>

Return tool names whose description byte length exceeds min_bytes, sorted.

Returns an empty Vec for an empty registry or when no description exceeds the threshold.

Source

pub fn max_description_bytes(&self) -> usize

Return the byte length of the longest tool description, or 0 when empty.

Source

pub fn min_description_bytes(&self) -> usize

Return the byte length of the shortest tool description, or 0 when empty.

Source

pub fn description_starts_with_any(&self, prefixes: &[&str]) -> bool

Return true if any tool description starts with any of the given prefixes.

Useful for checking whether a set of common description templates is in use (e.g. "Search", "Write", "Read"). Returns false for an empty registry or empty prefixes slice.

Source

pub fn tool_with_most_required_fields(&self) -> Option<&ToolSpec>

Return a reference to the ToolSpec with the most required fields.

When multiple tools share the maximum required-field count, the one that sorts first alphabetically by name is returned for deterministic output. Returns None for an empty registry.

Source

pub fn tool_by_name(&self, name: &str) -> Option<&ToolSpec>

Return a reference to the ToolSpec with the given name, or None.

Source

pub fn tools_without_validators(&self) -> Vec<&str>

Return the names of all tools that have no validators, sorted alphabetically.

Complements tool_count_with_validators by returning the actual names. Returns an empty Vec for an empty registry.

Source

pub fn tool_names_with_required_fields(&self) -> Vec<&str>

Return the names of all tools that have at least one required field, sorted alphabetically.

Returns an empty Vec when no tools have required fields or the registry is empty.

Source

pub fn has_all_tools(&self, names: &[&str]) -> bool

Return true if all of the given names are registered in this registry.

Returns true for an empty names slice (vacuously true).

Source

pub fn tools_with_required_fields_count(&self) -> usize

Return the number of tools that have at least one required field defined.

Returns 0 for an empty registry.

Source

pub fn tool_names_with_prefix<'a>(&'a self, prefix: &str) -> Vec<&'a str>

Return the names of all tools whose name starts with prefix, sorted alphabetically.

Returns an empty Vec for an empty registry or when no tool matches.

Trait Implementations§

Source§

impl Debug for ToolRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ToolRegistry

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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<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