Home Assistant MCP server

Connect to the Home Assistant platform, enabling smart home control.
Back to servers
Provider
tevonsb
Release date
Dec 12, 2024
Language
TypeScript
Stats
228 stars

This MCP server acts as a bridge between your Home Assistant instance and Language Learning Models (LLMs), allowing natural language control and monitoring of your smart home through the Model Context Protocol. It provides a comprehensive API for managing your entire Home Assistant ecosystem, from controlling devices to managing automations.

Installation

Basic Setup

# Clone the repository
git clone https://github.com/jango-blockchained/homeassistant-mcp.git
cd homeassistant-mcp

# Install dependencies
npm install

# Build the project
npm run build

Docker Setup (Recommended)

  1. Clone the repository:

    git clone https://github.com/jango-blockchained/homeassistant-mcp.git
    cd homeassistant-mcp
    
  2. Configure environment:

    cp .env.example .env
    

    Edit the .env file with your Home Assistant configuration:

    # Home Assistant Configuration
    HASS_HOST=http://homeassistant.local:8123
    HASS_TOKEN=your_home_assistant_token
    HASS_SOCKET_URL=ws://homeassistant.local:8123/api/websocket
    
    # Server Configuration
    PORT=3000
    NODE_ENV=production
    DEBUG=false
    
  3. Build and run with Docker Compose:

    # Build and start the containers
    docker compose up -d
    
    # View logs
    docker compose logs -f
    
    # Stop the service
    docker compose down
    
  4. Verify the installation: The server should now be running at http://localhost:3000. You can check the health endpoint at http://localhost:3000/health.

  5. Update the application:

    # Pull the latest changes
    git pull
    
    # Rebuild and restart the containers
    docker compose up -d --build
    

Configuration

Environment Variables

# Home Assistant Configuration
HASS_HOST=http://homeassistant.local:8123  # Your Home Assistant instance URL
HASS_TOKEN=your_home_assistant_token       # Long-lived access token
HASS_SOCKET_URL=ws://homeassistant.local:8123/api/websocket  # WebSocket URL

# Server Configuration
PORT=3000                # Server port (default: 3000)
NODE_ENV=production     # Environment (production/development)
DEBUG=false            # Enable debug mode

Adding to Claude Desktop (or other clients)

To use your new Home Assistant MCP server with Claude Desktop:

{
  "homeassistant": {
    "command": "node",
    "args": [<path/to/your/dist/folder>]
    "env": {
      "NODE_ENV": "development",
      "HASS_HOST": "http://homeassistant.local:8123",
      "HASS_TOKEN": "your_home_assistant_token",
      "PORT": "3000",
      "HASS_SOCKET_URL": "ws://homeassistant.local:8123/api/websocket",
      "LOG_LEVEL": "debug"
    }
  }
}

Real-time Updates with SSE

The server includes a powerful Server-Sent Events (SSE) system for real-time updates:

const eventSource = new EventSource(
  'http://localhost:3000/subscribe_events?token=YOUR_TOKEN&domain=light'
);

eventSource.onmessage = (event) => {
  const data = JSON.parse(event.data);
  console.log('Update received:', data);
};

Usage Examples

Device Control

Common Entity Controls

{
  "tool": "control",
  "command": "turn_on",  // or "turn_off", "toggle"
  "entity_id": "light.living_room"
}

Light Control

{
  "tool": "control",
  "command": "turn_on",
  "entity_id": "light.living_room",
  "brightness": 128,
  "color_temp": 4000,
  "rgb_color": [255, 0, 0]
}

Add-on Management

List Available Add-ons

{
  "tool": "addon",
  "action": "list"
}

Install Add-on

{
  "tool": "addon",
  "action": "install",
  "slug": "core_configurator",
  "version": "5.6.0"
}

Manage Add-on State

{
  "tool": "addon",
  "action": "start",  // or "stop", "restart"
  "slug": "core_configurator"
}

Package Management

List HACS Packages

{
  "tool": "package",
  "action": "list",
  "category": "integration"  // or "plugin", "theme", "python_script", "appdaemon", "netdaemon"
}

Install Package

{
  "tool": "package",
  "action": "install",
  "category": "integration",
  "repository": "hacs/integration",
  "version": "1.32.0"
}

Automation Management

Create Automation

{
  "tool": "automation_config",
  "action": "create",
  "config": {
    "alias": "Motion Light",
    "description": "Turn on light when motion detected",
    "mode": "single",
    "trigger": [
      {
        "platform": "state",
        "entity_id": "binary_sensor.motion",
        "to": "on"
      }
    ],
    "action": [
      {
        "service": "light.turn_on",
        "target": {
          "entity_id": "light.living_room"
        }
      }
    ]
  }
}

Duplicate Automation

{
  "tool": "automation_config",
  "action": "duplicate",
  "automation_id": "automation.motion_light"
}

Troubleshooting

Common Issues

  1. Node.js Version (toSorted is not a function)

    • Solution: Update to Node.js 20.10.0+
    nvm install 20.10.0
    nvm use 20.10.0
    
  2. Connection Issues

    • Verify Home Assistant is running
    • Check HASS_HOST accessibility
    • Validate token permissions
    • Ensure WebSocket connection for real-time updates
  3. Add-on Management Issues

    • Verify Supervisor access
    • Check add-on compatibility
    • Validate system resources
  4. HACS Integration Issues

    • Verify HACS installation
    • Check HACS integration status
    • Validate repository access
  5. Automation Issues

    • Verify entity availability
    • Check trigger conditions
    • Validate service calls
    • Monitor execution logs

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later