This Rust implementation of the MCP server provides a flexible framework for deploying Model Context Protocol services. It's a port of the original TypeScript implementation with complete feature parity.
Add mcp-rs to your Cargo.toml dependencies:
[dependencies]
mcp-rs = "0.1.0"
Here's how to create a simple MCP server with a tool that adds two numbers:
use mcp_rs::{Emcp, SimpleToolBuilder};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new server instance
let server = Emcp::new("my-mcp-server", "1.0.0", None);
// Add a tool
let add_tool = SimpleToolBuilder::new("add", |args| async move {
let a = args["a"].as_f64().unwrap_or(0.0);
let b = args["b"].as_f64().unwrap_or(0.0);
Ok(json!(a + b))
})
.description("Add two numbers")
.input_schema(json!({
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["a", "b"]
}))
.build();
server.add_tool(add_tool);
// Start the server
server.start(None).await?;
Ok(())
}
You can implement authentication by providing a custom handler:
use mcp_rs::{Emcp, EmcpOptions, SimpleToolBuilder};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create authentication handler
let auth_handler = Box::new(|request| {
// Check auth headers, tokens, etc.
Box::pin(async {
// Return true for authorized requests
true
})
});
// Create a new server with auth handler
let server = Emcp::new(
"mcp-server-with-auth",
"1.0.0",
Some(EmcpOptions {
authentication_handler: Some(auth_handler),
}),
);
// Add tools, resources, or prompts...
// Start the server
server.start(None).await?;
Ok(())
}
mcp-rs supports middleware for request preprocessing and postprocessing:
use mcp_rs::{Emcp, SimpleToolBuilder};
use std::time::Instant;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new server
let mut server = Emcp::new("mcp-server-with-middleware", "1.0.0", None);
// Add timing middleware
server.use_middleware(|request, next| {
let start_time = Instant::now();
let method = request.method.clone();
Box::pin(async move {
// Call next middleware in the chain
let response = next().await;
// Post-processing
let duration = start_time.elapsed();
println!(
"Request completed: method={}, duration={}ms",
method,
duration.as_millis()
);
response
})
});
// Add tools, resources, or prompts...
// Start the server
server.start(None).await?;
Ok(())
}
Middleware in mcp-rs follows an "onion-like" pattern:
You can use the StdioServerTransport directly for command-line tools:
use mcp_rs::{Emcp, StdioServerTransport, SimpleToolBuilder};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new server
let server = Emcp::new("my-mcp-server", "1.0.0", None);
// Add your tools, resources, and prompts
// ...
// Create a StdioServerTransport
let transport = StdioServerTransport::new();
// Connect the server to the transport
server.connect(transport).await?;
Ok(())
}
Try the included examples to see mcp-rs in action:
# Run the basic example
cargo run --example basic
# Run the auth example
cargo run --example auth
# Run the middleware example
cargo run --example middleware
# Run the stdio transport example
cargo run --example stdio_transport
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "mcp-rs" '{"command":"cargo","args":["run","--package","mcp-rs"]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"mcp-rs": {
"command": "cargo",
"args": [
"run",
"--package",
"mcp-rs"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"mcp-rs": {
"command": "cargo",
"args": [
"run",
"--package",
"mcp-rs"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect