rust-sdk/crates/rmcp/tests/test_complex_schema.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

65 lines
1.5 KiB
Rust

use rmcp::{
Error as McpError, handler::server::tool::Parameters, model::*, schemars, tool, tool_router,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub enum ChatRole {
System,
User,
Assistant,
Tool,
}
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ChatMessage {
pub role: ChatRole,
pub content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ChatRequest {
pub system: Option<String>,
pub messages: Vec<ChatMessage>,
}
#[derive(Clone, Default)]
pub struct Demo;
#[tool_router]
impl Demo {
pub fn new() -> Self {
Self
}
#[tool(description = "LLM")]
async fn chat(
&self,
chat_request: Parameters<ChatRequest>,
) -> Result<CallToolResult, McpError> {
let content = Content::json(chat_request.0)?;
Ok(CallToolResult::success(vec![content]))
}
}
#[test]
fn test_complex_schema() {
let attr = Demo::chat_tool_attr();
let input_schema = attr.input_schema;
let enum_number = input_schema
.get("definitions")
.unwrap()
.as_object()
.unwrap()
.get("ChatRole")
.unwrap()
.as_object()
.unwrap()
.get("enum")
.unwrap()
.as_array()
.unwrap()
.len();
assert_eq!(enum_number, 4);
println!("{}", serde_json::to_string_pretty(&input_schema).unwrap());
}