Skip to main content

Tool

Trait Tool 

Source
pub trait Tool:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn tool_spec(&self) -> ToolSpec;
    fn stream(&self, context: ToolContext) -> ToolStream;
}
Expand description

A tool the model can call (Tool).

stream yields zero or more progress events and ends with exactly one ToolOutcome: the result, or an interrupt that pauses the agent for human input. A failing tool returns an error result rather than failing the stream.

Most tools are written with #[tool] or FunctionTool; implement the trait directly for full control.

use strands::tools::{Tool, ToolContext, ToolOutcome, ToolStream};
use strands::{ToolResultBlock, ToolResultContent, ToolSpec};

struct Echo;

impl Tool for Echo {
    fn name(&self) -> &str {
        "echo"
    }

    fn description(&self) -> &str {
        "Echoes its input"
    }

    fn tool_spec(&self) -> ToolSpec {
        ToolSpec::new(self.name(), self.description())
    }

    fn stream(&self, context: ToolContext) -> ToolStream {
        ToolStream::from_future(async move {
            let text = context.tool_use().input.to_string();
            let id = context.tool_use().tool_use_id.clone();
            ToolOutcome::from(ToolResultBlock::success(id, vec![ToolResultContent::text(text)]))
        })
    }
}

Required Methods§

Source

fn name(&self) -> &str

The unique tool name; it must match the spec’s name.

Source

fn description(&self) -> &str

What the tool does; it must match the spec’s description.

Source

fn tool_spec(&self) -> ToolSpec

The spec sent to the model: name, description and input schema.

Source

fn stream(&self, context: ToolContext) -> ToolStream

Runs the tool. The stream is 'static, so the agent can run it on its own task.

Trait Implementations§

Source§

impl Debug for dyn Tool

Source§

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

Formats the value using the given formatter. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§