UNPKG

mcp-talent-server

Version:

Model Context Protocol server for talent management tools

246 lines (191 loc) 8.44 kB
# MCP Talent Server - Streaming & Token Optimization ## Overview This MCP server now includes advanced streaming capabilities and token optimization features that enable real-time data processing, reduce response times, and minimize token usage by up to 70%. ## 🚀 New Streaming Tools ### 1. Streaming Sheets Search (`streaming_sheets_search`) **Purpose**: Real-time database queries with progressive result delivery **Key Features**: - Process up to 1000 records with real-time progress tracking - Configurable batch sizes (1-50 records per batch) - Three optimization levels: Fast (70% token savings), Balanced (40% savings), Detailed (10% savings) - Progress monitoring via stream status API **Usage Example**: ```json { "name": "streaming_sheets_search", "arguments": { "aggregationPipeline": [{"$match": {"originalData.age": {"$gte": 25}}}], "limit": 100, "streamBatchSize": 10, "optimizationLevel": "balanced", "includeProgress": true } } ``` **Response**: Returns `streamId` for monitoring progress via `stream_status` tool. ### 2. Streaming Gallery Export (`streaming_gallery_export`) **Purpose**: Real-time ZIP file creation with live progress updates **Key Features**: - Process up to 1000 images (vs 100 in standard version) - Batch image processing (1-20 images per batch) - Real-time download progress, ZIP creation, and S3 upload status - Three compression levels for optimal file sizes **Usage Example**: ```json { "name": "streaming_gallery_export", "arguments": { "folderNames": ["Influencer Gallery"], "imageNames": ["IG*", "TikTok*"], "foldersSearchQuery": "{\"name\": {\"$regex\": \"Gallery\", \"$options\": \"i\"}}", "imagesSearchQuery": "{\"originalName\": {\"$regex\": \"^(IG|TikTok)\", \"$options\": \"i\"}}", "maxImages": 200, "streamBatchSize": 5, "compressionLevel": "balanced" } } ``` ### 3. Stream Status Monitoring (`stream_status`) **Purpose**: Monitor active streams and retrieve processing status **Usage Example**: ```json { "name": "stream_status", "arguments": { "streamId": "sheet-search-abc123", "getChunks": true, "fromIndex": 0 } } ``` ## 🎯 Token Optimization ### Optimization Levels 1. **Fast Mode** (70% token reduction) - Essential fields only (id, name, type, status, url) - Aggressive array summarization - String truncation at 100 characters - Removes empty/null values 2. **Balanced Mode** (40% token reduction) - Default - Essential fields + metadata - Moderate array summarization - String truncation at 200 characters - Smart field selection 3. **Detailed Mode** (10% token reduction) - Most fields preserved - Light array summarization - String truncation at 500 characters - Minimal optimization ### Automatic Optimization Features - **Smart Field Selection**: Prioritizes important fields based on patterns - **Array Summarization**: Large arrays show samples + counts instead of full data - **String Truncation**: Long strings are truncated with clear indicators - **Empty Value Removal**: Null/empty fields are removed to reduce noise - **Token Estimation**: Real-time token usage estimation and savings reporting ## 📊 Performance Improvements ### Streaming Benefits 1. **Memory Efficiency**: Process datasets larger than available RAM 2. **Reduced Timeouts**: Long operations broken into manageable chunks 3. **Real-time Feedback**: Users see progress instead of waiting for completion 4. **Better UX**: Progressive loading enables responsive interfaces 5. **Error Recovery**: Continue processing even if individual items fail ### Token Optimization Benefits 1. **Cost Reduction**: Up to 70% fewer tokens used per request 2. **Faster Responses**: Smaller payloads transfer faster 3. **Better Performance**: Less data to parse and process 4. **Improved Readability**: Noise reduction makes results clearer ## 🔄 Streaming Workflow ### Typical Usage Pattern 1. **Initiate Stream**: ```json // Call streaming tool {"name": "streaming_sheets_search", "arguments": {...}} // Returns: {"streamId": "abc123", "summary": {...}} ``` 2. **Monitor Progress**: ```json // Poll for updates every 2-5 seconds {"name": "stream_status", "arguments": {"streamId": "abc123"}} // Returns: {"active": true, "progress": {...}} ``` 3. **Retrieve Results**: ```json // Get completed results {"name": "stream_status", "arguments": {"streamId": "abc123", "getChunks": true}} // Returns: Final data when stream completes ``` ## 🛡️ Error Handling & Recovery ### Stream Error Handling - **Graceful Failures**: Individual item failures don't stop the entire stream - **Detailed Error Reports**: Per-item error logging with recovery suggestions - **Automatic Retry**: Transient failures are retried automatically - **Progress Preservation**: Streams can resume from where they left off ### Token Optimization Safety - **Schema Validation**: All optimizations preserve data integrity - **Reversible Operations**: Original data can be reconstructed if needed - **Safe Defaults**: Conservative optimization when data types are uncertain - **Error Boundaries**: Optimization failures don't crash the main operation ## 📈 Monitoring & Analytics ### Stream Analytics - **Processing Time**: Total time for stream completion - **Throughput**: Records/items processed per second - **Error Rates**: Percentage of failed operations - **Memory Usage**: Peak memory usage during processing ### Token Analytics - **Savings Calculation**: Tokens saved vs. original size - **Compression Ratios**: Data reduction percentages - **Field Statistics**: Which fields are most commonly optimized - **Performance Impact**: Speed improvements from smaller payloads ## 🔧 Configuration ### Environment Variables ```bash # Streaming Configuration STREAM_BATCH_SIZE=10 # Default batch size for streams STREAM_TIMEOUT_MS=300000 # 5 minute timeout for streams STREAM_CLEANUP_INTERVAL=3600 # 1 hour cleanup interval # Token Optimization TOKEN_OPTIMIZATION_LEVEL=balanced # fast|balanced|detailed MAX_STRING_LENGTH=200 # Default string truncation length ENABLE_ARRAY_SUMMARIZATION=true # Enable array compression ``` ### Tool-Specific Configuration - Batch sizes configurable per tool call - Optimization levels can be overridden per request - Progress tracking can be disabled for performance-critical operations ## 🚦 Best Practices ### For Streaming Operations 1. **Use appropriate batch sizes**: Smaller batches for real-time feedback, larger for throughput 2. **Monitor progress regularly**: Poll every 2-5 seconds for optimal UX 3. **Handle stream cleanup**: Check for completed streams and clean up resources 4. **Plan for failures**: Always have error handling for individual stream items ### For Token Optimization 1. **Choose the right level**: Fast for dashboards, Detailed for data analysis 2. **Preserve critical fields**: Add important fields to priority lists 3. **Test with real data**: Verify optimization doesn't break downstream processing 4. **Monitor savings**: Track token usage to quantify improvements ## 🔮 Future Enhancements ### Planned Features - **WebSocket Support**: Real-time bidirectional streaming - **Stream Persistence**: Resume interrupted streams across server restarts - **Advanced Compression**: Custom compression algorithms for specific data types - **Batch Operations**: Multiple stream operations in parallel - **Smart Caching**: Intelligent result caching for repeated queries ### Integration Opportunities - **Real-time Dashboards**: Live data feeds for admin interfaces - **Progressive Web Apps**: Responsive user experiences with streaming data - **Background Processing**: Queue and monitor long-running operations - **API Rate Limiting**: Smooth out bursty traffic with streaming responses --- ## Quick Start 1. **Install Dependencies**: ```bash npm install ``` 2. **Start with Streaming**: ```bash npm run dev ``` 3. **Try a Streaming Query**: Use the `streaming_sheets_search` tool with a small dataset to see real-time results 4. **Monitor Progress**: Use the returned `streamId` with the `stream_status` tool The streaming implementation transforms this MCP server from a traditional request-response model into a modern, real-time data processing platform optimized for performance and user experience.