Skip to main content

ToolHandler

Trait ToolHandler 

Source
pub trait ToolHandler:
    Send
    + Sync
    + 'static {
    // Required method
    fn call<'life0, 'async_trait>(
        &'life0 self,
        invocation: ToolInvocation,
    ) -> Pin<Box<dyn Future<Output = Result<ToolResult, Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

A client-defined tool’s runtime implementation.

Implement this trait when you want to bind a Rust function to a tool name and have the SDK dispatch matching external_tool.requested broadcasts to it. Attach the impl to a Tool via Tool::with_handler.

Named handler types (e.g. struct MyTool;) are visible in stack traces and navigable via “go to definition”, which is preferable to closure-based alternatives for non-trivial tools. For trivial tools, the define_tool helper function (available with the derive feature) wraps a free async fn or closure into a Tool with the handler already attached.

§Example

use github_copilot_sdk::tool::{schema_for, JsonSchema, ToolHandler};
use github_copilot_sdk::types::{Tool, ToolInvocation};
use github_copilot_sdk::{Error, ToolResult};
use serde::Deserialize;
use async_trait::async_trait;
use std::sync::Arc;

#[derive(Deserialize, JsonSchema)]
struct GetWeatherParams {
    /// City name
    city: String,
}

struct GetWeather;

#[async_trait]
impl ToolHandler for GetWeather {
    async fn call(&self, inv: ToolInvocation) -> Result<ToolResult, Error> {
        let params: GetWeatherParams = serde_json::from_value(inv.arguments)?;
        Ok(ToolResult::Text(format!("Weather in {}: sunny", params.city)))
    }
}

// Build the Tool declaration with the handler attached:
let tool = Tool::new("get_weather")
    .with_description("Get weather for a city")
    .with_parameters(schema_for::<GetWeatherParams>())
    .with_handler(Arc::new(GetWeather));

Required Methods§

Source

fn call<'life0, 'async_trait>( &'life0 self, invocation: ToolInvocation, ) -> Pin<Box<dyn Future<Output = Result<ToolResult, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Handle a tool invocation from the agent.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§