Crate mcp_protocol_sdk

Source
Expand description

§MCP Rust SDK

A comprehensive Rust SDK for the Model Context Protocol (MCP), providing both server and client implementations with full MCP specification compliance.

§Features

  • 🚀 High Performance: Built with Rust’s zero-cost abstractions and async/await
  • 🛡️ Type Safety: Leverages Rust’s type system to prevent runtime errors
  • 🔌 Multiple Transports: Support for STDIO, HTTP/SSE, and WebSocket transports
  • 🎯 Full MCP Compliance: Complete implementation of the MCP specification
  • 📚 Rich Ecosystem: Tools, resources, prompts, and sampling support

§Quick Start

§Server Example

use mcp_protocol_sdk::{
    server::McpServer,
    core::{tool::ToolHandler, error::McpResult},
    protocol::types::{Content, ToolResult},
};
use async_trait::async_trait;
use std::collections::HashMap;
use serde_json::Value;

struct EchoHandler;

#[async_trait]
impl ToolHandler for EchoHandler {
    async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
        let message = arguments.get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Hello, World!");
         
        Ok(ToolResult {
            content: vec![Content::Text { text: message.to_string() }],
            is_error: None,
        })
    }
}

#[tokio::main]
async fn main() -> McpResult<()> {
    let mut server = McpServer::new("echo-server".to_string(), "1.0.0".to_string());
     
    server.add_tool(
        "echo".to_string(),
        Some("Echo a message".to_string()),
        serde_json::json!({
            "type": "object",
            "properties": {
                "message": { "type": "string" }
            }
        }),
        EchoHandler,
    ).await?;
     
    Ok(())
}

§Module Organization

  • core: Core abstractions for resources, tools, prompts, and errors
  • protocol: MCP protocol types and message definitions
  • transport: Transport layer implementations (STDIO, HTTP, WebSocket)
  • server: MCP server implementation and lifecycle management
  • client: MCP client implementation and session management
  • utils: Utility functions and helpers

Re-exports§

pub use core::error::McpError;
pub use core::error::McpResult;
pub use protocol::types::*;

Modules§

client
MCP client implementation
core
Core abstractions and types for the MCP SDK
prelude
Prelude module for convenient imports
protocol
MCP protocol implementation
server
MCP server implementation
transport
Transport layer implementations
utils
Utility functions and helpers for the MCP Rust SDK

Macros§

tool
Helper macro for creating tools with schema validation