From f2ac228849463f7ac3ba1ef4cf4e5208bceb8fcc Mon Sep 17 00:00:00 2001
From: 3970260761 <3970260761@qq.com>
Date: Wed, 11 Nov 2026 19:45:54 +0800
Subject: [PATCH] =?UTF-8?q?=E7=B4=A0=E6=9D=90=E9=9D=A2=E6=9D=BF=E8=8F=9C?=
=?UTF-8?q?=E5=8D=95=E6=96=B0=E5=A2=9E=E3=80=8C=E4=B8=8B=E8=BD=BD=E6=9B=B4?=
=?UTF-8?q?=E5=A4=9A=E7=B4=A0=E6=9D=90=E3=80=8D=E7=9B=B4=E8=BE=BE=E5=85=A5?=
=?UTF-8?q?=E5=8F=A3=EF=BC=9B=E6=94=B6=E5=8F=A3=20unicut-mcp=20=E6=9C=8D?=
=?UTF-8?q?=E5=8A=A1=E7=AD=89=E6=9C=AA=E6=8F=90=E4=BA=A4=E6=94=B9=E5=8A=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 上传素材旁下拉菜单新增第三项「下载更多素材」,新窗口打开素材下载文档站 doc.88531.cn/docs/mrfx/mrfx-1g99qum57n5qk(zhCN/enUS 文案 my_download_more,Download 图标)
- sourceImport:showDirectoryPicker 不可用时降级,非 FS 浏览器可直接进编辑器
- 收口今日未提交改动:unicut-mcp 服务与 nginx/compose 反代、README/CHANGELOG 更新日志
---
.gitignore | 3 +
CHANGELOG.md | 22 +
README.md | 38 +-
docker-compose.yml | 17 +
mcp/Dockerfile | 17 +
mcp/README.md | 107 +++
mcp/requirements.txt | 5 +
mcp/server.py | 838 ++++++++++++++++++
nginx.conf | 43 +
package.json | 2 +-
src/language/enUS.ts | 1 +
src/language/zhCN.ts | 1 +
.../editor/components/sources/my/List.tsx | 10 +-
src/utils/sourceImport.ts | 3 +
stats.html | 2 +-
15 files changed, 1104 insertions(+), 5 deletions(-)
create mode 100644 mcp/Dockerfile
create mode 100644 mcp/README.md
create mode 100644 mcp/requirements.txt
create mode 100644 mcp/server.py
diff --git a/.gitignore b/.gitignore
index a122b6c..ef27ec3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,6 +27,9 @@ ssh
public/local-ai.json
.env
+# MCP 服务生成产物(工程 ZIP / 配音,运行时数据,勿提交)
+mcp-out/
+
# 数据质量检查的生成报告(每次运行都会变,不入库)
data-quality/reports/
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c0ae99b..872fed4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,28 @@
## 2026-11-11
+### ✨ 素材面板菜单
+
+**素材面板新增「下载更多素材」直达入口**
+
+- 上传素材右侧的下拉菜单(在线录音 / 文字转语音)新增第三项「下载更多素材」,点击在新窗口打开素材下载文档站 `https://doc.88531.cn/docs/mrfx/mrfx-1g99qum57n5qk`
+- 文案入语言表:`zhCN.my_download_more` / `enUS.my_download_more`,图标用 `Download`(@icon-park/react),菜单项渲染与既有项同构
+
+### 🤖 MCP 接入
+
+**新增 `unicut-mcp` 服务:AI 客户端直接调用米剪能力(Model Context Protocol)**
+
+- 新增 `mcp/` 服务(Python FastMCP,Streamable HTTP):抓取文章 `grab_url`(与前端 `extractFromHtml` 同规则)、口播文案 `generate_script`(OpenAI 兼容网关)、配音 `tts` / `list_voices`(unicut-tts)、工程生成 `create_video_project`、文件夹模式工程读写 `list/read/write_project`、自检 `service_status`
+- `create_video_project` 产出与米剪导出同构的工程 ZIP(`project.json + materials/{type}/`,过 `checkNdata` 九键门禁):图片校验对齐前端(≥160×120 / ≤10MB)、有配音自动铺满时长、文案按字数加权切字幕,元素结构照抄官方 demo 工程真实字段
+- nginx 同源反代 `https://<站点>/mcp`(SSE 关缓冲长连接)与 `https://<站点>/mcp-out/*`(工程 ZIP 下载);compose 新增 `unicut-mcp` 服务,密钥走项目 `.env`(`MCP_LLM_KEY` 等,不入库)
+- 文档:`mcp/README.md`(工具明细 + Claude Desktop / Claude Code / Cursor / Hermes 接入配置 + docker run / compose 两种部署),主 README「AI」章新增 MCP 小节与更新表行
+- 版本号 `1.0.0 → 1.0.1`(`package.json` / README 发布 tag 同步;授权中心基线仍为 1.0.0,客户端上报 1.0.1 判定「已是最新」)
+
+### 🐛 修复(/grab/ 抓取)
+
+- 公众号图片防盗链:`/grab/` 向上游清空 `Referer`(带异源 Referer 时 `mmbiz.qpic.cn` 只给 140×140 降级缩略图,无 Referer 才给原图,实测 2KB vs 47KB)
+- 正文根节点优先 `#js_content`:微信文章页没有 `article`/`main`,旧选择器会退化到整页 `body` 混进账号栏/留言噪音
+
### ✨ 新功能
**AI 一键出片 · 素材三选一(图片 / Word·PDF 文档 / 网址)**
diff --git a/README.md b/README.md
index de53ace..e8e2f0d 100644
--- a/README.md
+++ b/README.md
@@ -53,9 +53,11 @@
| 模板导入修复 | 修复"导入第一个模板后,再导入会把浏览器拖崩"的问题,支持连续换模板 |
| 品牌与首页 | 产品名改为米剪,首页新增导航站 / 开源地址入口,移除收费栏目与账号类外链 |
| 接入授权中心 | 版本号、公告、检查更新(授权中心不可达时静默降级,绝不拦功能) |
+| MCP 接入 | 新增 `unicut-mcp` 服务:Claude / Cursor / Hermes 等 AI 客户端经 `https://<站点>/mcp` 调用抓取文章、生成口播、edge-tts 配音、生成米剪工程 ZIP |
### 最近更新(2026-11-11)
+- **素材面板「下载更多素材」入口**:上传素材旁的下拉菜单(在线录音 / 文字转语音)新增第三项,点击新窗口直达素材下载文档站 https://doc.88531.cn/docs/mrfx/mrfx-1g99qum57n5qk;
- **AI 一键出片素材三选一**:图片 / Word·PDF 文档(自动提取正文与配图,扫描件有提示)/ 网址自动抓取正文和配图(同源 `/grab/` 反代,拒绝内网回环目标);纯文字素材自动补标题封面;
- **纯文本模型也能出片**:出片卡片新增提示「只需纯文本模型即可使用;模型支持多模态(视觉)就能自动生成出片」;未配视觉模型时剥掉图片块、按图片编号循环分配画面,纯文本主模型不再被顶回错误;
- **断网 lint 门禁修复**:`external_refs` 20 → 0(PDF.js 命名空间入册、示例域名清除、官方自有域名注明理由入白名单),构建门禁恢复全绿;
@@ -188,6 +190,20 @@ UNICUT 是一款面向创作者、开发者与企业团队的在线视频编辑
> [!IMPORTANT]
> AI 对话、AI 一键出片、AI 生图 / 生视频默认接入官方 AI 网关(`ai.88531.cn`),在编辑器右上角「配置模型」粘贴一个 API Key 即可解锁,对话 `mimo-v2.6-flash`、视觉 `deepseek-v4-flash-vision-exp`、生图 `flux-schnell`、生视频 `inkling` 均已预填。配音(本机 edge-tts)、抠图、字幕、导出等能力不消耗任何外部额度。
+### MCP:让 AI 客户端直接调用米剪
+
+米剪内置 [MCP](https://modelcontextprotocol.io)(Model Context Protocol)服务 `unicut-mcp`,站点同源端点 `https://<站点>/mcp`。接入后,Claude Desktop、Claude Code、Cursor、Hermes 等 AI 客户端可以把米剪当工具用:
+
+| 工具 | 能力 |
+| --- | --- |
+| `grab_url` | 抓网页正文与配图(公众号文章可用,规则与编辑器内抓取一致) |
+| `generate_script` | 大模型生成短视频口播文案 |
+| `tts` / `list_voices` | edge-tts 多音色配音(本机免费) |
+| `create_video_project` | 图片 + 文案 + 配音 → 生成米剪工程 ZIP,米剪「工程文件」一键导入即可出片 |
+| `list_projects` / `read_project` / `write_project` | 工作文件夹模式下的工程读写 |
+
+典型工作流:*AI 抓文章 → 写口播 → 配音 → 生成工程 ZIP → 你在米剪导入*。部署、客户端接入配置与工具明细见 [mcp/README.md](./mcp/README.md)。
+
## 🧰 技术实现
米剪采用 React + TypeScript 构建编辑器界面与业务层,通过 MobX 管理复杂编辑状态;画布由 PixiJS / WebGL 实时渲染,音视频解码、处理和导出由 Web Worker、FFmpeg WebAssembly、MediaBunny 等能力协同完成。素材文件可写入用户授权的本地目录,工程元数据和文件句柄通过 Dexie / IndexedDB 管理。
@@ -214,7 +230,7 @@ docker run -d --name unicut -p 3006:80 -p 3005:443 \
gitea.88531.cn/jinghui/unicut:latest
```
-打开 `http://服务器IP:3006` 即用。Registry 地址 `gitea.88531.cn` 免登录可拉;镜像与源码构建产物完全一致,每次发版同步更新 `latest` 与 `v1.0.0` 两个 tag。
+打开 `http://服务器IP:3006` 即用。Registry 地址 `gitea.88531.cn` 免登录可拉;镜像与源码构建产物完全一致,每次发版同步更新 `latest` 与 `v1.0.1` 两个 tag。
### 方式二:Docker Compose部署(源码 compose 构建)
@@ -246,6 +262,23 @@ services:
volumes:
- ../unicut-tts/audio:/data/audio
+ # MCP 服务(Model Context Protocol):AI 客户端经 https://<站点>/mcp 调用米剪能力
+ # (抓取正文配图 / 大模型口播文案 / edge-tts 配音 / 生成米剪工程 ZIP)。
+ # 密钥与站点地址从项目 .env 注入(MCP_LLM_KEY 等,勿提交仓库)。
+ unicut-mcp:
+ build: ./mcp
+ container_name: unicut-mcp
+ restart: always
+ environment:
+ - MCP_LLM_BASE=${MCP_LLM_BASE:-http://192.168.68.19:3099/v1}
+ - MCP_LLM_KEY=${MCP_LLM_KEY:-}
+ - MCP_LLM_MODEL=${MCP_LLM_MODEL:-mimo-v2.6-flash}
+ - MCP_PUBLIC_URL=${MCP_PUBLIC_URL:-https://127.0.0.1:3005}
+ - MCP_PROJECTS_DIR=${MCP_PROJECTS_DIR:-}
+ volumes:
+ # 生成的工程 ZIP / 配音 mp3 落盘(nginx /mcp-out/ 提供下载)
+ - ./mcp-out:/data/out
+
volumes:
certs:
```
@@ -254,7 +287,7 @@ volumes:
git clone https://gitee.com/jinghui111/unicut.git
cd unicut
docker compose up -d unicut # 只启动编辑器
-docker compose up -d # 全量(需 ../unicut-tts 源码)
+docker compose up -d # 全量(含配音 unicut-tts 与 MCP unicut-mcp)
```
首次构建包含前端打包,约需几分钟。完成后浏览器访问:
@@ -266,6 +299,7 @@ http://服务器IP:3006
- 端口映射:`3006 → 80`(HTTP)、`3005 → 443`(HTTPS,自签证书,首次访问需接受一次证书警告);
- 首次进入按提示选择一个本地文件夹并授权,工程与素材即保存在该文件夹;
- 可选:在 `.env` 里配置 `HUB_KEY` / `HUB_PROJECT_ID` 接入授权中心(公告 / 版本检查),不配置不影响任何功能;
+- 可选:在 `.env` 里配置 `MCP_LLM_KEY`(OpenAI 兼容网关 Key)启用 MCP 的文案生成,AI 客户端连接 `https://<站点>/mcp`,工具明细见 [mcp/README.md](./mcp/README.md);不配置则除大模型类工具外均可使用;
- 完整版另含配音服务 `unicut-tts`(本机 edge-tts),需单独部署该服务后用 `docker compose up -d` 全量启动;只用编辑器按上面的 `up -d unicut` 即可。
### 方式三:本地开发
diff --git a/docker-compose.yml b/docker-compose.yml
index 0ee8d05..ed4ac1d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -23,5 +23,22 @@ services:
volumes:
- ../unicut-tts/audio:/data/audio
+ # MCP 服务(Model Context Protocol):AI 客户端经 https://<站点>/mcp 调用米剪能力
+ # (抓取正文配图 / 大模型口播文案 / edge-tts 配音 / 生成米剪工程 ZIP)。
+ # 密钥与站点地址从项目 .env 注入(MCP_LLM_KEY 等,勿提交仓库)。
+ unicut-mcp:
+ build: ./mcp
+ container_name: unicut-mcp
+ restart: always
+ environment:
+ - MCP_LLM_BASE=${MCP_LLM_BASE:-http://192.168.68.19:3099/v1}
+ - MCP_LLM_KEY=${MCP_LLM_KEY:-}
+ - MCP_LLM_MODEL=${MCP_LLM_MODEL:-mimo-v2.6-flash}
+ - MCP_PUBLIC_URL=${MCP_PUBLIC_URL:-https://127.0.0.1:3005}
+ - MCP_PROJECTS_DIR=${MCP_PROJECTS_DIR:-}
+ volumes:
+ # 生成的工程 ZIP / 配音 mp3 落盘(nginx /mcp-out/ 提供下载)
+ - ./mcp-out:/data/out
+
volumes:
certs:
diff --git a/mcp/Dockerfile b/mcp/Dockerfile
new file mode 100644
index 0000000..0209a75
--- /dev/null
+++ b/mcp/Dockerfile
@@ -0,0 +1,17 @@
+# 米剪 MCP 服务:Streamable HTTP 端点 /mcp(8765),由 unicut nginx 同源反代
+FROM docker.1ms.run/library/python:3.11-slim
+
+ENV PYTHONUNBUFFERED=1 \
+ PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
+ PIP_DISABLE_PIP_VERSION_CHECK=1
+
+WORKDIR /app
+
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY server.py .
+
+EXPOSE 8765
+
+CMD ["python", "server.py"]
diff --git a/mcp/README.md b/mcp/README.md
new file mode 100644
index 0000000..c99e487
--- /dev/null
+++ b/mcp/README.md
@@ -0,0 +1,107 @@
+# 米剪 MCP 服务(unicut-mcp)
+
+让 AI 客户端以 [Model Context Protocol](https://modelcontextprotocol.io) 工具的形式调用米剪的创作能力:抓文章、写口播、配语音、生成米剪工程,人在米剪里打开就能剪。
+
+```
+AI(Claude / Cursor / Hermes …)
+ │ MCP(Streamable HTTP)
+ ▼
+https://<米剪站点>/mcp ──► unicut-mcp 容器(本目录)
+ ├─ grab_url 抓网页正文与配图(公众号文章可用)
+ ├─ generate_script 大模型生成口播文案(OpenAI 兼容网关)
+ ├─ tts / list_voices edge-tts 配音(unicut-tts 容器)
+ ├─ create_video_project 生成米剪工程 ZIP → 米剪「工程文件」导入
+ └─ list/read/write_project 工作文件夹(文件夹模式)工程读写
+```
+
+## 工具一览
+
+| 工具 | 作用 | 典型返回 |
+| --- | --- | --- |
+| `service_status` | 自检抓取/配音/大模型/工程目录可用性 | 各依赖状态 |
+| `grab_url(url)` | 抓取网页正文与配图(规则与前端 `extractFromHtml` 一致:`#js_content` 优先、懒加载属性、12000 字截断) | `{title, text, images[]}` |
+| `generate_script(source, requirement?, style?, target_seconds?, paragraph_count?)` | 大模型写口播文案,严格 JSON 输出 | `{title, paragraphs[], text}` |
+| `list_voices()` | edge-tts 音色列表 | `{count, voices[]}` |
+| `tts(text, voice?, rate?, pitch?, volume?)` | 合成配音,音频落盘并给出可播放 URL | `{url, path, duration}` |
+| `create_video_project(title, images, script?, audio?, width?, height?, …)` | 下载素材 → 组装 MovieData → 打包工程 ZIP(`project.json + materials/`,与米剪导出格式同构,过 `checkNdata` 九键门禁) | `{zip_path, zip_url, duration, captions, elements}` |
+| `list_projects()` / `read_project(id)` / `write_project(project)` | 工作文件夹模式下直接读写 `projects/*.json`(需配置 `MCP_PROJECTS_DIR`) | 工程 JSON |
+
+`create_video_project` 细节:
+
+- 图片校验与前端一致:≥160×120、≤10MB、识别失败自动跳过并在 `warnings` 里说明;
+- 有配音时图片自动拉伸平铺铺满配音时长,无配音时每图 `image_duration`(默认 3 秒);
+- `script` 按 `\n` 分段成字幕(单段超 24 字自动按句拆),按字数加权分配时间;
+- 字幕/图片/音频元素结构照抄官方 demo 工程(`public/assets/demo/test-app.json`)真实字段。
+
+## 连接方式
+
+**端点**:`https://<米剪站点>/mcp`(Streamable HTTP;本机默认 `https://127.0.0.1:3005/mcp`,自签证书需客户端允许 insecure)。局域网内也可直连容器端口 `http://<主机>:8765/mcp`(默认只在 compose 网络内,未映射宿主端口)。
+
+**Claude Desktop**(`claude_desktop_config.json`):
+
+```json
+{
+ "mcpServers": {
+ "unicut": {
+ "command": "npx",
+ "args": ["mcp-remote", "https://127.0.0.1:3005/mcp", "--allow-self-signed"]
+ }
+ }
+}
+```
+
+**Claude Code / Cursor**:
+
+```bash
+claude mcp add unicut --transport http https://127.0.0.1:3005/mcp
+# Cursor:~/.cursor/mcp.json → {"mcpServers":{"unicut":{"url":"https://127.0.0.1:3005/mcp"}}}
+```
+
+**Hermes**(`~/.hermes` 或项目 `config.yaml`):
+
+```yaml
+mcp:
+ servers:
+ unicut:
+ transport: http
+ url: https://127.0.0.1:3005/mcp
+```
+
+## 部署
+
+compose 已内置 `unicut-mcp` 服务(本仓库 `docker-compose.yml`),密钥走项目 `.env`:
+
+```bash
+# .env(勿提交)
+MCP_LLM_BASE=http://192.168.68.19:3099/v1 # OpenAI 兼容 /v1 网关
+MCP_LLM_KEY=sk-xxxx # 网关 Key
+MCP_LLM_MODEL=mimo-v2.6-flash
+MCP_PUBLIC_URL=https://127.0.0.1:3005 # 对外站点地址(生成 zip_url / 音频 URL 用)
+MCP_PROJECTS_DIR= # 可选:米剪「选择文件夹」的同一目录(文件夹模式)
+```
+
+```bash
+docker compose up -d --build unicut-mcp
+```
+
+单独运行(不走 nginx):
+
+```bash
+docker build -t unicut-mcp ./mcp
+docker run -d --name unicut-mcp --restart always \
+ -e MCP_LLM_KEY=sk-xxxx -e MCP_LLM_BASE=http://192.168.68.19:3099/v1 \
+ -p 8765:8765 -v "$PWD/mcp-out:/data/out" unicut-mcp
+```
+
+> 只有 `service_status` / `grab_url` / `generate_script` 需要网络依赖;`tts` 依赖同 compose 的 `unicut-tts`;不配置 `.env` 时除大模型类工具外均可使用。
+
+## 验证
+
+```bash
+# 协议握手
+curl -sk https://127.0.0.1:3005/mcp -H 'Content-Type: application/json' \
+ -H 'Accept: application/json, text/event-stream' \
+ -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl","version":"0"}}}'
+```
+
+产物下载:`https://<站点>/mcp-out/<工程名>.zip`;健康检查:经 nginx 用 `https://<站点>/mcp-health`,直连容器用 `http://<容器>:8765/health`(`/mcp/health` 不是端点,会被 SPA 回退成首页 HTML)。
diff --git a/mcp/requirements.txt b/mcp/requirements.txt
new file mode 100644
index 0000000..2484a14
--- /dev/null
+++ b/mcp/requirements.txt
@@ -0,0 +1,5 @@
+mcp>=1.9.0,<2
+httpx>=0.27.0
+beautifulsoup4>=4.12.0
+pillow>=10.0.0
+mutagen>=1.47.0
diff --git a/mcp/server.py b/mcp/server.py
new file mode 100644
index 0000000..e5d67f7
--- /dev/null
+++ b/mcp/server.py
@@ -0,0 +1,838 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+米剪(Unicut)MCP 服务
+======================
+
+让 AI 客户端(Claude Desktop / Claude Code / Cursor / Hermes 等)以 MCP 工具的形式
+调用米剪的创作能力:
+
+ - grab_url 抓取网页正文与配图(公众号文章等,复用前端 extractFromHtml 规则)
+ - generate_script 大模型生成短视频口播文案
+ - tts / list_voices edge-tts 配音合成(unicut-tts)
+ - create_video_project 组装米剪工程 ZIP(project.json + materials/),在米剪「工程文件」一键导入
+ - list_projects / read_project 工作文件夹(文件夹模式)下的工程读写
+ - service_status 各依赖服务可用性自检
+
+传输:Streamable HTTP,端点 /mcp(容器 8765,nginx 同源反代 https://<站点>/mcp)。
+工程格式:与米剪 projectExport.ts 导出的 ZIP 完全同构——
+ project.json MovieData(title/poster/width/height/background/transitions/
+ captions/elements/resouces 九键齐全,过 checkNdata 门禁)
+ materials/{type}/{文件名} 素材相对路径
+"""
+
+from __future__ import annotations
+
+import io
+import ipaddress
+import json
+import os
+import re
+import socket
+import time
+import unicodedata
+import uuid
+import zipfile
+from pathlib import Path
+from urllib.parse import urljoin, urlparse
+
+import httpx
+from bs4 import BeautifulSoup
+from mutagen.mp3 import MP3
+from PIL import Image
+
+from mcp.server.fastmcp import FastMCP
+
+# ---------------------------------------------------------------- 环境配置
+
+UNICUT_URL = os.getenv("UNICUT_URL", "http://unicut") # nginx 80(回环到 443)
+TTS_URL = os.getenv("TTS_URL", "http://unicut-tts:8000")
+LLM_BASE = os.getenv("MCP_LLM_BASE", "http://192.168.68.19:3099/v1").rstrip("/")
+LLM_KEY = os.getenv("MCP_LLM_KEY", "")
+LLM_MODEL = os.getenv("MCP_LLM_MODEL", "mimo-v2.6-flash")
+PUBLIC_URL = os.getenv("MCP_PUBLIC_URL", "https://127.0.0.1:3005").rstrip("/")
+PROJECTS_DIR = os.getenv("MCP_PROJECTS_DIR", "") # 文件夹模式工作目录(可空)
+OUT_DIR = Path(os.getenv("MCP_OUT_DIR", "/data/out"))
+
+IMG_MIN_W, IMG_MIN_H = 160, 120 # 与前端 sourceImport.ts 常量一致
+IMG_MAX_BYTES = 10 * 1024 * 1024
+HTTP_HEADERS = {
+ # 无 Referer:公众号图床 mmbiz.qpic.cn 带异源 Referer 时只给降级缩略图(实测)
+ "User-Agent": (
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
+ "(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
+ ),
+ "Accept": "*/*",
+ "Accept-Language": "zh-CN,zh;q=0.9",
+}
+
+mcp = FastMCP(
+ "unicut",
+ instructions=(
+ "米剪(Unicut)在线视频剪辑器的 MCP 服务。"
+ "典型工作流:grab_url 抓文章 → generate_script 写口播 → tts 配音 → "
+ "create_video_project 生成米剪工程 ZIP → 用户在米剪「工程文件」导入即可出片。"
+ ),
+ host="0.0.0.0",
+ port=8765,
+)
+
+
+def _client(timeout: float = 30.0) -> httpx.AsyncClient:
+ # trust_env=False:不读宿主代理变量,容器网络行为可预期
+ return httpx.AsyncClient(timeout=timeout, follow_redirects=True, trust_env=False)
+
+
+# ---------------------------------------------------------------- 安全与小工具
+
+def assert_public_url(url: str) -> str:
+ """拒绝非 http(s) 与内网/回环目标(与 nginx /grab/ 的断言同规则,防 SSRF 打内网)"""
+ p = urlparse(url)
+ if p.scheme not in ("http", "https"):
+ raise ValueError(f"只支持 http/https URL:{url}")
+ host = p.hostname
+ if not host:
+ raise ValueError(f"URL 缺少主机名:{url}")
+ if host == "localhost" or host.endswith(".local"):
+ raise ValueError(f"拒绝内网/回环目标:{host}")
+ try:
+ infos = socket.getaddrinfo(host, None, proto=socket.IPPROTO_TCP)
+ except socket.gaierror as e:
+ raise ValueError(f"域名解析失败:{host}({e})")
+ for info in infos:
+ ip = ipaddress.ip_address(info[4][0])
+ if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved or ip.is_multicast:
+ raise ValueError(f"拒绝内网地址目标:{host} → {ip}")
+ return str(p)
+
+
+def _uid() -> str:
+ return uuid.uuid4().hex[:10]
+
+
+def _slug(name: str) -> str:
+ """文件名安全化:保留中英文数字,替换掉路径/控制字符"""
+ s = unicodedata.normalize("NFKC", name or "project")
+ s = re.sub(r'[\\/:*?"<>|\r\n\t]', "-", s).strip(" .")
+ return (s or "project")[:60]
+
+
+def _ext_for(content_type: str, url: str, default: str = "bin") -> str:
+ ct = (content_type or "").split(";")[0].strip().lower()
+ table = {
+ "image/jpeg": "jpg", "image/jpg": "jpg", "image/png": "png",
+ "image/webp": "webp", "image/gif": "gif", "audio/mpeg": "mp3",
+ "audio/mp3": "mp3", "audio/wav": "wav", "audio/x-wav": "wav",
+ "audio/mp4": "m4a", "audio/aac": "aac",
+ }
+ if ct in table:
+ return table[ct]
+ suffix = Path(urlparse(url).path).suffix.lstrip(".").lower()
+ return suffix or default
+
+
+def _is_local_path(s: str) -> bool:
+ return s.startswith("/") and not s.startswith("//")
+
+
+# ---------------------------------------------------------------- 抓取(对齐前端 extractFromHtml)
+
+_BLOCK_TAGS = [
+ "p", "h1", "h2", "h3", "h4", "h5", "h6", "li", "blockquote", "pre",
+ "td", "th", "figcaption", "tr", "div", "br",
+]
+_NOISE_TAGS = [
+ "script", "style", "noscript", "svg", "iframe", "canvas", "form",
+ "nav", "footer", "header", "aside", "button",
+]
+
+
+def extract_from_html(html: str, base_url: str) -> dict:
+ """标题 / 正文 / 配图(相对转绝对、懒加载属性、去重)——与前端规则一致"""
+ soup = BeautifulSoup(html, "html.parser")
+
+ og = soup.find("meta", attrs={"property": "og:title"})
+ title = ((og.get("content") if og else None) or soup.title.get_text() if soup.title else "").strip()
+
+ for el in soup.find_all(_NOISE_TAGS):
+ el.decompose()
+
+ root = (
+ soup.find(id="js_content") # 公众号正文(微信页无 article/main)
+ or soup.find("article")
+ or soup.find("main")
+ or soup.find(attrs={"role": "main"})
+ or soup.body
+ or soup
+ )
+
+ # 图片:og:image 优先 + img 常见懒加载属性
+ seen: set[str] = set()
+ images: list[str] = []
+
+ def push(v: str | None) -> None:
+ if not v:
+ return
+ s = v.strip()
+ if not s or s.startswith("data:") or s.startswith("blob:"):
+ return
+ if re.search(r"\.svg(\?|#|$)", s, re.I):
+ return
+ try:
+ absu = urljoin(base_url, s)
+ except Exception:
+ return
+ if not re.match(r"^https?:", absu, re.I) or absu in seen:
+ return
+ seen.add(absu)
+ images.append(absu)
+
+ og_img = soup.find("meta", attrs={"property": "og:image"})
+ push(og_img.get("content") if og_img else None)
+ for img in root.find_all("img"):
+ for attr in ("src", "data-src", "data-original", "data-lazy-src", "data-actualsrc"):
+ push(img.get(attr))
+
+ # 正文:块级边界补换行 → textContent → 压空白(照抄前端清洗链)
+ for el in root.find_all(_BLOCK_TAGS):
+ el.insert(0, "\n")
+ el.append("\n")
+ text = (
+ root.get_text()
+ .replace("\r", "")
+ .replace("\t", " ")
+ .replace(" ", " ")
+ )
+ lines = [seg.strip() for seg in text.split("\n")]
+ kept: list[str] = []
+ for i, seg in enumerate(lines):
+ if seg or (i > 0 and lines[i - 1]):
+ kept.append(seg)
+ text = re.sub(r"\n{3,}", "\n\n", "\n".join(kept)).strip()[:12000]
+
+ return {"title": title, "text": text, "images": images[:20]}
+
+
+async def fetch_bytes(url: str, *, allow_local_file: bool = True, timeout: float = 25.0) -> tuple[bytes, str]:
+ """下载字节。url 可为公网 URL,或(默认允许)服务器本地文件路径。"""
+ if _is_local_path(url):
+ if not allow_local_file:
+ raise ValueError("此上下文不允许本地路径")
+ p = Path(url)
+ if not p.is_file():
+ raise ValueError(f"本地文件不存在:{url}")
+ return p.read_bytes(), ""
+ assert_public_url(url)
+ async with _client(timeout) as c:
+ r = await c.get(url, headers=HTTP_HEADERS)
+ r.raise_for_status()
+ return r.content, r.headers.get("content-type", "")
+
+
+# ---------------------------------------------------------------- 大模型
+
+async def _llm_chat(messages: list[dict], *, temperature: float = 0.7, max_tokens: int = 8000) -> str:
+ if not LLM_KEY:
+ raise ValueError("未配置 MCP_LLM_KEY(compose .env),无法调用大模型")
+ async with _client(timeout=180.0) as c:
+ r = await c.post(
+ f"{LLM_BASE}/chat/completions",
+ headers={"Authorization": f"Bearer {LLM_KEY}"},
+ json={
+ "model": LLM_MODEL,
+ "messages": messages,
+ "temperature": temperature,
+ "max_tokens": max_tokens,
+ },
+ )
+ if r.status_code >= 400:
+ raise ValueError(f"大模型网关 {r.status_code}: {r.text[:300]}")
+ data = r.json()
+ return data["choices"][0]["message"]["content"]
+
+
+def _parse_json_block(s: str) -> dict | None:
+ s = s.strip()
+ m = re.search(r"\{.*\}", s, re.S)
+ if not m:
+ return None
+ try:
+ return json.loads(m.group(0))
+ except json.JSONDecodeError:
+ # 容忍 ```json 围栏 / 尾逗号
+ cleaned = re.sub(r"^```(?:json)?|```$", "", m.group(0), flags=re.M).strip()
+ cleaned = re.sub(r",\s*([}\]])", r"\1", cleaned)
+ try:
+ return json.loads(cleaned)
+ except json.JSONDecodeError:
+ return None
+
+
+# ---------------------------------------------------------------- 工程组装(MovieData)
+
+def _split_captions(script: str, total: float, audio_dur: float | None) -> list[dict]:
+ """口播文案 → 字幕段(按字符数加权切分时间轴;有配音时铺满配音时长)"""
+ raw = [ln.strip() for ln in re.split(r"\n+", script) if ln.strip()]
+ lines: list[str] = []
+ for ln in raw:
+ if len(ln) <= 24:
+ lines.append(ln)
+ else: # 长段按句切(保留标点)
+ parts = [s for s in re.split(r"(?<=[。!?!?;;])", ln) if s.strip()]
+ lines.extend([p.strip() for p in parts] if parts else [ln])
+ if not lines:
+ return []
+
+ timeline = audio_dur if audio_dur else total
+ if timeline <= 0:
+ return []
+ weights = [max(len(x), 1) for x in lines]
+ s = sum(weights)
+ caps: list[dict] = []
+ t = 0.0
+ for i, (line, w) in enumerate(zip(lines, weights)):
+ dur = timeline * w / s
+ if i == len(lines) - 1:
+ dur = max(timeline - t, 0.3) # 末段补到结尾,避免浮点欠尾
+ caps.append(_caption_element(line, t, max(dur, 0.3)))
+ t += dur
+ return caps
+
+
+def _caption_element(text: str, start: float, dur: float, height: int = 1920, fill: str = "rgba(255, 255, 255, 1)") -> dict:
+ """字幕元素:结构照抄 demo/test-app.json 的真实字幕(字号缩放随画布高度)"""
+ scale = round(height / 1920, 4) or 1
+ return {
+ "id": _uid(),
+ "name": text,
+ "text": text,
+ "textStyle": {
+ "radius": [0, 0, 0, 0],
+ "type": "newTextStyle",
+ "direction": "horizontal",
+ "fillType": "normal",
+ "fill": fill,
+ "padding": [5, 5, 5, 5],
+ "fontSize": 200,
+ "letterSpacing": 0,
+ "lineHeight": 200,
+ "align": "center",
+ "fontFamily": "抖音美好体",
+ "stroke": {"color": "rgba(51, 51, 51, 1)", "lineJoin": "round", "lineWidth": 18},
+ "fontFamilyURL": "/assets/fonts/抖音美好体.otf",
+ },
+ "style": {
+ "x": 540,
+ "y": round(height * 0.77, 2),
+ "alpha": 1,
+ "width": 392.96,
+ "height": 67.2,
+ },
+ "type": "caption",
+ "trackIndex": 0.5,
+ "startTime": round(start, 3),
+ "duration": round(dur, 3),
+ "textScale": round(0.32 * scale, 4),
+ "scale": 1,
+ "templateEnable": True,
+ "sceneIndex": 1,
+ }
+
+
+def _image_element(resource_id: str, name: str, start: float, dur: float, w: int, h: int, idx: int) -> dict:
+ """图片元素:字段集对齐 demo 视频元素(同一 BaseElement 血统)+ ImageElement 特有 matting"""
+ return {
+ "id": _uid(),
+ "name": name,
+ "type": "image",
+ "trackIndex": 0,
+ "resourceId": resource_id,
+ "startTime": round(start, 3),
+ "duration": round(dur, 3),
+ "style": {"width": w, "height": h, "x": w / 2, "y": h / 2}, # x/y 是中心点(demo 实证)
+ "blendMode": 0,
+ "flipx": False,
+ "scale": 1,
+ "filters": [],
+ "matting": {
+ "enabled": False,
+ "color": "#00FF00",
+ "similarityThreshold": 0.5,
+ "emergence": 0.5,
+ },
+ "sceneIndex": idx + 1,
+ "templateEnable": False,
+ }
+
+
+def _audio_element(resource_id: str, dur: float, volume: float, name: str = "配音.mp3") -> dict:
+ return {
+ "id": _uid(),
+ "name": name,
+ "type": "audio",
+ "trackIndex": 1,
+ "resourceId": resource_id,
+ "startTime": 0,
+ "duration": round(dur, 3),
+ "muted": False,
+ "volume": volume,
+ "speed": 1,
+ "curveSpeed": False,
+ "clipTime": 0,
+ "fadeInTime": 0,
+ "fadeOutTime": 0,
+ }
+
+
+def build_movie_data(
+ title: str,
+ w: int,
+ h: int,
+ images: list[dict], # [{res_id, zip_rel, name, iw, ih, mime, size}]
+ audio: dict | None, # {res_id, zip_rel, name, dur, mime, size}
+ caption_lines: str,
+ with_caption: bool,
+ caption_fill: str,
+ voice_volume: float,
+ image_duration: float,
+) -> dict:
+ now = int(time.time() * 1000)
+ resouces: list[dict] = []
+ elements: list[dict] = []
+
+ n = len(images)
+ if audio:
+ total = float(audio["dur"])
+ span = total / n if n else 0.0
+ else:
+ span = float(image_duration)
+ total = n * span
+
+ for i, im in enumerate(images):
+ resouces.append({
+ "id": im["res_id"],
+ "name": im["name"],
+ "type": "image",
+ "url": im["zip_rel"],
+ "originId": im["res_id"],
+ "thumb": "",
+ "styleSize": {"width": im["iw"], "height": im["ih"]},
+ "fileType": im["mime"],
+ "from": "user",
+ "mustFetch": False,
+ "attrs": {"width": im["iw"], "height": im["ih"], "size": im["size"], "fileType": im["mime"]},
+ })
+ elements.append(_image_element(im["res_id"], im["name"], i * span, span, w, h, i))
+
+ if audio:
+ resouces.append({
+ "id": audio["res_id"],
+ "name": audio["name"],
+ "type": "audio",
+ "url": audio["zip_rel"],
+ "originId": audio["res_id"],
+ "thumb": "",
+ "styleSize": {"width": 0, "height": 0},
+ "fileType": audio["mime"],
+ "from": "user",
+ "mustFetch": False,
+ "duration": audio["dur"],
+ "attrs": {"size": audio["size"], "fileType": audio["mime"], "duration": audio["dur"]},
+ })
+ elements.append(_audio_element(audio["res_id"], audio["dur"], voice_volume, audio["name"]))
+
+ captions = _split_captions(caption_lines, total, audio["dur"] if audio else None) if with_caption else []
+
+ # checkNdata 门禁九键 + demo 同款元数据
+ return {
+ "title": title,
+ "createTime": now,
+ "updateTime": now,
+ "poster": "",
+ "version": "1.1.0",
+ "width": w,
+ "height": h,
+ "duration": round(total, 3),
+ "background": {},
+ "_hideLock": {},
+ "transitions": [],
+ "cameras": [],
+ "captions": captions,
+ "elements": elements,
+ "resouces": resouces,
+ }
+
+
+def write_project_zip(movie: dict, files: dict[str, bytes], zip_path: Path) -> None:
+ """project.json + materials/{type}/{file} —— 与 projectExport.ts 导出同构"""
+ zip_path.parent.mkdir(parents=True, exist_ok=True)
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
+ z.writestr("project.json", json.dumps(movie, ensure_ascii=False))
+ for rel, blob in files.items():
+ z.writestr(rel, blob)
+
+
+# ---------------------------------------------------------------- MCP 工具
+
+@mcp.tool()
+async def service_status() -> dict:
+ """自检:抓取反代 / 配音服务 / 大模型网关 / 工程目录 的可用性"""
+ out: dict[str, object] = {"time": time.strftime("%Y-%m-%d %H:%M:%S")}
+
+ try:
+ async with _client(8) as c:
+ r = await c.get(f"{UNICUT_URL}/")
+ out["unicut_nginx"] = f"OK({r.status_code})"
+ except Exception as e:
+ out["unicut_nginx"] = f"FAIL: {e}"
+
+ try:
+ async with _client(8) as c:
+ r = await c.get(f"{TTS_URL}/voices")
+ v = r.json()
+ n = len(v) if isinstance(v, list) else len(v.get("voices", [])) if isinstance(v, dict) else 0
+ out["tts"] = f"OK, {n} 个音色"
+ except Exception as e:
+ out["tts"] = f"FAIL: {e}"
+
+ out["llm"] = {
+ "base": LLM_BASE,
+ "model": LLM_MODEL,
+ "key": f"已配置({len(LLM_KEY)}位)" if LLM_KEY else "未配置 MCP_LLM_KEY",
+ }
+ out["projects_dir"] = PROJECTS_DIR or "未配置(文件夹模式工具不可用)"
+ out["out_dir"] = str(OUT_DIR)
+ out["public_url"] = PUBLIC_URL
+ return out
+
+
+@mcp.tool()
+async def grab_url(url: str, max_images: int = 20) -> dict:
+ """抓取网页正文与配图(支持公众号 mp.weixin.qq.com 文章)。
+
+ 返回 {ok, url, title, text(≤12000字), images[](绝对地址, 最多 max_images 张)}。
+ 图片只是候选地址;create_video_project 下载时会再校验尺寸(≥160x120)与大小(≤10MB)。
+ """
+ assert_public_url(url)
+ async with _client(30) as c:
+ r = await c.get(url, headers=HTTP_HEADERS)
+ r.raise_for_status()
+ final_url = str(r.url)
+ ctype = r.headers.get("content-type", "")
+ if "html" not in ctype and "xml" not in ctype:
+ # 非 HTML(如直接是文本/JSON):原样返回文本
+ return {"ok": True, "url": final_url, "title": final_url, "text": r.text[:12000], "images": []}
+ ext = extract_from_html(r.text, final_url)
+ return {
+ "ok": True,
+ "url": final_url,
+ "title": ext["title"],
+ "text": ext["text"],
+ "images": ext["images"][: max(0, min(int(max_images), 20))],
+ }
+
+
+@mcp.tool()
+async def generate_script(
+ source: str,
+ requirement: str = "",
+ style: str = "口播解说",
+ target_seconds: int = 60,
+ paragraph_count: int = 6,
+) -> dict:
+ """大模型生成短视频口播文案。
+
+ source:素材文本(抓取的文章正文)或主题描述;requirement:额外要求(受众/卖点/关键词)。
+ 返回 {ok, title, paragraphs[], text};paragraphs 每段一句,直接对应字幕与时间轴分段。
+ """
+ sys_prompt = (
+ "你是短视频口播文案编剧。只输出一个 JSON 对象,不要任何解释、不要 markdown 围栏:\n"
+ '{"title": "视频标题", "paragraphs": ["第一段口播", "第二段口播", ...]}\n'
+ "要求:口语化、有钩子、每段 1~2 句话适合做字幕;总字数 ≈ 目标秒数 × 4.5 字。"
+ )
+ user_prompt = (
+ f"风格:{style}\n目标时长:约 {target_seconds} 秒\n段落数:约 {paragraph_count} 段\n"
+ )
+ if requirement:
+ user_prompt += f"补充要求:{requirement}\n"
+ user_prompt += f"\n素材:\n{source[:8000]}"
+
+ content = await _llm_chat([
+ {"role": "system", "content": sys_prompt},
+ {"role": "user", "content": user_prompt},
+ ])
+ data = _parse_json_block(content)
+ if data and isinstance(data.get("paragraphs"), list):
+ paras = [str(p).strip() for p in data["paragraphs"] if str(p).strip()]
+ title = str(data.get("title") or "").strip()
+ else: # 兜底:按行切
+ paras = [ln.strip() for ln in content.splitlines() if ln.strip()]
+ title = ""
+ if not paras:
+ raise ValueError(f"模型未返回可用文案:{content[:300]}")
+ return {"ok": True, "title": title, "paragraphs": paras, "text": "\n".join(paras)}
+
+
+@mcp.tool()
+async def list_voices() -> dict:
+ """列出配音(edge-tts)可用音色 → {ok, count, voices[]}"""
+ async with _client(15) as c:
+ r = await c.get(f"{TTS_URL}/voices")
+ r.raise_for_status()
+ v = r.json()
+ voices = v if isinstance(v, list) else v.get("voices", [])
+ return {"ok": True, "count": len(voices), "voices": voices}
+
+
+@mcp.tool()
+async def tts(
+ text: str,
+ voice: str = "zh-CN-XiaoxiaoNeural",
+ rate: str = "+0%",
+ pitch: str = "+0Hz",
+ volume: str = "+0%",
+) -> dict:
+ """edge-tts 配音合成。返回 {ok, url(站点可直接播放), path(服务器文件), duration(秒), voice}。
+ text 建议 ≤2000 字;传给 create_video_project 的 audio 用 path。
+ """
+ if not text.strip():
+ raise ValueError("text 不能为空")
+ async with _client(180) as c:
+ r = await c.post(
+ f"{TTS_URL}/tts",
+ json={"text": text, "voice": voice, "rate": rate, "pitch": pitch, "volume": volume},
+ )
+ if r.status_code >= 400:
+ raise ValueError(f"TTS {r.status_code}: {r.text[:300]}")
+ j = r.json()
+ if not isinstance(j, dict) or not j.get("url"):
+ raise ValueError(f"TTS 返回异常:{str(j)[:300]}")
+ u = str(j["url"])
+ internal = TTS_URL + (u[len("/localtts"):] if u.startswith("/localtts") else u)
+ ar = await c.get(internal)
+ ar.raise_for_status()
+ blob = ar.content
+ if not blob:
+ raise ValueError("TTS 音频为空")
+
+ audio_dir = OUT_DIR / "audio"
+ audio_dir.mkdir(parents=True, exist_ok=True)
+ name = Path(urlparse(u).path).name or f"{_uid()}.mp3"
+ dest = audio_dir / name
+ dest.write_bytes(blob)
+ try:
+ dur = float(MP3(io.BytesIO(blob)).info.length)
+ except Exception:
+ dur = 0.0
+ public = PUBLIC_URL + (u if u.startswith("/") else "/" + u)
+ return {"ok": True, "url": public, "path": str(dest), "duration": round(dur, 3), "voice": voice}
+
+
+@mcp.tool()
+async def create_video_project(
+ title: str,
+ images: list[str] | None = None,
+ script: str = "",
+ audio: str = "",
+ width: int = 1080,
+ height: int = 1920,
+ image_duration: float = 3.0,
+ caption: bool = True,
+ caption_fill: str = "rgba(255, 255, 255, 1)",
+ voice_volume: float = 1.0,
+) -> dict:
+ """生成米剪工程 ZIP(project.json + materials/),在米剪顶部「工程文件」导入即可出片。
+
+ images:图片 URL 或服务器本地路径(tts 工具产出的 path 也行);script:口播文案→自动做字幕
+ (按\\n分段,单段过长自动按句拆);audio:配音 URL/path(通常来自 tts 工具)。
+ 有配音时图片自动拉伸铺满配音时长,否则每图 image_duration 秒。
+ 返回 {ok, zip_path, zip_url(可直接下载), id, duration, images_used, captions, elements}。
+ """
+ images = images or []
+ warnings: list[str] = []
+
+ # ---- 图片收集与校验
+ im_items: list[dict] = []
+ files: dict[str, bytes] = {}
+ for raw in images:
+ if len(im_items) >= 30:
+ warnings.append("图片超过 30 张,多余已丢弃")
+ break
+ try:
+ blob, ctype = await fetch_bytes(raw)
+ except Exception as e:
+ warnings.append(f"图片下载失败(跳过){raw[:80]}: {e}")
+ continue
+ if len(blob) > IMG_MAX_BYTES:
+ warnings.append(f"图片超 10MB(跳过){raw[:80]}")
+ continue
+ try:
+ im = Image.open(io.BytesIO(blob))
+ iw, ih = im.size
+ except Exception as e:
+ warnings.append(f"图片无法识别(跳过){raw[:80]}: {e}")
+ continue
+ if iw < IMG_MIN_W or ih < IMG_MIN_H:
+ warnings.append(f"图片太小 {iw}x{ih}(跳过){raw[:80]}")
+ continue
+ ext = _ext_for(ctype, raw, "jpg")
+ if ext not in ("jpg", "png", "webp", "gif"):
+ ext = "jpg" if im.format in ("JPEG", "PNG", "WEBP", "GIF") else "jpg"
+ fname = f"{uuid.uuid4().hex}.{ext}"
+ rel = f"materials/image/{fname}"
+ files[rel] = blob
+ im_items.append({
+ "res_id": _uid(),
+ "name": Path(urlparse(raw).path).name or fname,
+ "zip_rel": rel,
+ "iw": iw, "ih": ih,
+ "mime": f"image/{'jpeg' if ext == 'jpg' else ext}",
+ "size": len(blob),
+ })
+
+ # ---- 配音收集
+ aud: dict | None = None
+ if audio:
+ try:
+ blob, ctype = await fetch_bytes(audio)
+ ext = _ext_for(ctype, audio, "mp3")
+ fname = f"voice_{uuid.uuid4().hex[:8]}.{ext}"
+ rel = f"materials/audio/{fname}"
+ files[rel] = blob
+ try:
+ dur = float(MP3(io.BytesIO(blob)).info.length)
+ except Exception:
+ dur = 0.0
+ if dur <= 0:
+ warnings.append("配音时长无法识别,时间轴将按图片时长排布")
+ aud = None
+ else:
+ aud = {
+ "res_id": _uid(),
+ "name": Path(urlparse(audio).path).name or fname,
+ "zip_rel": rel,
+ "dur": dur,
+ "mime": f"audio/{'mpeg' if ext == 'mp3' else ext}",
+ "size": len(blob),
+ }
+ except Exception as e:
+ warnings.append(f"配音获取失败(忽略): {e}")
+
+ if not im_items and not aud:
+ raise ValueError("至少需要 1 张可用图片或 1 段配音")
+
+ # ---- 组装 MovieData
+ movie = build_movie_data(
+ title=title, w=int(width), h=int(height),
+ images=im_items, audio=aud,
+ caption_lines=script, with_caption=bool(caption and script.strip()),
+ caption_fill=caption_fill, voice_volume=float(voice_volume),
+ image_duration=float(image_duration),
+ )
+
+ # ---- 落盘 ZIP
+ ts = time.strftime("%Y%m%d-%H%M%S")
+ zip_name = f"{_slug(title)}-{ts}.zip"
+ zip_path = OUT_DIR / zip_name
+ write_project_zip(movie, files, zip_path)
+
+ # ---- 门禁自检:checkNdata 九键
+ required = ["title", "poster", "width", "height", "background", "transitions", "captions", "elements", "resouces"]
+ missing = [k for k in required if k not in movie]
+ if missing:
+ raise ValueError(f"工程缺少必需键:{missing}")
+
+ return {
+ "ok": True,
+ "id": f"mcp-{ts}-{_uid()[:6]}",
+ "zip_path": str(zip_path),
+ "zip_url": f"{PUBLIC_URL}/mcp-out/{zip_name}",
+ "duration": movie["duration"],
+ "size": zip_path.stat().st_size,
+ "images_used": len(im_items),
+ "has_audio": bool(aud),
+ "captions": len(movie["captions"]),
+ "elements": len(movie["elements"]),
+ "warnings": warnings,
+ "import_hint": "下载该 ZIP → 米剪顶部「工程文件」→ 导入(需先选择过工作文件夹)",
+ }
+
+
+@mcp.tool()
+async def list_projects() -> dict:
+ """列出工作文件夹(文件夹模式 PROJECTS_DIR)里的米剪工程 → {ok, dir, projects[]}"""
+ if not PROJECTS_DIR:
+ return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR(compose .env)。文件夹模式需把它指到米剪里选择的同一目录"}
+ pdir = Path(PROJECTS_DIR) / "projects"
+ if not pdir.is_dir():
+ return {"ok": True, "dir": str(pdir), "projects": []}
+ items = []
+ for f in sorted(pdir.glob("*.json")):
+ try:
+ d = json.loads(f.read_text(encoding="utf-8"))
+ items.append({
+ "file": f.name,
+ "id": d.get("id") or f.stem,
+ "title": d.get("title") or f.stem,
+ "width": d.get("width"),
+ "height": d.get("height"),
+ "updatedAt": d.get("updatedAt"),
+ })
+ except Exception as e:
+ items.append({"file": f.name, "error": str(e)})
+ return {"ok": True, "dir": str(pdir), "projects": items}
+
+
+@mcp.tool()
+async def read_project(project_id: str) -> dict:
+ """读取工作文件夹里的工程 JSON(folder 形态 {id,title,width,height,data})"""
+ if not PROJECTS_DIR:
+ return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR"}
+ f = Path(PROJECTS_DIR) / "projects" / f"{_slug(project_id)}.json"
+ if not f.is_file():
+ return {"ok": False, "error": f"工程不存在:{f}"}
+ data = json.loads(f.read_text(encoding="utf-8"))
+ return {"ok": True, "path": str(f), "project": data}
+
+
+@mcp.tool()
+async def write_project(project: dict) -> dict:
+ """写入/更新工作文件夹工程(folder 形态:{id,title,width,height,data})。谨慎调用:会覆盖同 id 文件"""
+ if not PROJECTS_DIR:
+ return {"ok": False, "error": "未配置 MCP_PROJECTS_DIR"}
+ if not isinstance(project, dict) or not project.get("data"):
+ return {"ok": False, "error": "需要 {id,title,width,height,data(MovieData)} 结构"}
+ pid = _slug(str(project.get("id") or _uid()))
+ project["id"] = pid
+ project["updatedAt"] = int(time.time() * 1000)
+ pdir = Path(PROJECTS_DIR) / "projects"
+ pdir.mkdir(parents=True, exist_ok=True)
+ f = pdir / f"{pid}.json"
+ f.write_text(json.dumps(project, ensure_ascii=False), encoding="utf-8")
+ return {"ok": True, "path": str(f), "id": pid}
+
+
+# ---------------------------------------------------------------- 静态产物与健康检查路由
+
+try:
+ from starlette.responses import FileResponse, JSONResponse
+
+ @mcp.custom_route("/health", methods=["GET"])
+ async def _health(_req):
+ return JSONResponse({"ok": True, "service": "unicut-mcp"})
+
+ @mcp.custom_route("/out/{name}", methods=["GET"])
+ async def _out(req):
+ name = Path(req.path_params["name"]).name # 防目录穿越
+ f = OUT_DIR / name
+ if not f.is_file():
+ return JSONResponse({"ok": False, "error": "not found"}, status_code=404)
+ return FileResponse(f, filename=name)
+
+except AttributeError: # 老版本 SDK 无 custom_route,静默降级
+ pass
+
+
+if __name__ == "__main__":
+ OUT_DIR.mkdir(parents=True, exist_ok=True)
+ mcp.run(transport="streamable-http")
diff --git a/nginx.conf b/nginx.conf
index feabf5d..cd884b3 100644
--- a/nginx.conf
+++ b/nginx.conf
@@ -116,6 +116,9 @@ server {
proxy_set_header Host $proxy_host;
proxy_set_header Connection "";
proxy_set_header X-Grab-Url "";
+ # 公众号图片防盗链:带异源 Referer 时 mmbiz.qpic.cn 只给降级缩略图
+ # (实测 2KB/140x140,无 Referer 则 47KB/原尺寸),向上游清空该头
+ proxy_set_header Referer "";
proxy_http_version 1.1;
proxy_read_timeout 20s;
proxy_send_timeout 20s;
@@ -143,6 +146,46 @@ server {
client_max_body_size 8m;
}
+ # MCP 服务(Model Context Protocol,unicut-mcp 容器)同源反代:
+ # AI 客户端(Claude Desktop / Claude Code / Cursor / Hermes 等)用
+ # https://<站点>/mcp
+ # 连接即可把米剪当工具用(抓取文章 / 生成口播文案 / edge-tts 配音 / 生成工程 ZIP)。
+ # Streamable HTTP 走 SSE 流:必须关缓冲、保持 HTTP/1.1 长连接、拉长读超时。
+ # 注:/mcp-out 前缀比 /mcp 更长,nginx 最长前缀匹配规则会优先后者,不冲突。
+ location /mcp {
+ proxy_pass http://unicut-mcp:8765;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+ proxy_set_header Connection "";
+ proxy_buffering off;
+ proxy_request_buffering off;
+ proxy_cache off;
+ proxy_read_timeout 600s;
+ proxy_send_timeout 600s;
+ client_max_body_size 20m;
+ }
+
+ # MCP 生成产物下载(工程 ZIP、配音 mp3),配合米剪「工程文件」导入
+ location /mcp-out/ {
+ proxy_pass http://unicut-mcp:8765/out/;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_read_timeout 300s;
+ client_max_body_size 100m;
+ }
+
+ # MCP 健康检查(容器 /health;注意 /mcp/health 不是端点,SPA 会回退成首页 HTML)
+ location = /mcp-health {
+ proxy_pass http://unicut-mcp:8765/health;
+ proxy_http_version 1.1;
+ proxy_set_header Host $host;
+ proxy_read_timeout 10s;
+ }
+
# UNICUT 在线服务反代(账号/素材/AI 等在线能力), 对应官方 dev server 的代理配置
location /api/ {
proxy_pass https://video.h5ds.com;
diff --git a/package.json b/package.json
index 87ac5b3..899ac5c 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "aacut",
"private": true,
- "version": "1.0.0",
+ "version": "1.0.1",
"scripts": {
"dev": "vite",
"build": "yarn clean && node scripts/patch-sdk-offline.mjs && yarn build:worker && vite build",
diff --git a/src/language/enUS.ts b/src/language/enUS.ts
index c08f783..2465610 100644
--- a/src/language/enUS.ts
+++ b/src/language/enUS.ts
@@ -80,6 +80,7 @@ export const enUS = {
my_upload: 'Upload',
my_upload_mobile_link: 'Upload Mobile',
my_upload_refresh: 'Refresh List',
+ my_download_more: 'Download More Assets',
my_tab_material: 'Current Materials',
my_tab_all_material: 'All Materials',
my_tab_no_material: 'No materials in this project. Please upload first.',
diff --git a/src/language/zhCN.ts b/src/language/zhCN.ts
index 180bef2..2f8ce7a 100644
--- a/src/language/zhCN.ts
+++ b/src/language/zhCN.ts
@@ -91,6 +91,7 @@ export const zhCN = {
my_upload: '上传素材',
my_upload_mobile_link: '手机上传',
my_upload_refresh: '刷新列表',
+ my_download_more: '下载更多素材',
my_tab_material: '当前素材',
my_tab_all_material: '全部素材',
my_tab_no_material: '该项目暂无素材,请先上传',
diff --git a/src/pages/editor/components/sources/my/List.tsx b/src/pages/editor/components/sources/my/List.tsx
index 6fe15ca..e803e58 100644
--- a/src/pages/editor/components/sources/my/List.tsx
+++ b/src/pages/editor/components/sources/my/List.tsx
@@ -12,7 +12,7 @@ import {
Pagination,
Empty,
} from '@douyinfe/semi-ui';
-import { Voice, Left, VoiceInput, Upload as UploadIcon, DeleteFive, Close, Refresh } from '@icon-park/react';
+import { Voice, Left, VoiceInput, Upload as UploadIcon, DeleteFive, Close, Refresh, Download } from '@icon-park/react';
import { useCallback, useEffect, useReducer, useRef, useState } from 'react';
import { getUploadBeforeData, isAPNG } from '@pages/editor/tools/uploadBeforeData';
import { pubsub, util } from '@utils/index';
@@ -497,6 +497,14 @@ export default function List(props: IProps) {
setShowType('tts');
},
},
+ {
+ node: 'item',
+ icon: ,
+ name: language.val('my_download_more'),
+ onClick: () => {
+ window.open('https://doc.88531.cn/docs/mrfx/mrfx-1g99qum57n5qk', '_blank');
+ },
+ },
]}
trigger="click"
position="bottomRight"
diff --git a/src/utils/sourceImport.ts b/src/utils/sourceImport.ts
index b9ddd96..a6144b1 100644
--- a/src/utils/sourceImport.ts
+++ b/src/utils/sourceImport.ts
@@ -313,6 +313,9 @@ export function extractFromHtml(
.forEach(el => el.remove());
const root =
+ // 公众号文章正文在 #js_content(页面其余部分是账号栏/留言/分享等噪音),
+ // 必须优先于 article/main 选择器——微信页两者都没有,否则会退化到整页 body
+ dom.querySelector('#js_content') ||
dom.querySelector('article') ||
dom.querySelector('main') ||
dom.querySelector('[role="main"]') ||
diff --git a/stats.html b/stats.html
index 777dba9..3632f11 100644
--- a/stats.html
+++ b/stats.html
@@ -4929,7 +4929,7 @@ var drawChart = (function (exports) {