详细教程:构建一个 MCP Server,将静态图片创意简报自动分解为结构化图片转视频提示词和含连续性检查的镜头计划,支持 stdio 传输对接任意 MCP 客户端。
将一张静态图片转化为一段令人信服的短视频,通常不在于写更长的 prompt,而在于把创作决策清晰地分开。主体需要一个主要动作,相机需要一个克制的指令,环境应该支持而非抢了主体动作的风头。
在本教程中,我们将构建一个小型 Model Context Protocol(MCP)服务器,它可以将一份粗略的创作简报转化为两个有用的输出:
一个结构化的 image-to-video prompt
一个包含连续性和迭代检查的镜头计划
该服务器通过 stdio 运行,因此可以被任何支持本地进程的 MCP 客户端使用。
mkdir image-to-video-mcp
cd image-to-video-mcp
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node
在 package.json 中使用 ESM 并添加构建脚本:
{
"type": "module",
"scripts": {
"build": "tsc"
}
}
一个最小的 tsconfig.json 可以面向现代 Node.js:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
从 stdio transport 和列出及调用工具所需的请求模式开始:
import {
CallToolRequestSchema,
ListToolsRequestSchema,
Tool,
} from "@modelcontextprotocol/sdk/types.js";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server(
{ name: "image-to-video-ai", version: "0.1.0" },
{ capabilities: { tools: {} } },
);
重要的设计选择是保持服务器的确定性。它不生成媒体或请求私人凭证。它将用户的意图组织成视频模型可以更可靠地遵循的格式。
第一个工具接受一个主体和主要动作,然后添加可选的制作控制:
const buildPromptTool: Tool = {
name: "image_to_video_build_prompt",
description: "Build a production-ready image-to-video motion prompt.",
inputSchema: {
type: "object",
properties: {
subject: { type: "string" },
motion: { type: "string" },
environment: { type: "string" },
camera: { type: "string" },
lighting: { type: "string" },
timing: { type: "string" },
style: { type: "string" },
avoid: { type: "string" },
},
required: ["subject", "motion"],
},
};
一个辅助函数在可选字段为空时将其排除在最终 prompt 之外:
function optionalLine(label: string, value: unknown): string {
return typeof value === "string" && value.trim()
? `${label}: ${value.trim()}`
: "";
}
请求处理器可以按稳定的顺序组装 prompt:
function buildPrompt(args: Record<string, unknown>): string {
const subject = String(args.subject || "").trim();
const motion = String(args.motion || "").trim();
if (!subject || !motion) {
throw new Error("subject and motion are required");
}
return [
`Subject: ${subject}`,
`Primary motion: ${motion}`,
optionalLine("Environment", args.environment),
optionalLine("Camera", args.camera),
optionalLine("Lighting", args.lighting),
optionalLine("Timing", args.timing),
optionalLine("Style", args.style),
optionalLine("Avoid", args.avoid),
"Preserve subject identity, coherent anatomy, stable geometry, and consistent lighting across every frame",
].filter(Boolean).join(". ");
}
这个排序很重要。主体和动作排在前面,因为它们定义了视觉目标。相机和环境动作作为支持指令放在后面。伪影约束放在最后。
第二个工具应该帮助用户在花费生成积分之前推理这个片段:
const shotPlanTool: Tool = {
name: "image_to_video_plan",
description: "Create a concise shot plan with continuity and iteration checks.",
inputSchema: {
type: "object",
properties: {
goal: { type: "string" },
image_description: { type: "string" },
duration_seconds: {
type: "number",
minimum: 2,
maximum: 30,
default: 5,
},
aspect_ratio: {
type: "string",
enum: ["16:9", "9:16", "1:1", "4:3", "3:4"],
default: "16:9",
},
end_frame_description: { type: "string" },
},
required: ["goal", "image_description"],
},
};
计划应该足够简短以便于浏览,但又足够具体以捕捉冲突指令。一个有用的响应包括:
最好的第一次测试一次只改变一个变量。如果主体动作错了,不要同时更改相机、风格、时间和光照。可控的迭代使失败可诊断。
从列表处理器返回两个工具:
const tools = [buildPromptTool, shotPlanTool];
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools,
}));
然后按工具名称路由调用,并将文本包装在 MCP content 格式中:
function textResult(text: string) {
return {
content: [{ type: "text" as const, text }],
};
}
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args = {} } = request.params;
if (name === "image_to_video_build_prompt") {
return textResult(buildPrompt(args));
}
if (name === "image_to_video_plan") {
return textResult(createShotPlan(args));
}
throw new Error(`Unknown tool: ${name}`);
});
最后,连接 stdio transport:
const transport = new StdioServerTransport();
await server.connect(transport);
用 npm run build 编译项目。在开发过程中,通过 MCP inspector 测试服务器,或直接在客户端中配置:
{
"mcpServers": {
"image-to-video-ai": {
"command": "npx",
"args": ["-y", "mcp-imagetovideoai-server"]
}
}
}
这样的工具在一致地强制执行一些约束时最有用:
例如,一个产品镜头可以指定缓慢的瓶子旋转、轻轻的推拉、稳定的标签排版和柔和的移动反光。这比包含几个不相关电影动作的段落更容易让模型解释。
一旦计划稳定了,你可以把它通过 Image to Video AI 生成器运行,并在每次尝试中不必重写创作简报的情况下比较模型输出。
这个服务器可以扩展而不会变成媒体处理后端。有用的添加包括:
关键在于保持边界清晰:MCP 服务器结构化决策,而视频平台执行生成。这种分离使工具更容易审计、在本地运行更安全,并且在不同的 image-to-video 模型中都有用。