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//! [![Crates.io](https://img.shields.io/crates/v/turul-http-mcp-server.svg)](https://crates.io/crates/turul-http-mcp-server)
9//! [![Documentation](https://docs.rs/turul-http-mcp-server/badge.svg)](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 middleware;
54pub mod notification_bridge;
55pub mod prelude;
56pub mod protocol;
57pub mod server;
58pub mod session_handler;
59pub mod sse;
60pub mod stream_manager;
61pub mod streamable_http;
62
63#[cfg(test)]
64mod tests;
65
66// Re-export main types
67pub use cors::CorsLayer;
68// McpHttpHandler removed in 0.2.0 - use SessionMcpHandler instead
69pub use notification_bridge::{
70    BroadcastError, NotificationBroadcaster, SharedNotificationBroadcaster,
71    StreamManagerNotificationBroadcaster,
72};
73pub use protocol::{
74    McpProtocolVersion, extract_last_event_id, extract_protocol_version, extract_session_id,
75};
76/// HTTP MCP server implementation with builder pattern and configuration
77pub use server::{HttpMcpServer, HttpMcpServerBuilder, ServerConfig, ServerStats};
78/// Session-aware request handler with SSE streaming capabilities
79pub use session_handler::{SessionMcpHandler, SessionSseStream};
80/// SSE stream management with connection tracking and event delivery
81pub use stream_manager::{StreamConfig, StreamError, StreamManager, StreamStats};
82pub use streamable_http::{StreamableHttpContext, StreamableHttpHandler};
83
84// Re-export foundational types
85/// JSON-RPC 2.0 dispatcher and handler trait for protocol operations
86pub use turul_mcp_json_rpc_server::{JsonRpcDispatcher, JsonRpcHandler};
87/// Core MCP protocol types and error handling
88pub use turul_mcp_protocol::*;
89
90/// Result type for HTTP MCP operations
91pub type Result<T> = std::result::Result<T, HttpMcpError>;
92
93/// HTTP MCP specific errors
94#[derive(Debug, thiserror::Error)]
95pub enum HttpMcpError {
96    #[error("HTTP error: {0}")]
97    Http(#[from] hyper::Error),
98
99    #[error("JSON-RPC error: {0}")]
100    JsonRpc(#[from] turul_mcp_json_rpc_server::JsonRpcError),
101
102    #[error("MCP protocol error: {0}")]
103    Mcp(#[from] turul_mcp_protocol::McpError),
104
105    #[error("Serialization error: {0}")]
106    Serialization(#[from] serde_json::Error),
107
108    #[error("IO error: {0}")]
109    Io(#[from] std::io::Error),
110
111    #[error("Invalid request: {0}")]
112    InvalidRequest(String),
113}