Skip to main content

Tool

Trait Tool 

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

    // Provided methods
    fn approval_with_context(
        &self,
        args: &str,
        _ctx: &ToolContext,
    ) -> ApprovalRequirement { ... }
    fn validate_args(&self, _args: &str) -> Result<(), String> { ... }
}

Required Methods§

Source

fn definition(&self) -> ToolDef

Source

fn approval(&self, args: &str) -> ApprovalRequirement

Source

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

Provided Methods§

Source

fn approval_with_context( &self, args: &str, _ctx: &ToolContext, ) -> ApprovalRequirement

Source

fn validate_args(&self, _args: &str) -> Result<(), String>

Pre-flight syntactic check on raw tool-call arguments. The runner calls this before approval and before execute, so a parse failure short-circuits to a tool-result error and the model receives a structured retry hint without bothering the user.

Default impl: Ok(()). Tools with strict required-field schemas (write_file / edit_file / search_replace) override to surface the serde error early. Implementations should be cheap (parse only, no I/O) — the runner re-parses inside execute() for actual use.

Trigger context (2026-05-02 datalog evidence): provider-side stream truncation can deliver [RAW ARGS: {] or [RAW ARGS: {"file_path":"..."] (closing-bracket wrong, content missing). The previous flow let those reach approval_with_context where the tool’s own fail-closed branch returned RequireApproval("Could not parse … for safety check.") and the user saw an approval prompt for an obviously-broken call. Pressing Allow then died on the same parse in execute(). Validating up front eliminates the user-visible round-trip entirely.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§