«

macOS 微信聊天记录 MCP 部署教程:Claude Code 接入 chatlog-bot

myluzh 发布于 阅读:6 AI Agent


0x00 前言

本文记录一次 macOS 微信聊天记录 MCP 部署过程。目标很简单:让 Claude Code 通过本机 MCP 查询微信聊天记录,不把聊天记录上传到第三方服务。

本次环境:

项目
Mac Apple Silicon / arm64
微信 macOS WeChat 4.1.8
Claude Code 2.1.138
服务地址 127.0.0.1:5030

整体分两步:

  1. wechat-mac-reader:提取微信数据库 key,生成 chatlog-bot 配置。
  2. chatlog-bot:解密数据库,暴露 HTTP API / MCP 给 Claude Code。

敏感文件:

注意:这些文件不要上传,服务只监听 127.0.0.1,不要改成 0.0.0.0

0x01 前置检查

# 检查 Mac 架构,本文按 Apple Silicon 写
uname -m
# arm64

# 检查 SIP,提取 key 通常需要 disabled
csrutil status
# System Integrity Protection status: disabled.

# 如果 SIP 是 enabled,需要进恢复模式执行:
# csrutil disable
#
# Apple Silicon 路径:
# 关机 -> 长按电源键进入恢复模式 -> 打开 Terminal -> csrutil disable -> 重启

# 打开微信并登录后,检查微信进程
pgrep -x WeChat

# 检查微信版本
defaults read /Applications/WeChat.app/Contents/Info.plist CFBundleShortVersionString
# 4.1.8

# 检查基础工具
xcode-select -p
lldb -P
go version
sqlcipher --version

# 缺什么装什么
xcode-select --install
brew install go sqlcipher

0x02 wechat-mac-reader 解密准备

下载项目

# 进入桌面
cd ~/Desktop

# 下载 wechat-mac-reader
git clone https://github.com/xingfanxia/wechat-mac-reader.git

# 下载 key 提取工具
cd ~/Desktop/wechat-mac-reader
mkdir -p lib
git clone https://github.com/Thearas/wechat-db-decrypt-macos.git lib/wechat-db-decrypt-macos

提取微信数据库 key

# 进入 key 提取工具目录
cd ~/Desktop/wechat-mac-reader/lib/wechat-db-decrypt-macos

# 建议先隐藏终端里的真实 key 输出,避免日志泄露
# 修改 find_key_memscan.py,把类似这一行:
# print(f"    key={enc_key_hex}")
#
# 改成:
# print("    key=<redacted>")
#
# 注意:只改打印,不要改写入 wechat_keys.json 的逻辑

# 执行 key 扫描
PYTHONPATH="$(lldb -P)" /usr/bin/python3 find_key_memscan.py

# 本次结果类似:
# Found 23 databases
# Results: 22/23 keys found
# Missing: migrate/unspportmsg.db
# Keys saved to wechat_keys.json
#
# migrate/unspportmsg.db 缺失不影响联系人、会话和主要聊天记录读取

# 移动 key 文件到 wechat-mac-reader 根目录
mv wechat_keys.json ~/Desktop/wechat-mac-reader/wechat_keys.json

# 确认文件存在
ls -l ~/Desktop/wechat-mac-reader/wechat_keys.json

生成 chatlog-bot 配置

# 回到 wechat-mac-reader
cd ~/Desktop/wechat-mac-reader

# generate-config.py 依赖 PyYAML,用项目虚拟环境
python3 -m venv .venv
.venv/bin/python -m pip install -q pyyaml

# 生成配置
.venv/bin/python scripts/generate-config.py wechat_keys.json data/config.yaml

# 检查配置摘要,不打印 key
.venv/bin/python - <<'PY'
import yaml
d = yaml.safe_load(open("data/config.yaml"))
acct = d.get("last_account")
keys = d.get("accounts", {}).get(acct, {}).get("derived_key_map", {}) if acct else {}
print("account:", acct)
print("derived_key_count:", len(keys))
print("http_addr:", d.get("http_addr"))
print("auto_decrypt:", d.get("auto_decrypt"))
PY

# 本次结果类似:
# derived_key_count: 22
# http_addr: 127.0.0.1:5030
# auto_decrypt: True

