refactor: refactor tool macros and router implementation (#261)

* refactor: refactor tool macros and router implementation

- Updated the `#[tool(tool_box)]` macro to `#[tool_router]` across various modules for consistency.
- Enhanced the `Calculator`, `Counter`, and `GenericService` structs to utilize `ToolRouter` for handling tool calls.
- Introduced `Parameters` struct for better parameter handling in tool functions.
- Added new methods for listing tools and calling tools in server handlers.
- Improved test cases to reflect changes in tool routing and parameter handling.
- Updated documentation and examples to align with the new router structure.

* fix: fix fmt and build error

* fix: fix test failure

* docs: documents for macros, fix ci

* fix: fix ci

* fix: fix wrongly replaced documents

* fix: remove useless file

* fix: change the parameter format for tool_router

* fix: update extract_doc_line to handle existing documentation and clean up unused code in server handler

* doc: update document for macro and examples

* doc: update readme and add contribute guide

* fix: fix type
This commit is contained in:
4t145 2025-06-24 04:01:22 +08:00 • committed by GitHub
commit 1f7f4d3055
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,8 +2,9 @@ use std::sync::Arc;
use rmcp::{
ServerHandler,
handler::server::{router::tool::ToolRouter, tool::Parameters},
model::{ServerCapabilities, ServerInfo},
schemars, tool,
schemars, tool, tool_handler, tool_router,
};
#[allow(dead_code)]
@ -40,14 +41,21 @@ impl DataService for MemoryDataService {
pub struct GenericService<DS: DataService> {
#[allow(dead_code)]
data_service: Arc<DS>,
tool_router: ToolRouter<Self>,
}
#[tool(tool_box)]
#[derive(Debug, schemars::JsonSchema, serde::Deserialize, serde::Serialize)]
pub struct SetDataRequest {
pub data: String,
}
#[tool_router]
impl<DS: DataService> GenericService<DS> {
#[allow(dead_code)]
pub fn new(data_service: DS) -> Self {
Self {
data_service: Arc::new(data_service),
tool_router: Self::tool_router(),
}
}
@ -57,13 +65,16 @@ impl<DS: DataService> GenericService<DS> {
}
#[tool(description = "set memory to service")]
pub async fn set_data(&self, #[tool(param)] data: String) -> String {
pub async fn set_data(
&self,
Parameters(SetDataRequest { data }): Parameters<SetDataRequest>,
) -> String {
let new_data = data.clone();
format!("Current memory: {}", new_data)
}
}
#[tool(tool_box)]
#[tool_handler]
impl<DS: DataService> ServerHandler for GenericService<DS> {
fn get_info(&self) -> ServerInfo {
ServerInfo {