Skip to main content
Glama

MCP E-commerce Demo

by uberr2000
SSE_CONNECTION_MANAGEMENT.md9.84 kB
# SSE Connection Management Implementation ## 🚀 Overview This document outlines the robust SSE (Server-Sent Events) connection management implementation for the MCP Laravel server, including client disconnect detection, connection cleanup, and keepalive mechanisms. ## ✅ **Key Features Implemented** ### 1. **Multi-Level Forceful Connection Cleanup** - **Primary Detection**: `connection_aborted()` checks before/after operations - **Secondary Detection**: `connection_status()` verification - **Tertiary Detection**: Heartbeat write test for broken pipe detection - **Consecutive Failure Tracking**: Monitors failed ping attempts (max 3 failures) - **Automatic Resource Cleanup**: Forces buffer cleanup and connection termination - **FastCGI Support**: Uses `fastcgi_finish_request()` for proper cleanup ### 2. **Enhanced Keepalive Mechanism** - **Periodic Ping Messages**: Sends heartbeat every 15 seconds with failure tracking - **Connection Health Monitoring**: Tracks uptime, ping counts, and memory usage - **Timeout Management**: Automatic disconnect after 1 hour maximum - **Wait-Period Monitoring**: Checks connection status during sleep intervals ### 3. **Robust Buffer Management** - **Immediate Output Flushing**: Forces buffer flush with error checking - **Stream Writing**: Uses `fwrite()` with error detection instead of `echo` - **Buffer Cleanup**: Explicit output buffer management on disconnect - **Nginx Compatibility**: Disables proxy buffering with enhanced headers ### 4. **Comprehensive Error Handling** - **Graceful Error Recovery**: Multi-level exception handling in stream context - **Connection Validation**: Pre and post-operation connection checks - **Error Reporting**: Structured error messages via SSE with fallback logging - **Resource Protection**: Memory and execution time monitoring --- ## 🔧 **Technical Implementation** ### **MCPSSEController Improvements** ```php // Enhanced features implemented: 1. Multi-Level Connection Detection: - connection_aborted() - Primary disconnect detection - connection_status() - Secondary state verification - Heartbeat write test - Tertiary broken pipe detection - Continuous monitoring during wait periods 2. Forceful Disconnect Handling: - Consecutive failure tracking (max 3 failures) - Resource cleanup with buffer management - FastCGI termination support - Error-resistant cleanup procedures 3. Robust Message Delivery: - fwrite() with error checking vs echo - Pre/post operation connection validation - Graceful degradation on send failures - Structured error responses 4. Enhanced Connection Lifecycle: - Setup with buffer optimization - Maintenance with failure monitoring - Cleanup with forced termination - Logging at each lifecycle stage ``` ### **Buffer and Output Management** ```php // Enhanced optimizations implemented: - set_time_limit(0): Unlimited execution time - ini_set('memory_limit', '512M'): Increased memory for robust handling - ignore_user_abort(false): Enable manual disconnect detection - ob_implicit_flush(true): Automatic buffer flushing - fwrite() error checking: Robust stream writing - fastcgi_finish_request(): Proper FastCGI cleanup - X-Accel-Buffering: no: Disable proxy buffering - Transfer-Encoding: chunked: Efficient streaming ``` --- ## 📋 **Connection Lifecycle** ### **1. Connection Establishment** ``` Client → POST /mcp/sse → Server ├── Generate unique connection ID ├── Set appropriate headers ├── Process initial request (if any) ├── Send welcome message └── Start keepalive loop ``` ### **2. Active Connection** ``` Every 15 seconds: ├── Check connection_aborted() ├── Send ping message ├── Flush output buffers ├── Validate connection state └── Sleep until next ping ``` ### **3. Connection Termination & Forceful Cleanup** ``` Triggered by: ├── Client disconnect (connection_aborted()) ├── Connection status abnormal ├── Consecutive ping failures (3 max) ├── Connection timeout (1 hour) ├── Server error/exception └── Manual disconnect Cleanup Process: ├── Attempt to send close message ├── Clean all output buffers ├── FastCGI request termination ├── Log connection closure ├── Release all resources └── Exit stream gracefully Forceful Closure: ├── Multiple connection checks ├── Error-resistant cleanup ├── Resource cleanup guarantee └── Memory leak prevention ``` --- ## 🧪 **Testing** ### **Server-Side Forceful Closure Test** ```bash cd /path/to/mcp_demo php test-sse-forceful-closure.php ``` **Tests Include:** - Normal disconnection (SIGTERM) - Abrupt disconnection (SIGKILL) - Connection timeout simulation - Multiple concurrent connections - Resource cleanup verification ### **Client-Side Test** 1. Start Laravel server: `php artisan serve` 2. Open browser: `http://localhost:8000/sse-test.html` 3. Test connection, ping, tools, and disconnect functionality ### **Manual cURL Test** ```bash curl -N -H "Accept: text/event-stream" \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","method":"ping","params":{},"id":"test"}' \ http://localhost:8000/mcp/sse ``` --- ## 📊 **Monitoring and Logging** ### **Connection Events Logged** - ✅ Connection establishment with client details - ✅ Multi-level disconnect detection - ✅ Consecutive ping failure tracking - ✅ Connection timeout events - ✅ Error conditions and recovery attempts - ✅ Forced closure procedures - ✅ Resource cleanup verification ### **Enhanced Log Examples** ``` [INFO] SSE Connection establishing {"connection_id":"mcp_sse_66f8a1b2c3d4e","client_ip":"127.0.0.1","user_agent":"..."} [DEBUG] Connection aborted detected {"connection_id":"mcp_sse_66f8a1b2c3d4e"} [WARNING] Ping failed {"connection_id":"mcp_sse_66f8a1b2c3d4e","consecutive_failures":2} [INFO] Max consecutive failures reached, closing connection {"connection_id":"mcp_sse_66f8a1b2c3d4e","failures":3} [INFO] Forcing connection closure {"connection_id":"mcp_sse_66f8a1b2c3d4e"} [INFO] Connection forcefully closed {"connection_id":"mcp_sse_66f8a1b2c3d4e"} ``` --- ## 🔄 **SSE Message Format** ### **Standard Messages** ```javascript // Welcome Message event: welcome data: {"jsonrpc":"2.0","result":{"status":"connected","connection_id":"..."},"id":"welcome"} // Ping/Keepalive event: ping data: {"event":"ping","data":{"timestamp":1234567890,"ping_count":42,"uptime":630}} // Response event: response data: {"jsonrpc":"2.0","result":{...},"id":"request-123"} // Error event: error data: {"jsonrpc":"2.0","error":{"code":-32603,"message":"..."},"id":"request-123"} ``` --- ## 🌐 **Client Integration** ### **JavaScript Client Example** ```javascript // Enhanced EventSource handling fetch('/mcp/sse', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'text/event-stream' }, body: JSON.stringify(mcpRequest) }).then(response => response.body) .then(body => { const reader = body.getReader(); // Handle streaming response with disconnect detection }); ``` ### **n8n Integration** - ✅ Compatible with n8n MCP client expectations - ✅ Proper SSE event format for parsing - ✅ Reliable connection management for long-running workflows --- ## 🚀 **Performance Optimizations** ### **Memory Management** - 256MB memory limit for connections - Buffer flushing to prevent memory buildup - Resource cleanup on disconnect ### **Network Efficiency** - 15-second ping interval (balance between responsiveness and overhead) - Structured ping data (avoids unnecessary parsing) - Immediate buffer flushing for low latency ### **Scalability Considerations** - Unique connection tracking - Configurable timeout limits - Graceful resource management --- ## ✅ **Production Readiness** ### **Features Implemented** - ✅ **Multi-Level Client Disconnect Detection**: connection_aborted() + connection_status() + heartbeat checks - ✅ **Forceful Resource Cleanup**: Guaranteed cleanup with error-resistant procedures - ✅ **Connection Monitoring**: Comprehensive logging and failure tracking - ✅ **Enhanced Keepalive System**: Reliable ping/pong with failure detection - ✅ **Robust Error Handling**: Graceful error recovery and structured reporting - ✅ **Timeout Management**: Configurable connection limits with forced cleanup - ✅ **Buffer Management**: Optimized for real-time streaming with fwrite() error checking - ✅ **Cross-Platform Support**: Works with various SSE clients and FastCGI environments ### **Security Considerations** - Connection timeout prevents resource exhaustion - Proper header validation - Memory limits prevent DoS attacks - Connection tracking for monitoring --- ## 🎯 **Summary** The enhanced SSE implementation provides: 1. **Robust Connection Management**: Multi-level disconnect detection and forceful cleanup 2. **Real-time Communication**: Low-latency message delivery with failure-resistant keepalive 3. **Production Stability**: Enhanced resource management and error-resistant cleanup 4. **Monitoring Capabilities**: Comprehensive logging and connection lifecycle tracking 5. **Client Compatibility**: Works with browsers, n8n, and custom clients with improved reliability **The SSE endpoint now provides enterprise-grade forceful connection management with guaranteed resource cleanup!** 🚀 ### **Endpoints Available** - **SSE Stream**: `POST /mcp/sse` - Main SSE endpoint with enhanced connection handling - **Test Page**: `GET /sse-test.html` - Interactive testing interface - **Server Info**: `GET /mcp/stdio` - MCP server capabilities and endpoints All MCP tools continue to work seamlessly with the improved connection reliability!

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/uberr2000/mcp_demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server