到这里,wechat-mac-reader 的工作结束。后面由 chatlog-bot 读取 data/config.yaml 并解密数据库。

0x03 chatlog-bot 暴露 MCP

下载并编译

# 进入桌面
cd ~/Desktop

# 下载 chatlog-bot
git clone https://github.com/TE0dollary/chatlog-bot.git

# 编译
cd ~/Desktop/chatlog-bot
CGO_ENABLED=1 go build -trimpath -o bin/chatlog main.go

# 安装到用户 bin
mkdir -p ~/go/bin
cp ~/Desktop/chatlog-bot/bin/chatlog ~/go/bin/chatlog

# 检查版本
~/go/bin/chatlog version

前台启动

# 终端 1:前台启动服务
cd ~/Desktop/wechat-mac-reader
~/go/bin/chatlog --config ~/Desktop/wechat-mac-reader/data server

# 首次启动会自动解密数据库,看到类似日志即可:
# Starting HTTP server on 127.0.0.1:5030
# start db failed, try to decrypt data.
# 开始批量解密
# 批量解密结束 failed=0
# decrypt data success
# 终端 2:验证 HTTP API

# 健康检查
curl http://127.0.0.1:5030/health
# {"status":"ok"}

# 最近会话
curl 'http://127.0.0.1:5030/api/v1/session?limit=3'

# 联系人
curl 'http://127.0.0.1:5030/api/v1/contact?limit=10'

# 群聊
curl 'http://127.0.0.1:5030/api/v1/chatroom?limit=10'

# 按群名或联系人查聊天记录
curl 'http://127.0.0.1:5030/api/v1/chatlog?talker=群名&limit=20'

# 按日期查
curl 'http://127.0.0.1:5030/api/v1/chatlog?talker=群名&time=2026-05-11&limit=100&format=json'

# 按日期范围查
curl 'http://127.0.0.1:5030/api/v1/chatlog?talker=群名&time=2026-05-01~2026-05-11&limit=200&format=json'

# 常用参数:
# talker: 群名、联系人名、wxid、群聊 ID
# time: YYYY-MM-DD 或 YYYY-MM-DD~YYYY-MM-DD
# limit: 返回条数
# offset: 分页偏移
# format: json / csv / text

验证 MCP

# MCP 地址
curl -i -m 3 http://127.0.0.1:5030/mcp

# SSE 地址
curl -i -m 3 http://127.0.0.1:5030/sse

# 看到下面内容即可:
# HTTP/1.1 200 OK
# Content-Type: text/event-stream
#
# curl 超时正常,因为 MCP/SSE 是长连接

配置 launchd 常驻

# 进入项目目录
cd ~/Desktop/wechat-mac-reader

# 创建日志目录
mkdir -p logs

# 创建 launchd plist
cat > ~/Library/LaunchAgents/com.chatlog-bot.plist <<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.chatlog-bot</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/myluzh/go/bin/chatlog</string>
        <string>--config</string>
        <string>/Users/myluzh/Desktop/wechat-mac-reader/data</string>
        <string>server</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/Users/myluzh/Desktop/wechat-mac-reader</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/myluzh/Desktop/wechat-mac-reader/logs/chatlog-bot.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/myluzh/Desktop/wechat-mac-reader/logs/chatlog-bot.err</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/Users/myluzh/go/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin</string>
        <key>HOME</key>
        <string>/Users/myluzh</string>
    </dict>
</dict>
</plist>
EOF

# 收紧权限
chmod 700 data logs
chmod 600 wechat_keys.json data/config.yaml ~/Library/LaunchAgents/com.chatlog-bot.plist
find data -type d -exec chmod 700 {} +
find data -type f -exec chmod 600 {} +

# 启动服务
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist
launchctl enable gui/$(id -u)/com.chatlog-bot

# 检查服务状态
launchctl print gui/$(id -u)/com.chatlog-bot

# 检查监听地址,必须是 127.0.0.1:5030
lsof -nP -iTCP:5030 -sTCP:LISTEN

日常管理命令:

# 停止服务
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist

# 重启服务
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist

# 查看日志
tail -f ~/Desktop/wechat-mac-reader/logs/chatlog-bot.log
tail -f ~/Desktop/wechat-mac-reader/logs/chatlog-bot.err

接入 Claude Code

# 检查 Claude Code
command -v claude
claude --version

