MCP Core Macros
A Rust library providing procedural macros for the MCP Core system.
Overview
This crate provides procedural macros that simplify the process of creating tool definitions for the MCP system. The macros handle generating tool metadata, parameter schemas, and the necessary boilerplate code for tool registration.
Macros
#[tool]
Attribute Macro
The tool
attribute macro transforms an async function into a tool that can be registered with the MCP system. It automatically generates:
- A struct named after the function (e.g.,
web_search_tool
→WebSearchTool
) - Tool definitions with proper metadata
- JSON schema for input parameters
- Methods to handle tool invocation
Arguments
name
- The name of the tool (optional, defaults to the function name)description
- A description of what the tool doesannotations
- Additional metadata for the tool:title
- Display title for the tool (defaults to function name)read_only_hint
- Whether the tool only reads data (defaults to false)destructive_hint
- Whether the tool makes destructive changes (defaults to true)idempotent_hint
- Whether the tool is idempotent (defaults to false)open_world_hint
- Whether the tool can access resources outside the system (defaults to true)
Example
use ;
use ToolResponseContent;
use tool_text_content;
use Result;
async
tool_param!
Macro
The tool_param!
macro allows specifying parameter attributes such as descriptions and visibility in the generated schema.
Arguments
hidden
- Excludes the parameter from the generated schemadescription
- Adds a description to the parameter in the schema
Example
use ;
use ToolResponseContent;
use tool_text_content;
use Result;
async
Generated Code
The tool
macro generates a structure with methods to handle tool registration and invocation. For example, the function:
async
Will generate code equivalent to:
;
License
This project is licensed under the Apache-2.0 License.