pub trait Tool: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn description(&self) -> &str;
fn schema(&self) -> &ToolSchema;
fn invoke(
&self,
input: Value,
) -> BoxFuture<'_, Result<ToolOutput, SynwireError>>;
}Expand description
Trait for callable tools.
Implementations must be Send + Sync so tools can be shared across
async tasks and threads.
§Cancel safety
The future returned by invoke is not cancel-safe
in general. If the tool performs side effects (file writes, API calls),
dropping the future mid-execution may leave those effects partially
applied. Callers should avoid dropping tool futures unless they are
prepared to retry or roll back.
§Example
use synwire_core::tools::{Tool, ToolOutput, ToolSchema};
use synwire_core::error::SynwireError;
use synwire_core::BoxFuture;
struct Echo;
impl Tool for Echo {
fn name(&self) -> &str { "echo" }
fn description(&self) -> &str { "Echoes input back" }
fn schema(&self) -> &ToolSchema {
// In real code, store this in a field
Box::leak(Box::new(ToolSchema {
name: "echo".into(),
description: "Echoes input back".into(),
parameters: serde_json::json!({"type": "object"}),
}))
}
fn invoke(
&self,
input: serde_json::Value,
) -> BoxFuture<'_, Result<ToolOutput, SynwireError>> {
Box::pin(async move {
Ok(ToolOutput {
content: input.to_string(),
..Default::default()
})
})
}
}Required Methods§
Sourcefn description(&self) -> &str
fn description(&self) -> &str
The tool’s description.
Sourcefn schema(&self) -> &ToolSchema
fn schema(&self) -> &ToolSchema
The tool’s schema for argument validation.
Sourcefn invoke(
&self,
input: Value,
) -> BoxFuture<'_, Result<ToolOutput, SynwireError>>
fn invoke( &self, input: Value, ) -> BoxFuture<'_, Result<ToolOutput, SynwireError>>
Invoke the tool with JSON arguments.