pub struct Tool { /* private fields */ }Expand description
Implementations§
Source§impl Tool
impl Tool
Sourcepub fn new<F, Fut>(
name: impl Into<String>,
description: impl Into<String>,
input_schema: Value,
handler: F,
) -> Self
pub fn new<F, Fut>( name: impl Into<String>, description: impl Into<String>, input_schema: Value, handler: F, ) -> Self
Creates a tool and normalizes its input schema.
Accepted schema forms:
- A full object containing
typeandpropertiesis passed through unchanged. - A property map such as
{"query": "string"}makes each property required. - Extended properties may include
optional,required, anddefault. Explicitrequired: truewins; otherwiseoptional: true,required: false, or a default makes the property optional. The two flags are removed afterward.
Type aliases include str, int, i32/i64/u32/u64, float/f32/f64,
bool, list/vec, and dict/map. Unknown names become string. Non-object schema
inputs produce an empty object schema. The handler still validates arguments.
§Example
use open_agent::Tool;
use serde_json::json;
let search = Tool::new(
"search",
"Search documents",
json!({
"query": "string",
"limit": {"type": "integer", "default": 10}
}),
|args| async move {
let limit = args["limit"].as_u64().unwrap_or(10);
Ok(json!({"query": args["query"], "limit": limit}))
},
);Sourcepub async fn execute(&self, arguments: Value) -> Result<Value>
pub async fn execute(&self, arguments: Value) -> Result<Value>
Runs the handler with the supplied JSON arguments.
The handler validates arguments; this method does not enforce the input schema.
§Errors
Propagates the handler’s error unchanged. Automatic execution converts that error to a tool-result JSON object so the model can respond to it.
Sourcepub fn to_openai_format(&self) -> Value
pub fn to_openai_format(&self) -> Value
Returns the tool name, description, and schema in OpenAI function-calling format.
The returned JSON contains no handler. Anthropic translation extracts the same metadata when building its request.
Sourcepub fn description(&self) -> &str
pub fn description(&self) -> &str
Returns the tool’s description.
Sourcepub fn input_schema(&self) -> &Value
pub fn input_schema(&self) -> &Value
Returns a reference to the tool’s input schema.