model_context_protocol_macros/lib.rs
1//! Procedural macros for MCP server and tool definitions.
2//!
3//! This crate provides macros to reduce boilerplate when defining MCP servers:
4//!
5//! - `#[mcp_server]` - Define server metadata and collect tools
6//! - `#[mcp_tool]` - Mark a method as an MCP tool
7//!
8//! # Example
9//!
10//! ```rust,ignore
11//! use mcp_macros::{mcp_server, mcp_tool};
12//!
13//! #[mcp_server(name = "memory", version = "1.0.0")]
14//! pub struct MemoryServer {
15//! store: MemoryStore,
16//! }
17//!
18//! #[mcp_server]
19//! impl MemoryServer {
20//! #[mcp_tool(description = "Store a value in memory")]
21//! pub fn memory_write(&self, scope: String, key: String, value: Value) -> ToolResult<String> {
22//! self.store.write(&scope, &key, value)
23//! }
24//! }
25//! ```
26
27use proc_macro::TokenStream;
28
29mod schema;
30mod server;
31mod tool;
32
33/// Marks a struct as an MCP server or an impl block as containing MCP tools.
34///
35/// # On Structs
36///
37/// ```rust,ignore
38/// #[mcp_server(name = "my-server", version = "1.0.0")]
39/// pub struct MyServer { ... }
40/// ```
41///
42/// # On Impl Blocks
43///
44/// ```rust,ignore
45/// #[mcp_server]
46/// impl MyServer {
47/// #[mcp_tool(description = "...")]
48/// pub fn my_tool(&self, ...) -> ToolResult<T> { ... }
49/// }
50/// ```
51#[proc_macro_attribute]
52pub fn mcp_server(attr: TokenStream, item: TokenStream) -> TokenStream {
53 server::mcp_server_impl(attr.into(), item.into()).into()
54}
55
56/// Marks a method as an MCP tool.
57///
58/// Parameter descriptions are extracted from doc comments on parameters.
59///
60/// # Example
61///
62/// ```rust,ignore
63/// #[mcp_tool(description = "Store a value in memory")]
64/// pub fn memory_write(
65/// &self,
66/// /// The scope/namespace for the memory
67/// scope: String,
68/// /// The key to store the value under
69/// key: String,
70/// /// The value to store
71/// value: Value,
72/// ) -> ToolResult<String> {
73/// self.store.write(&scope, &key, value)
74/// }
75/// ```
76#[proc_macro_attribute]
77pub fn mcp_tool(attr: TokenStream, item: TokenStream) -> TokenStream {
78 tool::mcp_tool_impl(attr.into(), item.into()).into()
79}