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 model-visible
tool error — the turn is never panicked by a handler.
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
impl FunctionTool
Sourcepub fn new<F, Fut, T, E>(
name: impl Into<String>,
description: impl Into<String>,
json_schema: Value,
handler: F,
) -> Self
pub fn new<F, Fut, T, E>( name: impl Into<String>, description: impl Into<String>, json_schema: Value, handler: F, ) -> Self
Wrap an async handler as a callable tool.
nameis the identifier the model calls (validated atAgent::build).descriptiontells the model when to use the tool.json_schemais the JSON Schema for the tool’s arguments (validated at build time).handleris an asyncFn(serde_json::Value) -> Result<T, E>whereT: IntoToolResult(aValue, aString, or aToolResponse) andE: Display. OnErr, the error’sDisplaytext is returned to the model as a tool error.
Trait Implementations§
Source§impl Clone for FunctionTool
impl Clone for FunctionTool
Source§fn clone(&self) -> FunctionTool
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FunctionTool
impl Debug for FunctionTool
Source§impl Tool for FunctionTool
impl Tool for FunctionTool
Source§fn description(&self) -> &str
fn description(&self) -> &str
Returns a description of what the tool does. Read more
Source§fn parameters_schema(&self) -> Value
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,
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>
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,
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
fn requires_context(&self) -> bool
Returns true if this tool requires context for execution. Read more
Source§fn required_context_services(&self) -> &'static [ToolContextService]
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
fn policy(&self) -> ToolPolicy
Returns the tool policy (auto or requires_approval). Read more
Source§fn hints(&self) -> ToolHints
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>
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>
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
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
fn to_definition(&self) -> ToolDefinition
Convert this tool to a ToolDefinition for the agent config. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for FunctionTool
impl !UnwindSafe for FunctionTool
impl Freeze for FunctionTool
impl Send for FunctionTool
impl Sync for FunctionTool
impl Unpin for FunctionTool
impl UnsafeUnpin for FunctionTool
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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> ⓘ
Converts
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> ⓘ
Converts
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request