rust-sdk/crates/rmcp/tests/test_tool_macro_annotations.rs
4t145 1f7f4d3055
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
2025-06-24 04:01:22 +08:00

46 lines
1.4 KiB
Rust

#[cfg(test)]
mod tests {
use rmcp::{ServerHandler, handler::server::router::tool::ToolRouter, tool, tool_handler};
#[derive(Debug, Clone, Default)]
pub struct AnnotatedServer {
tool_router: ToolRouter<AnnotatedServer>,
}
impl AnnotatedServer {
// Tool with inline comments for documentation
/// Direct annotation test tool
/// This is used to test tool annotations
#[tool(
name = "direct-annotated-tool",
annotations(title = "Annotated Tool", read_only_hint = true)
)]
pub async fn direct_annotated_tool(&self, input: String) -> String {
format!("Direct: {}", input)
}
}
#[tool_handler]
impl ServerHandler for AnnotatedServer {}
#[test]
fn test_direct_tool_attributes() {
// Get the tool definition
let tool = AnnotatedServer::direct_annotated_tool_tool_attr();
// Verify basic properties
assert_eq!(tool.name, "direct-annotated-tool");
// Verify description is extracted from doc comments
assert!(tool.description.is_some());
assert!(
tool.description
.as_ref()
.unwrap()
.contains("Direct annotation test tool")
);
let annotations = tool.annotations.unwrap();
assert_eq!(annotations.title.as_ref().unwrap(), "Annotated Tool");
assert_eq!(annotations.read_only_hint, Some(true));
}
}