mcp_protocol_sdk/
lib.rs

1// Copyright (c) 2025 MCP Rust Contributors
2// SPDX-License-Identifier: MIT
3
4//! # MCP Rust SDK
5//!
6//! A comprehensive Rust SDK for the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/),
7//! providing both server and client implementations with full MCP specification compliance.
8//!
9//! ## Features
10//!
11//! - 🚀 **High Performance**: Built with Rust's zero-cost abstractions and async/await
12//! - 🛡️ **Type Safety**: Leverages Rust's type system to prevent runtime errors
13//! - 🔌 **Multiple Transports**: Support for STDIO, HTTP/SSE, and WebSocket transports
14//! - 🎯 **Full MCP Compliance**: Complete implementation of the MCP specification
15//! - 📚 **Rich Ecosystem**: Tools, resources, prompts, and sampling support
16//!
17//! ## Quick Start
18//!
19//! ### Server Example
20//!
21//! ```rust,no_run
22//! use mcp_protocol_sdk::{
23//!     server::McpServer,
24//!     core::{tool::ToolHandler, error::McpResult},
25//!     protocol::types::{Content, ToolResult},
26//! };
27//! use async_trait::async_trait;
28//! use std::collections::HashMap;
29//! use serde_json::Value;
30//!
31//! struct EchoHandler;
32//!
33//! #[async_trait]
34//! impl ToolHandler for EchoHandler {
35//!     async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
36//!         let message = arguments.get("message")
37//!             .and_then(|v| v.as_str())
38//!             .unwrap_or("Hello, World!");
39//!         
40//!         Ok(ToolResult {
41//!             content: vec![Content::Text { text: message.to_string() }],
42//!             is_error: None,
43//!         })
44//!     }
45//! }
46//!
47//! #[tokio::main]
48//! async fn main() -> McpResult<()> {
49//!     let mut server = McpServer::new("echo-server".to_string(), "1.0.0".to_string());
50//!     
51//!     server.add_tool(
52//!         "echo".to_string(),
53//!         Some("Echo a message".to_string()),
54//!         serde_json::json!({
55//!             "type": "object",
56//!             "properties": {
57//!                 "message": { "type": "string" }
58//!             }
59//!         }),
60//!         EchoHandler,
61//!     ).await?;
62//!     
63//!     Ok(())
64//! }
65//! ```
66//!
67//! ## Module Organization
68//!
69//! - [`core`]: Core abstractions for resources, tools, prompts, and errors
70//! - [`protocol`]: MCP protocol types and message definitions  
71//! - [`transport`]: Transport layer implementations (STDIO, HTTP, WebSocket)
72//! - [`server`]: MCP server implementation and lifecycle management
73//! - [`client`]: MCP client implementation and session management
74//! - [`utils`]: Utility functions and helpers
75
76pub mod client;
77pub mod core;
78pub mod protocol;
79pub mod server;
80pub mod transport;
81pub mod utils;
82
83// Re-export commonly used types for convenience
84pub use core::error::{McpError, McpResult};
85pub use protocol::types::*;
86
87/// Prelude module for convenient imports
88pub mod prelude {
89    pub use crate::client::McpClient;
90    pub use crate::core::{
91        error::{McpError, McpResult},
92        prompt::{Prompt, PromptHandler},
93        resource::{Resource, ResourceHandler},
94        tool::{Tool, ToolHandler},
95        PromptInfo, ResourceInfo, ToolInfo,
96    };
97    pub use crate::protocol::types::*;
98    pub use crate::server::McpServer;
99    pub use async_trait::async_trait;
100    pub use serde_json::{json, Value};
101    pub use std::collections::HashMap;
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_library_exports() {
110        // Basic smoke test to ensure all modules are accessible
111        let _error = McpError::Protocol("test".to_string());
112    }
113}