Skip to main content

Crate fastmcp_derive

Crate fastmcp_derive 

Source
Expand description

Procedural macros for FastMCP.

This crate provides attribute macros for defining MCP handlers:

  • #[tool] - Define a tool handler
  • #[resource] - Define a resource handler
  • #[prompt] - Define a prompt handler

§Example

use fastmcp_rust::prelude::*;

/// Greets a user by name.
#[tool]
async fn greet(
    ctx: &McpContext,
    /// The name to greet
    name: String,
) -> String {
    format!("Hello, {name}!")
}

/// A configuration file resource.
#[resource(uri = "config://app")]
async fn app_config(ctx: &McpContext) -> String {
    std::fs::read_to_string("config.json").unwrap()
}

/// A code review prompt.
#[prompt]
async fn code_review(
    ctx: &McpContext,
    /// The code to review
    code: String,
) -> Vec<PromptMessage> {
    vec![PromptMessage {
        role: Role::User,
        content: Content::Text { text: format!("Review this code:\n\n{code}") },
    }]
}

§Role in the System

fastmcp-derive is the ergonomics layer of FastMCP. The attribute macros expand handler functions into the trait implementations used by fastmcp-server, and they also generate JSON Schema metadata consumed by fastmcp-protocol during tool registration.

Most users never need to depend on this crate directly; it is re-exported by the fastmcp-rust façade for use fastmcp_rust::prelude::*.

Attribute Macros§

prompt
Defines a prompt handler.
resource
Defines a resource handler.
tool
Defines a tool handler.

Derive Macros§

JsonSchema
Derives JSON Schema for a type.