Skip to main content
Glama

Hue MCP Server

by rmrfslashbin
DESIGN.md16.9 kB
# Hue MCP Server - Design Document ## Project Overview The Hue MCP Server is a Model Context Protocol (MCP) server that enables AI assistants to interact with Philips Hue smart lighting systems. This project demonstrates how to build a robust, production-ready MCP server for IoT device control with proper error handling, rate limiting, and user experience considerations. ## Architecture ### Core Components ``` ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ MCP Client │───▶│ MCP Server │───▶│ Hue Bridge │ │ (Claude Desktop)│ │ (index.ts) │ │ (REST API) │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ ▼ ┌─────────────────┐ │ State Manager │ │ (Optional Cache)│ └─────────────────┘ ``` ### Technology Stack - **Runtime**: Node.js with TypeScript - **MCP SDK**: @modelcontextprotocol/sdk - **Hue SDK**: node-hue-api (v5 with TypeScript support) - **Configuration**: zod + dotenv - **Discovery**: SSDP + Official Hue API - **Setup Tools**: CLI + Web Interface ## Key Design Principles ### 1. **Dual-Mode Operation** The system operates in two modes to ensure reliability: - **Cached Mode**: State manager periodically syncs data, reducing API calls by 95% - **Direct API Mode**: Falls back when state manager fails or rate limits occur ### 2. **Graceful Degradation** Every operation has fallback mechanisms: ```typescript if (stateManager.isReady()) { // Use cached data (fast, efficient) return stateManager.getLights(); } else { // Fall back to direct API (slower, but works) return await hue.listLights(); } ``` ### 3. **Rate Limiting Protection** Multiple strategies prevent API abuse: - Sequential API calls with delays (200-500ms) - Exponential backoff on errors - Configurable sync intervals (minimum 1 minute) - Ability to disable state manager entirely ### 4. **Comprehensive Error Handling** Every API interaction includes: - Specific error messages for debugging - Graceful fallbacks when components fail - User-friendly error reporting - Rate limit detection and recovery ## File Structure ``` hue-mcp/ ├── src/ │ ├── index.ts # Main MCP server │ ├── hue-bridge.ts # Hue API client │ ├── hue-state.ts # State management & caching │ ├── hue-events.ts # Real-time event stream │ ├── config.ts # Configuration management │ ├── discovery.ts # Bridge discovery │ ├── cli.ts # Setup CLI tool │ └── web-setup.ts # Web setup interface ├── dist/ # Compiled JavaScript ├── docs/ # Documentation ├── package.json # Dependencies & scripts ├── tsconfig.json # TypeScript config ├── .gitignore # Git exclusions └── hue-config.json # Runtime configuration ``` ## Configuration System ### Multi-Source Configuration Supports three configuration sources with priority: 1. **Environment variables** (highest priority) 2. **hue-config.json** file 3. **.env** file (lowest priority) ### Schema Validation Uses Zod for runtime validation: ```typescript const ConfigSchema = z.object({ HUE_BRIDGE_IP: z.string().ip(), HUE_API_KEY: z.string().min(32), HUE_SYNC_INTERVAL_MS: z.coerce.number().min(60000).default(300000), // ... more fields }); ``` ### Configuration Options | Variable | Description | Default | |----------|-------------|---------| | `HUE_BRIDGE_IP` | Bridge IP address | Required | | `HUE_API_KEY` | API authentication key | Required | | `HUE_SYNC_INTERVAL_MS` | State sync frequency | 300000 (5 min) | | `HUE_ENABLE_EVENTS` | Real-time events | false | | `NODE_TLS_REJECT_UNAUTHORIZED` | TLS validation | 1 (enabled) | | `LOG_LEVEL` | Logging verbosity | info | ## MCP Tools ### Core Tools | Tool | Purpose | State Required | |------|---------|----------------| | `list_lights` | List all lights | No | | `get_light` | Get single light details | No | | `set_light_state` | Control individual light | No | | `list_scenes` | List available scenes | No | | `activate_scene` | Activate a scene | No | | `get_summary` | Comprehensive ecosystem view | No | ### Room/Zone Tools | Tool | Purpose | State Required | |------|---------|----------------| | `list_rooms` | List all rooms | No | | `list_zones` | List all zones | No | | `get_room` | Get room details | Yes | | `get_zone` | Get zone details | Yes | | `control_room_lights` | Control all lights in room | No | ### Enhanced Summary Tool The `get_summary` tool provides comprehensive ecosystem information: - **High-level statistics**: Total counts, status overview - **Device breakdown**: Types and distribution - **Scene categorization**: Available scene types - **Room details**: Lights per room, current status - **Zone information**: Zone organization - **Live status**: Real-time on/off states ## State Management ### HueStateManager Class Manages cached state to reduce API calls: ```typescript class HueStateManager { private lights = new Map(); private rooms = new Map(); private scenes = new Map(); private zones = new Map(); private devices = new Map(); async syncAll() { // Sequential sync with delays await this.syncLights(); await this.delay(500); await this.syncRooms(); // ... } } ``` ### Sync Strategy - **Initial sync**: All data types fetched sequentially - **Periodic sync**: Configurable interval (default 5 minutes) - **Event-driven updates**: Real-time changes via Server-Sent Events - **Graceful degradation**: Direct API fallback on failures ## Hue API Integration ### API Client Design ```typescript class HueBridge { private baseUrl: string; private httpsAgent: https.Agent; async makeRequest(endpoint: string, method = 'GET', body?: any) { // Standard HTTP client with error handling } // Specific methods for each resource type async listLights() { /* ... */ } async setLightState(id: string, state: LightState) { /* ... */ } } ``` ### Key Features - **HTTPS-only**: Hue API v2 requirement - **Self-signed certificates**: Proper handling for local bridges - **Authentication**: API key in headers - **Color conversion**: HSV to XY color space - **Error handling**: Specific error types and recovery ### API Hierarchy Understanding The project correctly handles Hue API v2 hierarchy: ``` Room → Device → Light Zone → Device → Light ``` This was a critical fix - initial implementation incorrectly assumed `Room → Light` direct relationship. ## Discovery & Setup ### Bridge Discovery Multiple discovery methods for reliability: 1. **Official Hue discovery service**: `https://discovery.meethue.com/` 2. **SSDP multicast**: Local network discovery 3. **Manual entry**: IP address input fallback ### Setup Tools #### CLI Tool (`npm run setup`) ```bash $ npm run setup ? Enter Hue Bridge IP: 192.168.1.100 ? Press the bridge button and hit Enter... ✅ API key generated: abc123... ``` #### Web Setup Wizard (`npm run setup:web`) A modern, delightful web-based setup experience that auto-launches when configuration is missing. **Technology Stack**: - **Frontend**: React + Vite (fast development, modern tooling) - **UI Components**: Tailwind CSS + Radix UI (accessible, beautiful) - **Animations**: Framer Motion (smooth transitions) - **Backend**: Express server with SSE for real-time updates **Setup Flow**: 1. **Auto-Launch**: Server detects missing config and opens browser 2. **Bridge Discovery**: - Automatic discovery via mDNS/Bonjour - Official Hue API fallback - Manual IP entry option - Visual bridge selector with signal strength 3. **Authentication**: - Animated button press guide - Real-time polling with visual countdown - Audio feedback on success - Helpful retry tips on failure 4. **Validation & Test**: - Connection test with live data - "Blink all lights" test feature - Mini control panel preview - Scene browsing 5. **Configuration**: - Secure config file generation - Claude Desktop snippet with copy button - Quick start guide display **User Experience Features**: - Progress indicators with animations - Error recovery suggestions - Responsive design for all devices - Keyboard navigation support - Dark mode support **API Endpoints**: ```typescript POST /api/discover // Find bridges on network POST /api/authenticate // Poll for button press (SSE) POST /api/test // Test connection POST /api/save-config // Save configuration GET /api/claude-config // Get Claude Desktop snippet ``` ## Error Handling & Resilience ### Rate Limiting Strategy ```typescript // Sequential API calls for (const light of lights) { await this.setLightState(light.id, state); await delay(200); // Prevent rate limiting } ``` ### Error Recovery - **429 Rate Limit**: Exponential backoff, temporary disabling - **Network errors**: Retry with increased intervals - **Invalid responses**: Graceful degradation to direct mode - **State corruption**: Automatic re-sync ### User Experience - **Clear error messages**: Specific guidance for each error type - **Fallback modes**: Always attempt to complete user requests - **Status reporting**: Clear indication of system state - **Non-destructive**: Default to safe operations ## Security Considerations ### Authentication - API keys stored in configuration files (excluded from git) - No hardcoded credentials - Environment variable support for secure deployment ### Network Security - HTTPS-only communication - Certificate validation configurable - Local network operation (air-gapped from internet) ### Data Privacy - No external API calls except Hue discovery service - All lighting data stays local - No user data collection or transmission ## Performance Optimizations ### Caching Strategy - **95% API call reduction** through intelligent caching - **Parallel requests**: Multiple API calls in single operations - **Lazy loading**: Only fetch data when needed ### Startup Optimization - **Direct API mode**: Skip state manager for faster startup - **Concurrent operations**: Parallel data fetching - **Minimal dependencies**: Lean runtime footprint ### Memory Management - **Map-based storage**: Efficient data structures - **Event cleanup**: Proper listener management - **Graceful shutdown**: Resource cleanup on exit - **Connection pooling**: HTTP agent with configurable limits - **SSE connection limits**: Maximum concurrent connections with proper cleanup - **Automatic reconnection**: SSE connections with exponential backoff ## Testing Strategy ### Manual Testing Project includes test utilities for development: ```javascript // Read-only testing node test-read-only.js // ✅ Found 4 lights in living room // ✅ Logic test passed ``` ### Integration Testing - **Bridge connectivity**: API accessibility - **Authentication**: Valid API key - **Data consistency**: Hierarchy relationships - **Error scenarios**: Rate limiting, network failures ## Common Issues & Solutions ### Rate Limiting (429 Errors) **Problem**: Hue bridge returns "Too Many Requests" **Solution**: - Increase sync intervals - Add delays between requests - Disable state manager if persistent ### "No lights found in room" **Problem**: Room contains devices, not lights directly **Solution**: Traverse `Room → Device → Light` hierarchy ### State Manager Won't Initialize **Problem**: Continuous API failures prevent caching **Solution**: Automatic fallback to direct API mode ### Certificate Errors **Problem**: Self-signed certificates rejected **Solution**: Set `NODE_TLS_REJECT_UNAUTHORIZED=0` for development ### Health Monitoring The server implements basic health checks for production reliability: ```typescript // Health check endpoint returns: { status: 'healthy' | 'degraded' | 'unhealthy', bridge: { connected: boolean, lastSync: Date, apiCalls: number }, stateManager: { enabled: boolean, cacheHitRate: number }, uptime: number } ``` ## Deployment Considerations ### Claude Desktop Integration Add to `claude_desktop_config.json`: ```json { "mcpServers": { "hue-lights": { "command": "node", "args": ["/path/to/hue-mcp/dist/index.js"], "env": { "HUE_BRIDGE_IP": "192.168.1.100", "HUE_API_KEY": "your-api-key" } } } } ``` ### Production Setup 1. **Build project**: `npm run build` 2. **Configure bridge**: `npm run setup` 3. **Test connectivity**: Verify API access 4. **Add to Claude Desktop**: Update config file 5. **Restart Claude**: Reload MCP servers ## Future Enhancements ### Immediate Roadmap (Quick Wins) These improvements offer high value with minimal effort: 1. **Resource Cleanup** (1 day) - Implement connection pooling for HTTP agents - Add SSE connection limits and proper cleanup - Prevent memory leaks from orphaned connections 2. **Basic Health Checks** (1 day) - Add `/health` endpoint for monitoring - Track bridge connectivity status - Monitor cache hit rates and API call counts 3. **SSE Reconnection** (1 day) - Automatic reconnection with exponential backoff - Event deduplication during reconnects - Connection state tracking ### Long-Term Roadmap #### Phase 1: Testing & Quality (1-2 weeks) - **Comprehensive Test Suite** - Unit tests with mock Hue bridge - Integration tests for all MCP tools - Performance benchmarks (cache vs direct) - Automated regression testing #### Phase 2: Production Hardening (2-3 weeks) - **Advanced Error Recovery** - Circuit breaker pattern implementation - Bulkhead pattern for failure isolation - Retry budgets with jitter - **Monitoring & Observability** - Structured logging with correlation IDs - Metrics collection (Prometheus format) - Distributed tracing support - Performance profiling hooks #### Phase 3: Performance Optimization (1-2 weeks) - **State Management 2.0** - Differential sync (only changed data) - State versioning for consistency - Event sourcing for state changes - Snapshot/restore functionality - **API Optimization** - Request batching for bulk operations - Response compression - Field selection (GraphQL-style) - Lazy loading for large datasets #### Phase 4: Enhanced Security (2+ weeks) - **OAuth2 Integration** - Replace static API keys - Token refresh flow - Per-client rate limiting - **Audit & Compliance** - Comprehensive audit logging - API key rotation strategy - Security event monitoring ### Original Planned Features - **Scene creation**: Custom scene management - **Automation triggers**: Time-based lighting - **Group management**: Dynamic light groupings - **Analytics**: Usage patterns and reporting ### Scalability Considerations - **Multi-bridge support**: Enterprise environments - **Performance monitoring**: API usage tracking - **Configuration UI**: Web-based management interface - **Plugin architecture**: Extensible tool system ## Lessons Learned ### Critical Design Decisions 1. **Dual-mode operation**: Essential for reliability in IoT environments 2. **Hierarchy understanding**: API documentation doesn't always match reality 3. **Rate limiting protection**: Aggressive caching and delays required 4. **Comprehensive error handling**: IoT devices have many failure modes 5. **User experience focus**: Clear feedback more important than technical perfection ### Development Best Practices 1. **Test with real hardware**: Simulators don't capture real-world issues 2. **Plan for failures**: IoT devices are inherently unreliable 3. **Document thoroughly**: Complex integrations need detailed documentation 4. **Version carefully**: Breaking changes in IoT APIs are common 5. **Monitor actively**: Real-time status monitoring essential ## Conclusion This project demonstrates how to build a production-ready MCP server for IoT device control. Key success factors include robust error handling, graceful degradation, comprehensive state management, and focus on user experience. The architecture serves as a blueprint for similar IoT integration projects. The dual-mode operation pattern (cached + direct API) is particularly valuable for any system integrating with rate-limited APIs, while the comprehensive error handling ensures reliable operation in real-world deployments.

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/rmrfslashbin/hue-mcp'

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