Skip to content

fix: allow optional trailing commas in tool_box macro #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/rust-mcp-sdk/src/mcp_macros/tool_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/// // //.......
/// // }
macro_rules! tool_box {
($enum_name:ident, [$($tool:ident),*]) => {
($enum_name:ident, [$($tool:ident),* $(,)?]) => {
#[derive(Debug)]
pub enum $enum_name {
$(
Expand Down
52 changes: 52 additions & 0 deletions crates/rust-mcp-sdk/tests/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,55 @@ pub fn sse_event(sse_raw: &str) -> String {
pub fn sse_data(sse_raw: &str) -> String {
sse_raw.replace("data: ", "")
}

pub mod sample_tools {
use rust_mcp_sdk::macros::{mcp_tool, JsonSchema};
use rust_mcp_sdk::schema::{schema_utils::CallToolError, CallToolResult};

//****************//
// SayHelloTool //
//****************//
#[mcp_tool(
name = "say_hello",
description = "Accepts a person's name and says a personalized \"Hello\" to that person",
idempotent_hint = false,
destructive_hint = false,
open_world_hint = false,
read_only_hint = false
)]
#[derive(Debug, ::serde::Deserialize, ::serde::Serialize, JsonSchema)]
pub struct SayHelloTool {
/// The name of the person to greet with a "Hello".
name: String,
}

impl SayHelloTool {
pub fn call_tool(&self) -> Result<CallToolResult, CallToolError> {
let hello_message = format!("Hello, {}!", self.name);
Ok(CallToolResult::text_content(hello_message, None))
}
}

//******************//
// SayGoodbyeTool //
//******************//
#[mcp_tool(
name = "say_goodbye",
description = "Accepts a person's name and says a personalized \"Goodbye\" to that person.",
idempotent_hint = false,
destructive_hint = false,
open_world_hint = false,
read_only_hint = false
)]
#[derive(Debug, ::serde::Deserialize, ::serde::Serialize, JsonSchema)]
pub struct SayGoodbyeTool {
/// The name of the person to say goodbye to.
name: String,
}
impl SayGoodbyeTool {
pub fn call_tool(&self) -> Result<CallToolResult, CallToolError> {
let hello_message = format!("Goodbye, {}!", self.name);
Ok(CallToolResult::text_content(hello_message, None))
}
}
}
28 changes: 28 additions & 0 deletions crates/rust-mcp-sdk/tests/test_tool_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[path = "common/common.rs"]
pub mod common;

use common::sample_tools::{SayGoodbyeTool, SayHelloTool};
use rust_mcp_sdk::tool_box;

// Define tool box without trailing comma
tool_box!(FileSystemToolsNoComma, [SayHelloTool, SayGoodbyeTool]);

// Define tool box with trailing comma
// Related Issue: https://github.com/rust-mcp-stack/rust-mcp-sdk/issues/57
tool_box!(FileSystemTools, [SayHelloTool, SayGoodbyeTool,]);

#[test]
fn test_tools_with_trailing_comma() {
let tools = FileSystemTools::tools();
assert_eq!(tools.len(), 2);
assert_eq!(tools[0].name, "say_hello");
assert_eq!(tools[1].name, "say_goodbye");
}

#[test]
fn test_tools_without_trailing_comma() {
let tools = FileSystemToolsNoComma::tools();
assert_eq!(tools.len(), 2);
assert_eq!(tools[0].name, "say_hello");
assert_eq!(tools[1].name, "say_goodbye");
}