pub struct ToolRegistry { /* private fields */ }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
impl ToolRegistry
Sourcepub fn register_tool<T: Tool>(&mut self, tool: T) -> &mut Self
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.
Sourcepub fn register<F, Fut>(
&mut self,
name: impl Into<String>,
schema: Value,
handler: F,
) -> &mut Self
pub fn register<F, Fut>( &mut self, name: impl Into<String>, schema: Value, handler: F, ) -> &mut Self
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"));Sourcepub fn register_described<F, Fut>(
&mut self,
name: impl Into<String>,
description: impl Into<String>,
schema: Value,
handler: F,
) -> &mut Self
pub fn register_described<F, Fut>( &mut self, name: impl Into<String>, description: impl Into<String>, schema: Value, handler: F, ) -> &mut Self
Like Self::register but also attaches a description.
Sourcepub fn to_messages_tools(&self) -> Vec<MessagesTool>
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§impl ToolRegistry
impl ToolRegistry
Sourcepub fn register_typed<A, F, Fut>(
&mut self,
name: impl Into<String>,
handler: F,
) -> &mut Self
Available on crate feature schemars-tools only.
pub fn register_typed<A, F, Fut>( &mut self, name: impl Into<String>, handler: F, ) -> &mut Self
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.
Sourcepub fn register_typed_described<A, F, Fut>(
&mut self,
name: impl Into<String>,
description: impl Into<String>,
handler: F,
) -> &mut Self
Available on crate feature schemars-tools only.
pub fn register_typed_described<A, F, Fut>( &mut self, name: impl Into<String>, description: impl Into<String>, handler: F, ) -> &mut Self
schemars-tools only.Like Self::register_typed but also attaches a description.
Trait Implementations§
Source§impl Debug for ToolRegistry
impl Debug for ToolRegistry
Source§impl Default for ToolRegistry
impl Default for ToolRegistry
Source§fn default() -> ToolRegistry
fn default() -> ToolRegistry
Auto Trait Implementations§
impl Freeze for ToolRegistry
impl !RefUnwindSafe for ToolRegistry
impl Send for ToolRegistry
impl Sync for ToolRegistry
impl Unpin for ToolRegistry
impl UnsafeUnpin for ToolRegistry
impl !UnwindSafe for ToolRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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