Skip to main content

Tool

Struct Tool 

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

Ad-hoc tool defined inline with a closure handler.

Chain builder methods to configure, then hand the tool to an agent:

let greet = Tool::new("greet", "Say hello")
    .schema(serde_json::json!({"type": "object", "properties": {}}))
    .handler(|_input, _ctx| async {
        Ok(ToolResult::success("hi"))
    });

A handler is required — omitting Tool::handler causes the first invocation to panic. For tools with complex state, implement ToolLike directly on your own type instead.

Implementations§

Source§

impl Tool

Source

pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self

A new tool with an empty-object input schema and no handler. Set the handler with Tool::handler before handing the tool to an agent.

Source

pub fn from_tool_file(json: &str) -> Self

Construct a Tool from a .tool.json definition. The returned tool has its name, rendered description, input schema, and read-only flag populated from the JSON; attach a handler via Tool::handler before registering it with an agent. Panics on malformed JSON.

Source

pub fn schema(self, schema: Value) -> Self

Replace the input schema (JSON Schema). Defaults to an empty-object schema.

Source

pub fn read_only(self, read_only: bool) -> Self

Mark the tool read-only so the loop runs it concurrently with other read-only calls in the same step.

Source

pub fn defer(self, defer: bool) -> Self

Hide the tool’s full definition until it is discovered via ToolSearchTool.

Source

pub fn handler<F, Fut>(self, f: F) -> Self
where F: Fn(Value, ToolContext) -> Fut + Send + Sync + 'static, Fut: Future<Output = ProviderResult<ToolResult>> + Send + 'static,

Install the closure that runs when the model calls this tool. The closure may be a bare async block — the builder boxes the returned future internally. Required: omitting this causes the first invocation to panic.

Trait Implementations§

Source§

impl ToolLike for Tool

Source§

fn name(&self) -> &str

Unique name the model uses to call the tool.
Source§

fn description(&self) -> &str

Human-readable description shown to the model.
Source§

fn input_schema(&self) -> Value

JSON Schema describing the tool’s arguments.
Source§

fn is_read_only(&self) -> bool

Whether this tool has no side effects. Read-only tools in the same step run concurrently; non-read-only tools run serially. Default: false.
Source§

fn should_defer(&self) -> bool

Whether the tool’s full definition is hidden until it is discovered via ToolSearchTool. Deferred tools appear to the model as name-only stubs. Default: false.
Source§

fn call<'a>( &'a self, input: Value, ctx: &'a ToolContext, ) -> Pin<Box<dyn Future<Output = ProviderResult<ToolResult>> + Send + 'a>>

Run the tool. The future is held by the agent loop and dropped on cancellation; pair long-running work with ToolContext::wait_for_cancel in a tokio::select! to drop the losing branch promptly.

Auto Trait Implementations§

§

impl Freeze for Tool

§

impl !RefUnwindSafe for Tool

§

impl Send for Tool

§

impl Sync for Tool

§

impl Unpin for Tool

§

impl UnsafeUnpin for Tool

§

impl !UnwindSafe for Tool

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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