Get the FREE Ultimate OpenClaw Setup Guide →

mcp-attr

A library for declaratively building Model Context Protocol servers.

Installation
Run this command in your terminal to add the MCP server to Claude Code.
Run in terminal:
Command
claude mcp add --transport stdio frozenlib-mcp-attr cargo run --bin mcp-attr-example

How to use

mcp-attr is a Rust crate designed to make it easy to declaratively build MCP servers using attribute macros. By annotating your server type with #[mcp_server] and implementing the McpServer trait, you describe the available MCP tools, prompts, resources, and other capabilities in a concise, type-safe way. The library focuses on reducing boilerplate and enabling AI-assisted code generation by providing descriptive metadata through comments and attributes. To run a server, you typically implement McpServer and call serve_stdio to start an MCP server over standard input/output, leveraging Tokio for asynchronous execution. This approach enables you to expose tools, prompts, and resources to MCP clients in a structured, human- and machine-readable manner.

To use it, you would create a small Rust binary that wires your server implementation into the runtime provided by mcp-attr. You can define methods decorated with #[tool], #[prompt], or #[resource] to indicate their role in the MCP protocol, and provide descriptive doc comments or description attributes to convey their meaning to clients (including AI). The library handles the protocol details, argument parsing, and return value conversion, allowing you to focus on the business logic of your server.

How to install

Prerequisites:

  • Rust toolchain (rustc, cargo) installed. You can install via rustup: https://rustup.rs/
  • A supported Rust edition (the crate examples use 2021/2024 compatibility).

Step-by-step:

  1. Install Rust toolchain
# If you don't have Rust installed
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  1. Create a new Rust project (or use an existing one)
cargo new my-mcp-server --bin
cd my-mcp-server
  1. Add mcp-attr as a dependency (and tokio for async runtime)
# Cargo.toml
[dependencies]
mcp-attr = "0.0.7"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
  1. Implement your MCP server using the examples from the README and annotate with #[mcp_server]. Build and run
cargo run
  1. If you’re testing locally, you can also wire a small binary to call serve_stdio with your server type as shown in the examples.

Additional notes

Tips and common issues:

  • Ensure you enable the Tokio runtime (e.g., #[tokio::main] async fn main) when starting the server.
  • Use interior mutability (e.g., Mutex) for shared server state across async methods, since &self is the only allowed receiver for MCP server methods.
  • Provide thorough documentation/comments for methods and arguments to improve AI interpretability by MCP clients.
  • Stay aligned with the supported protocol versions listed in the repository’s docs; future versions may add transports beyond stdio.
  • If you plan to expose resources, remember that resource methods should implement FromStr for arguments and return appropriate resource results as defined by the library traits.
  • Check cargo features or crate versions for compatibility with your Rust toolchain and other dependencies.

Related MCP Servers

Sponsor this space

Reach thousands of developers