turbomcp_server/handlers/traits/
tool.rs

1//! Tool handler trait for processing tool calls
2
3use async_trait::async_trait;
4use serde_json::Value;
5use turbomcp_protocol::RequestContext;
6use turbomcp_protocol::types::{CallToolRequest, CallToolResult, Tool};
7
8use crate::ServerResult;
9
10/// Tool handler trait for processing tool calls
11#[async_trait]
12pub trait ToolHandler: Send + Sync {
13    /// Handle a tool call request
14    async fn handle(
15        &self,
16        request: CallToolRequest,
17        ctx: RequestContext,
18    ) -> ServerResult<CallToolResult>;
19
20    /// Get the tool definition
21    fn tool_definition(&self) -> Tool;
22
23    /// Validate tool input (optional, default implementation allows all)
24    fn validate_input(&self, _input: &Value) -> ServerResult<()> {
25        Ok(())
26    }
27
28    /// Allowed roles for this tool (RBAC). None means unrestricted.
29    fn allowed_roles(&self) -> Option<&[String]> {
30        None
31    }
32}