apiarya/wemo-mcp-server

分类Web
作者Community
星标379
定价Free

简介

apiarya/wemo-mcp-server 是一个将 WeMo 智能家居设备接入 AI 助手的 MCP 服务端。它基于 pywemo 实现,最大的特点是 100% 本地控制,无需依赖云端,有效解决了隐私和响应延迟问题。开发者可以通过自然语言直接让 AI 帮你开关灯、调节亮度或重命名设备。对于已经在用 WeMo 设备且希望通过 Claude 等支持 MCP 的客户端构建个人智能家居控制中心的玩家来说,这是一个低门槛的增强方案,上手简单,配置后即可将 AI 变为你的家庭管家。

核心亮点

  • 全本地化控制,无需云端,响应快且隐私安全
  • 支持自然语言调节亮度、开关及设备重命名
  • 兼容多平台,轻松将 WeMo 设备接入 AI 助手
  • 基于 pywemo 构建,具备多阶段设备发现能力

完整文档

WeMo MCP Server

通过 AI 助手使用自然语言控制 WeMo 智能家居设备。

mcp-name: io.github.apiarya/wemo

![CI](https://github.com/apiarya/wemo-mcp-server/actions/workflows/ci.yml)
![codecov](https://codecov.io/gh/apiarya/wemo-mcp-server)
![Quality Gate](https://sonarcloud.io/project/overview?id=apiarya_wemo-mcp-server)
![Security Rating](https://sonarcloud.io/project/overview?id=apiarya_wemo-mcp-server)
![PyPI version](https://pypi.org/project/wemo-mcp-server/)
![Python 3.10+](https://www.python.org/downloads/)

![MCP Registry](https://registry.modelcontextprotocol.io/?q=apiarya/wemo)
![MCP](https://modelcontextprotocol.io)
![Transport](https://modelcontextprotocol.io/docs/concepts/transports)![License: MIT](https://opensource.org/licenses/MIT)

目录

- scan_network - list_devices - get_device_status - control_device - rename_device - get_homekit_code - get_cache_info - clear_cache - get_configuration - Resources - Prompts - Elicitations - 本地控制信号流

概述

通过 Model Context Protocol 将 WeMo 智能家居设备与 AI 助手无缝集成。本服务器基于 pywemo 构建,支持通过智能多阶段发现机制,使用自然语言控制您的 WeMo 设备。

使用示例

!使用自然语言开启灯光*通过 AI 助手使用自然语言控制 WeMo 设备 —— 只需用简单的英语询问即可!*

!Goodnight scene turns off all devices

*"Goodnight" —— 一个命令关闭家中所有设备*

核心特性

  • 🔍 智能发现 - 多阶段扫描(UPnP/SSDP + 网络端口),确保 100% 可靠性
  • ⚡ 快速扫描 - 采用 60 个并发 worker 进行并行探测(全子网扫描约 23-30s)
  • 🎛️ 全面控制 - 支持所有设备类型的 开/关/切换/亮度控制
  • ✏️ 设备管理 - 重命名设备并提取 HomeKit 设置代码
  • 📊 实时状态 - 查询设备状态和亮度
  • 💾 智能缓存 - 持久化设备缓存,TTL 为 1 小时,重启后依然有效
  • 🔧 高可配置 - 所有设置均支持 YAML 配置文件 + 环境变量
  • 🔄 自动重试 - 针对网络错误采用指数退避算法自动重试
  • 🛡️ 错误处理 - 提供带有可操作建议的详细错误信息
  • 🔌 通用性 - 兼容任何 MCP 客户端(Claude, VS Code, Cursor 等)
  • 📡 MCP Resources - 通过 devices://device://{id} URI 获取实时设备状态
  • 💬 MCP Prompts - 内置引导式 Prompt:发现设备、状态报告、场景控制、故障排除
  • 🗣️ MCP Elicitations - 当子网或设备名称模糊时进行交互式澄清

---

Prerequisites所有配置均使用 uvx(来自 uv Python 包管理器)来运行服务器。请先安装 uv
bash
# macOS/Linux

curl -LsSf https://astral.sh/uv/install.sh | sh

macOS with Homebrew

brew install uv

Windows

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
安装完成后,重启终端并验证:
bash
uvx --version
## 快速上手

通过 Claude Code CLI 在几秒钟内开始使用:

bash
claude mcp add wemo -- uvx wemo-mcp-server
---

连接

一键安装

点击对应的客户端立即安装:

| 客户端 | 安装 |
|--------|---------|
| Claude Desktop | ![Claude Desktop](https://modelcontextprotocol.io/quickstart/user) |
| Claude Code CLI | 运行:claude mcp add wemo -- uvx wemo-mcp-server |
| VS Code | ![Install](https://vscode.dev/redirect/mcp/install?name=wemo&config=%7B%22command%22%3A%22uvx%22%2C%22args%22%3A%5B%22wemo-mcp-server%22%5D%7D) |
| Cursor | ![Add to Cursor](cursor://anysphere.cursor-deeplink/mcp/install?name=wemo&config=eyJjb21tYW5kIjoidXZ4IiwiYXJncyI6WyJ3ZW1vLW1jcC1zZXJ2ZXIiXX0%3D) |
| Cline | 手动配置 (VS Code 扩展) |
| Windsurf | 手动配置 |
| Zed | 手动配置 |
| Continue | 手动配置 (VS Code 扩展) |

手动配置

#### Claude Desktop

编辑 ~/Library/Application Support/Claude/claude_desktop_config.json

json
{
"mcpServers": {
"wemo": {
"command": "uvx",
"args": ["wemo-mcp-server"],
"env": {
"WEMO_MCP_DEFAULT_SUBNET": "192.168.1.0/24"
}
}
}
}
保存后重启 Claude Desktop。

#### VS Code

编辑 ~/.vscode/mcp.json

json
{
"servers": {
"wemo": {
"type": "stdio",
"command": "uvx",
"args": ["wemo-mcp-server"],
"env": {
"WEMO_MCP_DEFAULT_SUBNET": "192.168.1.0/24"
}
}
}
}
保存后重启 VS Code。

#### Cursor

编辑 ~/.cursor/mcp.json

json
{
"servers": {
"wemo": {
"type": "stdio",
"command": "uvx",
"args": ["wemo-mcp-server"]
}
}
}
保存后重启 Cursor。

#### Cline

Cline 是一个 VS Code 扩展。将其添加到 VS Code 的 settings.json 中:

json
{
"mcp.servers": {
"wemo": {
"command": "uvx",
"args": ["wemo-mcp-server"]
}
}
}
保存后重启 VS Code。

#### Windsurf

编辑 ~/.windsurf/mcp.json

json
{
"mcpServers": {
"wemo": {
"command": "uvx",
"args": ["wemo-mcp-server"]
}
}
}
保存后重启 Windsurf。

#### Zed

编辑 ~/.config/zed/settings.json

json
{
"context_servers": {
"wemo": {
"command": "uvx",
"args": ["wemo-mcp-server"]
}
}
}
保存后重启 Zed。

#### Continue

Continue 是一个 VS Code 扩展。编辑 ~/.continue/config.json

json
{
"mcpServers": [
{
"name": "wemo",
"command": "uvx",
"args": ["wemo-mcp-server"]
}
]
}
保存后重新加载 VS Code。

---

Configuration

WeMo MCP Server 支持通过 YAML 文件和环境变量进行灵活配置。

Quick Configuration

最重要的设置是您的 network subnet —— 服务器默认值为 192.168.1.0/24,但您的设备可能处于不同的子网(例如 192.168.86.0/24)。

在您的 MCP 客户端配置中使用 env 直接进行设置:

json
"env": {
"WEMO_MCP_DEFAULT_SUBNET": "192.168.86.0/24"
}
或者在启动服务器前将其导出:

使用环境变量(最简单):

bash
export WEMO_MCP_DEFAULT_SUBNET="192.168.1.0/24"
export WEMO_MCP_CACHE_TTL=7200
export WEMO_MCP_LOG_LEVEL=DEBUG
使用 YAML 配置文件
bash
# Copy example config and customize
cp config.example.yaml config.yaml

Edit config.yaml with your settings

### 配置选项

| 设置 | 环境变量 | 默认值 | 描述 |
|---------|---------------------|---------|-------------|
| Network ||||
| 默认子网 | WEMO_MCP_DEFAULT_SUBNET | 192.168.1.0/24 | 用于扫描设备的网络 |
| 扫描超时 | WEMO_MCP_SCAN_TIMEOUT | 0.6 | 端口探测超时(秒) |
| 最大工作线程 | WEMO_MCP_MAX_WORKERS | 60 | 并发扫描线程数 |
| Cache ||||
| 启用缓存 | WEMO_MCP_CACHE_ENABLED | true | 持久化设备缓存 |
| 缓存文件 | WEMO_MCP_CACHE_FILE | ~/.wemo_mcp_cache.json | 缓存文件路径 |
| 缓存 TTL | WEMO_MCP_CACHE_TTL | 3600 | 缓存有效期(秒) |
| Logging ||||
| 日志级别 | WEMO_MCP_LOG_LEVEL | INFO | DEBUG, INFO, WARNING, ERROR |

配置示例

大型网络(多个子网):

bash
export WEMO_MCP_DEFAULT_SUBNET="10.0.0.0/16"
export WEMO_MCP_SCAN_TIMEOUT=1.0
export WEMO_MCP_MAX_WORKERS=100
Debug 模式
bash
export WEMO_MCP_LOG_LEVEL=DEBUG
export WEMO_MCP_CACHE_TTL=300 # 5 minutes
禁用缓存
bash
export WEMO_MCP_CACHE_ENABLED=false
请参阅 config.example.yaml.env.example 获取完整的配置模板。

有关详细的配置指南,请参阅 CONFIGURATION.md

---

MCP Tools

1. scan_network

使用智能多阶段扫描在网络中发现 WeMo 设备。

Example Prompts:

  • "Scan for WeMo devices on my network"

  • "Find all WeMo devices"

  • "Discover devices on 192.168.1.0/24"

Example Response:

code
Found 12 WeMo devices in 23.5 seconds:

1. Office Light (Dimmer) - 192.168.1.100 - OFF
2. Living Room (Switch) - 192.168.1.101 - ON
3. Bedroom Lamp (Dimmer) - 192.168.1.102 - OFF
...

### 2. list_devices

列出所有从之前扫描中缓存的设备。

示例提示词:

  • "List all my WeMo devices"

  • "Show me all devices"

  • "What devices do you know about?"

示例响应:

code
12 devices in cache:

  • Office Light (Dimmer) at 192.168.1.100
  • Living Room (Switch) at 192.168.1.101
  • Bedroom Lamp (Dimmer) at 192.168.1.102
...
### 3. get_device_status

获取特定设备的当前状态和信息。

示例提示词:

  • "办公室的灯开了吗?"

  • "卧室灯的状态是什么?"

  • "检查客厅开关"

  • "办公室灯的亮度是多少?"

示例响应:

code
Office Light (Dimmer):
  • State: OFF

  • Brightness: 75%

  • IP: 192.168.1.100

  • Model: DimmerLongPress
### 4. control_device

控制 WeMo 设备(开启/关闭/切换/亮度)。

示例提示词:

  • "Turn on the office light"

  • "Turn off the living room"

  • "Toggle the bedroom lamp"

  • "Set office light to 75%"

  • "Dim the bedroom lamp to 50%"

示例响应:

code
✓ Office Light turned ON
Brightness set to 75%
Current state: ON
### 5. rename_device

重命名 WeMo 设备(更改其友好名称)。

示例提示词:

  • "Rename Office Dimmer to Office Light"

  • "Change the name of the bedroom device to Bedroom Lamp"

  • "Call the living room switch 'Main Light'"

示例响应:

code
✓ Device renamed successfully
'Office Dimmer' → 'Office Light'
IP: 192.168.1.100

The new name will appear in the WeMo app and all control interfaces.

### 6. get_homekit_code

获取 WeMo 设备的 HomeKit 设置代码。

示例提示词:

  • "Get the HomeKit code for Office Light"

  • "What's the HomeKit setup code for the bedroom lamp?"

  • "Show me the HomeKit code for all devices"

示例响应:

code
HomeKit Setup Code for 'Office Light':
123-45-678

Use this code to add the device to Apple Home.

注意: 并非所有 WeMo 设备都支持 HomeKit。如果设备不支持 HomeKit,您将收到一条错误消息。

!HomeKit code retrieval step 1

!HomeKit code retrieval step 2

7. get_cache_info

获取关于持久化设备缓存的信息。

示例提示词:

  • "Show me cache information"

  • "Is the device cache expired?"

  • "How many devices are cached?"

示例响应:

code
Device Cache Status:
✅ Cache exists
📁 Location: ~/.wemo_mcp_cache.json
📊 Devices: 12
⏰ Age: 1,234 seconds (20.6 minutes)
💾 TTL: 3,600 seconds (1 hour)
✅ Status: Valid (not expired)
### 8. clear_cache

清除持久化设备缓存以强制重新扫描。

示例提示词:

  • "Clear the device cache"

  • "Reset the cache and rescan"

  • "Delete cached devices"

示例响应:

code
✅ Cache cleared successfully
Next scan will discover devices fresh.
Run scan_network to rebuild the cache.
注意: 这将同时清除持久化缓存文件和内存缓存。清除后,请运行 scan_network 以重新发现设备。

9. get_configuration

查看当前服务器配置设置。

示例提示词:

  • "Show me the server configuration"

  • "What are the current settings?"

  • "Display configuration"

示例响应:

code
Current Configuration:
Network:
• Default subnet: 192.168.1.0/24
• Scan timeout: 0.6 seconds
• Max workers: 60
Cache:
• Enabled: true
• File: ~/.wemo_mcp_cache.json
• TTL: 3600 seconds (1 hour)
Logging:
• Level: INFO
注意: 显示所有配置,包括默认值和环境变量覆盖。使用 WEMO_MCP_ 前缀的环境变量进行自定义。

---

MCP Capabilities

除了 tools,该服务器还提供了完整的 MCP primitives。

Resources

无需调用 tool 即可订阅实时设备数据:

| URI | Description |
|-----|-------------|
| devices:// | 所有缓存设备的 JSON 索引 |
| device://{name-or-ip} | 特定设备的实时状态(支持 URL 编码的名称) |

支持 MCP Resources 的客户端(如 VS Code, MCP Inspector)可以直接读取这些内容。

Prompts

在支持的客户端中,可通过 / 斜杠命令使用四个内置的引导式 prompts:

| Prompt | Description |
|--------|-------------|
| discover-devices | 带有子网选择的引导式网络扫描 |
| device-status-report | 所有设备状态的汇总报告 |
| activate-scene | 将多个设备作为场景进行控制 |
| troubleshoot-device | 分步设备故障排除 |

!MCP prompts in VS Code

*这四个 prompts 在 VS Code 中均以 /mcp.wemo.* 斜杠命令的形式呈现*

Elicitations

服务器会主动请求缺失的信息,而不是静默失败:- scan_network — 如果未配置自定义子网(默认 192.168.1.0/24),在继续之前会询问要扫描哪个子网

  • control_device — 如果在缓存中未找到设备名称,会显示最接近的匹配项并询问目标设备

!MCP elicitation prompting for subnet

*Elicitation 实际运行效果 — 服务器请求子网信息,而不是静默地扫描错误的网络*

Client Support Matrix

| Feature | Claude Desktop | VS Code | Cursor | MCP Inspector |
|---------|:--------------:|:-------:|:------:|:-------------:|
| Tools | ✅ | ✅ | ✅ | ✅ |
| Resources | ⚠️ 仅协议支持 | ✅ | ✅ | ✅ |
| Prompts | ⚠️ 无 slash UI | ✅ / 命令 | ✅ | ✅ |
| Elicitations | ✅ v1.1+ | ❌ | ❌ | ✅ v0.20+ |

---

How It Works

Multi-Phase Discovery

服务器采用一个经过可靠性优化的三阶段发现流程:

1. Phase 1 - UPnP/SSDP Discovery (Primary)
- 通过组播发现所有有响应的设备(约 12s)
- 最可靠的方法,可发现不对端口探测做出响应的设备
- 使用 pywemo 内置的发现机制

2. Phase 2 - Network Port Scanning (Backup)
- 在子网内并行探测 WeMo 端口 (49152-49155)
- 采用 60 个并发 worker 进行快速扫描(254 个 IP 约 10s)
- 捕捉被 UPnP 遗漏的设备3. 阶段 3 - 设备验证 (备份)
- 通过 /setup.xml 对活跃 IP 进行 HTTP 验证
- 使用 60 个 worker 进行并行验证
- 验证并提取设备信息

该方法在保持快速扫描时间(完整网络仅需 23-30 秒)的同时,实现了 100% 的设备发现可靠性

本地控制信号流

所有设备命令仅在您的 local network 中传输 —— 任何阶段都无需经过云端跳转。

语音路径 (Google Home + WeMo):

mermaid
sequenceDiagram
participant U as User
participant GH as Google Home Hub
participant GC as Google Cloud (ASR only)
participant WD as WeMo Device

U->>GH: "Hey Google, turn on chandelier"
GH->>GC: Audio stream for speech-to-text
GC-->>GH: Intent: {action: ON, device: chandelier}
GH->>WD: Matter OnOff.On (UDP 5540, LAN)
WD-->>GH: ACK
GH-->>U: "OK, turning on chandelier"

MCP 路径 (AI assistant + 此 server):
mermaid
sequenceDiagram
participant U as User
participant AI as AI Assistant
participant MS as MCP Server
participant WD as WeMo Device

U->>AI: "Turn on the desk light"
AI->>MS: tools/call control_device("desk light", "on")
MS->>WD: UPnP/SOAP BinaryState=1 (TCP 49153, LAN)
WD-->>MS: HTTP 200 OK
MS-->>AI: {success: true, state: "on"}
AI-->>U: "Desk light is now on!"

在初始语音识别之后,两条路径均仅使用 local protocols(Google Cloud 处理 speech-to-text;不涉及 Belkin 的 cloud)。

功能对比

MCP Server vs wemo-ops-center

本 MCP server 与主项目 wemo-ops-center 的功能对比:

| 功能 | wemo-ops-center | MCP Server | 备注 |
|---------|-----------------|------------|-------|
| 设备发现 | ✅ UPnP + Port Scan | ✅ 已实现 | 多阶段发现,100% 可靠性 |
| 设备控制 | ✅ On/Off/Toggle | ✅ 已实现 | 包含对 dimmers 的亮度控制 |
| 设备状态 | ✅ 实时 | ✅ 已实现 | 通过名称或 IP 地址查询 |
| 设备重命名 | ✅ Friendly names | ✅ 已实现 | 自动更新设备缓存 |
| HomeKit 代码 | ✅ 提取代码 | ✅ 已实现 | 适用于 HomeKit 兼容设备 |
| 多子网 | ✅ VLAN 支持 | ❌ 计划中 | 目前每次扫描仅限单个子网 |
| WiFi 配置 | ✅ Smart setup | ❌ 未计划 | 需要更改 PC WiFi 连接 |
| 定时任务 | ✅ 时间 + 太阳 | ❌ 未计划 | 需要持久化 daemon(与 MCP 模型不兼容) |
| 维护工具 | ✅ 重置 | ❌ 未计划 | 恢复出厂设置、清除 WiFi、清除数据 |
| 配置文件管理 | ✅ 保存/加载 | ❌ 未计划 | 用于批量设置的 WiFi 凭据配置文件 |
| 用户界面 | ✅ GUI + Web | ❌ 不适用 | MCP 使用 AI 助手界面 |图例:

  • 已实现 - 功能可用

  • 未计划 - 功能与 MCP 架构或用例冲突

  • 计划中 - 功能可在未来添加

为什么某些功能未在 MCP 中计划:

  • Scheduling:需要 24/7 后台守护进程轮询。MCP servers 通常由 AI assistants 按需调用,而非作为持久服务运行。

  • WiFi Provisioning:需要将主机 PC 的 WiFi 连接更改为设备设置网络,这具有破坏性且依赖于具体平台。

  • Maintenance Tools:破坏性操作(如 factory reset 等)更适合带有确认对话框的专用 GUI。

当前 MCP 覆盖率: 11 项核心功能中的 5 项 (45%) - 专注于符合 MCP 模型的设备发现、监控和控制用例。

Development

Setup
bash
git clone https://github.com/apiarya/wemo-mcp-server.git

cd wemo-mcp-server uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate uv sync --dev
### 运行测试
bash
# Unit tests (CI-compatible, ~4 seconds, 128 tests)
.venv/bin/python -m pytest tests/test_server.py tests/test_phase2.py tests/test_models.py -v

With coverage report

pytest tests/test_server.py tests/test_phase2.py tests/test_models.py --cov=wemo_mcp_server --cov-report=html

E2E tests (requires WeMo devices on network)

python tests/test_e2e.py
### 使用开发版本

在你的 MCP 客户端配置中使用:

json
{
"command": "python",
"args": ["-m", "wemo_mcp_server"],
"env": {
"PYTHONPATH": "/path/to/mcp/src"
}
}
## Contributing

欢迎贡献!请执行以下步骤:

1. Fork 本仓库
2. 创建特性分支 (git checkout -b feature/amazing-feature)
3. 完成修改并编写测试
4. 运行测试套件 (python tests/test_e2e.py)
5. 提交更改 (git commit -m 'Add amazing feature')
6. 推送到分支 (git push origin feature/amazing-feature)
7. 提交 Pull Request

License

MIT License - 详情请参阅 LICENSE 文件。

Acknowledgments

查看官方来源