prism_mcp_rs/
lib.rs

1// Copyright (c) 2025 Prismworks AI Inc.
2// SPDX-License-Identifier: MIT
3
4//! # MCP Rust SDK (2025-06-18)
5//!
6//! A comprehensive Rust SDK for the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
7//! version 2025-06-18, providing both server and client implementations with MCP specification
8//! compliance including audio content, annotations, and improved capabilities.
9// !
10//! ## Features
11//!
12//! - ⚡ **High Performance**: Built with Rust's zero-cost abstractions and async/await
13//! - 🛡️ **Type Safety**: Leverages Rust's type system to prevent runtime errors
14//! - 🔌 **Multiple Transports**: Support for STDIO, HTTP/SSE, and WebSocket transports
15//! - ✅ **MCP 2025-06-18 Compliance**: Comprehensive implementation of the latest MCP specification
16//! - 🚀 **Rich Ecosystem**: Tools, resources, prompts, and sampling support
17//! - 🎵 **Audio Support**: NEW in 2025-06-18 - Audio content support for multimodal interactions
18//! - 🏷️ **Annotations**: NEW in 2025-06-18 - Tool and content annotations for improved metadata
19//! - 💡 **Autocompletion**: NEW in 2025-06-18 - Argument autocompletion capabilities
20//! - 📁 **Roots Support**: NEW in 2025-06-18 - File system roots for improved resource access
21// !
22//! ## Quick Start
23//!
24//! The easiest way to get started is with the prelude module:
25//!
26//! ```rust
27//! use prism_mcp_rs::prelude::*;
28//! ```
29//!
30//! This imports all the commonly used types and traits.
31//!
32//! ### Server Example
33//!
34//! ```rust,ignore
35//! use prism_mcp_rs::prelude::*;
36//! use std::collections::HashMap;
37//! use serde_json::{json, Value};
38//! use async_trait::async_trait;
39//!
40//! struct EchoHandler;
41//!
42//! #[async_trait]
43//! impl ToolHandler for EchoHandler {
44//!     async fn call(&self, arguments: HashMap<String, Value>) -> McpResult<ToolResult> {
45//!         let message = arguments.get("message")
46//!             .and_then(|v| v.as_str())
47//!             .unwrap_or("Hello, World!");
48//!
49//!         Ok(ToolResult {
50//!             content: vec![ContentBlock::text(message)],
51//!             is_error: Some(false),
52//!             structured_content: None,
53//!             meta: None,
54//!         })
55//!     }
56//! }
57//!
58//! #[tokio::main]
59//! async fn main() -> McpResult<()> {
60//!     let mut server = McpServer::new("echo-server".to_string(), "1.0.0".to_string());
61//!
62//!     server.add_tool(
63//!         "echo".to_string(),
64//!         Some("Echo a message".to_string()),
65//!         json!({
66//!             "type": "object",
67//!             "properties": {
68//!                 "message": { "type": "string" }
69//!             }
70//!         }),
71//!         EchoHandler,
72//!     ).await?;
73//!
74//!     #[cfg(feature = "stdio")]
75//!     server.run_with_stdio().await?;
76//!     
77//!     Ok(())
78//! }
79//! ```
80//!
81//! ### Client Example
82//!
83//! ```rust,ignore
84//! use prism_mcp_rs::prelude::*;
85//! use std::collections::HashMap;
86//! use serde_json::{json, Value};
87//!
88//! #[tokio::main]
89//! async fn main() -> McpResult<()> {
90//!     // Create a client
91//!     let mut client = McpClient::new("my-client".to_string(), "1.0.0".to_string());
92//!     
93//!     // Set up transport (stdio feature required)
94//!     #[cfg(feature = "stdio")]
95//!     {
96//!         use prism_mcp_rs::transport::StdioClientTransport;
97//!         let transport = StdioClientTransport::new("server-command", vec!["arg1"]).await?;
98//!         client.connect(transport).await?;
99//!     }
100//!     
101//!     // List available tools  
102//!     let tools = client.list_tools(None).await?;
103//!     println!("Available tools: {:?}", tools);
104//!     
105//!     // Call a tool
106//!     let mut args = HashMap::new();
107//!     args.insert("message".to_string(), json!("Hello from client!"));
108//!     
109//!     let result = client.call_tool(
110//!         "echo".to_string(),
111//!         Some(args)
112//!     ).await?;
113//!     
114//!     println!("Tool result: {:?}", result);
115//!     Ok(())
116//! }
117//! ```
118//!
119//! ## Module Organization
120//!
121//! - [`core`]: Core abstractions for resources, tools, prompts, and errors
122//! - plugin: Plugin system for dynamic tool loading
123//! - [`protocol`]: MCP protocol types and message definitions (2025-06-18)
124//! - [`transport`]: Transport layer implementations (STDIO, HTTP, WebSocket)
125//! - [`server`]: MCP server implementation and lifecycle management
126//! - [`client`]: MCP client implementation and session management
127//! - [`utils`]: Utility functions and helpers
128
129#[cfg(feature = "http")]
130pub mod auth;
131pub mod client;
132pub mod core;
133#[cfg(feature = "plugin")]
134pub mod plugin;
135pub mod protocol;
136pub mod server;
137pub mod transport;
138pub mod utils;
139
140// Re-export commonly used types for convenience
141pub use core::error::{McpError, McpResult};
142pub use protocol::types::*;
143pub use protocol::{
144    ErrorObject, JsonRpcError, JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, ServerCapabilities,
145};
146
147/// Prelude module for convenient imports (2025-06-18)
148///
149/// Module re-exports the most commonly used types and traits for easy access.
150/// Use `use prism_mcp_rs::prelude::*;` to import everything you need.
151pub mod prelude {
152    // Core types and traits
153    pub use crate::core::{
154        error::{McpError, McpResult},
155        prompt::{Prompt, PromptHandler},
156        resource::{Resource, ResourceHandler},
157        tool::{Tool, ToolHandler},
158    };
159
160    // Protocol types and messages
161    pub use crate::protocol::error_codes;
162    pub use crate::protocol::error_helpers::IntoJsonRpcMessage;
163    pub use crate::protocol::messages::*;
164    pub use crate::protocol::missing_types::*;
165    pub use crate::protocol::types::*;
166
167    // Client and completion handlers
168    pub use crate::client::{
169        AutomatedClientRequestHandler, ClientRequestHandler, InteractiveClientRequestHandler,
170    };
171    pub use crate::core::completion::{
172        CompletionHandler, PromptCompletionHandler, ResourceUriCompletionHandler,
173    };
174    pub use crate::core::completion_handlers::{
175        CompositeCompletionHandler as completeCompositeCompletionHandler,
176        FileSystemCompletionHandler, FuzzyCompletionHandler, SchemaCompletionHandler,
177    };
178
179    // Server and Client
180    pub use crate::client::McpClient;
181    pub use crate::server::{McpServer, ServerBuilder, ServerConfig};
182
183    // Transport layer implementations
184    #[cfg(feature = "stdio")]
185    pub use crate::transport::{StdioClientTransport, StdioServerTransport};
186
187    #[cfg(feature = "http")]
188    pub use crate::transport::{HttpClientTransport, HttpServerTransport};
189
190    #[cfg(feature = "websocket")]
191    pub use crate::transport::{WebSocketClientTransport, WebSocketServerTransport};
192
193    // Plugin system
194    #[cfg(feature = "plugin")]
195    pub use crate::plugin::{LoadResult, LoadedPluginInfo, PluginManager};
196
197    // Tool builder
198    pub use crate::core::tool::ToolBuilder;
199
200    // Essential external types
201    pub use async_trait::async_trait;
202    pub use serde_json::{json, Value};
203    pub use std::collections::HashMap;
204}
205
206// Testing utilities (only available in tests)
207#[cfg(test)]
208pub mod test_utils;
209
210#[cfg(test)]
211mod tests {
212    use super::*;
213
214    #[test]
215    fn test_library_exports() {
216        // Basic smoke test to ensure all modules are accessible
217        let _error = McpError::Protocol("test".to_string());
218    }
219}