Home Automation MCP Server

Home Automation MCP Server

A comprehensive smart home automation system that allows AI assistants to control and monitor smart home devices through natural language using the Model Context Protocol (MCP).

Category
访问服务器

README

Home Automation MCP Server

A comprehensive smart home automation system that allows AI assistants to control and monitor smart home devices through natural language using the Model Context Protocol (MCP).

🏗️ Architecture

AI Assistant (Claude Desktop/VS Code)
        ↓ stdio MCP Protocol
    FastMCP Server
        ↓
    SQLite Database ← [Real-time Polling] ← FastAPI Server
                                                ↓ WebSocket
                                            React Frontend

Key Components:

  • FastMCP Server - Handles AI interactions via stdio protocol
  • FastAPI Server - REST API + WebSocket for real-time frontend updates
  • SQLite Database - Shared state between both servers with timestamp-based change detection
  • React Frontend - Real-time dashboard with WebSocket updates

✨ Features

MCP Tools (9 Tools)

  1. control_device - Universal device control (on/off/set/toggle)
  2. get_device_status - Query device states
  3. get_sensor_reading - Read temperature, motion sensors
  4. set_home_mode - Execute scenes (home/away/sleep/vacation)
  5. get_home_mode - Check current mode
  6. feed_fish - Trigger fish feeder
  7. water_plants - Control sprinkler system
  8. start_ev_charging / stop_ev_charging - EV charger control

Supported Devices (24+ Sample Devices)

  • 💡 Lights (with brightness control)
  • 🌡️ Thermostat (temperature + mode control)
  • 🔒 Locks
  • 🪟 Blinds (with position control)
  • 💨 Fans (with speed control)
  • 🚗 Garage door
  • 🐠 Fish feeder
  • 💧 Sprinkler system
  • 🔌 EV charger
  • 🌡️ Temperature sensors
  • 👁️ Motion sensors

Home Modes

  • Home - Welcome mode (lights on, 72°F)
  • Away - Security mode (lights off, locks engaged, 65°F)
  • Sleep - Night mode (bedroom dim 20%, doors locked, 68°F)
  • Vacation - Extended away (everything secured, 60°F)

🚀 Quick Start

1. Install Dependencies

pip install -r requirements.txt
cd frontend
npm install  # First time only

2. Start the System

Option A: Using the Menu (Easiest)

start.bat

Then select what to start from the menu.

Option B: Direct Commands (Recommended for Development)

Open 2 separate terminals:

# Terminal 1: Start API Server (Backend)
python app/main.py
# → Available at http://localhost:8000

# Terminal 2: Start Frontend (Dashboard)
cd frontend
npm run dev
# → Available at http://localhost:5173

3. Configure MCP Server for Claude Desktop (Optional)

Run the configuration helper:

python app/stdio_config.py

Copy the output and add it to your Claude Desktop config file:

  • Windows: %APPDATA%/Claude/claude_desktop_config.json
  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

Example configuration:

{
  "mcpServers": {
    "home-automation": {
      "command": "python",
      "args": ["C:/path/to/home_automation/app/mcp_server_stdio.py"]
    }
  }
}

Restart Claude Desktop after adding the configuration.

💬 Example AI Interactions

Device Control

"Turn on the living room lights to 75%"
"Set bedroom temperature to 72 degrees"
"Close all the blinds"
"Lock all doors"

Status Queries

"What's the status of my home?"
"What's the temperature in the bedroom?"
"Are all the doors locked?"

Home Modes

"I'm leaving" → Sets away mode
"I'm going to bed" → Sets sleep mode
"Good morning" → Sets home mode

Special Actions

"Feed the fish"
"Water the front yard for 10 minutes"
"Start charging my car"

📁 Project Structure

