turul_http_mcp_server/
lib.rs

1//! # HTTP MCP Server
2//!
3//! This crate provides HTTP transport for Model Context Protocol (MCP) servers.
4//! It supports both modern Streamable HTTP (2025-03-26+) and legacy HTTP+SSE transports
5//! for maximum compatibility with all MCP clients.
6//!
7//! ## Supported Transports
8//! - **Streamable HTTP (2025-03-26+)**: Recommended for production deployments
9//! - **HTTP+SSE (2024-11-05)**: Legacy transport for backwards compatibility
10//!
11//! ## Features
12//! - Automatic protocol version detection and routing
13//! - CORS support for browser-based clients
14//! - Session management with cryptographically secure IDs
15//! - Graceful error handling and JSON-RPC 2.0 compliance
16
17pub mod cors;
18pub mod handler;
19pub mod json_rpc_responses;
20pub mod mcp_session;
21pub mod notification_bridge;
22pub mod protocol;
23pub mod server;
24pub mod session_handler;
25pub mod sse;
26pub mod stream_manager;
27pub mod streamable_http;
28
29#[cfg(test)]
30mod tests;
31
32// Re-export main types
33pub use cors::CorsLayer;
34pub use handler::McpHttpHandler;
35pub use notification_bridge::{
36    BroadcastError, NotificationBroadcaster, SharedNotificationBroadcaster,
37    StreamManagerNotificationBroadcaster,
38};
39pub use protocol::{
40    extract_last_event_id, extract_protocol_version, extract_session_id, McpProtocolVersion,
41};
42pub use server::{HttpMcpServer, HttpMcpServerBuilder, ServerConfig, ServerStats};
43pub use session_handler::{SessionMcpHandler, SessionSseStream};
44pub use stream_manager::{StreamConfig, StreamError, StreamManager, StreamStats};
45pub use streamable_http::{StreamableHttpContext, StreamableHttpHandler};
46
47// Re-export foundational types
48pub use turul_mcp_json_rpc_server::{JsonRpcDispatcher, JsonRpcHandler};
49pub use turul_mcp_protocol::*;
50
51/// Result type for HTTP MCP operations
52pub type Result<T> = std::result::Result<T, HttpMcpError>;
53
54/// HTTP MCP specific errors
55#[derive(Debug, thiserror::Error)]
56pub enum HttpMcpError {
57    #[error("HTTP error: {0}")]
58    Http(#[from] hyper::Error),
59
60    #[error("JSON-RPC error: {0}")]
61    JsonRpc(#[from] turul_mcp_json_rpc_server::JsonRpcError),
62
63    #[error("MCP protocol error: {0}")]
64    Mcp(#[from] turul_mcp_protocol::McpError),
65
66    #[error("Serialization error: {0}")]
67    Serialization(#[from] serde_json::Error),
68
69    #[error("IO error: {0}")]
70    Io(#[from] std::io::Error),
71
72    #[error("Invalid request: {0}")]
73    InvalidRequest(String),
74}