turul_http_mcp_server/lib.rs
1//! # HTTP MCP Server
2//!
3//! **Production HTTP transport layer for Model Context Protocol (MCP) servers.**
4//!
5//! Provides both modern Streamable HTTP and legacy HTTP+SSE transports with automatic
6//! protocol detection, CORS support, and session management for maximum compatibility.
7//!
8//! [](https://crates.io/crates/turul-http-mcp-server)
9//! [](https://docs.rs/turul-http-mcp-server)
10//!
11//! ## Installation
12//!
13//! ```toml
14//! [dependencies]
15//! turul-http-mcp-server = "0.2"
16//! turul-mcp-server = "0.2"
17//! ```
18//!
19//! ## Supported Transports
20//! - **Streamable HTTP**: Recommended for production deployments
21//! - **HTTP+SSE**: Legacy transport for backwards compatibility
22//!
23//! ## Features
24//! - Automatic protocol version detection and routing
25//! - CORS support for browser-based clients
26//! - Session management with cryptographically secure IDs
27//! - Graceful error handling and JSON-RPC 2.0 compliance
28//!
29//! ## Quick Start
30//!
31//! ```rust,no_run
32//! use turul_http_mcp_server::HttpMcpServerBuilder;
33//! use std::net::SocketAddr;
34//!
35//! #[tokio::main]
36//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
37//! let addr: SocketAddr = "127.0.0.1:8080".parse()?;
38//! let server = HttpMcpServerBuilder::new()
39//! .bind_address(addr)
40//! .cors(true)
41//! .build();
42//!
43//! println!("Server running on http://127.0.0.1:8080/mcp");
44//! server.run().await?;
45//! Ok(())
46//! }
47//! ```
48
49pub mod cors;
50pub mod handler;
51pub mod json_rpc_responses;
52pub mod mcp_session;
53pub mod notification_bridge;
54pub mod prelude;
55pub mod protocol;
56pub mod server;
57pub mod session_handler;
58pub mod sse;
59pub mod stream_manager;
60pub mod streamable_http;
61
62#[cfg(test)]
63mod tests;
64
65// Re-export main types
66pub use cors::CorsLayer;
67// McpHttpHandler removed in 0.2.0 - use SessionMcpHandler instead
68pub use notification_bridge::{
69 BroadcastError, NotificationBroadcaster, SharedNotificationBroadcaster,
70 StreamManagerNotificationBroadcaster,
71};
72pub use protocol::{
73 McpProtocolVersion, extract_last_event_id, extract_protocol_version, extract_session_id,
74};
75/// HTTP MCP server implementation with builder pattern and configuration
76pub use server::{HttpMcpServer, HttpMcpServerBuilder, ServerConfig, ServerStats};
77/// Session-aware request handler with SSE streaming capabilities
78pub use session_handler::{SessionMcpHandler, SessionSseStream};
79/// SSE stream management with connection tracking and event delivery
80pub use stream_manager::{StreamConfig, StreamError, StreamManager, StreamStats};
81pub use streamable_http::{StreamableHttpContext, StreamableHttpHandler};
82
83// Re-export foundational types
84/// JSON-RPC 2.0 dispatcher and handler trait for protocol operations
85pub use turul_mcp_json_rpc_server::{JsonRpcDispatcher, JsonRpcHandler};
86/// Core MCP protocol types and error handling
87pub use turul_mcp_protocol::*;
88
89/// Result type for HTTP MCP operations
90pub type Result<T> = std::result::Result<T, HttpMcpError>;
91
92/// HTTP MCP specific errors
93#[derive(Debug, thiserror::Error)]
94pub enum HttpMcpError {
95 #[error("HTTP error: {0}")]
96 Http(#[from] hyper::Error),
97
98 #[error("JSON-RPC error: {0}")]
99 JsonRpc(#[from] turul_mcp_json_rpc_server::JsonRpcError),
100
101 #[error("MCP protocol error: {0}")]
102 Mcp(#[from] turul_mcp_protocol::McpError),
103
104 #[error("Serialization error: {0}")]
105 Serialization(#[from] serde_json::Error),
106
107 #[error("IO error: {0}")]
108 Io(#[from] std::io::Error),
109
110 #[error("Invalid request: {0}")]
111 InvalidRequest(String),
112}