Crate mcp_rs

Source
Expand description

Β§MCP Rust SDK (2025-03-26) - UNIFIED EDITION

πŸ¦€ The complete, unified Rust SDK for the Model Context Protocol (MCP) version 2025-03-26. This crate consolidates all MCP functionality into a single, powerful library.

Β§πŸ“¦ Consolidation Notice (v0.4.0+)

Starting with v0.4.0, this crate includes ALL MCP functionality:

  • βœ… Client implementation (replaces mcp-protocol-client)
  • βœ… Server implementation (replaces mcp-protocol-server)
  • βœ… Core types (replaces mcp-protocol-types)
  • βœ… All transports (STDIO, HTTP/SSE, WebSocket)
  • βœ… Full MCP 2025-03-26 compliance

Migration: Simply replace all separate crates with mcp-protocol-sdk = "0.4.0"

Β§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 2025-03-26 Compliance: Complete implementation of the latest MCP specification
  • πŸ“š Rich Ecosystem: Tools, resources, prompts, and sampling support
  • 🎡 Audio Support: NEW in 2025-03-26 - Audio content support for multimodal interactions
  • 🏷️ Annotations: NEW in 2025-03-26 - Tool and content annotations for enhanced metadata
  • πŸ”§ Autocompletion: NEW in 2025-03-26 - Argument autocompletion capabilities
  • πŸ“ Roots Support: NEW in 2025-03-26 - File system roots for enhanced resource access
  • πŸ‘₯ Client & Server: Complete implementations for both sides of MCP communication

Β§Quick Start

Β§Server Example

use mcp_protocol_sdk::{
    server::McpServer,
    core::{tool::ToolHandler, error::McpResult},
    protocol::types::{Content, CallToolResult},
};
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<CallToolResult> {
        let message = arguments.get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Hello, World!");
         
        Ok(CallToolResult {
            content: vec![Content::text(message)],
            is_error: Some(false),
            meta: 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(())
}

Β§Client Example

use mcp_protocol_sdk::{
    client::McpClient,
    core::error::McpResult,
};

#[tokio::main]
async fn main() -> McpResult<()> {
    let mut client = McpClient::new_stdio("path/to/server").await?;
     
    // List available tools
    let tools = client.list_tools().await?;
    println!("Available tools: {:?}", tools);
     
    // Call a tool
    let result = client.call_tool("echo", serde_json::json!({
        "message": "Hello from client!"
    })).await?;
     
    println!("Tool result: {:?}", result);
    Ok(())
}

Β§Module Organization

  • core: Core abstractions for resources, tools, prompts, and errors
  • protocol: MCP protocol types and message definitions (2025-03-26)
  • 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 (2025-03-26)
protocol
MCP protocol implementation (2025-03-26)
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