修正文件夹目录
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
npm-debug.log
|
||||
.DS_Store
|
||||
@@ -0,0 +1,15 @@
|
||||
FROM node:22-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 复制 package.json 并安装依赖
|
||||
COPY package.json ./
|
||||
RUN npm install --omit=dev
|
||||
|
||||
# 复制源码
|
||||
COPY . .
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
# 启用 HTTP/SSE 模式,监听 3000 端口
|
||||
CMD ["node", "src/mcp-server.js", "--http", "--port", "3000", "--host", "0.0.0.0"]
|
||||
@@ -0,0 +1,406 @@
|
||||
# MCP Video Gen
|
||||
|
||||
基于 [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) 的视频生成服务,对接智谱 BigModel 的视频生成**异步**接口,支持**文生视频**和**图生视频**。Server 只负责提交任务 + 单次查询结果,**不内部轮询**——AI 拿到 `task_id` 后自己决定什么时候调用 `query_video` 复查。
|
||||
|
||||
- 协议:MCP **Streamable HTTP**(单端点 `/mcp`,支持 SSE 流)
|
||||
- 上游:[智谱 BigModel 视频生成(异步)](https://docs.bigmodel.cn/api-reference/模型-api/视频生成异步) + [查询异步结果](https://docs.bigmodel.cn/api-reference/模型-api/查询异步结果)
|
||||
- 默认模型:`cogvideox-flash`
|
||||
- 端口:`3000`
|
||||
- 鉴权:**无服务端密钥**,每次请求必须从 header 携带智谱 token
|
||||
- 附带 `/upload` 端点:供 AI 临时上传本地图片获取 URL(**不持久化**,容器重启即丢)
|
||||
|
||||
---
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 启动(docker-compose)
|
||||
|
||||
仓库根目录 `docker-compose.yml` 已包含本服务:
|
||||
|
||||
```bash
|
||||
cd /root/workspace/tools/mcp
|
||||
docker compose up -d --build mcp_video_gen
|
||||
```
|
||||
|
||||
服务容器名 `mcp_video_gen`,通过反代暴露为 `https://video-gen.ai.foresh.com/mcp`。
|
||||
|
||||
直接连容器调试:
|
||||
|
||||
```bash
|
||||
docker exec -it mcp_video_gen sh
|
||||
# 或本机
|
||||
curl http://localhost:3000/health
|
||||
# {"status":"ok","service":"video_gen","sessions":0}
|
||||
```
|
||||
|
||||
### MCP 客户端配置
|
||||
|
||||
#### Claude Desktop / Cursor 等支持 `headers` 配置的客户端
|
||||
|
||||
```jsonc
|
||||
// Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json
|
||||
// Cursor: ~/.cursor/mcp.json
|
||||
{
|
||||
"mcpServers": {
|
||||
"video_gen": {
|
||||
"url": "https://video-gen.ai.foresh.com/mcp",
|
||||
"headers": {
|
||||
"Authorization": "Bearer <your-zhipu-api-key>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> **重要**:客户端必须在 **每一个 HTTP 请求**(`initialize` / `notifications/initialized` / `tools/call`)都带上 `Authorization` header。
|
||||
|
||||
---
|
||||
|
||||
## 工具一览
|
||||
|
||||
| 工具 | 作用 | 同步/异步 |
|
||||
|---|---|---|
|
||||
| `video_gen` | 提交一个视频生成任务,立刻返回 `task_id` | 同步(仅提交) |
|
||||
| `query_video` | 用 `task_id` 拉一次当前结果 | 同步(单次查询,不轮询) |
|
||||
|
||||
---
|
||||
|
||||
## 上传图片:POST /upload
|
||||
|
||||
供 AI 临时上传本地图片、拿到 URL 后喂给 `video_gen.image_url`。**不走 MCP、不走鉴权**,仅依赖内网隔离(`my-app-network`)+ 反代层控制访问。
|
||||
|
||||
### 配置
|
||||
|
||||
通过环境变量告诉服务端「对外公开的 base URL」,用于拼接返回的 `url` 字段:
|
||||
|
||||
```yaml
|
||||
# docker-compose.yml
|
||||
environment:
|
||||
- PUBLIC_BASE_URL=https://video-gen.ai.foresh.com
|
||||
```
|
||||
|
||||
未设置时 fallback 到 `http://localhost:3000`(本机调试)。
|
||||
|
||||
可选环境变量:
|
||||
|
||||
| 变量 | 默认 | 说明 |
|
||||
|---|---|---|
|
||||
| `PUBLIC_BASE_URL` | `http://localhost:3000` | 返回 `url` 字段里拼接的对外 base URL |
|
||||
| `UPLOAD_DIR` | `/tmp/video_gen_uploads` | 磁盘存储目录(容器内临时) |
|
||||
| `UPLOAD_TTL_HOURS` | `24` | 超过该小时数的文件会在启动时和每小时被自动删除 |
|
||||
|
||||
### 接口
|
||||
|
||||
```
|
||||
POST /upload
|
||||
Content-Type: multipart/form-data
|
||||
|
||||
file: <二进制图片文件>
|
||||
```
|
||||
|
||||
### 限制
|
||||
|
||||
| 项 | 值 |
|
||||
|---|---|
|
||||
| 最大体积 | 8MB(智谱 `image_url` 上限5MB,留余量) |
|
||||
| 白名单 content-type | `image/png`、`image/jpeg`、`image/jpg` |
|
||||
| 存储位置 | 容器内 `/tmp/video_gen_uploads/`(`UPLOAD_DIR` 可覆盖) |
|
||||
| 持久化 | **不持久化**。容器重启即丢,请尽快消费 URL |
|
||||
| 过期清理 | 启动时 + 每小时一次删除 `>UPLOAD_TTL_HOURS` 的文件,默认 24h |
|
||||
| 鉴权 | 无 |
|
||||
|
||||
### 返回
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"url": "https://video-gen.ai.foresh.com/uploads/abc123.png",
|
||||
"filename": "abc123.png",
|
||||
"size": 12345,
|
||||
"content_type": "image/png",
|
||||
"original_name": "my-cat.png"
|
||||
}
|
||||
```
|
||||
|
||||
返回的 `url` 可直接作为 `video_gen.image_url` 使用。
|
||||
|
||||
### 反代路由
|
||||
|
||||
反代需要额外暴露两条路由到 `mcp_video_gen:3000`:
|
||||
|
||||
- `POST /upload` → `/upload`
|
||||
- `GET /uploads/*` → `/uploads/*`
|
||||
|
||||
### curl 示例
|
||||
|
||||
```bash
|
||||
curl -F "file=@/tmp/cat.png" https://video-gen.ai.foresh.com/upload
|
||||
# {"url":"https://video-gen.ai.foresh.com/uploads/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.png", ...}
|
||||
|
||||
# 然后把 url 喂给 MCP:
|
||||
# tool: video_gen
|
||||
# args: { image_url: "https://video-gen.ai.foresh.com/uploads/xxx.png", prompt: "..." }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 工具:video_gen
|
||||
|
||||
提交一个视频生成任务,支持**文生视频**和**图生视频**。`prompt` 为**必填**;图生视频另传 `image_url`(两者都传也行,此时 prompt 会附加为运动/动作描述)。
|
||||
|
||||
### 参数(AI 可见)
|
||||
|
||||
| 名称 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `prompt` | string | ✅ | 视频文本描述,最大 512 字符 |
|
||||
| `image_url` | string (URL) | ❌ | 参考图 URL(**单张**,不支持数组 / 多张 / 首尾帧)。图片 ≤ 5MB,格式 png/jpeg/jpg。本地文件请走 `/upload` |
|
||||
| `size` | string | ❌ | 视频尺寸,例如 `1920x1080`、`1080x1920`、`1024x1024`、`1280x960` 等 |
|
||||
| `with_audio` | boolean | ❌ | 是否生成 AI 音效,默认 `false` |
|
||||
|
||||
### 服务端 Header 覆盖项(AI 看不到)
|
||||
|
||||
| Header | 类型 | 默认 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `X-Video-Gen-Model` / `X-Mcp-Model` / `X-Model` | string | `cogvideox-flash` | 覆盖模型 |
|
||||
| `X-Video-Gen-Quality` | string | `speed` | `speed`(速度优先)或 `quality`(质量优先) |
|
||||
| `X-Video-Gen-Fps` | int | `30` | `30` 或 `60` |
|
||||
| `X-Video-Gen-Watermark` | bool | `true` | 是否添加 AI 水印;关闭需先在智谱后台签署免责声明 |
|
||||
|
||||
模型名串识别顺序(首个非空值生效):`X-Video-Gen-Model` → `X-Mcp-Model` → `X-Model` → 默认。
|
||||
|
||||
### 返回
|
||||
|
||||
工具立刻返回智谱提交结果:
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"content": [
|
||||
{ "type": "text", "text": "Model: cogvideox-flash\ntask_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\nrequest_id: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy\ntask_status: PROCESSING\n\nVideo generation is asynchronous. Call query_video with task_id=\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" to fetch the result." }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
`task_status` 通常为 `PROCESSING`,极少数情况下可能直接 `SUCCESS`(极快完成的短任务)。
|
||||
|
||||
---
|
||||
|
||||
## 工具:query_video
|
||||
|
||||
根据 `task_id` 拉一次智谱异步结果。**不内部轮询**——AI 自己决定调几次、调多频。
|
||||
|
||||
### 参数(AI 可见)
|
||||
|
||||
| 名称 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `task_id` | string | ✅ | `video_gen` 返回的 `id` |
|
||||
|
||||
### 返回
|
||||
|
||||
- **`SUCCESS`**:返回 `Video URL`(智谱签名 URL,**24h 左右有效**,需及时转存),外加一张 `Cover` 封面图:
|
||||
```jsonc
|
||||
{
|
||||
"content": [
|
||||
{ "type": "text", "text": "task_id: ...\nmodel: cogvideox-flash\ntask_status: SUCCESS\nVideo URL: https://...\nCover: https://..." },
|
||||
{ "type": "text", "text": "https://...video url..." }
|
||||
]
|
||||
}
|
||||
```
|
||||
- **`PROCESSING`**:`isError: false`,正文给出当前状态,建议稍候再查。
|
||||
- **`FAIL`**:`isError: true`,正文含 `task_status` 和智谱返回的 `error` 详情。
|
||||
|
||||
---
|
||||
|
||||
## 鉴权细节
|
||||
|
||||
每个 HTTP 请求必须携带智谱 API Token,server 从以下 header 字段按顺序识别,**首个非空值生效**:
|
||||
|
||||
| 优先级 | Header | 说明 |
|
||||
|---|---|---|
|
||||
| 1 | `X-Video-Gen-Token: <token>` | 直传,避免与客户端自身的 Authorization 冲突 |
|
||||
| 2 | `X-Mcp-Token: <token>` | 通用兜底 |
|
||||
| 3 | `Authorization: Bearer <token>` | 标准 OAuth 风格(排最后是为避免误用) |
|
||||
|
||||
Token 仅在每次 tool call 时从 header 读取,不写入任何存储、不进 session。
|
||||
|
||||
### 错误响应
|
||||
|
||||
| 场景 | HTTP 状态 | 表现 |
|
||||
|---|---|---|
|
||||
| initialize 时缺 token | `401` | JSON-RPC `error.message` 提示补充 header |
|
||||
| 智谱 API 4xx/5xx | `200`(MCP 工具返回) | `isError: true`,正文含智谱返回的状态码和 body |
|
||||
| 网络失败 | `200`(MCP 工具返回) | `isError: true`,正文含 `fetch` 错误信息 |
|
||||
|
||||
---
|
||||
|
||||
## 推荐用法(AI 视角)
|
||||
|
||||
```
|
||||
1. 如果手头是【本地文件】:先调 POST {PUBLIC_BASE_URL}/upload 拿到 url,再进第 2 步
|
||||
2. 调用 video_gen({ prompt, image_url?, size?, with_audio? })
|
||||
3. 拿到 task_id → 等待若干秒(建议 2~5 秒)
|
||||
4. 调用 query_video({ task_id })
|
||||
- 若 PROCESSING → 等一会再调
|
||||
- 若 SUCCESS → 拿到 video URL,贴给用户 / 下载 / 二次编辑
|
||||
- 若 FAIL → 展示错误信息,结束
|
||||
```
|
||||
|
||||
> `video_gen` 的 tool description 里已经嵌入了完整 curl 上传命令,AI 一看就知道怎么传本地图片。
|
||||
|
||||
---
|
||||
|
||||
## 用 curl 手工调试
|
||||
|
||||
MCP Streamable HTTP 不是普通 REST,**必须按顺序**:`initialize` → `notifications/initialized` → `tools/call`。下面用一行版串起来:
|
||||
|
||||
```bash
|
||||
# 把 <your-zhipu-api-key> 替换成你智谱的 API Key
|
||||
TOKEN="<your-zhipu-api-key>"
|
||||
|
||||
# 1. initialize 拿 sessionId
|
||||
SESSION=$(curl -s -D - -X POST http://localhost:3000/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {},
|
||||
"clientInfo": { "name": "curl", "version": "1.0" }
|
||||
}
|
||||
}' | grep -i '^mcp-session-id:' | awk '{print $2}' | tr -d '\r\n')
|
||||
|
||||
echo "Session: $SESSION"
|
||||
|
||||
# 2. notifications/initialized
|
||||
curl -s -X POST http://localhost:3000/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{"jsonrpc": "2.0", "method": "notifications/initialized"}'
|
||||
|
||||
# 3. 列出工具(可选验证)
|
||||
curl -s -X POST http://localhost:3000/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
|
||||
|
||||
# 4. 提交文生视频任务
|
||||
curl -s -X POST http://localhost:3000/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 3,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "video_gen",
|
||||
"arguments": {
|
||||
"prompt": "A cat is playing with a ball.",
|
||||
"size": "1920x1080",
|
||||
"with_audio": true
|
||||
}
|
||||
}
|
||||
}'
|
||||
|
||||
# 5. 查结果(替换下面的 <task_id>)
|
||||
curl -s -X POST http://localhost:3000/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 4,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "query_video",
|
||||
"arguments": { "task_id": "<task_id>" }
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
响应是 SSE 格式(`event: message` + `data: {...}`),用 `grep '^data:'` 抽 JSON 即可。
|
||||
|
||||
### 单独调试 `/upload`
|
||||
|
||||
```bash
|
||||
# 上传图片,返回 url
|
||||
curl -F "file=@/tmp/cat.png" https://video-gen.ai.foresh.com/upload
|
||||
# {"url":"https://video-gen.ai.foresh.com/uploads/xxx.png", ...}
|
||||
|
||||
# 下载上传过的文件
|
||||
curl -o cat.png https://video-gen.ai.foresh.com/uploads/xxx.png
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 架构与文件
|
||||
|
||||
```
|
||||
video_gen/
|
||||
├── Dockerfile # node:22-slim,安装依赖后启动 MCP HTTP server
|
||||
├── .dockerignore # 排除 node_modules
|
||||
package.json # @modelcontextprotocol/sdk + express + multer
|
||||
├── README.md # 本文件
|
||||
└── src/
|
||||
└── mcp-server.js # 单文件实现:HTTP server + MCP transport + 双工具
|
||||
```
|
||||
|
||||
### 关键设计
|
||||
|
||||
- **每 session 一个 transport + server**:通过 `Mcp-Session-Id` 复用,避免每请求重建
|
||||
- **Token 闭包注入**:在 Express 入口处从 header 提取 token,闭包传给 tool 回调;不依赖 SDK 内部字段
|
||||
- **AI 可见 vs AI 不可见**:
|
||||
- AI 只看到 `prompt / image_url / size / with_audio`
|
||||
- `model / quality / fps / watermark_enabled` 全部由 HTTP header 注入,避免 AI 误传或被诱导调高价档
|
||||
- **异步语义暴露给 AI**:服务端不做轮询,`video_gen` 只提交、`query_video` 只查一次。AI 自主决定节奏
|
||||
- **服务端自动 `request_id`**:每次提交服务端生成一个 UUID 作为智谱 `request_id`,便于日志关联,但不暴露给 AI
|
||||
- **上传接口与 MCP 走同一进程**:额外 `POST /upload` + `GET /uploads/*` 不走 MCP、不鉴权,靠内网隔离;`PUBLIC_BASE_URL` 环境变量控制返回 URL 的拼接 host,AI 看到的 tool description 启动时拼接进真实地址,避免 AI 自由发挥
|
||||
- **上传过期清理**:启动时 + 每小时一次删除 `mtime > UPLOAD_TTL_HOURS` 的文件,避免磁盘被不消费的上传堆满;清理 timer 用 `unref()` 不阻塞进程退出
|
||||
|
||||
### 默认模型
|
||||
|
||||
```js
|
||||
// src/mcp-server.js
|
||||
const DEFAULT_MODEL = "cogvideox-flash";
|
||||
```
|
||||
|
||||
修改后 `docker compose up -d --build mcp_video_gen` 即可生效。
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
**Q: AI 拿到 task_id 后立刻调 query_video 一直 PROCESSING,怎么办?**
|
||||
A: AI 自己 sleep 几秒再调(建议 2~5 秒间隔)。这是设计取舍:把节奏控制权交给 AI 模型,而不是服务端硬编码超时。
|
||||
|
||||
**Q: 视频 URL 多久过期?**
|
||||
A: 智谱签发 URL 默认 24h 左右。需要在收到后及时转存到自己的 OSS/S3。
|
||||
|
||||
**Q: 为什么 quality / fps 不让 AI 直接传?**
|
||||
A: 这些是技术档位,与成本/单价挂钩。让 AI 自主挑 `quality=quality + fps=60` 会爆账号预算。改成 header 由部署方注入更可控。
|
||||
|
||||
**Q: 能否限制 `size` 白名单?**
|
||||
A: 当前透传给智谱 API,不做强制校验。如需加白名单,修改 `src/mcp-server.js` 中 `video_gen` 的 `size` schema 即可。
|
||||
|
||||
**Q: 报错 `Missing valid Mcp-Session-Id`?**
|
||||
A: 漏发了 `Mcp-Session-Id` header,或调 `tools/call` 前没先 `initialize`。
|
||||
|
||||
**Q: 同一 task_id 能并发查吗?**
|
||||
A: 可以。`/async-result/{id}` 是幂等的读接口,并发无副作用。
|
||||
|
||||
**Q: 换模型需要重连 MCP session 吗?**
|
||||
A: 不需要。Model header 在每次 `tools/call` 都从当前请求读取。
|
||||
|
||||
**Q: 上传的图片多久过期?**
|
||||
A: 容器重启即丢(本服务不持久化)。请在上传后立刻把返回的 `url` 喂给 `video_gen.image_url`。URL 本身只有可访问性,不受单独 TTL 限制;但容器一旦重启,该文件物理消失,URL 变 404。
|
||||
|
||||
**Q: `PUBLIC_BASE_URL` 不设会怎样?**
|
||||
A: fallback 到 `http://localhost:3000`,AI 拿到的 curl 是 `http://localhost:3000/upload`。外网访问不到。本机调试无所谓,生产请显式设置。
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "mcp_video_gen",
|
||||
"version": "1.0.0",
|
||||
"description": "MCP server for video generation via Zhipu BigModel cogvideox-flash (text-to-video / image-to-video, async)",
|
||||
"type": "module",
|
||||
"main": "src/mcp-server.js",
|
||||
"scripts": {
|
||||
"start": "node src/mcp-server.js --http --port 3000 --host 0.0.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.0.4",
|
||||
"express": "^4.21.0",
|
||||
"multer": "^1.4.5-lts.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,623 @@
|
||||
#!/usr/bin/env node
|
||||
import express from "express";
|
||||
import multer from "multer";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { z } from "zod";
|
||||
|
||||
const ZHIPU_SUBMIT_URL =
|
||||
"https://open.bigmodel.cn/api/paas/v4/videos/generations";
|
||||
const ZHIPU_QUERY_URL = (id) =>
|
||||
`https://open.bigmodel.cn/api/paas/v4/async-result/${encodeURIComponent(id)}`;
|
||||
|
||||
// 默认模型:你说的 cogvideox-flash,文档枚举里也确认可用
|
||||
const DEFAULT_MODEL = "cogvideox-flash";
|
||||
|
||||
// AI 不可见的服务端默认(可被同名 header 覆盖)
|
||||
const DEFAULT_QUALITY = "speed"; // speed | quality
|
||||
const DEFAULT_FPS = 30; // 30 | 60
|
||||
const DEFAULT_WATERMARK_ENABLED = true; // 平台政策默认 true
|
||||
|
||||
// ===== 上传文件相关 =====
|
||||
// 公开的 base URL:拼接进 tool description,告诉 AI 该往哪里 POST 上传
|
||||
// 优先级:环境变量 > 默认 http://localhost:<port>
|
||||
function resolvePublicBaseUrl({ port, host }) {
|
||||
const env = (process.env.PUBLIC_BASE_URL || "").trim();
|
||||
if (env) return env.replace(/\/+$/, "");
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
|
||||
// 上传文件存储目录:容器内临时目录,服务重启即丢(你明确要求)
|
||||
const UPLOAD_DIR = process.env.UPLOAD_DIR || "/tmp/video_gen_uploads";
|
||||
const UPLOAD_PATH_PREFIX = "/uploads"; // 对外的 URL 路径前缀
|
||||
const UPLOAD_MAX_BYTES = 8 * 1024 * 1024; // 8MB(智谱 image_url 上限 5MB,留余量)
|
||||
const UPLOAD_ALLOWED_MIME = new Set([
|
||||
"image/png",
|
||||
"image/jpeg",
|
||||
"image/jpg",
|
||||
]);
|
||||
|
||||
// 过期清理:UPLOAD_TTL_HOURS 小时之前的文件会被定时删除
|
||||
const UPLOAD_TTL_HOURS = (() => {
|
||||
const n = parseInt(process.env.UPLOAD_TTL_HOURS || "24", 10);
|
||||
return Number.isFinite(n) && n > 0 ? n : 24;
|
||||
})();
|
||||
const UPLOAD_TTL_MS = UPLOAD_TTL_HOURS * 60 * 60 * 1000;
|
||||
const UPLOAD_CLEANUP_INTERVAL_MS = 60 * 60 * 1000; // 每小时一次
|
||||
|
||||
// 清理 UPLOAD_DIR 下 mtime 早于 TTL 的文件。返回 {kept, removed, errors}
|
||||
async function cleanupUploads({ now = Date.now(), dir = UPLOAD_DIR } = {}) {
|
||||
let entries;
|
||||
try {
|
||||
entries = await fs.promises.readdir(dir);
|
||||
} catch (e) {
|
||||
if (e && e.code === "ENOENT") return { kept: 0, removed: 0, errors: 0 };
|
||||
console.error(`[video_gen] cleanup: readdir failed: ${e.message}`);
|
||||
return { kept: 0, removed: 0, errors: 1 };
|
||||
}
|
||||
|
||||
let kept = 0;
|
||||
let removed = 0;
|
||||
let errors = 0;
|
||||
await Promise.all(
|
||||
entries.map(async (name) => {
|
||||
const full = path.join(dir, name);
|
||||
try {
|
||||
const st = await fs.promises.stat(full);
|
||||
if (!st.isFile()) return;
|
||||
if (now - st.mtimeMs >= UPLOAD_TTL_MS) {
|
||||
await fs.promises.unlink(full);
|
||||
removed++;
|
||||
} else {
|
||||
kept++;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e && e.code === "ENOENT") return;
|
||||
errors++;
|
||||
console.error(`[video_gen] cleanup: ${name}: ${e.message}`);
|
||||
}
|
||||
})
|
||||
);
|
||||
return { kept, removed, errors };
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {
|
||||
http: false,
|
||||
port: 3000,
|
||||
host: "0.0.0.0",
|
||||
};
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--http") args.http = true;
|
||||
else if (a === "--port") args.port = parseInt(argv[++i], 10);
|
||||
else if (a === "--host") args.host = argv[++i];
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function pickToken(headers) {
|
||||
if (!headers) return null;
|
||||
const candidates = [
|
||||
headers["x-video-gen-token"],
|
||||
headers["x-mcp-token"],
|
||||
headers["authorization"],
|
||||
];
|
||||
for (const v of candidates) {
|
||||
if (!v) continue;
|
||||
const s = String(v).trim();
|
||||
if (!s) continue;
|
||||
if (/^Bearer\s+/i.test(s)) {
|
||||
const t = s.replace(/^Bearer\s+/i, "").trim();
|
||||
if (t) return t;
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function pickModel(headers) {
|
||||
if (!headers) return null;
|
||||
const v =
|
||||
headers["x-video-gen-model"] ||
|
||||
headers["x-mcp-model"] ||
|
||||
headers["x-model"];
|
||||
if (!v) return null;
|
||||
const s = String(v).trim();
|
||||
return s || null;
|
||||
}
|
||||
|
||||
function pickHeaderString(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const s = String(v).trim();
|
||||
return s ? s : fallback;
|
||||
}
|
||||
|
||||
function pickHeaderInt(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const n = parseInt(String(v).trim(), 10);
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
}
|
||||
|
||||
function pickHeaderBool(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const s = String(v).trim().toLowerCase();
|
||||
if (s === "true" || s === "1" || s === "yes") return true;
|
||||
if (s === "false" || s === "0" || s === "no") return false;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
async function callZhipu({ url, token, method = "POST", body }) {
|
||||
const init = {
|
||||
method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
if (body !== undefined) init.body = JSON.stringify(body);
|
||||
const resp = await fetch(url, init);
|
||||
const text = await resp.text();
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch {
|
||||
data = { raw: text };
|
||||
}
|
||||
if (!resp.ok) {
|
||||
const err = new Error(
|
||||
`Zhipu API error: ${resp.status} ${resp.statusText} - ${text}`
|
||||
);
|
||||
err.status = resp.status;
|
||||
err.detail = data;
|
||||
throw err;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// ===== 工具:提交视频生成(同步只提交,立刻返回)=====
|
||||
async function submitVideo({
|
||||
token,
|
||||
model,
|
||||
prompt,
|
||||
image_url,
|
||||
size,
|
||||
quality,
|
||||
with_audio,
|
||||
fps,
|
||||
watermark_enabled,
|
||||
}) {
|
||||
// 平台要求 image_url 和 prompt 至少传一个
|
||||
const body = {
|
||||
model,
|
||||
quality,
|
||||
with_audio: Boolean(with_audio),
|
||||
watermark_enabled: Boolean(watermark_enabled),
|
||||
fps,
|
||||
// 由服务端生成唯一 request_id,便于日志关联;不暴露给 AI
|
||||
request_id: randomUUID(),
|
||||
};
|
||||
if (prompt) body.prompt = prompt;
|
||||
if (image_url) body.image_url = image_url;
|
||||
if (size) body.size = size;
|
||||
|
||||
const data = await callZhipu({
|
||||
url: ZHIPU_SUBMIT_URL,
|
||||
token,
|
||||
body,
|
||||
});
|
||||
return data; // { model, id, request_id, task_status }
|
||||
}
|
||||
|
||||
// ===== 工具:查询异步结果(单次拉取,不内部轮询)=====
|
||||
async function queryAsyncResult({ token, id }) {
|
||||
const data = await callZhipu({
|
||||
url: ZHIPU_QUERY_URL(id),
|
||||
token,
|
||||
method: "GET",
|
||||
});
|
||||
return data; // { id, request_id, model, task_status, video_result?, ... }
|
||||
}
|
||||
|
||||
function buildServer({ publicBaseUrl }) {
|
||||
const server = new McpServer(
|
||||
{
|
||||
name: "video_gen",
|
||||
version: "1.0.0",
|
||||
},
|
||||
{
|
||||
capabilities: {
|
||||
tools: {},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const uploadUrl = `${publicBaseUrl}/upload`;
|
||||
|
||||
// ---------- 工具 1:video_gen ----------
|
||||
server.tool(
|
||||
"video_gen",
|
||||
`Submit a video generation task (text-to-video or image-to-video).
|
||||
Returns the task_id immediately; poll the result later with query_video.
|
||||
prompt is required. Add image_url for image-to-video.
|
||||
|
||||
If you need to use a LOCAL image file as image_url, upload it FIRST with this exact command (replace the path):
|
||||
|
||||
curl -F "file=@/path/to/your/image.png" ${uploadUrl}
|
||||
|
||||
The response JSON has a "url" field — pass that URL directly as the image_url argument below.
|
||||
Supported formats: png, jpeg, jpg. Max 5MB per image.
|
||||
Uploaded files live in container temp storage and are discarded on restart — do not rely on long-term URLs.`,
|
||||
{
|
||||
prompt: z
|
||||
.string()
|
||||
.describe(
|
||||
"Video text description (max 512 chars). Required; when combined with image_url it serves as the motion/action description."
|
||||
),
|
||||
image_url: z
|
||||
.string()
|
||||
.url()
|
||||
.optional()
|
||||
.describe(
|
||||
"Reference image URL string for image-to-video. Image <= 5MB, .png/.jpeg/.jpg. Use the /upload endpoint to get a URL from a local file."
|
||||
),
|
||||
size: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Video size, e.g. 1920x1080, 1080x1920, 1024x1024, 1280x720, etc."
|
||||
),
|
||||
with_audio: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe("Whether to generate AI audio. Default false."),
|
||||
},
|
||||
async (args, extra) => {
|
||||
const headers = extra?.requestInfo?.headers;
|
||||
const token = pickToken(headers);
|
||||
if (!token) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Missing authentication token." }],
|
||||
};
|
||||
}
|
||||
|
||||
// AI 不可见:全部走 header
|
||||
const model = pickModel(headers) || DEFAULT_MODEL;
|
||||
const quality = pickHeaderString(headers, "x-video-gen-quality", DEFAULT_QUALITY);
|
||||
const fps = pickHeaderInt(headers, "x-video-gen-fps", DEFAULT_FPS);
|
||||
const watermark_enabled = pickHeaderBool(
|
||||
headers,
|
||||
"x-video-gen-watermark",
|
||||
DEFAULT_WATERMARK_ENABLED
|
||||
);
|
||||
|
||||
if (!args.prompt && !args.image_url) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Either prompt or image_url must be provided.",
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await submitVideo({
|
||||
token,
|
||||
model,
|
||||
prompt: args.prompt,
|
||||
image_url: args.image_url,
|
||||
size: args.size,
|
||||
quality,
|
||||
with_audio: args.with_audio ?? false,
|
||||
fps,
|
||||
watermark_enabled,
|
||||
});
|
||||
|
||||
const summary =
|
||||
`Model: ${model}\n` +
|
||||
`task_id: ${data.id}\n` +
|
||||
`request_id: ${data.request_id}\n` +
|
||||
`task_status: ${data.task_status}\n` +
|
||||
`\n` +
|
||||
`Video generation is asynchronous. Call query_video with task_id="${data.id}" to fetch the result.`;
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: summary }],
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Failed to submit video task: ${e.message}${
|
||||
e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""
|
||||
}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// ---------- 工具 2:query_video ----------
|
||||
server.tool(
|
||||
"query_video",
|
||||
"Query the status of a previously submitted video generation task. " +
|
||||
"Returns task_status and, if SUCCESS, the video URL(s).",
|
||||
{
|
||||
task_id: z
|
||||
.string()
|
||||
.min(1)
|
||||
.describe("The task id returned by video_gen."),
|
||||
},
|
||||
async (args, extra) => {
|
||||
const headers = extra?.requestInfo?.headers;
|
||||
const token = pickToken(headers);
|
||||
if (!token) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Missing authentication token." }],
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await queryAsyncResult({
|
||||
token,
|
||||
id: args.task_id,
|
||||
});
|
||||
|
||||
const status = data.task_status || "UNKNOWN";
|
||||
if (status === "SUCCESS") {
|
||||
const v = (data.video_result || [])[0];
|
||||
if (!v || !v.url) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Task succeeded but no video URL returned. Raw: ${JSON.stringify(data)}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
const summary =
|
||||
`task_id: ${data.id}\n` +
|
||||
`model: ${data.model}\n` +
|
||||
`task_status: SUCCESS\n` +
|
||||
`Video URL: ${v.url}\n` +
|
||||
(v.cover_image_url ? `Cover: ${v.cover_image_url}\n` : "");
|
||||
return {
|
||||
content: [
|
||||
{ type: "text", text: summary },
|
||||
{ type: "text", text: v.url },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// PROCESSING / FAIL / 其他:原样返回关键字段
|
||||
const errBlock = data.error
|
||||
? `\nerror: ${JSON.stringify(data.error)}`
|
||||
: "";
|
||||
return {
|
||||
isError: status === "FAIL",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`task_id: ${data.id}\n` +
|
||||
`model: ${data.model}\n` +
|
||||
`task_status: ${status}` +
|
||||
errBlock,
|
||||
},
|
||||
],
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Failed to query video task: ${e.message}${
|
||||
e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""
|
||||
}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
async function startHttp({ port, host }) {
|
||||
const app = express();
|
||||
app.use(express.json({ limit: "2mb" }));
|
||||
|
||||
const publicBaseUrl = resolvePublicBaseUrl({ port, host });
|
||||
console.log(`[video_gen] PUBLIC_BASE_URL = ${publicBaseUrl}`);
|
||||
|
||||
// ===== /upload:接收图片,返回 URL(不走 MCP)=====
|
||||
// 磁盘存储:保证 UPLOAD_DIR 存在(容器内临时,重启即丢)
|
||||
fs.mkdirSync(UPLOAD_DIR, { recursive: true });
|
||||
|
||||
// 过期清理:启动时跑一次,之后每小时一次
|
||||
cleanupUploads()
|
||||
.then((r) =>
|
||||
console.log(
|
||||
`[video_gen] startup cleanup: kept=${r.kept} removed=${r.removed} errors=${r.errors} ttl=${UPLOAD_TTL_HOURS}h`
|
||||
)
|
||||
)
|
||||
.catch((e) => console.error(`[video_gen] startup cleanup failed: ${e.message}`));
|
||||
const cleanupTimer = setInterval(() => {
|
||||
cleanupUploads()
|
||||
.then((r) => {
|
||||
if (r.removed > 0 || r.errors > 0) {
|
||||
console.log(
|
||||
`[video_gen] periodic cleanup: kept=${r.kept} removed=${r.removed} errors=${r.errors}`
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((e) => console.error(`[video_gen] periodic cleanup failed: ${e.message}`));
|
||||
}, UPLOAD_CLEANUP_INTERVAL_MS);
|
||||
cleanupTimer.unref(); // 不阻止进程退出
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: (_req, _file, cb) => cb(null, UPLOAD_DIR),
|
||||
filename: (_req, file, cb) => {
|
||||
const ext = (path.extname(file.originalname) || ".bin").toLowerCase();
|
||||
cb(null, `${randomUUID()}${ext}`);
|
||||
},
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: UPLOAD_MAX_BYTES, files: 1 },
|
||||
fileFilter: (_req, file, cb) => {
|
||||
if (UPLOAD_ALLOWED_MIME.has(file.mimetype)) return cb(null, true);
|
||||
cb(new Error(`Unsupported content-type: ${file.mimetype}. Allowed: ${[...UPLOAD_ALLOWED_MIME].join(", ")}`));
|
||||
},
|
||||
});
|
||||
|
||||
app.post("/upload", (req, res) => {
|
||||
upload.single("file")(req, res, (err) => {
|
||||
if (err) {
|
||||
const status = err.code === "LIMIT_FILE_SIZE" ? 413 : 400;
|
||||
return res.status(status).json({
|
||||
error: err.message || "upload failed",
|
||||
});
|
||||
}
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: "Missing form field 'file'" });
|
||||
}
|
||||
const url = `${publicBaseUrl}${UPLOAD_PATH_PREFIX}/${req.file.filename}`;
|
||||
return res.json({
|
||||
url,
|
||||
filename: req.file.filename,
|
||||
size: req.file.size,
|
||||
content_type: req.file.mimetype,
|
||||
original_name: req.file.originalname,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 静态下载:只要文件存在于 UPLOAD_DIR 就吐出去(不鉴权、不持久化)
|
||||
app.use(
|
||||
UPLOAD_PATH_PREFIX,
|
||||
express.static(UPLOAD_DIR, {
|
||||
fallthrough: false,
|
||||
maxAge: 0,
|
||||
})
|
||||
);
|
||||
|
||||
// session -> { server, transport } 映射
|
||||
const sessions = new Map();
|
||||
|
||||
app.get("/health", (_req, res) => {
|
||||
res.json({ status: "ok", service: "video_gen", sessions: sessions.size });
|
||||
});
|
||||
|
||||
async function handleSessionRequest(req, res) {
|
||||
const sessionId = req.headers["mcp-session-id"];
|
||||
|
||||
// 已有 session:复用 transport
|
||||
if (sessionId && sessions.has(sessionId)) {
|
||||
const { transport } = sessions.get(sessionId);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
}
|
||||
|
||||
// 新 session:必须是 initialize 请求
|
||||
if (!isInitializeRequest(req.body)) {
|
||||
res.status(400).json({
|
||||
jsonrpc: "2.0",
|
||||
error: {
|
||||
code: -32000,
|
||||
message: "Bad Request: missing valid Mcp-Session-Id for non-initialize request",
|
||||
},
|
||||
id: req.body?.id ?? null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const newId = randomUUID();
|
||||
const server = buildServer({ publicBaseUrl });
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => newId,
|
||||
onsessioninitialized: (id) => {
|
||||
sessions.set(id, { server, transport });
|
||||
},
|
||||
});
|
||||
|
||||
transport.onclose = () => {
|
||||
if (transport.sessionId) {
|
||||
sessions.delete(transport.sessionId);
|
||||
}
|
||||
};
|
||||
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
|
||||
// POST /mcp: 主入口
|
||||
app.post("/mcp", handleSessionRequest);
|
||||
|
||||
// GET /mcp & DELETE /mcp: 用于 SSE 流 / 会话关闭
|
||||
const handleOther = async (req, res) => {
|
||||
const sessionId = req.headers["mcp-session-id"];
|
||||
if (!sessionId || !sessions.has(sessionId)) {
|
||||
res.status(400).send("Invalid or missing session ID");
|
||||
return;
|
||||
}
|
||||
const { transport } = sessions.get(sessionId);
|
||||
await transport.handleRequest(req, res);
|
||||
};
|
||||
app.get("/mcp", handleOther);
|
||||
app.delete("/mcp", handleOther);
|
||||
|
||||
app.listen(port, host, () => {
|
||||
console.log(
|
||||
`[video_gen] MCP HTTP server listening on http://${host}:${port}/mcp`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function startStdio() {
|
||||
// stdio 模式下上传接口不可用,用 placeholder 避免 description 里出现奇怪的占位
|
||||
const server = buildServer({ publicBaseUrl: "http://localhost:3000" });
|
||||
const { StdioServerTransport } = await import(
|
||||
"@modelcontextprotocol/sdk/server/stdio.js"
|
||||
);
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("[video_gen] MCP stdio server started");
|
||||
}
|
||||
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.http) {
|
||||
startHttp({ port: args.port, host: args.host }).catch((e) => {
|
||||
console.error("[video_gen] failed to start http server:", e);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
startStdio().catch((e) => {
|
||||
console.error("[video_gen] failed to start stdio server:", e);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user