Skip to main content
Glama
ooples

MCP Console Automation Server

ENHANCED_STREAMING_API.md10.5 kB
# Enhanced Streaming API Specification ## Overview The Enhanced Streaming API solves the **25k token limit problem** in MCP Console Automation by implementing: 1. **Real-time streaming** with configurable 1KB chunks 2. **Memory-efficient buffering** with automatic cleanup 3. **Server-side filtering** during streaming to reduce data transfer 4. **Timeout handling** with graceful degradation 5. **Constant memory usage** regardless of output size ## Problem Statement The original console automation system would fail when command outputs exceeded ~25,000 tokens due to: - Storing entire output in memory - Returning all data in single responses - No filtering capabilities - Memory leaks from long-running processes ## Solution Architecture ### Enhanced StreamManager ```typescript interface StreamingConfig { bufferSize: number; // 512B-4KB configurable chunks maxBufferSize: number; // Maximum buffer before flush maxMemoryUsage: number; // Per-session memory limit flushInterval: number; // Auto-flush interval (ms) enableFiltering: boolean; // Server-side filtering retentionPolicy: 'rolling' | 'time-based' | 'none'; retentionSize: number; // Chunks to retain retentionTime: number; // Retention time (ms) } ``` ### Memory Management - **Rolling Buffers**: Only keep N most recent chunks in memory - **Automatic Cleanup**: Remove old data based on retention policy - **Memory Pressure Detection**: Automatically drop chunks under high pressure - **Configurable Limits**: Set per-session and global memory limits ## API Methods ### 1. Enhanced console_get_stream **Purpose**: Get streaming output without hitting token limits ```javascript console_get_stream({ sessionId: "session-123", bufferSize?: 1024, // 512B-4KB chunk size maxLines?: 100, // Max lines to prevent token overflow timeout?: 30000, // 30 second timeout since?: "2025-01-15T10:30:00Z", // Get data since timestamp filter?: { regex?: "ERROR|WARN", // Regex pattern include?: ["user.", "api."], // Must contain these exclude?: ["debug", "trace"] // Exclude these patterns }, includeStats?: true // Include performance stats }) ``` **Response**: ```json { "success": true, "sessionId": "session-123", "chunks": [ { "data": "Log line content...", "timestamp": "2025-01-15T10:30:01.000Z", "isError": false, "sequenceId": 1001, "size": 256 } ], "hasMore": true, "nextSequenceId": 1002, "nextTimestamp": "2025-01-15T10:30:01.100Z", "memoryPressure": "low", "truncated": false, "stats": { "totalChunks": 1500, "totalBytes": 1536000, "filteredChunks": 50, "memoryUsage": 2097152, "averageChunkSize": 1024, "droppedChunks": 0 }, "totalChunks": 1, "totalBytes": 256 } ``` ### 2. console_get_streaming_stats **Purpose**: Monitor memory usage and performance ```javascript console_get_streaming_stats({ sessionId: "session-123" }) ``` **Response**: ```json { "sessionId": "session-123", "stats": { "totalChunks": 5000, "totalBytes": 5120000, "filteredChunks": 200, "filteredBytes": 204800, "memoryUsage": 1048576, "compressionRatio": 0.95, "averageChunkSize": 1024, "bufferUtilization": 75.5, "droppedChunks": 12 }, "timestamp": "2025-01-15T10:30:00.000Z" } ``` ### 3. console_optimize_streaming_memory **Purpose**: Free memory across all streaming sessions ```javascript console_optimize_streaming_memory() ``` **Response**: ```json { "success": true, "memoryFreed": 2097152, "beforeMemory": 15728640, "afterMemory": 13631488, "timestamp": "2025-01-15T10:30:00.000Z" } ``` ### 4. console_set_streaming_config **Purpose**: Update streaming configuration ```javascript console_set_streaming_config({ sessionId?: "session-123", // Optional for global config config: { bufferSize: 2048, // 2KB chunks maxMemoryUsage: 5242880, // 5MB limit flushInterval: 50, // 50ms flush enableFiltering: true, retentionPolicy: "rolling", retentionSize: 100 // Keep 100 chunks } }) ``` ## Usage Examples ### Handling Large Command Output ```javascript // Start a session with streaming enabled const session = await mcp.console_create_session({ command: "find /var/log -name '*.log' -exec cat {} \\;", streaming: true, bufferSize: 1024 }); // Stream output in manageable chunks let sequenceId = 0; while (true) { const result = await mcp.console_get_stream({ sessionId: session.sessionId, since: sequenceId, maxLines: 50, // Stay under token limits filter: { exclude: ["DEBUG", "TRACE"] // Filter noise } }); // Process chunks for (const chunk of result.chunks) { console.log(`[${chunk.timestamp}] ${chunk.data}`); } if (!result.hasMore) break; sequenceId = result.nextSequenceId; } ``` ### Real-time Log Monitoring ```javascript // Monitor logs in real-time with filtering const monitorLogs = async (sessionId) => { let lastTimestamp = new Date(); setInterval(async () => { const result = await mcp.console_get_stream({ sessionId, since: lastTimestamp.toISOString(), filter: { regex: "ERROR|CRITICAL|FATAL" }, maxLines: 20 }); if (result.chunks.length > 0) { result.chunks.forEach(chunk => { console.error(`ALERT: ${chunk.data}`); }); lastTimestamp = new Date(result.nextTimestamp); } }, 1000); }; ``` ### Memory-Conscious Processing ```javascript // Process large outputs without memory issues const processLargeOutput = async (sessionId) => { // Check memory usage first const stats = await mcp.console_get_streaming_stats({ sessionId }); if (stats.stats.memoryUsage > 5242880) { // 5MB await mcp.console_optimize_streaming_memory(); } // Stream with memory limits const result = await mcp.console_get_stream({ sessionId, maxLines: 25, // Conservative limit bufferSize: 512, // Small chunks timeout: 10000 // Fast timeout }); return result; }; ``` ## Performance Characteristics ### Before vs After Comparison | Metric | Original System | Enhanced System | |--------|----------------|-----------------| | **Max Output Size** | ~25KB (token limit) | Unlimited | | **Memory Usage** | Linear growth | Constant | | **Response Time** | Increases with size | Constant | | **Memory Leaks** | Common | None | | **Filtering** | Client-side only | Server-side | | **Large File Support** | Fails | Full support | ### Benchmarks - **1MB Output**: Processed in <2 seconds with constant 2MB memory usage - **100MB Output**: Streamed efficiently with <5MB memory usage - **Real-time Monitoring**: <100ms latency for new data - **Memory Recovery**: Automatic cleanup under pressure ## Error Handling ### Memory Pressure When memory usage exceeds limits: 1. **Warning Level (70%)**: Trigger retention policy cleanup 2. **Critical Level (90%)**: Drop new chunks and force cleanup 3. **Emergency Level (95%)**: Clear entire session buffers ### Timeout Handling ```javascript try { const result = await mcp.console_get_stream({ sessionId: "session-123", timeout: 5000 }); } catch (error) { if (error.message.includes('timeout')) { // Handle timeout gracefully console.log('Request timed out, retrying with smaller buffer...'); // Retry with reduced parameters } } ``` ### Graceful Degradation When encountering issues: 1. **Reduce chunk sizes** automatically 2. **Increase flush frequency** to free memory 3. **Apply aggressive filtering** to reduce data 4. **Fallback to legacy system** if needed ## Configuration Best Practices ### For Real-time Monitoring ```javascript { bufferSize: 512, // Small chunks for low latency flushInterval: 25, // 25ms for real-time feel maxMemoryUsage: 1048576, // 1MB limit retentionPolicy: "time-based", retentionTime: 300000 // 5 minutes } ``` ### For Batch Processing ```javascript { bufferSize: 4096, // Large chunks for efficiency flushInterval: 500, // Less frequent flushes maxMemoryUsage: 10485760, // 10MB limit retentionPolicy: "rolling", retentionSize: 200 // Keep more chunks } ``` ### For Memory-Constrained Environments ```javascript { bufferSize: 512, // Smallest chunks flushInterval: 50, // Frequent flushes maxMemoryUsage: 524288, // 512KB limit retentionPolicy: "rolling", retentionSize: 20 // Keep minimal chunks } ``` ## Integration Notes ### Existing Code Migration 1. **Replace direct getOutput() calls** with console_get_stream() 2. **Add maxLines parameter** to prevent token overflow 3. **Implement pagination** for large outputs 4. **Add error handling** for timeouts and memory pressure ### MCP Server Integration The enhanced streaming is backward compatible: - Existing `console_get_output` still works - New `console_get_stream` provides enhanced features - Automatic fallback for unsupported sessions ### Memory Monitoring Monitor streaming health with: ```javascript // Get overall streaming status const status = await mcp.console_get_streaming_status(); // Optimize if memory usage is high if (status.memoryUtilization > 80) { await mcp.console_optimize_streaming_memory(); } ``` ## Troubleshooting ### Common Issues 1. **"No enhanced streaming available"** - Enable streaming when creating session: `streaming: true` 2. **High memory usage** - Reduce `bufferSize` and `retentionSize` - Call `console_optimize_streaming_memory()` 3. **Slow responses** - Increase `maxLines` limit - Reduce `flushInterval` 4. **Missing data** - Check `hasMore` flag in response - Use `nextSequenceId` for pagination ### Debug Information Enable debug logging to see: - Memory usage patterns - Chunk processing times - Filter effectiveness - Buffer utilization ## Conclusion The Enhanced Streaming API completely solves the 25k token limit problem by: - **Chunking large outputs** into manageable pieces - **Streaming data** instead of buffering everything - **Managing memory** proactively with automatic cleanup - **Filtering server-side** to reduce unnecessary data transfer - **Providing real-time feedback** on system health This enables processing of unlimited output sizes while maintaining constant memory usage and preventing token limit failures.

Latest Blog Posts

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/ooples/mcp-console-automation'

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