1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Procedural macros for MCP server and tool definitions.
//!
//! This crate provides macros to reduce boilerplate when defining MCP servers:
//!
//! - `#[mcp_server]` - Define server metadata and collect tools from impl blocks
//! - `#[mcp_tool]` - Mark a method as an MCP tool (used within `#[mcp_server]` impl blocks)
//! - `#[param(...)]` - Mark a parameter with description for the tool schema
//!
//! # Example
//!
//! ```rust,ignore
//! use model_context_protocol::macros::mcp_server;
//!
//! #[mcp_server(name = "calculator", description = "A simple calculator server")]
//! pub struct Calculator;
//!
//! #[mcp_server]
//! impl Calculator {
//! #[mcp_tool("Add two numbers together")]
//! pub fn add(
//! &self,
//! #[param("The first number")] a: f64,
//! #[param("The second number")] b: f64,
//! ) -> f64 {
//! a + b
//! }
//! }
//! ```
//!
//! Note: `#[mcp_tool]` and `#[param]` are inert marker attributes processed by `#[mcp_server]`.
//! They should only be used within impl blocks marked with `#[mcp_server]`.
use TokenStream;
/// Marks a struct as an MCP server or an impl block as containing MCP tools.
///
/// When applied to a struct, it adds server name and version metadata.
/// When applied to an impl block, it processes methods marked with `#[mcp_tool]`
/// and generates the `MacroServer` trait implementation.
///
/// # On Structs
///
/// ```rust,ignore
/// #[mcp_server(name = "my-server", version = "1.0.0", description = "My server description")]
/// pub struct MyServer { ... }
/// ```
///
/// Available attributes:
/// - `name` - Server name (defaults to lowercase struct name)
/// - `version` - Server version (optional)
/// - `description` - Server description (optional)
///
/// # On Impl Blocks
///
/// ```rust,ignore
/// #[mcp_server]
/// impl MyServer {
/// #[mcp_tool(description = "Tool description")]
/// pub fn my_tool(
/// &self,
/// #[param("Parameter description")] param: String,
/// ) -> Result<String, String> { ... }
/// }
/// ```
///
/// # Parameter Attributes
///
/// Within `#[mcp_tool]` methods, use `#[param(...)]` on parameters:
///
/// **Shorthand (recommended):**
/// - `#[param("description")]` - Just the description
///
/// **Full form:**
/// - `#[param(description = "...", name = "...", required = true)]`
///
/// Options:
/// - `description` - Description shown to the LLM
/// - `name` - Custom parameter name override (optional)
/// - `required` - Override required/optional inference (optional)
///
/// All non-self parameters must have `#[param(...)]` - unmarked parameters cause compile errors.
/// Marks a method as an MCP tool.
///
/// **Note**: This is an inert marker attribute that should only be used within
/// impl blocks marked with `#[mcp_server]`. When used outside of `#[mcp_server]`,
/// it will generate tool metadata but won't be collected into a server.
///
/// # Example
///
/// ```rust,ignore
/// #[mcp_server]
/// impl MyServer {
/// #[mcp_tool(description = "Store a value in memory")]
/// pub fn memory_write(
/// &self,
/// #[mcp(description = "The scope/namespace")]
/// scope: String,
/// #[mcp(description = "The key to store under")]
/// key: String,
/// ) -> Result<String, String> {
/// // implementation
/// }
/// }
/// ```