Skip to main content
Glama
WeChat MCP Monitor
by Cybing521
README.md
# WeChat MCP Monitor 🔔

> 一个基于 Model Context Protocol (MCP) 的微信群聊消息监控工具,专为 macOS 设计

[![TypeScript](https://img.shields.io/badge/TypeScript-5.3-blue.svg)](https://www.typescriptlang.org/)
[![Node.js](https://img.shields.io/badge/Node.js-20+-green.svg)](https://nodejs.org/)
[![macOS](https://img.shields.io/badge/macOS-only-lightgrey.svg)](https://www.apple.com/macos/)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

## ✨ 功能特性

- 🎯 **精准监控** - 监控指定的微信群聊,避免信息过载
- 🔍 **智能过滤** - 支持关键词和特定发送者双重过滤
- 🔔 **原生通知** - 使用 macOS 系统通知,无需额外应用
- 🚀 **无需登录** - 基于 macOS 微信客户端,无需扫码
- 🔌 **MCP 集成** - 完美集成 Claude Desktop 和其他 MCP 客户端
- ⚙️ **灵活配置** - 可自定义检查间隔和通知内容

## 📋 目录

- [快速开始](#快速开始)
- [安装](#安装)
- [配置](#配置)
- [使用方法](#使用方法)
- [MCP 工具](#mcp-工具)
- [权限设置](#权限设置)
- [故障排除](#故障排除)
- [工作原理](#工作原理)
- [注意事项](#注意事项)
- [技术栈](#技术栈)
- [贡献](#贡献)
- [许可证](#许可证)

## 🚀 快速开始

```bash
# 1. 克隆仓库
git clone https://github.com/Cybing521/wechat-mcp.git
cd wechat-mcp

# 2. 安装依赖
npm install

# 3. 创建配置文件
cp config.example.json config.json

# 4. 编辑配置文件(填入你的群聊名称、关键词等)
nano config.json

# 5. 构建项目
npm run build

# 6. 配置 Claude Desktop(见下方详细说明)
```

## 📦 安装

### 前置要求

- **macOS** 10.15 或更高版本
- **Node.js** 20.0 或更高版本
- **微信 macOS 客户端** 已安装并登录
- **辅助功能权限** 已授予

### 安装步骤

```bash
# 安装依赖
npm install

# 构建项目
npm run build
```

## ⚙️ 配置

### 1. 创建配置文件

```bash
cp config.example.json config.json
```

### 2. 编辑配置文件

打开 `config.json` 并根据你的需求修改:

```json
{
  "monitoredGroups": [
    "工作群",
    "项目讨论组"
  ],
  "keywords": [
    "@所有人",
    "紧急",
    "重要"
  ],
  "monitoredSenders": [
    "老板",
    "项目经理"
  ],
  "notificationTitle": "微信群消息提醒",
  "checkInterval": 5000
}
```

### 配置项说明

| 配置项 | 类型 | 说明 | 默认值 |
|--------|------|------|--------|
| `monitoredGroups` | `string[]` | 要监控的群聊名称列表 | `[]` |
| `keywords` | `string[]` | 触发通知的关键词列表 | `[]` |
| `monitoredSenders` | `string[]` | 触发通知的发送者昵称列表 | `[]` |
| `notificationTitle` | `string` | 通知标题 | `"微信群消息提醒"` |
| `checkInterval` | `number` | 检查间隔(毫秒) | `5000` |

### 过滤规则

消息会在以下情况触发通知:
- ✅ 消息来自 `monitoredGroups` 中的群聊 **且**
- ✅ 消息包含 `keywords` 中的任一关键词 **或** 发送者在 `monitoredSenders` 列表中

## 🎯 使用方法

### 方式一:作为 MCP 服务器(推荐)

#### 1. 配置 Claude Desktop

编辑 Claude Desktop 配置文件:

**macOS 路径:**
```bash
~/Library/Application Support/Claude/claude_desktop_config.json
```

**添加配置:**
```json
{
  "mcpServers": {
    "wechat-monitor": {
      "command": "node",
      "args": ["/完整路径/wechat-mcp/dist/index.js"]
    }
  }
}
```

> 💡 **提示:** 将 `/完整路径/` 替换为你的实际项目路径

#### 2. 重启 Claude Desktop

配置完成后,重启 Claude Desktop 以加载 MCP 服务器。

#### 3. 使用工具

在 Claude Desktop 中,你可以直接使用以下命令:

```
请帮我启动微信消息监控
```

或者直接调用工具:
- `start_monitoring`
- `stop_monitoring`
- `get_status`

### 方式二:独立运行(测试用)

```bash
npm start
```

> ⚠️ **注意:** 独立运行模式下,MCP 工具无法使用,仅用于测试服务器是否正常启动。

## 🛠️ MCP 工具

### `start_monitoring`

启动微信群聊消息监控。

**使用示例:**
```
启动微信监控
```

**返回信息:**
- 监控启动成功提示
- 检查间隔时间
- 权限提醒

**前置条件:**
- ✅ 微信客户端已打开并登录
- ✅ 已授予辅助功能权限
- ✅ 配置文件存在且格式正确

---

### `stop_monitoring`

停止消息监控。

**使用示例:**
```
停止微信监控
```

**返回信息:**
- 监控停止确认

---

### `get_status`

查看当前监控状态和配置信息。

**使用示例:**
```
查看微信监控状态
```

**返回信息:**
```json
{
  "isRunning": true,
  "wechatRunning": true,
  "monitoredGroups": ["工作群", "项目讨论组"],
  "keywords": ["@所有人", "紧急"],
  "monitoredSenders": ["老板"],
  "checkInterval": 5000
}
```

## 🔐 权限设置

### 授予辅助功能权限

此工具需要辅助功能权限才能读取微信窗口内容。

#### 设置步骤:

1. 打开 **系统偏好设置**
2. 进入 **安全性与隐私** > **隐私** > **辅助功能**
3. 点击左下角 🔒 解锁(需要管理员密码)
4. 添加以下应用:
   - ✅ **终端** (Terminal.app) - 如果通过终端运行
   - ✅ **Claude Desktop** - 如果作为 MCP 服务器运行
5. 确保应用旁边的复选框已勾选

#### 验证权限

运行以下命令测试权限是否生效:

```bash
osascript -e 'tell application "System Events" to (name of processes) contains "WeChat"'
```

如果返回 `true` 或 `false`(而不是错误),说明权限已正确设置。

## 🔧 故障排除

### 问题 1: "WeChat is not running"

**原因:** 微信客户端未打开

**解决方案:**
```bash
# 打开微信
open -a WeChat
```

---

### 问题 2: 没有收到通知

**可能原因:**
1. 未授予辅助功能权限
2. 配置文件中的群聊名称不匹配
3. 关键词或发送者设置不正确

**解决方案:**
1. 检查权限设置(见上方)
2. 使用 `get_status` 工具查看当前配置
3. 确认群聊名称与微信中显示的完全一致(包括空格)
4. 检查终端输出的错误信息

---

### 问题 3: "Permission denied" 或权限错误

**原因:** 未授予辅助功能权限

**解决方案:**
1. 按照 [权限设置](#权限设置) 部分的步骤操作
2. 重启终端或 Claude Desktop
3. 重新运行程序

---

### 问题 4: 构建失败

**原因:** 依赖安装不完整或 TypeScript 版本问题

**解决方案:**
```bash
# 清理并重新安装
rm -rf node_modules package-lock.json
npm install
npm run build
```

---

### 问题 5: 监控延迟

**原因:** `checkInterval` 设置过大

**解决方案:**
在 `config.json` 中调整 `checkInterval`:
```json
{
  "checkInterval": 3000  // 改为 3 秒检查一次
}
```

> ⚠️ **注意:** 间隔过小可能增加系统负担

## 🔍 工作原理

```
┌─────────────────┐
│  MCP Client     │
│ (Claude Desktop)│
└────────┬────────┘
         │
         │ MCP Protocol
         │
┌────────▼────────┐
│  MCP Server     │
│  (本工具)        │
└────────┬────────┘
         │
         │ AppleScript
         │
┌────────▼────────┐
│  macOS          │
│  Accessibility  │
│  API            │
└────────┬────────┘
         │
         │ 读取窗口内容
         │
┌────────▼────────┐
│  WeChat macOS   │
│  Client         │
└─────────────────┘
```

### 工作流程

1. **定期轮询** - 根据 `checkInterval` 设置,定期检查微信窗口
2. **内容提取** - 使用 AppleScript 和 Accessibility API 读取窗口文本
3. **消息解析** - 解析文本内容,识别群聊和消息
4. **规则匹配** - 根据配置的关键词和发送者进行过滤
5. **通知推送** - 匹配成功时通过 macOS 原生通知提醒

## ⚠️ 注意事项

### 使用限制

- ✅ **仅支持 macOS** - 依赖 macOS 特有的 AppleScript 和 Accessibility API
- ✅ **需要微信客户端** - 必须使用 macOS 版微信客户端
- ✅ **依赖界面结构** - 微信客户端更新可能影响功能
- ✅ **轮询机制** - 基于定期检查,存在延迟(取决于 `checkInterval`)

### 隐私和安全

- 🔒 **本地运行** - 所有数据处理均在本地进行
- 🔒 **无数据上传** - 不会将消息内容发送到任何服务器
- 🔒 **配置文件** - 敏感配置存储在本地 `config.json`
- 🔒 **开源透明** - 代码完全开源,可自行审查

### 性能考虑

- ⚡ **CPU 使用** - 轮询会占用少量 CPU 资源
- ⚡ **内存占用** - 约 50-100MB 内存
- ⚡ **检查间隔** - 建议设置在 3-10 秒之间

## 🛠️ 技术栈

- **[TypeScript](https://www.typescriptlang.org/)** - 类型安全的 JavaScript
- **[Node.js](https://nodejs.org/)** - JavaScript 运行时
- **[MCP SDK](https://github.com/modelcontextprotocol/sdk)** - Model Context Protocol SDK
- **[AppleScript](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/)** - macOS 自动化脚本
- **[node-notifier](https://github.com/mikaelbr/node-notifier)** - 跨平台通知库

## 📝 开发

### 项目结构

```
wechat-mcp/
├── src/
│   └── index.ts          # 主程序入口
├── dist/                 # 编译输出目录
├── config.json           # 配置文件(需自行创建)
├── config.example.json   # 配置文件模板
├── package.json          # 项目依赖
├── tsconfig.json         # TypeScript 配置
├── README.md             # 项目文档
└── .gitignore            # Git 忽略文件
```

### 开发命令

```bash
# 开发模式(编译并运行)
npm run dev

# 仅编译
npm run build

# 运行(需先编译)
npm start
```

## 🤝 贡献

欢迎贡献代码、报告问题或提出建议!

### 贡献步骤

1. Fork 本仓库
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
4. 推送到分支 (`git push origin feature/AmazingFeature`)
5. 开启 Pull Request

### 报告问题

如果遇到问题,请在 [Issues](https://github.com/Cybing521/wechat-mcp/issues) 中报告,并提供:

- macOS 版本
- 微信版本
- 错误信息
- 复现步骤

## 📄 许可证

本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件

## 🙏 致谢

- [Model Context Protocol](https://github.com/modelcontextprotocol) - 提供 MCP SDK
- [Anthropic](https://www.anthropic.com/) - Claude Desktop 和 MCP 生态
- 所有贡献者和使用者

## 📮 联系方式

- **Issues:** [GitHub Issues](https://github.com/Cybing521/wechat-mcp/issues)
- **Discussions:** [GitHub Discussions](https://github.com/Cybing521/wechat-mcp/discussions)

---

<div align="center">

**如果这个项目对你有帮助,请给个 ⭐️ Star!**

Made with ❤️ by Cybing521

</div>

TDQS

A3.5/5.0

Scored across 3 tools

Disambiguation5/5

Each tool maps to a distinct lifecycle action: start, stop, and status query. There is no overlap or plausible confusion between them.

Naming Consistency5/5

All three follow a consistent snake_case verb_noun pattern (start/stop/get + monitoring/status). The convention is predictable and readable.

Tool Count4/5

Three tools is a tight, well-scoped surface for a monitoring lifecycle. It is slightly thin in that there is no way to surface or fetch collected data, but no tool feels redundant.

Completeness3/5

Start/stop/status covers the lifecycle, but the apparent purpose—monitoring group messages—implies agents should be able to retrieve or read captured messages, and that operation is missing. This is a notable gap unless messages are delivered out-of-band.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues