turul-http-mcp-server
HTTP and SSE transport layer for the turul-mcp-server
framework.
Overview
This crate provides the low-level HTTP and SSE transport implementation.
For most use cases, you should not use this crate directly. The main turul-mcp-server
crate provides a simpler, integrated experience with its .run().await
method, which uses this transport layer internally.
Use this crate only when you need to:
- Integrate the MCP server into an existing
hyper
oraxum
application. - Customize the HTTP transport layer beyond what
turul-mcp-server
offers. - Build a custom server with a different transport mechanism.
Advanced Usage: Pluggable Transport
use *;
use McpServer;
use HttpMcpServerBuilder;
use mcp_tool;
use Arc;
async
async
Correct Usage (Use This Instead)
use *;
async
That's it! No need for manual HTTP transport setup.
Features
- ✅ MCP 2025-06-18 Streamable HTTP - Full protocol compliance with SSE streaming
- ✅ Session Management - UUID v7 session IDs with automatic cleanup
- ✅ SSE Resumability - Last-Event-ID support with event replay
- ✅ CORS Support - Browser client compatibility with configurable origins
- ✅ Protocol Version Detection - Automatic feature flags based on client capabilities
- ✅ JSON-RPC Dispatch - Efficient method routing and error handling
Architecture
Transport Layer Components
This crate provides the building blocks used by the main server:
┌─────────────────────────────────────────────────┐
│ MCP Client │
├─────────────────────────────────────────────────┤
│ POST /mcp + Accept: application/json │ ← JSON-RPC requests
│ GET /mcp + Accept: text/event-stream │ ← SSE notifications
├─────────────────────────────────────────────────┤
│ turul-http-mcp-server │ ← This crate
│ ├─ SessionMcpHandler │ ← Session management
│ ├─ StreamManager │ ← SSE event streaming
│ ├─ NotificationBroadcaster │ ← Real-time notifications
│ └─ JsonRpcDispatcher │ ← JSON-RPC routing
├─────────────────────────────────────────────────┤
│ turul-mcp-server │ ← Main framework
└─────────────────────────────────────────────────┘
Core Components
use ;
Usage
Advanced: Direct Transport Configuration
⚠️ For advanced use cases only. Most users should use turul-mcp-server
instead.
use ;
use InMemorySessionStorage;
use SocketAddr;
use Arc;
// Advanced: Direct HTTP transport configuration
// This is for custom scenarios where you need direct control over the transport layer
let transport = new
.bind_address
.mcp_path
.cors
.sse
.session_expiry_minutes
.build;
// Note: This only creates the transport layer. You'll need to integrate it
// with your own application logic and MCP message handling.
Session Management Configuration
use ;
use InMemorySessionStorage;
use Arc;
// Configure session storage
let storage = new;
let server = with_storage
.bind_address
.session_expiry_minutes
.build;
SSE Stream Configuration
use ;
let stream_config = StreamConfig ;
let server = new
.stream_config
.get_sse // Enable GET SSE for notifications
.post_sse // Disable POST SSE for compatibility
.build;
CORS Configuration
use ;
// Simple CORS enablement
let server = new
.cors // Uses permissive defaults for development
.build;
// Custom CORS configuration (Note: CorsLayer configuration
// is handled internally - this crate provides the components
// but turul-mcp-server provides the full API)
JSON-RPC Handler Registration
use ;
use async_trait;
// Domain error type for handlers
// Custom handler implementation
;
// Note: This crate provides transport layer components.
// For full server functionality including handler registration,
// use turul-mcp-server which builds on this transport layer.
let server = new
.build;
Protocol Version Detection
The transport layer automatically detects client capabilities:
use ;
// Protocol version extraction from headers
let version = extract_protocol_version;
match version
Session Management
Session ID Extraction
use ;
// Extract session ID from request headers
let session_id = extract_session_id;
// Session handler for managing session lifecycle
let handler = new;
Session Storage Integration
use HttpMcpServerBuilder;
use ;
use Arc;
// In-memory storage (development)
let memory_storage = new;
let server = with_storage.build;
// SQLite storage (production)
Notification Broadcasting
SSE Event Streaming
use ;
use Arc;
// Create notification broadcaster
let stream_manager = new;
let broadcaster = new;
// Send notifications to specific sessions using typed APIs
use ProgressNotification;
let progress_notification = ProgressNotification ;
broadcaster.send_progress_notification.await?;
Event Replay and Resumability
use ;
// Extract Last-Event-ID for resumability
let last_event_id = extract_last_event_id;
// Stream manager handles event replay automatically
let stream = stream_manager.create_stream.await?;
Protocol Headers
MCP Header Handling
The transport layer automatically handles MCP-specific headers:
// Client sends: MCP-Protocol-Version: 2025-06-18
// Server returns: mcp-session-id: <uuid-v7>
// The transport layer extracts and processes these headers automatically
use ;
// Headers are processed internally by SessionMcpHandler
// Protocol version determines feature availability
// Session ID manages state isolation between clients
Lifecycle Management
Optional strict lifecycle gating can be configured:
// Note: Use McpServer::builder() for complete lifecycle management
let server = builder
.name
.version
.bind_address
.build?;
server.run.await
Error Handling
HTTP Transport Errors
use ;
Server Statistics and Monitoring
use ;
// Server statistics (if implemented by the specific server)
// Note: Full stats API is available in turul-mcp-server
Testing the Transport Layer
Manual HTTP Testing
# Test session creation
# Test SSE streaming
# Test event resumability
Integration Testing
use ;
async
Framework Integration
✅ Recommended: Using turul-mcp-server
This is the recommended approach for most users:
// Recommended: Use the main server framework
use McpServer;
let server = builder
.name
.version
.bind_address
.build?;
// Note: Server configuration complete - HTTP transport layer is included
// Refer to turul-mcp-server docs for deployment patterns
// from turul-http-mcp-server with sensible defaults
Advanced Transport Customization
// Advanced: Direct transport layer usage for custom scenarios
use ;
use InMemorySessionStorage;
// Build custom HTTP transport
let transport = new
.bind_address
.session_expiry_minutes
.max_body_size
.build;
// Integrate with custom application logic
Feature Flags
[]
= { = "0.2.0", = ["sse"] }
Available features:
default
=["sse"]
- Includes SSE support by defaultsse
- Server-Sent Events streaming for real-time notifications
Performance Notes
- Connection Handling: Uses Hyper for efficient HTTP/1.1 connections
- Stream Management: Optimized SSE event delivery with configurable buffers
- Session Cleanup: Automatic cleanup every 60 seconds with configurable expiry
- JSON-RPC Dispatch: Fast method routing with minimal allocations
Compatibility
MCP Protocol Versions
This transport layer supports all MCP protocol versions:
- Basic MCP: Core protocol without streamable HTTP
- Streamable HTTP: Enhanced protocol with SSE support
- Full Feature Set: Complete protocol with meta fields and enhanced capabilities
HTTP Specifications
- HTTP/1.1: Full support with connection keep-alive
- Server-Sent Events: Compliant with EventSource specification
- CORS: Cross-Origin Resource Sharing for browser clients
- JSON-RPC 2.0: Complete specification compliance
Related Crates
- turul-mcp-server: Complete MCP server framework (recommended for most users)
- turul-mcp-session-storage: Pluggable session storage backends
- turul-mcp-protocol: MCP protocol types and traits
- turul-mcp-json-rpc-server: JSON-RPC 2.0 server foundation
License
Licensed under the MIT License. See LICENSE for details.