asimov_server/http/mcp/
tool.rs

1// This is free and unencumbered software released into the public domain.
2
3use std::sync::Arc;
4
5use rmcp::model::Content;
6use serde_json::{Map, Value};
7
8use super::server::Error;
9
10pub type ToolCallback =
11    Arc<dyn Fn(Option<Map<String, Value>>) -> Result<Vec<Content>, Error> + Send + Sync>;
12
13#[derive(Clone)]
14pub struct Tool {
15    pub name: String,
16    pub description: Option<String>,
17    pub input_schema: Arc<Map<String, Value>>,
18    pub callback: ToolCallback,
19}
20
21impl Tool {
22    pub fn new<N, D, F>(name: N, description: Option<D>, callback: F) -> Self
23    where
24        N: Into<String>,
25        D: Into<String>,
26        F: Fn() -> Result<Vec<Content>, Error> + Send + Sync + 'static,
27    {
28        Self {
29            name: name.into(),
30            description: description.map(Into::into),
31            input_schema: Arc::new(Map::new()),
32            callback: Arc::new(move |_args| callback()),
33        }
34    }
35
36    pub fn new_with_args<N, D, F>(
37        name: N,
38        description: Option<D>,
39        input_schema: Map<String, Value>,
40        callback: F,
41    ) -> Self
42    where
43        N: Into<String>,
44        D: Into<String>,
45        F: Fn(Option<Map<String, Value>>) -> Result<Vec<Content>, Error> + Send + Sync + 'static,
46    {
47        Self {
48            name: name.into(),
49            description: description.map(Into::into),
50            input_schema: Arc::new(input_schema),
51            callback: Arc::new(callback),
52        }
53    }
54}