Ethereal Rust MCP server

Provides a flexible, type-safe Rust foundation for building applications with tool management, authentication handling, and custom middleware support, offering both stdio and SSE transports.
Back to servers
Setup instructions
Provider
ethereumdegen
Release date
Mar 04, 2025
Language
Rust
Stats
3 stars

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.

Installation

Add mcp-rs to your Cargo.toml dependencies:

[dependencies]
mcp-rs = "0.1.0"

Basic Usage

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(())
}

Authentication

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(())
}

Using Middleware

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(())
}

How Middleware Works

Middleware in mcp-rs follows an "onion-like" pattern:

  • Executes in the order it was registered
  • Each middleware can perform pre-processing before calling next()
  • Post-processing (code after next()) runs in reverse order after the core handler completes

Transport Options

Using StdioServerTransport

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(())
}

Running Examples

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

How to install this MCP server

For Claude Code

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.

For Cursor

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.

Adding an MCP server to Cursor globally

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"
            ]
        }
    }
}

Adding an MCP server to a project

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.

How to use the MCP server

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.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.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

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later