pub trait ToolExecutor {
// Required method
fn execute<'life0, 'async_trait>(
&'life0 self,
arguments: Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<ToolsCallResponse>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided method
fn schema<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
}
Expand description
Trait that users must implement to provide actual tool functionality
This trait separates the tool metadata (handled by the derive macro) from
the tool execution logic (implemented by the user). It’s designed to work
with the #[derive(Tool)]
macro from mocopr_macros
.
§Example
use mocopr_core::{ToolExecutor, types::{ToolsCallResponse, Content, TextContent}, Result};
use serde_json::Value;
use async_trait::async_trait;
struct Calculator;
#[async_trait]
impl ToolExecutor for Calculator {
async fn execute(&self, arguments: Option<Value>) -> Result<ToolsCallResponse> {
let args = arguments.unwrap_or_default();
let operation = args.get("operation").and_then(|v| v.as_str()).unwrap_or("add");
let a: f64 = args.get("a").and_then(|v| v.as_f64()).unwrap_or(0.0);
let b: f64 = args.get("b").and_then(|v| v.as_f64()).unwrap_or(0.0);
let result = match operation {
"add" => a + b,
"subtract" => a - b,
"multiply" => a * b,
"divide" => {
if b == 0.0 {
return Ok(ToolsCallResponse::error(vec![
Content::Text(TextContent::new("Division by zero"))
]));
}
a / b
}
_ => return Ok(ToolsCallResponse::error(vec![
Content::Text(TextContent::new("Unknown operation"))
])),
};
Ok(ToolsCallResponse::success(vec![
Content::Text(TextContent::new(&result.to_string()))
]))
}
}
Required Methods§
Sourcefn execute<'life0, 'async_trait>(
&'life0 self,
arguments: Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<ToolsCallResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
arguments: Option<Value>,
) -> Pin<Box<dyn Future<Output = Result<ToolsCallResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Execute the tool with the given arguments.
This method must be implemented by the user to provide the actual tool functionality. The arguments parameter contains the JSON arguments passed to the tool.
§Arguments
arguments
- Optional JSON value containing tool arguments
§Returns
A ToolsCallResponse
containing the tool execution results
Provided Methods§
Sourcefn schema<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
fn schema<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
Provide the JSON schema for tool arguments (optional)
Override this method to provide a custom JSON schema for tool arguments. If not overridden, a basic empty object schema is used.
§Returns
Optional JSON schema for the tool’s arguments