rust-mcp-macros.
mcp_tool Macro
A procedural macro, part of the rust-mcp-sdk ecosystem, to generate rust_mcp_schema::Tool instance from 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 arust_mcp_schema::Toolinstance 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).title- An optional human-readable and easily understood title.meta- An optional JSON string that provides additional metadata for the tool.destructive_hint– Optional boolean, indicates whether the tool may make destructive changes to its environment.idempotent_hint– Optional boolean, indicates whether repeated calls with the same input have the same effect.open_world_hint– Optional boolean, indicates whether the tool can interact with external or unknown entities.read_only_hint– Optional boolean, indicates whether the tool makes no modifications to its environment.
Usage Example
Note: The following attributes are available only in version 2025_03_26 and later of the MCP Schema, and their values will be used in the annotations attribute of the *Tool struct.
destructive_hintidempotent_hintopen_world_hintread_only_hint
mcp_elicit Macro
The mcp_elicit macro generates implementations for the annotated struct to facilitate data elicitation. It enables struct to generate ElicitRequestedSchema and also parsing a map of field names to ElicitResultContentValue values back into the struct, supporting both required and optional fields. The generated implementation includes:
- A
message()method returning the elicitation message as a string. - A
requested_schema()method returning anElicitRequestedSchemabased on the struct’s JSON schema. - A
from_content_map()method to convert a map ofElicitResultContentValuevalues into a struct instance.
Attributes
message- An optional string (orconcat!(...)expression) to prompt the user or system for input. Defaults to an empty string if not provided.
Supported Field Types
String: Maps toElicitResultContentValue::String.bool: Maps toElicitResultContentValue::Boolean.i32: Maps toElicitResultContentValue::Integer(with bounds checking).i64: Maps toElicitResultContentValue::Integer.enumOnly simple enums are supported. The enum must implement the FromStr trait.Option<T>: Supported for any of the above types, mapping toNoneif the field is missing.
Usage Example
use ;
use RpcError;
use FromStr;
// Simple enum with FromStr trait implemented
// A struct that could be used to send elicit request and get the input from the user
// ....
// .......
// ...........
// send a Elicit Request , ask for UserInfo data and convert the result back to a valid UserInfo instance
let result: ElicitResult = server
.elicit_input
.await?;
// Create a UserInfo instance using data provided by the user on the client side
let user_info = from_content_map?;
Check out rust-mcp-sdk, a high-performance, asynchronous toolkit for building MCP servers and clients. Focus on your app's logic while rust-mcp-sdk takes care of the rest!