Skip to main content

ToolRegistry

Struct ToolRegistry 

Source
pub struct ToolRegistry { /* private fields */ }
Available on crate feature async only.
Expand description

In-memory registry of tools keyed by name.

Names must be unique; registering twice with the same name replaces the existing entry. Use Self::contains to check first when overwrite is undesired.

Implementations§

Source§

impl ToolRegistry

Source

pub fn new() -> Self

An empty registry.

Source

pub fn register_tool<T: Tool>(&mut self, tool: T) -> &mut Self

Register a value that implements Tool directly. Useful for tools that have their own state or non-trivial logic worth giving a dedicated type.

Source

pub fn register<F, Fut>( &mut self, name: impl Into<String>, schema: Value, handler: F, ) -> &mut Self
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,

Register a closure-based tool. The closure receives the model’s raw input as a serde_json::Value and returns the tool result. Use ToolError::invalid_input for input-shape failures and ToolError::execution to wrap any other error type.

§Example
use claude_api::tool_dispatch::ToolRegistry;
use serde_json::json;

let mut registry = ToolRegistry::new();
registry.register(
    "echo",
    json!({"type": "object", "properties": {"text": {"type": "string"}}}),
    |input| async move { Ok(input) },
);
assert!(registry.contains("echo"));
Source

pub fn register_described<F, Fut>( &mut self, name: impl Into<String>, description: impl Into<String>, schema: Value, handler: F, ) -> &mut Self
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,

Like Self::register but also attaches a description.

Source

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

Borrow a registered tool by name.

Source

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

Whether a tool with the given name is registered.

Source

pub fn len(&self) -> usize

Number of registered tools.

Source

pub fn is_empty(&self) -> bool

Whether the registry is empty.

Source

pub fn names(&self) -> impl Iterator<Item = &str>

Iterator over registered tool names.

Source

pub fn to_messages_tools(&self) -> Vec<MessagesTool>

Build the Vec<MessagesTool> you pass to CreateMessageRequestBuilder::tools. Includes name, description, and schema for every registered tool.

Source

pub async fn dispatch( &self, name: &str, input: Value, ) -> Result<Value, ToolError>

Look up a tool by name and invoke it with the given input.

Returns ToolError::Unknown if no tool by that name is registered. Other errors are propagated from the tool’s invoke impl.

Source§

impl ToolRegistry

Source

pub fn register_typed<A, F, Fut>( &mut self, name: impl Into<String>, handler: F, ) -> &mut Self
where A: JsonSchema + DeserializeOwned + Send + Sync + 'static, F: Fn(A) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,

Available on crate feature schemars-tools only.

Register a tool with a typed input struct.

The schema is generated from A via schemars, and the model’s raw Value input is deserialized into A before the handler runs. Deserialization failures surface as ToolError::InvalidInput so the model can self-correct.

Source

pub fn register_typed_described<A, F, Fut>( &mut self, name: impl Into<String>, description: impl Into<String>, handler: F, ) -> &mut Self
where A: JsonSchema + DeserializeOwned + Send + Sync + 'static, F: Fn(A) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,

Available on crate feature schemars-tools only.

Like Self::register_typed but also attaches a description.

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() -> ToolRegistry

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
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, 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