Skip to main content

FunctionTool

Struct FunctionTool 

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

A custom agent tool backed by an async function or closure.

Construct one with FunctionTool::new and hand it to AgentBuilder::tool. The handler is invoked with the model’s call arguments deserialized into a serde_json::Value and returns any IntoToolResult. A returned Err becomes a redacted internal error — the turn is never panicked by a handler. Return ToolResponse::error from the Ok path for errors that are safe to show to the model.

The Err channel is redacted because a handler’s error text is written against the host — a path, a query, a connection string — and the model is an untrusted reader of anything it is shown. The full error is still logged with the tool name and call id, so nothing is lost for the developer. #[everruns::tool] is unaffected: it maps a function’s own Err to a model-visible tool error before it reaches this channel, and reserves this channel for failures the model did not cause.

A FunctionTool is cheap to clone (the handler is reference-counted) and is Send + Sync, so the same tool can execute calls concurrently.

Implementations§

Source§

impl FunctionTool

Source

pub fn new<F, Fut, T, E>( name: impl Into<String>, description: impl Into<String>, json_schema: Value, handler: F, ) -> Self
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<T, E>> + Send + 'static, T: IntoToolResult + 'static, E: Display + 'static,

Wrap an async handler as a callable tool.

  • name is the identifier the model calls (validated at Agent::build).
  • description tells the model when to use the tool.
  • json_schema is the JSON Schema for the tool’s arguments (validated at build time).
  • handler is an async Fn(serde_json::Value) -> Result<T, E> where T: IntoToolResult (a Value, a String, or a ToolResponse) and E: Display. On Err, the error’s Display text is retained for internal diagnostics but redacted from the model-facing tool result.
Source

pub fn name(&self) -> &str

The tool’s name (the identifier the model calls).

Trait Implementations§

Source§

impl Clone for FunctionTool

Source§

fn clone(&self) -> FunctionTool

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FunctionTool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IntoTool for FunctionTool

Source§

fn into_tool(self) -> Tool

Convert self into a Tool.
Source§

impl Tool for FunctionTool

Source§

fn name(&self) -> &str

Returns the tool’s unique name. Read more
Source§

fn description(&self) -> &str

Returns a description of what the tool does. Read more
Source§

fn parameters_schema(&self) -> Value

Returns the JSON schema for the tool’s parameters. Read more
Source§

fn execute<'life0, 'async_trait>( &'life0 self, arguments: Value, ) -> Pin<Box<dyn Future<Output = ToolExecutionResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the tool with the given arguments. Read more
Source§

fn display_name(&self) -> Option<&str>

Returns a human-readable display name for UI rendering. Read more
Source§

fn execute_with_context<'life0, 'life1, 'async_trait>( &'life0 self, arguments: Value, _context: &'life1 ToolContext, ) -> Pin<Box<dyn Future<Output = ToolExecutionResult> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Execute the tool with context. Read more
Source§

fn requires_context(&self) -> bool

Returns true if this tool requires context for execution. Read more
Source§

fn required_context_services(&self) -> &'static [ToolContextService]

Runtime services that must be present before this tool can be exposed. Read more
Source§

fn policy(&self) -> ToolPolicy

Returns the tool policy (auto or requires_approval). Read more
Source§

fn hints(&self) -> ToolHints

Returns semantic hints describing the tool’s behavioral properties. Read more
Source§

fn narrate( &self, _tool_call: &ToolCall, _phase: ToolNarrationPhase, _locale: Option<&str>, _ctx: ToolNarrationContext<'_>, ) -> Option<String>

Returns backend-authored narration for a call to this tool, e.g. “Read AGENTS.md”. Read more
Source§

fn as_background_executable(&self) -> Option<&dyn BackgroundExecutableTool>

Returns native background execution support when this tool opts into detached execution via hints().supports_background.
Source§

fn deferrable_policy(&self) -> DeferrablePolicy

Deferral policy for progressive tool-schema disclosure (tool search). Hot-path or “consult-first” tools can return DeferrablePolicy::Never to always keep their full schema directly callable. Defaults to DeferrablePolicy::Automatic.
Source§

fn to_definition(&self) -> ToolDefinition

Convert this tool to a ToolDefinition for the agent config. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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