Skip to main content

Crate turbomcp_macros

Crate turbomcp_macros 

Source
Expand description

§TurboMCP Macros

Zero-overhead procedural macros for ergonomic MCP server development.

§Usage

The #[server] macro transforms a struct impl block into a complete MCP server with automatic McpHandler trait implementation.

use turbomcp::prelude::*;

#[derive(Clone)]
struct Calculator;

#[server(name = "calculator", version = "1.0.0")]
impl Calculator {
    /// Add two numbers together
    #[tool]
    async fn add(
        &self,
        #[description("First operand")] a: i64,
        #[description("Second operand")] b: i64,
    ) -> i64 {
        a + b
    }

    /// Greet someone by name
    #[tool]
    async fn greet(
        &self,
        #[description("The name of the person to greet")] name: String,
    ) -> String {
        format!("Hello, {}!", name)
    }

    /// Get application configuration
    #[resource("config://app")]
    async fn config(&self, uri: String, ctx: &RequestContext) -> String {
        r#"{"debug": true}"#.to_string()
    }

    /// Generate a greeting prompt
    #[prompt]
    async fn greeting(&self, name: String, ctx: &RequestContext) -> String {
        format!("Hello {}! How can I help you today?", name)
    }
}

#[tokio::main]
async fn main() {
    Calculator.run_stdio().await.unwrap();
}

§Features

  • Zero Boilerplate: Just add #[server], #[tool], #[resource], #[prompt] attributes
  • Automatic Schema Generation: JSON schemas generated from Rust types
  • Per-Parameter Documentation: Use #[description("...")] for rich JSON Schema docs
  • Type-Safe Parameters: Function parameters become tool arguments
  • Doc Comments: /// comments become tool/resource/prompt descriptions
  • Complex Type Support: Use schemars::JsonSchema for nested object schemas
  • Multiple Transports: Run on STDIO, HTTP, WebSocket, TCP with .run_*() methods
  • Portable Code: Same server works on native and WASM with platform-specific entry points

Attribute Macros§

description
Provides a description for a tool parameter.
prompt
Marks a method as a prompt handler within a #[server] block.
resource
Marks a method as a resource handler within a #[server] block.
server
Marks an impl block as an MCP server with automatic McpHandler implementation.
tool
Marks a method as a tool handler within a #[server] block.