Skip to main content

MCP Server for Douyin — let AI search users, read and send Douyin direct messages via browser automation

Project description

Douyin MCP Server

将抖音网页版私信/聊天功能暴露为 MCP (Model Context Protocol) 工具,让 AI 助手可以搜索用户、读取和发送抖音私信。

核心功能

# 功能 MCP 工具 实现方式
1 🔍 搜索用户 search_user(keyword) 搜索页面 → DOM 提取用户信息
2 📥 读取私信 read_messages(contact, limit) DOM 提取消息文本(零成本)
3 📤 发送私信 send_message(user_id, text) Draft.js JS 注入 + 点击发送
4 📋 会话列表 list_conversations() DOM 提取会话项(昵称、最后消息、未读)

技术架构

┌─────────────────────────────────────┐
│          MCP Host (Claude 等)         │
├─────────────────────────────────────┤
│         Douyin MCP Server            │
│  ┌─────────┐  ┌───────────────────┐  │
│  │ server.py │→│  FastMCP 暴露工具  │  │
│  ├─────────┤  ├───────────────────┤  │
│  │ core.py  │→│  DouyinController   │  │
│  ├─────────┤  ├───────────────────┤  │
│  │browser.py│→│ BrowserManager     │  │
│  │          │  │ 扫码登录 + 持久化   │  │
│  └─────────┘  └───────────────────┘  │
├─────────────────────────────────────┤
│       Playwright + Chromium           │
├─────────────────────────────────────┤
│      抖音网页版 (www.douyin.com)       │
└─────────────────────────────────────┘

快速开始

前提条件

  • Python 3.10+
  • 手机抖音 App(用于扫码登录)

安装

# 1. 克隆项目
git clone https://github.com/Lozzi1910/Douyin-mcp.git
cd Douyin-mcp

# 2. 安装依赖
pip install -e .

# 3. 安装 Playwright Chromium 浏览器
playwright install chromium

启动

# 方式一:直接运行(stdio 模式,适合本地 MCP Host)
python -m douyin_mcp.server

# 方式二:SSE 模式(HTTP 端口,适合远程调用)
python -m douyin_mcp.server --transport sse --port 6789

# 方式三:通过 mcp CLI
mcp run douyin_mcp/server.py --port 6789

Docker 部署

首次部署

# 1. 构建镜像
docker compose build

# 2. 启动
docker compose up -d

# 3. 查看日志(等待二维码出现)
docker compose logs -f

# 4. 用手机抖音 App 扫码
#    二维码保存在 ./data/login_qrcode.png
open ./data/login_qrcode.png

连接 MCP Host

# 容器运行在 SSE 模式
# MCP Host 通过 SSE 端点连接:
#   http://localhost:6789/sse   (SSE 事件流)
#   http://localhost:6789/mcp   (MCP 消息端点)
#   http://localhost:6789/health (健康检查)

# 测试健康状态
curl http://localhost:6789/health

Docker Compose 配置

services:
  douyin-mcp:
    build: .
    container_name: douyin-mcp
    ports:
      - "6789:6789"
    volumes:
      - ./data:/root/.douyin_mcp        # 持久化登录态
      - ./logs:/var/log/douyin-mcp      # 日志
    environment:
      - TZ=Asia/Shanghai
      - DOUYIN_HEADLESS=true            # Docker 强制 headless
      - DOUYIN_TRANSPORT=sse            # SSE 传输模式
      - DOUYIN_PORT=6789                # 监听端口
    restart: unless-stopped

管理命令

# 查看日志
docker compose logs -f

# 重启
docker compose restart

# 停止
docker compose down

# 重新构建(代码更新后)
docker compose build --no-cache && docker compose up -d

使用指南

首次启动

首次运行需要扫描抖音二维码登录:

  1. 启动服务后,浏览器自动打开抖音登录页面
  2. 终端显示二维码截图路径
  3. 用手机抖音 App 扫描二维码
  4. 登录成功后,登录态自动保存到 ~/.douyin_mcp/storage.json
  5. 后续启动自动恢复登录态

工具说明

