Attribute Macro mcp_tool

Source
#[mcp_tool]
Expand description

A procedural macro attribute to generate rust_mcp_schema::Tool related utility methods for a struct.

The mcp_tool macro generates an implementation for the annotated struct that includes:

  • A tool_name() method returning the tool’s name as a string.
  • A tool() method returning a rust_mcp_schema::Tool instance with the tool’s name, description, and input schema derived from the struct’s fields.

§Attributes

  • name - The name of the tool (required, non-empty string).
  • description - A description of the tool (required, non-empty string).

§Panics

Panics if the macro is applied to anything other than a struct.

§Example

#[rust_mcp_macros::mcp_tool(name = "example_tool", description = "An example tool", idempotent_hint=true )]
#[derive(rust_mcp_macros::JsonSchema)]
struct ExampleTool {
    field1: String,
    field2: i32,
}

assert_eq!(ExampleTool::tool_name() , "example_tool");
let tool : rust_mcp_schema::Tool = ExampleTool::tool();
assert_eq!(tool.name , "example_tool");
assert_eq!(tool.description.unwrap() , "An example tool");
assert_eq!(tool.annotations.unwrap().idempotent_hint.unwrap() , true);

let schema_properties = tool.input_schema.properties.unwrap();
assert_eq!(schema_properties.len() , 2);
assert!(schema_properties.contains_key("field1"));
assert!(schema_properties.contains_key("field2"));