mcp_rs/lib.rs
1// Copyright (c) 2025 MCP Rust Contributors
2// SPDX-License-Identifier: MIT
3
4//! # MCP Rust SDK (2025-03-26) - UNIFIED EDITION
5//!
6//! 🦀 **The complete, unified Rust SDK for the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/)**
7//! version 2025-03-26. This crate consolidates all MCP functionality into a single, powerful library.
8//!
9//! ## 📦 Consolidation Notice (v0.4.0+)
10//!
11//! Starting with v0.4.0, this crate includes ALL MCP functionality:
12//! - ✅ **Client implementation** (replaces `mcp-protocol-client`)
13//! - ✅ **Server implementation** (replaces `mcp-protocol-server`)
14//! - ✅ **Core types** (replaces `mcp-protocol-types`)
15//! - ✅ **All transports** (STDIO, HTTP/SSE, WebSocket)
16//! - ✅ **Full MCP 2025-03-26 compliance**
17//!
18//! **Migration**: Simply replace all separate crates with `mcp-protocol-sdk = "0.4.0"`
19//!
20//! ## Features
21//!
22//! - 🚀 **High Performance**: Built with Rust's zero-cost abstractions and async/await
23//! - 🛡️ **Type Safety**: Leverages Rust's type system to prevent runtime errors
24//! - 🔌 **Multiple Transports**: Support for STDIO, HTTP/SSE, and WebSocket transports
25//! - 🎯 **Full MCP 2025-03-26 Compliance**: Complete implementation of the latest MCP specification
26//! - 📚 **Rich Ecosystem**: Tools, resources, prompts, and sampling support
27//! - 🎵 **Audio Support**: NEW in 2025-03-26 - Audio content support for multimodal interactions
28//! - 🏷️ **Annotations**: NEW in 2025-03-26 - Tool and content annotations for enhanced metadata
29//! - 🔧 **Autocompletion**: NEW in 2025-03-26 - Argument autocompletion capabilities
30//! - 📁 **Roots Support**: NEW in 2025-03-26 - File system roots for enhanced resource access
31//! - 👥 **Client & Server**: Complete implementations for both sides of MCP communication
32//!
33//! ## Quick Start
34//!
35//! ### Server Example
36//!
37//! ```rust,no_run
38//! use mcp_protocol_sdk::{
39//! server::McpServer,
40//! core::{tool::ToolHandler, error::McpResult},
41//! protocol::types::{Content, CallToolResult},
42//! };
43//! use async_trait::async_trait;
44//! use std::collections::HashMap;
45//! use serde_json::Value;
46//!
47//! struct EchoHandler;
48//!
49//! #[async_trait]
50//! impl ToolHandler for EchoHandler {
51//! async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<CallToolResult> {
52//! let message = arguments.get("message")
53//! .and_then(|v| v.as_str())
54//! .unwrap_or("Hello, World!");
55//!
56//! Ok(CallToolResult {
57//! content: vec![Content::text(message)],
58//! is_error: Some(false),
59//! meta: None,
60//! })
61//! }
62//! }
63//!
64//! #[tokio::main]
65//! async fn main() -> McpResult<()> {
66//! let mut server = McpServer::new("echo-server".to_string(), "1.0.0".to_string());
67//!
68//! server.add_tool(
69//! "echo".to_string(),
70//! Some("Echo a message".to_string()),
71//! serde_json::json!({
72//! "type": "object",
73//! "properties": {
74//! "message": { "type": "string" }
75//! }
76//! }),
77//! EchoHandler,
78//! ).await?;
79//!
80//! Ok(())
81//! }
82//! ```
83//!
84//! ### Client Example
85//!
86//! ```rust,no_run
87//! use mcp_protocol_sdk::{
88//! client::McpClient,
89//! core::error::McpResult,
90//! };
91//!
92//! #[tokio::main]
93//! async fn main() -> McpResult<()> {
94//! let mut client = McpClient::new_stdio("path/to/server").await?;
95//!
96//! // List available tools
97//! let tools = client.list_tools().await?;
98//! println!("Available tools: {:?}", tools);
99//!
100//! // Call a tool
101//! let result = client.call_tool("echo", serde_json::json!({
102//! "message": "Hello from client!"
103//! })).await?;
104//!
105//! println!("Tool result: {:?}", result);
106//! Ok(())
107//! }
108//! ```
109//!
110//! ## Module Organization
111//!
112//! - [`core`]: Core abstractions for resources, tools, prompts, and errors
113//! - [`protocol`]: MCP protocol types and message definitions (2025-03-26)
114//! - [`transport`]: Transport layer implementations (STDIO, HTTP, WebSocket)
115//! - [`server`]: MCP server implementation and lifecycle management
116//! - [`client`]: MCP client implementation and session management
117//! - [`utils`]: Utility functions and helpers
118
119pub mod client;
120pub mod core;
121pub mod protocol;
122pub mod server;
123pub mod transport;
124pub mod utils;
125
126// Re-export commonly used types for convenience
127pub use core::error::{McpError, McpResult};
128pub use protocol::types::*;
129
130/// Prelude module for convenient imports (2025-03-26)
131pub mod prelude {
132 pub use crate::client::McpClient;
133 pub use crate::core::{
134 error::{McpError, McpResult},
135 prompt::{Prompt, PromptHandler},
136 resource::{Resource, ResourceHandler},
137 tool::{Tool, ToolHandler},
138 };
139 pub use crate::protocol::types::*;
140 pub use crate::server::McpServer;
141 pub use async_trait::async_trait;
142 pub use serde_json::{json, Value};
143 pub use std::collections::HashMap;
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn test_library_exports() {
152 // Basic smoke test to ensure all modules are accessible
153 let _error = McpError::Protocol("test".to_string());
154 }
155}