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, input schema, meta, and title derived from the struct’s fields and attributes.

§Attributes

  • name - The name of the tool (required, non-empty string).
  • description - A description of the tool (required, non-empty string).
  • meta - Optional JSON object as a string literal for metadata.
  • title - Optional string for the tool’s title.

§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",
    meta = "{\"version\": \"1.0\"}",
    title = "Example Tool"
)]
#[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.meta.as_ref().unwrap().get("version").unwrap(), "1.0");
assert_eq!(tool.title.unwrap(), "Example Tool");

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"));