search_user(keyword) — 搜索抖音用户

  • 在抖音搜索页面搜索指定关键词
  • 返回用户昵称、抖音号、简介、粉丝数

list_conversations() — 列举私信会话

  • 提取 /messages 页面的会话列表
  • 返回每个会话的昵称、最后消息、未读标记

read_messages(contact, limit=20) — 读取私信

  • 在会话列表中定位联系人
  • 从 DOM 提取消息文本和发送者信息

send_message(user_id, text) — 发送私信

  • 在会话列表查找用户
  • 通过 Draft.js JS 注入输入文本
  • 点击发送按钮完成发送

项目结构

douyin-mcp-server/
├── pyproject.toml            # 项目配置
├── README.md                 # 本文件
├── TECHNICAL_REPORT.md       # 技术调研报告
├── douyin_mcp/
│   ├── __init__.py           # 包导出
│   ├── server.py             # MCP 服务器入口(FastMCP)
│   ├── core.py               # 核心操作(DouyinController)
│   └── browser.py            # 浏览器管理(扫码登录、持久化)
└── tests/
    └── test_controller.py    # 测试(TODO)

关键技术实现

Draft.js 输入

抖音聊天输入框基于 Draft.js,不能使用标准 fill()type()。 通过 page.evaluate() 注入 ClipboardEvent('paste') 事件触发 Draft.js 的 onChange 回调。

登录持久化

使用 Playwright 的 storage_state() 保存 cookie 和 localStorage 到 ~/.douyin_mcp/storage.json,后续启动自动恢复。

反检测

通过 add_init_script() 注入 stealth 脚本,修改 navigator.webdrivernavigator.pluginsnavigator.languages 等浏览器指纹,绕过抖音反自动化检测。

对比 WeChat-mcp

维度 WeChat-mcp Douyin-mcp
自动化对象 macOS 微信客户端 抖音网页版 (Web SPA)
自动化方式 cua-driver + osascript + screencapture Playwright 浏览器控制
消息读取 截图 → Qwen-VL OCR DOM 提取(更可靠、零成本)
消息发送 剪贴板 + Cmd+V Draft.js JS 注入
登录方式 用户手动登录微信 Playwright 扫码 + storage_state

开发

# 开发模式安装
pip install -e ".[dev]"

# 代码检查
ruff check douyin_mcp/

💰 运营成本

每次 MCP 调用完全免费(DOM 提取消息,零 API 费用),Docker 部署无额外成本。

💳 定价 Pricing

版本 价格 说明
个人版 免费 AGPL v3 开源,个人/内部使用
商业版 ¥499/项目 闭源商用,含 1 年更新
企业版 ¥2999/年 无限项目、优先支持、定制开发

购买方式:

☕ 支持作者 Support

如果这个项目对你有帮助,欢迎打赏支持开源!

方式 链接
🌟 GitHub Sponsors github.com/sponsors/Lozzi1910
Buy Me a Coffee buymeacoffee.com/Lozzi1910
💰 支付宝 / 微信支付 联系作者获取收款码

许可证

AGPL-3.0-only

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

douyin_chat_mcp_server-0.1.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

douyin_chat_mcp_server-0.1.0-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file douyin_chat_mcp_server-0.1.0.tar.gz.

File metadata

  • Download URL: douyin_chat_mcp_server-0.1.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for douyin_chat_mcp_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 24ad482c0416e823fd9a48f3e1a5b59373622d48f128271705cdedba70ae87ed
MD5 72966a73092488f03e3efc03a823f91c
BLAKE2b-256 cd18b68cebaed44bc8d2811109c709c55339b8531d0f20a315712049b065eb8a

See more details on using hashes here.

File details

Details for the file douyin_chat_mcp_server-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for douyin_chat_mcp_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 489d6aec3ea75fcc272881449b69ef0143d2a6e825027eaf66dfccbe8fe07543
MD5 993769ea78c8198aec8cab48a06e78f3
BLAKE2b-256 dd16a6fc8ae339d017f7d0459f22090db9c0ca58f7ca1a0ac7bf3e6a813a9c65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page