pub struct FnTool { /* private fields */ }Expand description
A tool built entirely from closures and configuration, implementing
AgentTool without requiring a custom struct.
Use the builder methods to configure the tool’s schema, approval requirements, and execution logic.
Implementations§
Source§impl FnTool
impl FnTool
Sourcepub fn new(
name: impl Into<String>,
label: impl Into<String>,
description: impl Into<String>,
) -> Self
pub fn new( name: impl Into<String>, label: impl Into<String>, description: impl Into<String>, ) -> Self
Create a new FnTool with the given name, label, and description.
The default schema accepts any object and the default execute returns an error indicating the tool is not implemented.
Sourcepub fn with_schema_for<T: JsonSchema>(self) -> Self
pub fn with_schema_for<T: JsonSchema>(self) -> Self
Set the parameters schema from a type implementing
JsonSchema.
Sourcepub fn with_schema(self, schema: Value) -> Self
pub fn with_schema(self, schema: Value) -> Self
Set the parameters schema from a raw JSON value.
Sourcepub const fn with_requires_approval(self, requires: bool) -> Self
pub const fn with_requires_approval(self, requires: bool) -> Self
Set whether this tool requires user approval before execution.
Sourcepub fn with_execute<F, Fut>(self, f: F) -> Selfwhere
F: Fn(String, Value, CancellationToken, Option<Box<dyn Fn(AgentToolResult) + Send + Sync>>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
pub fn with_execute<F, Fut>(self, f: F) -> Selfwhere
F: Fn(String, Value, CancellationToken, Option<Box<dyn Fn(AgentToolResult) + Send + Sync>>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
Set the execution function using the full signature.
The closure receives (tool_call_id, params, cancellation_token, on_update).
Sourcepub fn with_execute_simple<F, Fut>(self, f: F) -> Selfwhere
F: Fn(Value, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
pub fn with_execute_simple<F, Fut>(self, f: F) -> Selfwhere
F: Fn(Value, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
Set the execution function using a simplified signature.
The closure receives only (params, cancellation_token), ignoring the
tool call ID and update callback.
Sourcepub fn with_execute_async<F, Fut>(self, f: F) -> Selfwhere
F: Fn(Value, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
pub fn with_execute_async<F, Fut>(self, f: F) -> Selfwhere
F: Fn(Value, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
Set the execution function using an explicit untyped async signature.
This is equivalent to Self::with_execute_simple and exists as a
discoverability alias for callers looking for an untyped async builder.
Sourcepub fn with_execute_typed<T, F, Fut>(self, f: F) -> Selfwhere
T: DeserializeOwned + JsonSchema + Send + 'static,
F: Fn(T, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
pub fn with_execute_typed<T, F, Fut>(self, f: F) -> Selfwhere
T: DeserializeOwned + JsonSchema + Send + 'static,
F: Fn(T, CancellationToken) -> Fut + Send + Sync + 'static,
Fut: Future<Output = AgentToolResult> + Send + 'static,
Set the execution function using a typed parameter struct.
This derives the schema from T and deserializes validated params into
T before calling the closure. On deserialization failure, execution
returns AgentToolResult::error("invalid parameters: ...").
Sourcepub fn with_approval_context<F>(self, f: F) -> Self
pub fn with_approval_context<F>(self, f: F) -> Self
Set a closure that provides rich context for the approval UI.
When the tool requires approval, this closure is called to produce
context that is attached to the ToolApprovalRequest.
Trait Implementations§
Source§impl AgentTool for FnTool
impl AgentTool for FnTool
Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§fn parameters_schema(&self) -> &Value
fn parameters_schema(&self) -> &Value
Source§fn requires_approval(&self) -> bool
fn requires_approval(&self) -> bool
false — tools execute immediately.