Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn parameters(&self) -> Value;
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: ToolContext<'life1>,
    ) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn timeout_secs(&self) -> Option<u64> { ... }
    fn max_output_bytes(&self) -> usize { ... }
    fn max_output_lines(&self) -> usize { ... }
    fn concurrent(&self) -> bool { ... }
    fn supports_background(&self) -> bool { ... }
    fn humanize(&self, args: &Value) -> String { ... }
}
Expand description

Trait for tools that can be invoked by the agent.

Implementations define a name, description, JSON schema parameters, and async execution logic. Tools are registered in a ToolRegistry and become available to the LLM during conversation turns via function calling.

§Required methods

  • name — unique identifier used by the LLM to invoke the tool
  • description — shown to the LLM to decide when to call the tool
  • parameters — JSON Schema object describing expected arguments
  • execute — async logic that runs when the tool is called

§Optional overrides

  • humanize — display-friendly summary (default: comma-separated arg keys)
  • timeout_secs — per-tool timeout (default: uses global default)
  • max_output_bytes — output size limit (default: 256 KiB)
  • max_output_lines — output line count limit (default: 10,000)

Required Methods§

Source

fn name(&self) -> &str

Unique tool name (used for invocation). Must match the name the LLM sees in its function definitions.

Source

fn description(&self) -> &str

Human-readable description for the LLM. Used to help the model decide when to invoke this tool.

Source

fn parameters(&self) -> Value

JSON Schema object describing the tool’s parameters.

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, ctx: ToolContext<'life1>, ) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute the tool with given arguments. Returns a ToolResult on success.

Provided Methods§

Source

fn timeout_secs(&self) -> Option<u64>

Optional per-tool timeout in seconds. Returns None to use the global default.

Source

fn max_output_bytes(&self) -> usize

Maximum allowed output size in bytes. Tools that produce large payloads (e.g. base64-encoded images, arbitrary MCP responses) should return usize::MAX to bypass the limit.

Source

fn max_output_lines(&self) -> usize

Maximum allowed output line count. Tools that produce large payloads should return usize::MAX to bypass the limit.

Source

fn concurrent(&self) -> bool

Whether this tool may run at the same time as other tools from the same model reply, when the agent allows parallel tool calls. Return false for tools that share state or act on one device, so they run alone between concurrent batches.

Source

fn supports_background(&self) -> bool

Whether a running call can continue as a managed background task.

Source

fn humanize(&self, args: &Value) -> String

Generate a human-readable description of what this tool call does, suitable for display in a TUI or chat card (e.g. “Reading file Cargo.toml”).

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§