Media Creation MCP
# Media Creation MCP (`media-creation-mcp`)
> Production-grade **Model Context Protocol (MCP)** server for multimodal AI image & video creation, supporting both local **`stdio`** and remote **`Streamable HTTP + OAuth 2.0`** transports, stateful multi-shot storyboard continuity, idempotent async task execution, and AES-256-GCM encrypted credential storage.
面向多模态 AI 创作(文生图 / 图生视频 / 多参考图分镜编排)的生产级 MCP 服务端,兼容 **Claude Code / Claude Desktop / ChatGPT / Codex / Cherry Studio** 等主流 Agent 客户端。
---
## Core Highlights
- **Dual Transport + Full OAuth 2.0 (`stdio` & `Streamable HTTP`)**
- Local mode (`stdio`) for CLI / desktop agents using environment-injected API keys.
- Remote mode (`--http`) with OAuth 2.0 Dynamic Client Registration, PKCE (`S256`), CSRF-protected browser consent page, and strict Host/Origin validation.
- **Stateful Multi-Shot Continuity (`prepare_story_shot`)**
- Zero-cost, side-effect-free continuity planner that merges story brief, character reference sheets, and shot start/end states into deterministic generation payloads and returns `next_shot_context`.
- Solves character & visual drift in long multi-shot video workflows by persisting shot context into local project files instead of relying on fragile chat memory.
- **Idempotent Async Execution & Key-Fingerprint Recovery**
- Enforces client-supplied `operation_id` deduplication to prevent accidental duplicate billing/generations on retries.
- Binds async tasks to SHA-256 credential fingerprints so clients can resume polling (`get_task`) seamlessly across reconnects or credential alias renames.
- **Defense-in-Depth Security**
- **AES-256-GCM Encrypted Vault** (`lib/store.mjs`) with file locking (`proper-lockfile`), `0700` data directories, and `0600` file permissions; user API keys never leak to the LLM context.
- **SSRF & Attachment Guard** (`lib/attachment-download.mjs`) using `ipaddr.js` DNS pinning + private IP range blocking + streaming byte limits.
- **Signed Ephemeral Uploads** (`create_upload_link` / `get_upload`) for clients that cannot forward chat attachments directly to MCP tools.
---
## Architecture
```mermaid
flowchart LR
subgraph Clients["MCP Clients"]
C1["Claude Code / Codex (stdio)"]
C2["ChatGPT / Claude / Cherry Studio (HTTP + OAuth)"]
end
subgraph Server["media-creation-mcp (Node.js >= 22)"]
T1["MCP Server & Tool Router\n(lib/server.mjs)"]
OA["OAuth 2.0 + CSRF Consent\n(lib/oauth.mjs)"]
CT["Storyboard Continuity Planner\n(lib/continuity.mjs)"]
EN["Async Media Engine + Idempotency\n(lib/media.mjs)"]
UP["Signed Upload & SSRF Guard\n(lib/uploads.mjs, attachment-download.mjs)"]
ST["AES-256-GCM Vault + Lock\n(lib/store.mjs)"]
end
GW["OpenAI-Compatible Multimodal Gateway\n(Image / Video Models)"]
C1 --> T1
C2 --> OA --> T1
T1 --> CT
T1 --> EN
T1 --> UP
EN --> ST
OA --> ST
EN --> GW
```
---
## MCP Tools & Prompts
| Primitive | Type | Billing | Purpose |
| :--- | :--- | :--- | :--- |
| `list_media_models` | Tool | Free | Query available image/video models, pricing, and multi-reference limits per credential group |
| `prepare_story_shot` | Tool | Free | Merge brief, character references, and start/end states into next-shot generation params + `next_shot_context` |
| `generate_image` | Tool | Paid | Submit async image generation / multi-reference editing task with `operation_id` idempotency |
| `generate_video` | Tool | Paid | Submit async video generation task (first-frame or multi-reference) with `operation_id` idempotency |
| `get_task` | Tool | Read-only | Poll task progress and retrieve local file paths or signed ephemeral download links |
| `create_upload_link` | Tool (HTTP) | Free | Issue a 30-min signed web upload link (up to 14 images, 10 MB each) for clients without attachment forwarding |
| `get_upload` | Tool (HTTP) | Free | Resolve uploaded images into `upload:` references usable by generation tools |
| `make_short_video` | Prompt | Free | 6-step guided workflow: storyboard → cost estimate → character sheet → per-shot continuity → review → delivery |
---
## Quick Start
### Requirements
- Node.js `>= 22`
### Install & Run Tests
```bash
npm install
npm test
```
The test suite (`test/*.test.mjs`) covers:
- `media.test.mjs`: async submission, idempotent `operation_id`, key-fingerprint recovery, artifact expiry
- `continuity.test.mjs`: multi-shot character sheet & state carryover (`prepare_story_shot`)
- `references.test.mjs`: multi-reference image limits, first-frame conflict validation, upload flows
- `clients.test.mjs`: end-to-end `stdio` client & `Streamable HTTP + OAuth 2.0` handshake + CSRF/Origin defense
- `safety.test.mjs`: SSRF DNS/IP blocking, redirect guards, upload byte limits, disk capacity protection
### 1. Local `stdio` Mode (e.g. Claude Code / Claude Desktop)
```bash
export MEDIA_API_BASE_URL="https://your-gateway.example.com/v1"
export MEDIA_API_KEY="sk-your-key"
export MEDIA_MCP_DATA_DIR="$HOME/.local/share/media-creation-mcp"
node media-creation-mcp.mjs
```
Claude Code registration example:
```bash
claude mcp add --scope user --transport stdio media-creation -- node /abs/path/to/media-creation-mcp.mjs
```
### 2. Remote `Streamable HTTP + OAuth` Mode
```bash
export MEDIA_MCP_PUBLIC_URL="https://mcp.example.com"
export MEDIA_API_BASE_URL="https://api.example.com/v1"
export MEDIA_MCP_DATA_DIR="/var/lib/media-creation-mcp"
export MEDIA_MCP_STORAGE_KEY="$(openssl rand -base64 32)"
export PORT=18780
node media-creation-mcp.mjs --http
```
See [deploy/README.md](./deploy/README.md) for systemd and Nginx reverse-proxy templates.
---
## Project Structure
```text
media-creation-mcp/
├── media-creation-mcp.mjs # CLI entrypoint (stdio & --http server)
├── lib/
│ ├── server.mjs # MCP tool/prompt definitions & Express HTTP router
│ ├── media.mjs # Async media engine, idempotency, artifact lifecycle
│ ├── continuity.mjs # Multi-shot storyboard state & character continuity planner
│ ├── oauth.mjs # OAuth 2.0 provider, PKCE & CSRF-protected consent flow
│ ├── store.mjs # AES-256-GCM encrypted JSON vault with file locking
│ ├── uploads.mjs # Streaming multipart upload handler with byte caps
│ ├── attachment-download.mjs # SSRF-hardened attachment fetcher (DNS pinning + IP filter)
│ └── pages.mjs # HTML UI templates for OAuth consent & uploads
├── test/ # 6 end-to-end & security test suites (node --test)
└── deploy/ # systemd & Nginx production deployment templates
```
## License
MIT
TDQS
Scored across 5 tools
The five tools split cleanly along lifecycle lines: discovery (list_media_models), planning (prepare_story_shot), execution (generate_image/generate_video), and polling (get_task). The only soft spots are generate_image vs generate_video, which share reference_images semantics, estimated_cost_usd, and task_id return behavior, requiring prose warnings (e.g. 'do not call video for editing') to keep them apart.
All five names follow a strict verb_noun snake_case pattern (list_, prepare_, generate_, generate_, get_). Verb choice is also semantically consistent: list/get for reads, prepare for non-billable assembly, generate for billable creation, which reinforces the cost-safety contract.
Five tools is well-scoped for a media generation server: one discovery tool, one cost-free planning tool, two generation tools split by modality, and one task poller. No tool looks redundant, and the small surface keeps the 'only call when the user asks to create' rule enforceable.
The core loop is closed: find models and limits, assemble shot parameters, submit a billable job, poll for the artifact. Minor gaps remain—there is no tool to list recent tasks, cancel/retry a job, or query usage/billing records, even though the descriptions defer actual charges to '使用记录' that no tool exposes.