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 - requires "stdio" feature in Cargo.toml:
94//! // prism-mcp-rs = { version = "*", features = ["stdio"] }
95//! #[cfg(feature = "stdio")]
96//! {
97//! use prism_mcp_rs::transport::StdioClientTransport;
98//! let transport = StdioClientTransport::new("server-command", vec!["arg1"]).await?;
99//! client.connect(transport).await?;
100//! }
101//!
102//! // List available tools
103//! let tools = client.list_tools(None).await?;
104//! println!("Available tools: {:?}", tools);
105//!
106//! // Call a tool
107//! let mut args = HashMap::new();
108//! args.insert("message".to_string(), json!("Hello from client!"));
109//!
110//! let result = client.call_tool(
111//! "echo".to_string(),
112//! Some(args)
113//! ).await?;
114//!
115//! println!("Tool result: {:?}", result);
116//! Ok(())
117//! }
118//! ```
119//!
120//! ## Module Organization
121//!
122//! - [`core`]: Core abstractions for resources, tools, prompts, and errors
123//! - plugin: Plugin system for dynamic tool loading
124//! - [`protocol`]: MCP protocol types and message definitions (2025-06-18)
125//! - [`transport`]: Transport layer implementations (STDIO, HTTP, WebSocket)
126//! - [`server`]: MCP server implementation and lifecycle management
127//! - [`client`]: MCP client implementation and session management
128//! - [`utils`]: Utility functions and helpers
129
130#[cfg(feature = "http")]
131pub mod auth;
132pub mod client;
133pub mod core;
134#[cfg(feature = "plugin")]
135pub mod plugin;
136pub mod protocol;
137pub mod server;
138pub mod transport;
139pub mod utils;
140
141// Re-export commonly used types for convenience
142pub use core::error::{McpError, McpResult};
143pub use protocol::types::*;
144pub use protocol::{
145 ErrorObject, JsonRpcError, JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, ServerCapabilities,
146};
147
148/// Prelude module for convenient imports (2025-06-18)
149///
150/// Module re-exports the most commonly used types and traits for easy access.
151/// Use `use prism_mcp_rs::prelude::*;` to import everything you need.
152pub mod prelude {
153 // Core types and traits
154 pub use crate::core::{
155 error::{McpError, McpResult},
156 prompt::{Prompt, PromptHandler},
157 resource::{Resource, ResourceHandler},
158 tool::{Tool, ToolHandler},
159 };
160
161 // Protocol types and messages
162 pub use crate::protocol::error_codes;
163 pub use crate::protocol::error_helpers::IntoJsonRpcMessage;
164 pub use crate::protocol::messages::*;
165 pub use crate::protocol::missing_types::*;
166 pub use crate::protocol::types::*;
167
168 // Client and completion handlers
169 pub use crate::client::{
170 AutomatedClientRequestHandler, ClientRequestHandler, InteractiveClientRequestHandler,
171 };
172 pub use crate::core::completion::{
173 CompletionHandler, PromptCompletionHandler, ResourceUriCompletionHandler,
174 };
175 pub use crate::core::completion_handlers::{
176 CompositeCompletionHandler as ExtendedCompositeCompletionHandler,
177 FileSystemCompletionHandler, FuzzyCompletionHandler, SchemaCompletionHandler,
178 };
179
180 // Server and Client
181 pub use crate::client::McpClient;
182 pub use crate::server::{McpServer, ServerBuilder, ServerConfig};
183
184 // Transport layer implementations
185 #[cfg(feature = "stdio")]
186 pub use crate::transport::{StdioClientTransport, StdioServerTransport};
187
188 #[cfg(feature = "http")]
189 pub use crate::transport::{HttpClientTransport, HttpServerTransport};
190
191 #[cfg(feature = "websocket")]
192 pub use crate::transport::{WebSocketClientTransport, WebSocketServerTransport};
193
194 // Plugin system
195 #[cfg(feature = "plugin")]
196 pub use crate::plugin::{LoadResult, LoadedPluginInfo, PluginManager};
197
198 // Tool builder
199 pub use crate::core::tool::ToolBuilder;
200
201 // Essential external types
202 pub use async_trait::async_trait;
203 pub use serde_json::{json, Value};
204 pub use std::collections::HashMap;
205}
206
207// Testing utilities (only available in tests)
208#[cfg(test)]
209pub mod test_utils;
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn test_library_exports() {
217 // Basic smoke test to ensure all modules are accessible
218 let _error = McpError::Protocol("test".to_string());
219 }
220}