pulseengine_mcp_server/
lib.rs

1//! Generic MCP server infrastructure with pluggable backends
2//!
3//! This crate provides a complete MCP server implementation that can be extended
4//! with custom backends for different domains (home automation, databases, APIs, etc.).
5//!
6//! # Quick Start
7//!
8//! ```rust,ignore
9//! use pulseengine_mcp_server::{McpServer, McpBackend, ServerConfig};
10//! use pulseengine_mcp_protocol::*;
11//! use async_trait::async_trait;
12//!
13//! #[derive(Clone)]
14//! struct MyBackend;
15//!
16//! #[async_trait]
17//! impl McpBackend for MyBackend {
18//!     type Error = Box<dyn std::error::Error + Send + Sync>;
19//!     type Config = ();
20//!
21//!     async fn initialize(_: ()) -> std::result::Result<Self, Self::Error> {
22//!         Ok(MyBackend)
23//!     }
24//!
25//!     fn get_server_info(&self) -> ServerInfo {
26//!         ServerInfo {
27//!             protocol_version: ProtocolVersion::default(),
28//!             capabilities: ServerCapabilities::default(),
29//!             server_info: Implementation {
30//!                 name: "My Server".to_string(),
31//!                 version: "1.0.0".to_string(),
32//!             },
33//!             instructions: Some("Example server".to_string()),
34//!         }
35//!     }
36//!
37//!     async fn list_tools(&self, _: PaginatedRequestParam) -> std::result::Result<ListToolsResult, Self::Error> {
38//!         Ok(ListToolsResult { tools: vec![], next_cursor: None })
39//!     }
40//!
41//!     async fn call_tool(&self, _: CallToolRequestParam) -> Result<CallToolResult, Self::Error> {
42//!         Ok(CallToolResult { content: vec![], is_error: Some(false) })
43//!     }
44//!
45//!     // Implement other required methods (simplified for example)
46//! #   async fn list_resources(&self, _: PaginatedRequestParam) -> Result<ListResourcesResult, Self::Error> {
47//! #       Ok(ListResourcesResult { resources: vec![], next_cursor: String::new() })
48//! #   }
49//! #   async fn read_resource(&self, _: ReadResourceRequestParam) -> Result<ReadResourceResult, Self::Error> {
50//! #       Err("No resources".into())
51//! #   }
52//! #   async fn list_prompts(&self, _: PaginatedRequestParam) -> Result<ListPromptsResult, Self::Error> {
53//! #       Ok(ListPromptsResult { prompts: vec![], next_cursor: String::new() })
54//! #   }
55//! #   async fn get_prompt(&self, _: GetPromptRequestParam) -> Result<GetPromptResult, Self::Error> {
56//! #       Err("No prompts".into())
57//! #   }
58//! }
59//!
60//! #[tokio::main]
61//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
62//!     let backend = MyBackend::initialize(()).await?;
63//!     let config = ServerConfig::default();
64//!     let mut server = McpServer::new(backend, config).await?;
65//!     server.run().await?;
66//!     Ok(())
67//! }
68//! ```
69//!
70//! # Authentication Options
71//!
72//! The server supports multiple authentication backends for different deployment scenarios:
73//!
74//! ## File-based Authentication (Default)
75//!
76//! ```rust,ignore
77//! use pulseengine_mcp_server::{McpServer, ServerConfig, AuthConfig};
78//! use pulseengine_mcp_auth::config::StorageConfig;
79//! use std::path::PathBuf;
80//!
81//! let auth_config = AuthConfig {
82//!     storage: StorageConfig::File {
83//!         path: PathBuf::from("~/.pulseengine/mcp-auth/keys.enc"),
84//!         file_permissions: 0o600,
85//!         dir_permissions: 0o700,
86//!         require_secure_filesystem: true,
87//!         enable_filesystem_monitoring: false,
88//!     },
89//!     enabled: true,
90//!     // ... other config
91//! };
92//!
93//! let server_config = ServerConfig {
94//!     auth_config: Some(auth_config),
95//!     // ... other config
96//! };
97//! ```
98//!
99//! ## Environment Variable Authentication
100//!
101//! For containerized deployments without filesystem access:
102//!
103//! ```rust,ignore
104//! use pulseengine_mcp_server::{McpServer, ServerConfig, AuthConfig};
105//! use pulseengine_mcp_auth::config::StorageConfig;
106//!
107//! let auth_config = AuthConfig {
108//!     storage: StorageConfig::Environment {
109//!         prefix: "MCP_AUTH".to_string(),
110//!     },
111//!     enabled: true,
112//!     // ... other config
113//! };
114//!
115//! // Set environment variables:
116//! // MCP_AUTH_API_KEY_ADMIN_1=admin-key-12345
117//! // MCP_AUTH_API_KEY_OPERATOR_1=operator-key-67890
118//! ```
119//!
120//! ## Memory-Only Authentication
121//!
122//! For temporary deployments where keys don't need persistence:
123//!
124//! ```rust,ignore
125//! use pulseengine_mcp_server::{McpServer, ServerConfig, AuthConfig};
126//! use pulseengine_mcp_auth::{config::StorageConfig, types::{ApiKey, Role}};
127//! use std::collections::HashMap;
128//!
129//! // Create memory-only auth config
130//! let auth_config = AuthConfig::memory();
131//!
132//! let server_config = ServerConfig {
133//!     auth_config: Some(auth_config),
134//!     // ... other config
135//! };
136//!
137//! // Add API keys programmatically during runtime
138//! let api_key = ApiKey {
139//!     id: "temp_key_1".to_string(),
140//!     key: "temporary-secret-key".to_string(),
141//!     role: Role::Admin,
142//!     created_at: chrono::Utc::now(),
143//!     last_used: None,
144//!     permissions: vec![],
145//!     rate_limit: None,
146//!     ip_whitelist: None,
147//!     expires_at: None,
148//!     metadata: HashMap::new(),
149//! };
150//!
151//! // Add to server's auth manager after initialization
152//! server.auth_manager().save_api_key(&api_key).await?;
153//! ```
154//!
155//! ## Disabled Authentication
156//!
157//! For development or trusted environments:
158//!
159//! ```rust,ignore
160//! let auth_config = AuthConfig::disabled();
161//! let server_config = ServerConfig {
162//!     auth_config: Some(auth_config),
163//!     // ... other config
164//! };
165//! ```
166
167pub mod backend;
168pub mod context;
169pub mod handler;
170pub mod middleware;
171pub mod server;
172
173// Endpoint modules
174pub mod alerting_endpoint;
175pub mod dashboard_endpoint;
176pub mod health_endpoint;
177pub mod metrics_endpoint;
178
179// Test modules
180#[cfg(test)]
181mod backend_tests;
182#[cfg(test)]
183mod context_tests;
184#[cfg(test)]
185mod handler_tests;
186#[cfg(test)]
187mod lib_tests;
188#[cfg(test)]
189mod middleware_tests;
190#[cfg(test)]
191mod server_tests;
192
193// Re-export core types
194pub use backend::{BackendError, McpBackend};
195pub use context::RequestContext;
196pub use handler::{GenericServerHandler, HandlerError};
197pub use middleware::{Middleware, MiddlewareStack};
198pub use server::{McpServer, ServerConfig, ServerError};
199
200// Re-export from dependencies for convenience
201pub use pulseengine_mcp_auth::{self as auth, AuthConfig, AuthenticationManager};
202pub use pulseengine_mcp_monitoring::{self as monitoring, MetricsCollector, MonitoringConfig};
203pub use pulseengine_mcp_protocol::{self as protocol, *};
204pub use pulseengine_mcp_security::{self as security, SecurityConfig, SecurityMiddleware};
205pub use pulseengine_mcp_transport::{self as transport, Transport, TransportConfig};