home_automation/
├── app/
│   ├── config.py                 # Configuration settings
│   ├── main.py                   # FastAPI server
│   ├── mcp_server_stdio.py       # FastMCP server with tools
│   ├── stdio_config.py           # MCP configuration helper
│   ├── db/
│   │   ├── schema.sql           # Database schema
│   │   ├── database.py          # Database manager
│   │   └── seed_data.py         # Sample devices
│   ├── models/
│   │   └── device.py            # Device models
│   ├── schemas/
│   │   └── responses.py         # API response schemas
│   └── utils/
│       └── websocket_manager.py # WebSocket manager
├── frontend/                     # React dashboard
├── requirements.txt
├── home_automation.db           # SQLite database (auto-created)
├── README.md
└── DEVELOPMENT.md               # Development guide

🔧 API Endpoints

REST API

  • GET / - API information
  • GET /api/devices - Get all devices (supports ?room= and ?type= filters)
  • GET /api/rooms - Get list of rooms
  • GET /api/stats - Get dashboard statistics
  • WebSocket /ws - Real-time device updates

WebSocket Messages

From Server:

{
  "type": "device_update",
  "device_id": "living_room_light_main",
  "state": "on",
  "properties": {"brightness": 75}
}

{
  "type": "mode_change",
  "mode": "away"
}

{
  "type": "full_refresh"
}

🧪 Testing

Test API Server

curl http://localhost:8000
curl http://localhost:8000/api/devices
curl http://localhost:8000/api/stats

Test MCP Tools Directly

python app/mcp_server_stdio.py

Test with MCP Inspector

npx @modelcontextprotocol/inspector python app/mcp_server_stdio.py

Open browser to: http://localhost:6274

📋 All Available Commands

Command Purpose URL
start.bat Menu-driven launcher -
python app/main.py Start API server http://localhost:8000
cd frontend && npm run dev Start frontend http://localhost:5173
python app/mcp_server_stdio.py Start MCP server stdio only
npx @modelcontextprotocol/inspector python app/mcp_server_stdio.py Test with inspector http://localhost:6274
python app/stdio_config.py Get Claude config -

🔄 Real-time Updates Flow

  1. AI assistant calls MCP tool (e.g., control_device)
  2. MCP server updates SQLite database with timestamp
  3. FastAPI server detects timestamp change (polls every 100ms)
  4. FastAPI broadcasts update via WebSocket to all connected clients
  5. Frontend receives update and re-renders affected devices
  6. Total latency: < 300ms

📊 Performance Metrics

  • ✅ Database queries: < 10ms
  • ✅ MCP tool execution: < 100ms
  • ✅ Change detection: 100ms polling
  • ✅ WebSocket broadcast: < 50ms
  • ✅ End-to-end update: < 300ms
  • ✅ Concurrent WebSocket connections: 100+

🐛 Troubleshooting

MCP Server Not Connecting

  • Check Claude Desktop config file path
  • Verify Python path in configuration
  • Restart Claude Desktop after config changes

Frontend Not Updating

  • Verify FastAPI server is running on port 8000
  • Check browser console for WebSocket errors
  • Ensure CORS origins include your frontend URL

Database Locked Errors

  • Verify only one process is accessing the database at a time
  • WAL mode is enabled automatically in database.py

Port Already in Use

# Windows PowerShell - Kill process on port
Get-NetTCPConnection -LocalPort 8000 | 
  Select-Object -ExpandProperty OwningProcess | 
  ForEach-Object { Stop-Process -Id $_ -Force }

🛠️ Development

Add New Device Type

  1. Add device to app/db/seed_data.py
  2. Update type hints in app/models/device.py
  3. Add icon in frontend DeviceCard.jsx

Add New MCP Tool

  1. Add @mcp.tool() decorated function in app/mcp_server_stdio.py
  2. Include database operations
  3. Document in docstring for AI assistant context

For detailed development information, see DEVELOPMENT.md

📚 Resources

📝 License

MIT License - See LICENSE file for details

🤝 Contributing

Contributions welcome! Please open an issue or submit a pull request.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选