mcp-host-macros 0.1.7

Procedural macros for mcp-host crate
Documentation
//! Procedural macros for mcp-host
//!
//! Provides attribute macros for ergonomic MCP tool, resource, and prompt definition.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use mcp_host::prelude::*;
//! use schemars::JsonSchema;
//!
//! #[derive(Deserialize, JsonSchema)]
//! struct CalcParams {
//!     x: f64,
//!     y: f64,
//! }
//!
//! #[mcp_tool_router]
//! impl MyServer {
//!     /// A calculator tool
//!     #[mcp_tool(name = "calculate")]
//!     async fn calculate(&self, ctx: Ctx, params: Parameters<CalcParams>) -> ToolResult {
//!         Ok(vec![text(&format!("Result: {}", params.0.x + params.0.y))])
//!     }
//!
//!     #[mcp_tool(visible = "ctx.is_admin()")]
//!     async fn admin_tool(&self, ctx: Ctx, params: Parameters<AdminParams>) -> ToolResult {
//!         // Only visible to admin users
//!     }
//! }
//!
//! // Registration:
//! let server = Arc::new(MyServer::new());
//! MyServer::tool_router().register_all(&registry, server);
//! ```

mod tool;
mod tool_router;

use proc_macro::TokenStream;

/// Mark an async function as an MCP tool handler
///
/// Generates:
/// - `{fn_name}_tool_info()` - Returns `ToolInfo` with schema from `Parameters<T>`
/// - `{fn_name}_handler()` - Handler wrapper for the router
/// - `{fn_name}_visibility()` - Visibility predicate (if `visible` attribute specified)
///
/// # Attributes
///
/// - `name`: Tool name (default: function name)
/// - `description`: Tool description (default: doc comments)
/// - `visible`: Visibility predicate expression (default: always visible)
///
/// # Example
///
/// ```rust,ignore
/// #[mcp_tool(name = "calculate", description = "Performs math")]
/// async fn calculate(&self, ctx: Ctx, params: Parameters<CalcParams>) -> ToolResult {
///     Ok(vec![text(&format!("Result: {}", params.0.value))])
/// }
/// ```
#[proc_macro_attribute]
pub fn mcp_tool(attr: TokenStream, item: TokenStream) -> TokenStream {
    tool::expand_mcp_tool(attr, item)
}

/// Generate a tool router from an impl block
///
/// Collects all methods marked with `#[mcp_tool]` and generates a `tool_router()`
/// method that returns an `McpToolRouter`.
///
/// # Attributes
///
/// - `router`: Name of generated router function (default: "tool_router")
///
/// # Example
///
/// ```rust,ignore
/// #[mcp_tool_router]
/// impl MyServer {
///     #[mcp_tool]
///     async fn tool_a(&self, ctx: Ctx, params: Parameters<ParamsA>) -> ToolResult { ... }
///
///     #[mcp_tool]
///     async fn tool_b(&self, ctx: Ctx, params: Parameters<ParamsB>) -> ToolResult { ... }
/// }
///
/// // Generated: MyServer::tool_router() -> McpToolRouter<MyServer>
/// ```
#[proc_macro_attribute]
pub fn mcp_tool_router(attr: TokenStream, item: TokenStream) -> TokenStream {
    tool_router::expand_mcp_tool_router(attr, item)
}