Skip to main content

ToolRegistry

Struct ToolRegistry 

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

Registry of available tools, keyed by name.

Implementations§

Source§

impl ToolRegistry

Source

pub fn new() -> Self

Creates an empty registry.

Source

pub fn register( &mut self, spec: ToolSpec, handler: ToolFn, ) -> Result<(), AgentError>

Registers a tool with its specification and handler.

§Errors

Returns AgentError::InvalidToolSignature if the tool name is empty.

Source

pub fn dispatch( &self, tool_name: &str, input: &str, ) -> Result<ToolResult, AgentError>

Dispatches a tool call by name with the given JSON input string.

§Errors

Returns AgentError::ToolNotFound if no tool with that name is registered.

Source

pub fn dispatch_call(&self, call: &ToolCall) -> Result<ToolResult, AgentError>

Dispatches a native tool call (see crate::native). Its arguments arrive as JSON. For a tool with an object schema they are passed on unchanged; for a tool with any other schema the API saw {"input": ...}, so that value is unwrapped (a JSON string is passed as plain text). Arguments that are not valid JSON produce a failed ToolResult the model can see and correct, not an error.

§Errors

AgentError::ToolNotFound if no tool with that name is registered.

Source

pub fn openai_tools(&self) -> Result<Vec<Value>, AgentError>

The tools array for OpenAI Chat Completions (and OpenAI-compatible APIs: Mistral, Groq, DeepSeek, OpenRouter, Ollama’s /v1), sorted by name.

§Errors

AgentError::InvalidToolSignature if a tool’s schema is not JSON.

Source

pub fn anthropic_tools(&self) -> Result<Vec<Value>, AgentError>

The tools array for Anthropic Messages, sorted by name.

§Errors

AgentError::InvalidToolSignature if a tool’s schema is not JSON.

Source

pub fn register_typed<A, F>( &mut self, name: &str, description: &str, f: F, ) -> Result<(), AgentError>
where A: DeserializeOwned + JsonSchema + 'static, F: Fn(A) -> Result<String, String> + 'static,

Available on crate feature schemars only.

Register a tool whose input is a Rust type: the JSON Schema comes from schemars and the input is parsed with serde before f runs. Input that does not match the type gives the model a failed result explaining why, so it can retry. Needs the schemars feature.

use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct Add { a: i64, b: i64 }

let mut tools = wasm_agent::ToolRegistry::new();
tools.register_typed("add", "Adds two integers", |x: Add| Ok((x.a + x.b).to_string()))?;
assert_eq!(tools.dispatch("add", r#"{"a": 2, "b": 3}"#)?.output, "5");
assert!(!tools.dispatch("add", r#"{"a": "two"}"#)?.success);
§Errors

AgentError::InvalidToolSignature if the name is empty.

Source

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

Returns the spec for a registered tool, or None if not found.

Source

pub fn tool_count(&self) -> usize

Returns the number of registered tools.

Source

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

Returns the names of all registered tools, sorted.

Source

pub fn tools_prompt(&self) -> String

Generates a system-prompt snippet listing all registered tools, sorted by name, with each tool’s input schema when it has one.

Trait Implementations§

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

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

fn try_from(value: U) -> Result<T, !>

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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more