Home Assistant Integration
STDIOStandardized protocol for AI assistants to interact with Home Assistant devices and automation.
Standardized protocol for AI assistants to interact with Home Assistant devices and automation.
A standardized protocol for AI assistants to interact with Home Assistant, providing a secure, typed, and extensible interface for controlling smart home devices.
The Model Context Protocol (MCP) server acts as a bridge between AI models (like Claude, GPT, etc.) and Home Assistant, enabling AI assistants to:
# Clone the repository git clone https://github.com/your-repo/homeassistant-mcp.git # Install dependencies cd homeassistant-mcp npm install # Build the project npm run build
# Start with standard I/O transport (for AI assistant integration) npm start -- --stdio # Start with HTTP transport (for API access) npm start -- --http # Start with both transports npm start -- --stdio --http
Configure the server using environment variables or a .env
file:
# Server configuration PORT=3000 NODE_ENV=development # Execution settings EXECUTION_TIMEOUT=30000 STREAMING_ENABLED=true # Transport settings USE_STDIO_TRANSPORT=true USE_HTTP_TRANSPORT=true # Debug and logging DEBUG_MODE=false DEBUG_STDIO=false DEBUG_HTTP=false SILENT_STARTUP=false # CORS settings CORS_ORIGIN=*
The MCP server is built with a layered architecture:
Tools are the primary way to add functionality to the MCP server. Each tool:
Example tool registration:
import { LightsControlTool } from "./tools/homeassistant/lights.tool.js"; import { ClimateControlTool } from "./tools/homeassistant/climate.tool.js"; // Register tools server.registerTool(new LightsControlTool()); server.registerTool(new ClimateControlTool());
When running with HTTP transport, the server provides a JSON-RPC 2.0 API:
POST /api/mcp/jsonrpc
- Execute a toolGET /api/mcp/stream
- Connect to SSE stream for real-time updatesGET /api/mcp/info
- Get server informationGET /health
- Health check endpointimport { createClaudeToolDefinitions } from "./mcp/index.js"; // Generate Claude-compatible tool definitions const claudeTools = createClaudeToolDefinitions([ new LightsControlTool(), new ClimateControlTool() ]); // Use with Claude API const messages = [ { role: "user", content: "Turn on the lights in the living room" } ]; const response = await claude.messages.create({ model: "claude-3-opus-20240229", messages, tools: claudeTools });
To use the Home Assistant MCP server with Cursor, add the following to your .cursor/config/config.json
file:
{ "mcpServers": { "homeassistant-mcp": { "command": "bash", "args": ["-c", "cd ${workspaceRoot} && bun run dist/index.js --stdio 2>/dev/null | grep -E '\\{\"jsonrpc\":\"2\\.0\"'"], "env": { "NODE_ENV": "development", "USE_STDIO_TRANSPORT": "true", "DEBUG_STDIO": "true" } } } }
This configuration:
{"jsonrpc":"2.0"
, ensuring clean JSON-RPC outputIf you encounter a "failed to create client" error when using the MCP server with Cursor:
Make sure you're using the correct command and arguments in your Cursor configuration
bun run build
before trying to connectEnsure the server is properly outputting JSON-RPC messages to stdout:
bun run dist/index.js --stdio 2>/dev/null | grep -E '\{"jsonrpc":"2\.0"' > json_only.txt
Then examine json_only.txt to verify it contains only valid JSON-RPC messages.
Make sure grep is installed on your system (it should be available by default on most systems)
Try rebuilding the server with:
bun run build
Enable debug mode by setting DEBUG_STDIO=true
in the environment variables
If the issue persists, you can try:
{ "command": "bash", "args": ["-c", "cd ${workspaceRoot} && node dist/index.js --stdio 2>/dev/null | grep -E '\\{\"jsonrpc\":\"2\\.0\"'"] }
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
MCP (Model Context Protocol) Server is my lightweight integration tool for Home Assistant, providing a flexible interface for device management and automation. It's designed to be fast, secure, and easy to use. Built with Bun for maximum performance.
I chose Bun as the runtime for several key benefits:
⚡ Blazing Fast Performance
🎯 All-in-One Solution
🔋 Built-in Features
💾 Resource Efficient
🔄 Node.js Compatibility
git clone https://github.com/jango-blockchained/homeassistant-mcp.git cd homeassistant-mcp
# Make my setup script executable chmod +x scripts/setup-env.sh # Run setup (defaults to development) ./scripts/setup-env.sh # Or specify an environment: NODE_ENV=production ./scripts/setup-env.sh # Force override existing files: ./scripts/setup-env.sh --force
.env
file with your Home Assistant detailsHASS_TOKEN
(long-lived access token)# Standard build ./docker-build.sh # Launch: docker compose up -d
My Docker build script (docker-build.sh
) supports different configurations:
./docker-build.sh
./docker-build.sh --speech
onerahmet/openai-whisper-asr-webservice
rhasspy/wyoming-openwakeword
./docker-build.sh --speech --gpu
I've implemented a hierarchical configuration system:
.env.example
- My template with all options.env
- Your configuration (copy from .env.example).env.dev
- Development settings.env.prod
- Production settings.env.test
- Test settingsFiles load in this order:
.env
(base config)NODE_ENV=development
→ .env.dev
NODE_ENV=production
→ .env.prod
NODE_ENV=test
→ .env.test
Later files override earlier ones.
# Install dependencies bun install # Run in development mode bun run dev # Run tests bun test # Run with hot reload bun --hot run dev # Build for production bun build ./src/index.ts --target=bun # Run production build bun run start
Operation | Bun | Node.js |
---|---|---|
Install Dependencies | ~2s | ~15s |
Cold Start | 300ms | 1000ms |
Build Time | 150ms | 4000ms |
Memory Usage | ~150MB | ~400MB |
Add to .cursor/config/config.json
:
{ "mcpServers": { "homeassistant-mcp": { "command": "bash", "args": ["-c", "cd ${workspaceRoot} && bun run dist/index.js --stdio 2>/dev/null | grep -E '\\{\"jsonrpc\":\"2\\.0\"'"], "env": { "NODE_ENV": "development", "USE_STDIO_TRANSPORT": "true", "DEBUG_STDIO": "true" } } } }
Add to your Claude config:
{ "mcpServers": { "homeassistant-mcp": { "command": "bun", "args": ["run", "start", "--port", "8080"], "env": { "NODE_ENV": "production" } } } }
Windows users can use the provided script:
scripts
directorystart_mcp.cmd
MCP Server optionally supports speech processing capabilities:
.env
:ENABLE_SPEECH_FEATURES=true ENABLE_WAKE_WORD=true ENABLE_SPEECH_TO_TEXT=true WHISPER_MODEL_PATH=/models WHISPER_MODEL_TYPE=base
# For standard Whisper STT_ENGINE=whisper # For Fast Whisper (GPU recommended) STT_ENGINE=fast-whisper CUDA_VISIBLE_DEVICES=0 # Set GPU device
Choose based on your needs:
tiny.en
: Fastest, basic accuracybase.en
: Good balance (recommended)small.en
: Better accuracy, slowermedium.en
: High accuracy, resource intensivelarge-v2
: Best accuracy, very resource intensive# Build with speech support ./docker-build.sh --speech # Launch with speech features: docker compose -f docker-compose.yml -f docker-compose.speech.yml up -d
I've included several powerful tools in the extra/
directory to enhance your Home Assistant experience:
Home Assistant Analyzer CLI (ha-analyzer-cli.ts
)
Speech-to-Text Example (speech-to-text-example.ts
)
Claude Desktop Setup (claude-desktop-macos-setup.sh
)
See Extras Documentation for detailed usage instructions and examples.
MIT License. See LICENSE for details.
Created by jango-blockchained
MCP Server supports a JSON-RPC 2.0 stdio transport mode for direct integration with AI assistants like Claude:
✅ JSON-RPC 2.0 Compatibility: Full support for the MCP protocol standard
✅ NPX Support: Run directly without installation using npx homeassistant-mcp
✅ Auto Configuration: Creates necessary directories and default configuration
✅ Cross-Platform: Works on macOS, Linux, and Windows
✅ Claude Desktop Integration: Ready to use with Claude Desktop
✅ Parameter Validation: Automatic validation of tool parameters
✅ Error Handling: Standardized error codes and handling
✅ Detailed Logging: Logs to files without polluting stdio
Run the MCP server directly without installation using npx:
# Basic usage npx homeassistant-mcp # Or with environment variables HASS_URL=http://your-ha-instance:8123 HASS_TOKEN=your_token npx homeassistant-mcp
This will:
Perfect for integration with Claude Desktop or other MCP clients.
To use MCP with Claude Desktop:
npx homeassistant-mcp
Claude will now use the MCP server for Home Assistant integration, allowing you to control your smart home directly through Claude.
Update your .env
file to enable stdio transport:
USE_STDIO_TRANSPORT=true
Run the server using the stdio-start script:
./stdio-start.sh
Available options:
./stdio-start.sh --debug # Enable debug mode
./stdio-start.sh --rebuild # Force rebuild
./stdio-start.sh --help # Show help
When running in stdio mode:
logs/
directory{ "jsonrpc": "2.0", "id": "unique-request-id", "method": "tool-name", "params": { "param1": "value1", "param2": "value2" } }
{ "jsonrpc": "2.0", "id": "unique-request-id", "result": { // Tool-specific result data } }
{ "jsonrpc": "2.0", "id": "unique-request-id", "error": { "code": -32000, "message": "Error message", "data": {} // Optional error details } }
{ "jsonrpc": "2.0", "method": "notification-type", "params": { // Notification data } }
Code | Description | Meaning |
---|---|---|
-32700 | Parse error | Invalid JSON was received |
-32600 | Invalid request | JSON is not a valid request object |
-32601 | Method not found | Method does not exist or is unavailable |
-32602 | Invalid params | Invalid method parameters |
-32603 | Internal error | Internal JSON-RPC error |
-32000 | Tool execution | Error executing the tool |
-32001 | Validation error | Parameter validation failed |
To use this MCP server with Claude Desktop:
Create or edit your Claude Desktop configuration:
# On macOS nano ~/Library/Application\ Support/Claude/claude_desktop_config.json # On Linux nano ~/.config/Claude/claude_desktop_config.json # On Windows notepad %APPDATA%\Claude\claude_desktop_config.json
Add the MCP server configuration:
{ "mcpServers": { "homeassistant-mcp": { "command": "npx", "args": ["homeassistant-mcp"], "env": { "HASS_TOKEN": "your_home_assistant_token_here", "HASS_HOST": "http://your_home_assistant_host:8123" } } } }
Restart Claude Desktop.
In Claude, you can now use the Home Assistant MCP tools.
The simplest way to use the Home Assistant MCP server is through NPX:
# Start the server in stdio mode npx homeassistant-mcp
This will automatically:
You can redirect stderr to hide logs and only see the JSON-RPC output:
npx homeassistant-mcp 2>/dev/null
If you prefer to install the package globally or locally:
# Install globally npm install -g homeassistant-mcp # Then run homeassistant-mcp
Or install locally:
# Install locally npm install homeassistant-mcp # Then run using npx npx homeassistant-mcp