- Replaced Express with Elysia for improved performance and type safety - Integrated Elysia middleware for rate limiting, security headers, and request validation - Refactored security utilities to work with Elysia's context and request handling - Updated token management and validation logic - Added comprehensive security headers and input sanitization - Simplified server initialization and error handling - Updated documentation with new setup and configuration details
7.3 KiB
Troubleshooting Guide
This guide helps you diagnose and fix common issues with the Home Assistant MCP.
Common Issues
Connection Issues
Cannot Connect to Home Assistant
Symptoms:
- Connection timeout errors
- "Failed to connect to Home Assistant" messages
- 401 Unauthorized errors
Solutions:
- Verify Home Assistant is running
- Check HASS_HOST environment variable
- Validate HASS_TOKEN is correct
- Ensure network connectivity
- Check firewall settings
SSE Connection Drops
Symptoms:
- Frequent disconnections
- Missing events
- Connection reset errors
Solutions:
- Check network stability
- Increase connection timeout
- Implement reconnection logic
- Monitor server resources
Authentication Issues
Invalid Token
Symptoms:
- 401 Unauthorized responses
- "Invalid token" messages
- Authentication failures
Solutions:
- Generate new Long-Lived Access Token
- Check token expiration
- Verify token format
- Update environment variables
Rate Limiting
Symptoms:
- 429 Too Many Requests
- "Rate limit exceeded" messages
Solutions:
- Implement request throttling
- Adjust rate limit settings
- Cache responses
- Optimize request patterns
Tool Issues
Tool Not Found
Symptoms:
- "Tool not found" errors
- 404 Not Found responses
Solutions:
- Check tool name spelling
- Verify tool registration
- Update tool imports
- Check tool availability
Tool Execution Fails
Symptoms:
- Tool execution errors
- Unexpected responses
- Timeout issues
Solutions:
- Validate input parameters
- Check error logs
- Debug tool implementation
- Verify Home Assistant permissions
Debugging
Server Logs
-
Enable debug logging:
LOG_LEVEL=debug -
Check logs:
npm run logs -
Filter logs:
npm run logs | grep "error"
Network Debugging
-
Check API endpoints:
curl -v http://localhost:3000/api/health -
Monitor SSE connections:
curl -N http://localhost:3000/api/sse/stats -
Test WebSocket:
wscat -c ws://localhost:3000
Performance Issues
-
Monitor memory usage:
npm run stats -
Check response times:
curl -w "%{time_total}\n" -o /dev/null -s http://localhost:3000/api/health -
Profile code:
npm run profile
FAQ
Q: How do I reset my configuration?
A: Delete .env and copy .env.example to start fresh.
Q: Why are my events delayed?
A: Check network latency and server load. Consider adjusting buffer sizes.
Q: How do I update my token?
A: Generate a new token in Home Assistant and update HASS_TOKEN.
Q: Why do I get "Maximum clients reached"?
A: Adjust SSE_MAX_CLIENTS in configuration or clean up stale connections.
Error Codes
E001: Connection ErrorE002: Authentication ErrorE003: Rate Limit ErrorE004: Tool ErrorE005: Configuration Error
Support Resources
-
Documentation
-
Community
- GitHub Issues
- Discussion Forums
- Stack Overflow
-
Tools
- Diagnostic Scripts
- Testing Tools
- Monitoring Tools
Still Need Help?
-
Create a detailed issue:
- Error messages
- Steps to reproduce
- Environment details
- Logs
-
Contact support:
- GitHub Issues
- Email Support
- Community Forums
Security Middleware Troubleshooting
Common Issues and Solutions
Rate Limiting Problems
Symptom: Unexpected 429 (Too Many Requests) errors
Possible Causes:
- Misconfigured rate limit settings
- Shared IP addresses (e.g., behind NAT)
- Aggressive client-side retry mechanisms
Solutions:
-
Adjust rate limit parameters
// Customize rate limit for specific scenarios checkRateLimit(ip, maxRequests = 200, windowMs = 30 * 60 * 1000) -
Implement more granular rate limiting
- Use different limits for different endpoints
- Consider user authentication level
Request Validation Failures
Symptom: 400 or 415 status codes on valid requests
Possible Causes:
- Incorrect
Content-Typeheader - Large request payloads
- Malformed authorization headers
Debugging Steps:
-
Verify request headers
// Check content type and size validateRequestHeaders(request, 'application/json') -
Log detailed validation errors
try { validateRequestHeaders(request); } catch (error) { console.error('Request validation failed:', error.message); }
Input Sanitization Issues
Symptom: Unexpected data transformation or loss
Possible Causes:
- Complex nested objects
- Non-standard input formats
- Overly aggressive sanitization
Troubleshooting:
-
Test sanitization with various input types
const input = { text: '<script>alert("xss")</script>', nested: { html: '<img src="x" onerror="alert(1)">World' } }; const sanitized = sanitizeValue(input); -
Custom sanitization for specific use cases
function customSanitize(value) { // Add custom sanitization logic return sanitizeValue(value); }
Security Header Configuration
Symptom: Missing or incorrect security headers
Possible Causes:
- Misconfigured Helmet options
- Environment-specific header requirements
Solutions:
- Custom security header configuration
const customHelmetConfig = { contentSecurityPolicy: { directives: { defaultSrc: ["'self'"], scriptSrc: ["'self'", 'trusted-cdn.com'] } } }; applySecurityHeaders(request, customHelmetConfig);
Error Handling and Logging
Symptom: Inconsistent error responses
Possible Causes:
- Incorrect environment configuration
- Unhandled error types
Debugging Techniques:
-
Verify environment settings
const errorResponse = handleError(error, process.env.NODE_ENV); -
Add custom error handling
function enhancedErrorHandler(error, env) { // Add custom logging or monitoring console.error('Security error:', error); return handleError(error, env); }
Performance and Security Monitoring
-
Logging
- Enable debug logging for security events
- Monitor rate limit and validation logs
-
Metrics
- Track rate limit hit rates
- Monitor request validation success/failure ratios
-
Continuous Improvement
- Regularly review and update security configurations
- Conduct periodic security audits
Environment-Specific Considerations
Development
- More verbose error messages
- Relaxed rate limiting
- Detailed security logs
Production
- Minimal error details
- Strict rate limiting
- Comprehensive security headers
External Resources
Getting Help
If you encounter persistent issues:
- Check application logs
- Verify environment configurations
- Consult the project's issue tracker
- Reach out to the development team with detailed error information