pub struct ToolRegistry { /* private fields */ }Expand description
Registry of available tools, keyed by name.
Implementations§
Source§impl ToolRegistry
impl ToolRegistry
Sourcepub fn register(
&mut self,
spec: ToolSpec,
handler: ToolFn,
) -> Result<(), AgentError>
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.
Sourcepub fn dispatch(
&self,
tool_name: &str,
input: &str,
) -> Result<ToolResult, AgentError>
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.
Sourcepub fn dispatch_call(&self, call: &ToolCall) -> Result<ToolResult, AgentError>
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.
Sourcepub fn openai_tools(&self) -> Result<Vec<Value>, AgentError>
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.
Sourcepub fn anthropic_tools(&self) -> Result<Vec<Value>, AgentError>
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.
Sourcepub fn register_typed<A, F>(
&mut self,
name: &str,
description: &str,
f: F,
) -> Result<(), AgentError>
Available on crate feature schemars only.
pub fn register_typed<A, F>( &mut self, name: &str, description: &str, f: F, ) -> Result<(), AgentError>
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.
Sourcepub fn spec(&self, name: &str) -> Option<&ToolSpec>
pub fn spec(&self, name: &str) -> Option<&ToolSpec>
Returns the spec for a registered tool, or None if not found.
Sourcepub fn tool_count(&self) -> usize
pub fn tool_count(&self) -> usize
Returns the number of registered tools.
Sourcepub fn tool_names(&self) -> Vec<&str>
pub fn tool_names(&self) -> Vec<&str>
Returns the names of all registered tools, sorted.
Sourcepub fn tools_prompt(&self) -> String
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.