System Configuration
This document provides detailed information about configuring the Home Assistant MCP Server.
Environment File Structure
The MCP Server uses a flexible environment configuration system with support for different environments and local overrides:
Environment Files
.env.example
- Template file containing all available configuration options with example values- Use this as a reference to create your environment-specific configuration files
-
Not loaded by the application
-
Environment-specific files (loaded based on NODE_ENV):
.env.dev
- Development environment (default).env.test
- Test environment-
.env.prod
- Production environment -
.env
- Optional local override file - If present, values in this file override those from the environment-specific file
- Useful for local development without modifying the environment-specific files
File Loading Order
- First, the environment-specific file is loaded based on NODE_ENV:
NODE_ENV=production
→.env.prod
NODE_ENV=development
→.env.dev
(default)-
NODE_ENV=test
→.env.test
-
Then, if a
.env
file exists, its values override any previously loaded values
Example setup:
# .env.dev - Development configuration
PORT=4000
HASS_HOST=http://homeassistant.local:8123
LOG_LEVEL=debug
# .env - Local overrides
PORT=3000 # Overrides PORT from .env.dev
HASS_HOST=http://localhost:8123 # Overrides HASS_HOST from .env.dev
Configuration File Structure
The MCP Server uses environment variables for configuration, with support for different environments (development, test, production):
# .env, .env.development, or .env.test
PORT=4000
NODE_ENV=development
HASS_HOST=http://192.168.178.63:8123
HASS_TOKEN=your_token_here
JWT_SECRET=your_secret_key
Server Settings
Basic Server Configuration
PORT
: Server port number (default: 4000)NODE_ENV
: Environment mode (development, production, test)HASS_HOST
: Home Assistant instance URLHASS_TOKEN
: Home Assistant long-lived access token
Security Settings
JWT_SECRET
: Secret key for JWT token generationRATE_LIMIT
: Rate limiting configurationwindowMs
: Time window in milliseconds (default: 15 minutes)max
: Maximum requests per window (default: 100)
WebSocket Settings
SSE
: Server-Sent Events configurationMAX_CLIENTS
: Maximum concurrent clients (default: 1000)PING_INTERVAL
: Keep-alive ping interval in ms (default: 30000)
Speech Features (Optional)
ENABLE_SPEECH_FEATURES
: Enable speech processing features (default: false)ENABLE_WAKE_WORD
: Enable wake word detection (default: false)ENABLE_SPEECH_TO_TEXT
: Enable speech-to-text conversion (default: false)WHISPER_MODEL_PATH
: Path to Whisper models directory (default: /models)WHISPER_MODEL_TYPE
: Whisper model type (default: base)- Available models: tiny.en, base.en, small.en, medium.en, large-v2
Environment Variables
All configuration is managed through environment variables:
# Server
PORT=4000
NODE_ENV=development
# Home Assistant
HASS_HOST=http://your-hass-instance:8123
HASS_TOKEN=your_token_here
# Security
JWT_SECRET=your-secret-key
# Logging
LOG_LEVEL=info
LOG_DIR=logs
LOG_MAX_SIZE=20m
LOG_MAX_DAYS=14d
LOG_COMPRESS=true
LOG_REQUESTS=true
# Speech Features (Optional)
ENABLE_SPEECH_FEATURES=false
ENABLE_WAKE_WORD=false
ENABLE_SPEECH_TO_TEXT=false
WHISPER_MODEL_PATH=/models
WHISPER_MODEL_TYPE=base
Advanced Configuration
Security Rate Limiting
Rate limiting is enabled by default to protect against brute force attacks:
RATE_LIMIT: {
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per window
}
Logging
The server uses Bun's built-in logging capabilities with additional configuration:
LOGGING: {
LEVEL: "info", // debug, info, warn, error
DIR: "logs",
MAX_SIZE: "20m",
MAX_DAYS: "14d",
COMPRESS: true,
TIMESTAMP_FORMAT: "YYYY-MM-DD HH:mm:ss:ms",
LOG_REQUESTS: true
}
Speech-to-Text Configuration
When speech features are enabled, you can configure the following options:
SPEECH: {
ENABLED: false, // Master switch for all speech features
WAKE_WORD_ENABLED: false, // Enable wake word detection
SPEECH_TO_TEXT_ENABLED: false, // Enable speech-to-text
WHISPER_MODEL_PATH: "/models", // Path to Whisper models
WHISPER_MODEL_TYPE: "base", // Model type to use
}
Available Whisper models: - tiny.en
: Fastest, lowest accuracy - base.en
: Good balance of speed and accuracy - small.en
: Better accuracy, slower - medium.en
: High accuracy, much slower - large-v2
: Best accuracy, very slow
For production deployments, we recommend using system tools like logrotate
for log management.
Example logrotate configuration (/etc/logrotate.d/mcp-server
):
/var/log/mcp-server.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 644 mcp mcp
}
Best Practices
- Always use environment variables for sensitive information
- Keep .env files secure and never commit them to version control
- Use different environment files for development, test, and production
- Enable SSL/TLS in production (preferably via reverse proxy)
- Monitor log files for issues
- Regularly rotate logs in production
- Start with smaller Whisper models and upgrade if needed
- Consider GPU acceleration for larger Whisper models
Validation
The server validates configuration on startup using Zod schemas: - Required fields are checked (e.g., HASS_TOKEN) - Value types are verified - Enums are validated (e.g., LOG_LEVEL, WHISPER_MODEL_TYPE) - Default values are applied when not specified
Troubleshooting
Common configuration issues: 1. Missing required environment variables 2. Invalid environment variable values 3. Permission issues with log directories 4. Rate limiting too restrictive 5. Speech model loading failures 6. Docker not available for speech features 7. Insufficient system resources for larger models
See the Troubleshooting Guide for solutions.
Configuration Guide
This document describes the environment configuration system for the Home Assistant MCP Server.
Environment Setup
Using the Setup Script
The MCP Server provides a setup script to help manage your environment configuration:
# Make the script executable
chmod +x scripts/setup-env.sh
# Basic usage (uses NODE_ENV or defaults to development)
./scripts/setup-env.sh
# Specify an environment
NODE_ENV=production ./scripts/setup-env.sh
# Force override existing files
./scripts/setup-env.sh --force
The setup script will: 1. Check for .env.example
and create .env
if it doesn't exist 2. Detect the environment (development/production/test) 3. Optionally override .env
with environment-specific settings 4. Maintain your existing configuration unless forced to override
Manual Setup
If you prefer to set up manually:
# Copy the example configuration
cp .env.example .env
# Then copy the appropriate environment override
cp .env.dev .env # For development
cp .env.prod .env # For production
cp .env.test .env # For testing
Environment File Hierarchy
Base Configuration Files
.env.example
- Template with all available options and documentation.env
- Your main configuration file (copied from .env.example)
Environment-Specific Files
.env.dev
- Development environment settings.env.prod
- Production environment settings.env.test
- Test environment settings
Loading Order and Priority
Files are loaded in the following sequence, with later files overriding earlier ones:
.env
(base configuration)- Environment-specific file based on NODE_ENV:
NODE_ENV=development
→.env.dev
NODE_ENV=production
→.env.prod
NODE_ENV=test
→.env.test
Docker Environment Handling
When using Docker, the environment is loaded as follows:
.env
file (base configuration).env.${NODE_ENV}
file (environment-specific overrides)- Environment variables from docker-compose.yml
- Command-line environment variables
Example docker-compose.yml configuration:
services:
homeassistant-mcp:
env_file:
- .env
- .env.${NODE_ENV:-development}
environment:
- NODE_ENV=${NODE_ENV:-development}
- PORT=4000
- HASS_HOST
- HASS_TOKEN
- LOG_LEVEL=${LOG_LEVEL:-info}
Override examples:
# Override NODE_ENV
NODE_ENV=production docker compose up -d
# Override multiple variables
NODE_ENV=production LOG_LEVEL=debug docker compose up -d
Configuration Options
Required Settings
# Server Configuration
PORT=4000 # Server port number
NODE_ENV=development # Environment (development/production/test)
# Home Assistant
HASS_HOST=http://homeassistant.local:8123 # Home Assistant URL
HASS_TOKEN=your_token_here # Long-lived access token
# Security
JWT_SECRET=your_secret_key # JWT signing secret
Optional Settings
Security
# Rate Limiting
RATE_LIMIT_WINDOW=900000 # Time window in ms (15 minutes)
RATE_LIMIT_MAX_REQUESTS=100 # Max requests per window
RATE_LIMIT_REGULAR=100 # Regular endpoint rate limit
RATE_LIMIT_WEBSOCKET=1000 # WebSocket connection rate limit
# CORS Configuration
CORS_ORIGINS=http://localhost:3000,http://localhost:8123
CORS_METHODS=GET,POST,PUT,DELETE,OPTIONS
CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-Requested-With
CORS_EXPOSED_HEADERS=
CORS_CREDENTIALS=true
CORS_MAX_AGE=86400
# Cookie Security
COOKIE_SECRET=your_cookie_secret_key_min_32_chars
COOKIE_SECURE=true
COOKIE_HTTP_ONLY=true
COOKIE_SAME_SITE=Strict
Logging
# Logging Configuration
LOG_LEVEL=info # debug, info, warn, error
LOG_DIR=logs # Log directory
LOG_MAX_SIZE=20m # Max log file size
LOG_MAX_DAYS=14d # Log retention period
LOG_COMPRESS=true # Enable log compression
LOG_REQUESTS=true # Log HTTP requests
Speech Features
# Speech Processing
ENABLE_SPEECH_FEATURES=false # Master switch for speech features
ENABLE_WAKE_WORD=false # Enable wake word detection
ENABLE_SPEECH_TO_TEXT=false # Enable speech-to-text
WHISPER_MODEL_PATH=/models # Path to Whisper models
WHISPER_MODEL_TYPE=base # Whisper model type
# Audio Configuration
NOISE_THRESHOLD=0.05
MIN_SPEECH_DURATION=1.0
SILENCE_DURATION=0.5
SAMPLE_RATE=16000
CHANNELS=1
CHUNK_SIZE=1024
PULSE_SERVER=unix:/run/user/1000/pulse/native
Best Practices
- Version Control
- Never commit
.env
files to version control - Always commit
.env.example
with documentation -
Consider committing
.env.dev
and.env.test
for team development -
Security
- Use strong, unique values for secrets
- Enable HTTPS in production
-
Keep tokens and secrets in
.env
only -
Development
- Use
.env.dev
for shared development settings - Keep
.env
for personal overrides -
Enable debug logging in development
-
Production
- Use
.env.prod
for production defaults - Set appropriate rate limits
- Configure proper logging
-
Enable all security features
-
Testing
- Use
.env.test
for test configuration - Use mock tokens and endpoints
- Enable detailed logging for debugging
Troubleshooting
Common Issues
- Missing Required Variables
- Error: "Missing required environment variable: HASS_TOKEN"
-
Solution: Ensure HASS_TOKEN is set in your .env file
-
Permission Issues
- Error: "EACCES: permission denied, access '/app/logs'"
-
Solution: Ensure proper permissions on the logs directory
-
Invalid Configuration
- Error: "Invalid configuration value for PORT"
-
Solution: Check the value format in your .env file
-
Environment Override Issues
- Problem: Environment-specific settings not applying
- Solution: Check NODE_ENV value and file naming
See Troubleshooting Guide for more solutions.