|
| 1 | +//! Tools for MCP servers. |
| 2 | +//! |
| 3 | +//! It's straightforward to define tools using [`tool_router`][crate::tool_router] and |
| 4 | +//! [`tool`][crate::tool] macro. |
| 5 | +//! |
| 6 | +//! ```rust |
| 7 | +//! # use rmcp::{ |
| 8 | +//! # tool_router, tool, |
| 9 | +//! # handler::server::{wrapper::{Parameters, Json}, tool::ToolRouter}, |
| 10 | +//! # schemars |
| 11 | +//! # }; |
| 12 | +//! # use serde::{Serialize, Deserialize}; |
| 13 | +//! struct Server { |
| 14 | +//! tool_router: ToolRouter<Self>, |
| 15 | +//! } |
| 16 | +//! #[derive(Deserialize, schemars::JsonSchema, Default)] |
| 17 | +//! struct AddParameter { |
| 18 | +//! left: usize, |
| 19 | +//! right: usize |
| 20 | +//! } |
| 21 | +//! #[derive(Serialize, schemars::JsonSchema)] |
| 22 | +//! struct AddOutput { |
| 23 | +//! sum: usize |
| 24 | +//! } |
| 25 | +//! #[tool_router] |
| 26 | +//! impl Server { |
| 27 | +//! #[tool(name = "adder", description = "Modular add two integers")] |
| 28 | +//! fn add( |
| 29 | +//! &self, |
| 30 | +//! Parameters(AddParameter { left, right }): Parameters<AddParameter> |
| 31 | +//! ) -> Json<AddOutput> { |
| 32 | +//! Json(AddOutput { sum: left.wrapping_add(right) }) |
| 33 | +//! } |
| 34 | +//! } |
| 35 | +//! ``` |
| 36 | +//! |
| 37 | +//! Using the macro-based code pattern above is suitable for small MCP servers with simple interfaces. |
| 38 | +//! When the business logic become larger, it is recommended that each tool should reside |
| 39 | +//! in individual file, combined into MCP server using [`SyncTool`] and [`AsyncTool`] traits. |
| 40 | +//! |
| 41 | +//! ```rust |
| 42 | +//! # use rmcp::{ |
| 43 | +//! # handler::server::{ |
| 44 | +//! # tool::ToolRouter, |
| 45 | +//! # router::tool::{SyncTool, AsyncTool, ToolBase}, |
| 46 | +//! # }, |
| 47 | +//! # schemars, ErrorData |
| 48 | +//! # }; |
| 49 | +//! # pub struct MyCustomError; |
| 50 | +//! # impl From<MyCustomError> for ErrorData { |
| 51 | +//! # fn from(err: MyCustomError) -> ErrorData { unimplemented!() } |
| 52 | +//! # } |
| 53 | +//! # use serde::{Serialize, Deserialize}; |
| 54 | +//! # use std::borrow::Cow; |
| 55 | +//! // In tool1.rs |
| 56 | +//! pub struct ComplexTool1; |
| 57 | +//! #[derive(Deserialize, schemars::JsonSchema, Default)] |
| 58 | +//! pub struct ComplexTool1Input { /* ... */ } |
| 59 | +//! #[derive(Serialize, schemars::JsonSchema)] |
| 60 | +//! pub struct ComplexTool1Output { /* ... */ } |
| 61 | +//! |
| 62 | +//! impl ToolBase for ComplexTool1 { |
| 63 | +//! type Parameter = ComplexTool1Input; |
| 64 | +//! type Output = ComplexTool1Output; |
| 65 | +//! type Error = MyCustomError; |
| 66 | +//! fn name() -> Cow<'static, str> { |
| 67 | +//! "complex-tool1".into() |
| 68 | +//! } |
| 69 | +//! |
| 70 | +//! fn description() -> Option<Cow<'static, str>> { |
| 71 | +//! Some("...".into()) |
| 72 | +//! } |
| 73 | +//! } |
| 74 | +//! impl SyncTool<MyToolServer> for ComplexTool1 { |
| 75 | +//! fn invoke(service: &MyToolServer, param: Self::Parameter) -> Result<Self::Output, Self::Error> { |
| 76 | +//! // ... |
| 77 | +//! # unimplemented!() |
| 78 | +//! } |
| 79 | +//! } |
| 80 | +//! // In tool2.rs |
| 81 | +//! pub struct ComplexTool2; |
| 82 | +//! #[derive(Deserialize, schemars::JsonSchema, Default)] |
| 83 | +//! pub struct ComplexTool2Input { /* ... */ } |
| 84 | +//! #[derive(Serialize, schemars::JsonSchema)] |
| 85 | +//! pub struct ComplexTool2Output { /* ... */ } |
| 86 | +//! |
| 87 | +//! impl ToolBase for ComplexTool2 { |
| 88 | +//! type Parameter = ComplexTool2Input; |
| 89 | +//! type Output = ComplexTool2Output; |
| 90 | +//! type Error = MyCustomError; |
| 91 | +//! fn name() -> Cow<'static, str> { |
| 92 | +//! "complex-tool2".into() |
| 93 | +//! } |
| 94 | +//! |
| 95 | +//! fn description() -> Option<Cow<'static, str>> { |
| 96 | +//! Some("...".into()) |
| 97 | +//! } |
| 98 | +//! } |
| 99 | +//! impl AsyncTool<MyToolServer> for ComplexTool2 { |
| 100 | +//! async fn invoke(service: &MyToolServer, param: Self::Parameter) -> Result<Self::Output, Self::Error> { |
| 101 | +//! // ... |
| 102 | +//! # unimplemented!() |
| 103 | +//! } |
| 104 | +//! } |
| 105 | +//! |
| 106 | +//! // In tool_router.rs |
| 107 | +//! struct MyToolServer { |
| 108 | +//! tool_router: ToolRouter<Self>, |
| 109 | +//! } |
| 110 | +//! impl MyToolServer { |
| 111 | +//! pub fn tool_router() -> ToolRouter<Self> { |
| 112 | +//! ToolRouter::new() |
| 113 | +//! .with_sync_tool::<ComplexTool1>() |
| 114 | +//! .with_async_tool::<ComplexTool2>() |
| 115 | +//! } |
| 116 | +//! } |
| 117 | +//! ``` |
| 118 | +//! |
| 119 | +//! It's also possible to use macro-based and trait-based tool definition together: Since |
| 120 | +//! [`ToolRouter`] implements [`Add`][std::ops::Add], you can add two tool routers into final |
| 121 | +//! router as showed in [the documentation of `tool_router`][crate::tool_router]. |
| 122 | +
|
| 123 | +mod tool_traits; |
| 124 | + |
1 | 125 | use std::{borrow::Cow, sync::Arc}; |
2 | 126 |
|
3 | 127 | use futures::{FutureExt, future::BoxFuture}; |
4 | 128 | use schemars::JsonSchema; |
| 129 | +pub use tool_traits::{AsyncTool, SyncTool, ToolBase}; |
5 | 130 |
|
6 | 131 | use crate::{ |
7 | 132 | handler::server::{ |
@@ -219,6 +344,42 @@ where |
219 | 344 | self |
220 | 345 | } |
221 | 346 |
|
| 347 | + /// Add a tool that implements [`SyncTool`] |
| 348 | + pub fn with_sync_tool<T>(self) -> Self |
| 349 | + where |
| 350 | + T: SyncTool<S> + 'static, |
| 351 | + { |
| 352 | + if T::input_schema().is_some() { |
| 353 | + self.with_route(( |
| 354 | + tool_traits::tool_attribute::<T>(), |
| 355 | + tool_traits::sync_tool_wrapper::<S, T>, |
| 356 | + )) |
| 357 | + } else { |
| 358 | + self.with_route(( |
| 359 | + tool_traits::tool_attribute::<T>(), |
| 360 | + tool_traits::sync_tool_wrapper_with_empty_params::<S, T>, |
| 361 | + )) |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + /// Add a tool that implements [`AsyncTool`] |
| 366 | + pub fn with_async_tool<T>(self) -> Self |
| 367 | + where |
| 368 | + T: AsyncTool<S> + 'static, |
| 369 | + { |
| 370 | + if T::input_schema().is_some() { |
| 371 | + self.with_route(( |
| 372 | + tool_traits::tool_attribute::<T>(), |
| 373 | + tool_traits::async_tool_wrapper::<S, T>, |
| 374 | + )) |
| 375 | + } else { |
| 376 | + self.with_route(( |
| 377 | + tool_traits::tool_attribute::<T>(), |
| 378 | + tool_traits::async_tool_wrapper_with_empty_params::<S, T>, |
| 379 | + )) |
| 380 | + } |
| 381 | + } |
| 382 | + |
222 | 383 | pub fn add_route(&mut self, item: ToolRoute<S>) { |
223 | 384 | let new_name = &item.attr.name; |
224 | 385 | validate_and_warn_tool_name(new_name); |
|
0 commit comments