descry-tool-core 0.3.1

Core traits and types for descry-tool framework
Documentation
//! Basic example demonstrating Descry API
//!
//! This example shows the new simplified tool definition and automatic registration.

use descry_tool_core::{call_tool, tool_exists, tool_names, Tool, ToolContext, ToolError};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

// Define parameter type
#[derive(Deserialize, JsonSchema)]
struct AddParams {
    a: i32,
    b: i32,
}

// Define output type
#[derive(Serialize, JsonSchema)]
struct AddOutput {
    result: i32,
}

// Define tool
struct AddTool;

impl Tool for AddTool {
    type Params = AddParams;
    type Output = AddOutput;

    const NAME: &'static str = "add";
    const DESCRIPTION: &'static str = "Add two numbers";

    async fn call(_ctx: Arc<ToolContext>, params: Self::Params) -> Result<Self::Output, ToolError> {
        Ok(AddOutput {
            result: params.a + params.b,
        })
    }
}

// Manual inventory registration (in real code, this will be done by #[tool] macro)
inventory::submit! {
    descry_tool_core::ToolMeta {
        name: AddTool::NAME,
        description: AddTool::DESCRIPTION,
        call: |ctx, params| {
            Box::pin(async move {
                let params: AddParams = serde_json::from_value(params)?;
                let result = <AddTool as Tool>::call(ctx, params).await?;
                Ok(serde_json::to_value(result)?)
            })
        },
        schema: || <AddTool as Tool>::schema(),
        examples: || <AddTool as Tool>::EXAMPLES,
    }
}

#[tokio::main]
async fn main() {
    println!("=== Descry Basic Example ===\n");

    // Show all registered tools
    let names = tool_names();
    println!("Registered tools: {:?}", names);
    println!("Tool count: {}\n", names.len());

    // Check if tool exists
    println!("Tool 'add' exists: {}", tool_exists("add"));
    println!("Tool 'nonexistent' exists: {}\n", tool_exists("nonexistent"));

    // Call tool
    let ctx = Arc::new(ToolContext::new());
    let params = serde_json::json!({"a": 5, "b": 3});
    
    println!("Calling add({a}, {b})...", a = 5, b = 3);
    match call_tool("add", params, ctx).await {
        Ok(result) => {
            println!("Result: {}", serde_json::to_string_pretty(&result).unwrap());
        }
        Err(e) => {
            eprintln!("Error: {}", e);
        }
    }

    // Get tool schema
    if let Some(schema) = descry_tool_core::get_tool_schema("add") {
        println!("\nTool schema:");
        println!("{}", serde_json::to_string_pretty(schema).unwrap());
    }
}