# 添加 MCP,优先使用 HTTP
claude mcp add --scope user --transport http chatlog http://127.0.0.1:5030/mcp

# 检查 MCP 状态
claude mcp get chatlog
claude mcp list

# 成功时类似:
# chatlog:
#   Scope: User config
#   Status: Connected
#   Type: http
#   URL: http://127.0.0.1:5030/mcp

# 如果 HTTP 不行,切 SSE
claude mcp remove chatlog -s user
claude mcp add --scope user --transport sse chatlog http://127.0.0.1:5030/sse
claude mcp get chatlog

# 删除 MCP
claude mcp remove chatlog -s user

0x04 使用示例

配置完成后,Claude Code 里可以直接这样问:

建议每次明确群名或联系人名、日期范围、是否需要原文引用、输出结构。

0x05 重新提取 key

下面情况需要重提:

# 重新提取 key
cd ~/Desktop/wechat-mac-reader/lib/wechat-db-decrypt-macos
PYTHONPATH="$(lldb -P)" /usr/bin/python3 find_key_memscan.py
mv wechat_keys.json ~/Desktop/wechat-mac-reader/wechat_keys.json

# 重新生成配置
cd ~/Desktop/wechat-mac-reader
.venv/bin/python scripts/generate-config.py wechat_keys.json data/config.yaml

# 重启服务
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist

# 验证
curl http://127.0.0.1:5030/health
claude mcp get chatlog

0x06 常见问题

Error attaching 或扫不到 key

# 检查 SIP
csrutil status

# 检查微信是否运行
pgrep -x WeChat

# 检查 lldb Python 路径
lldb -P

常见原因:

PyYAML 安装失败

cd ~/Desktop/wechat-mac-reader
python3 -m venv .venv
.venv/bin/python -m pip install -q pyyaml
.venv/bin/python scripts/generate-config.py wechat_keys.json data/config.yaml

dataKey or derivedKeyMap is required

cd ~/Desktop/wechat-mac-reader

.venv/bin/python - <<'PY'
import yaml
d = yaml.safe_load(open("data/config.yaml"))
acct = d.get("last_account")
print(acct)
print(len(d.get("accounts", {}).get(acct, {}).get("derived_key_map", {})))
PY

如果数量是 0,重新生成配置。

health 不通

# 检查 launchd
launchctl print gui/$(id -u)/com.chatlog-bot

# 检查端口
lsof -nP -iTCP:5030 -sTCP:LISTEN

# 看错误日志
tail -n 100 ~/Desktop/wechat-mac-reader/logs/chatlog-bot.err

# 前台调试
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.chatlog-bot.plist
cd ~/Desktop/wechat-mac-reader
~/go/bin/chatlog --config ~/Desktop/wechat-mac-reader/data server

Claude Code MCP Failed

# 先看底层服务
curl http://127.0.0.1:5030/health
curl -i -m 3 http://127.0.0.1:5030/mcp

# 再看 Claude MCP
claude mcp get chatlog
claude mcp list

# 重建 HTTP MCP
claude mcp remove chatlog -s user
claude mcp add --scope user --transport http chatlog http://127.0.0.1:5030/mcp

# HTTP 不行再试 SSE
claude mcp remove chatlog -s user
claude mcp add --scope user --transport sse chatlog http://127.0.0.1:5030/sse

聊天记录不全

Mac 只能读取本机已有记录。手机上的历史记录需要先从手机微信迁移到电脑:我 -> 设置 -> 通用 -> 聊天记录迁移与备份 -> 迁移 -> 迁移到电脑

迁移完成后,重新提取 key 并重启服务。

0x07 总结

这套方案的关键点是:微信数据库 key 只在本机提取,解密后的数据也只在本机使用,chatlog-bot 只监听 127.0.0.1:5030

日常使用里,最有价值的是按群名、联系人和时间范围查聊天记录,再让 Claude Code 直接整理重点、待办和风险。

macos llm mcp ai agent 微信 claude code chatlog-bot


正文到此结束
版权声明:若无特殊注明,本文皆为 Myluzh Blog 原创,转载请保留文章出处。
文章内容:https://itho.cn/ai-agent/597.html
文章标题:《macOS 微信聊天记录 MCP 部署教程:Claude Code 接入 chatlog-bot