| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Annotated, Any, Literal |
| 4 | |
| 5 | from mcp.server import MCPServer |
| 6 | from mcp.types import ToolAnnotations |
| 7 | from pydantic import Field, StrictBool |
| 8 | |
| 9 | from .mcp_jobs import MCPJobs |
| 10 | |
| 11 | INSTRUCTIONS = ( |
| 12 | "ViMax is a video production tool. Ask the user to confirm the chosen workflow and " |
| 13 | "source before starting planning. Ask separately before starting a render; planning " |
| 14 | "approval never authorizes paid image/video generation. Set confirmed=true only after " |
| 15 | "that explicit user approval. Poll vimax_status using the returned job ID, and present " |
| 16 | "the resulting artifacts. Do not start another job just to check progress. " |
| 17 | "Use vimax_agent when the user wants to delegate a chat turn or draft to the ViMax " |
| 18 | "agent itself; it cannot run the planning or rendering pipelines." |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | def build_server(jobs: MCPJobs) -> MCPServer: |
| 23 | server = MCPServer("ViMax", instructions=INSTRUCTIONS) |
| 24 | |
| 25 | @server.tool(annotations=ToolAnnotations(read_only_hint=False, destructive_hint=False, idempotent_hint=False, open_world_hint=True)) |
| 26 | def vimax_plan( |
| 27 | workflow: Literal["idea2video", "script2video", "novel2video"], |
| 28 | source: Annotated[str, Field(min_length=1, max_length=200_000)], |
| 29 | confirmed: StrictBool = False, |
| 30 | project_name: Annotated[str, Field(max_length=64)] = "", |
| 31 | requirements: Annotated[str, Field(max_length=4096)] = "", |
| 32 | style: Annotated[str, Field(max_length=1024)] = "", |
| 33 | ) -> dict[str, Any]: |
| 34 | """Start text-only storyboard planning in a new ViMax project after explicit user confirmation.""" |
| 35 | if not confirmed: |
| 36 | return {"status": "confirmation_required", "message": "Ask the user to confirm the workflow and source before planning."} |
| 37 | if not source.strip(): |
| 38 | return {"status": "invalid_input", "message": "Provide a nonempty idea, script, or novel excerpt."} |
| 39 | return jobs.start("plan", { |
| 40 | "workflow": workflow, |
| 41 | "source": source.strip(), |
| 42 | "project_name": project_name.strip(), |
| 43 | "requirements": requirements, |
| 44 | "style": style, |
| 45 | }) |
| 46 | |
| 47 | @server.tool(annotations=ToolAnnotations(read_only_hint=False, destructive_hint=False, idempotent_hint=False, open_world_hint=True)) |
| 48 | def vimax_render(session_id: str, confirmed: StrictBool = False) -> dict[str, Any]: |
| 49 | """Start a background video render for an existing project after separate user confirmation.""" |
| 50 | if not confirmed: |
| 51 | return {"status": "confirmation_required", "message": "Ask the user to confirm rendering and provider usage separately."} |
| 52 | try: |
| 53 | jobs.project(session_id) |
| 54 | except (ValueError, KeyError): |
| 55 | return {"status": "not_found", "message": "No project with that ViMax session ID exists in this workspace."} |
| 56 | return jobs.start("render", {"session_id": session_id}) |
| 57 | |
| 58 | @server.tool(annotations=ToolAnnotations(read_only_hint=False, destructive_hint=False, idempotent_hint=False, open_world_hint=True)) |
| 59 | def vimax_agent( |
| 60 | prompt: Annotated[str, Field(min_length=1, max_length=32_000)], |
| 61 | session_id: str = "", |
| 62 | ) -> dict[str, Any]: |
| 63 | """Delegate one chat turn to the ViMax agent for ideas, drafts, or project artifact review. Uses the ViMax text model; cannot plan or render.""" |
| 64 | if not prompt.strip(): |
| 65 | return {"status": "invalid_input", "message": "Provide a nonempty prompt."} |
| 66 | if session_id: |
| 67 | try: |
| 68 | jobs.project(session_id) |
| 69 | except (ValueError, KeyError): |
| 70 | return {"status": "not_found", "message": "No project with that ViMax session ID exists in this workspace."} |
| 71 | return jobs.start("agent", {"session_id": session_id, "prompt": prompt.strip()}) |
| 72 | |
| 73 | @server.tool(annotations=ToolAnnotations(read_only_hint=True, open_world_hint=False)) |
| 74 | def vimax_status(job_id: str = "", session_id: str = "") -> dict[str, Any]: |
| 75 | """Inspect a job's progress and artifacts by job_id, or list an existing project's artifacts by session_id.""" |
| 76 | if bool(job_id) == bool(session_id): |
| 77 | return {"status": "invalid_input", "message": "Provide exactly one of job_id or session_id."} |
| 78 | try: |
| 79 | state = jobs.read(job_id) if job_id else {"session_id": session_id, "status": jobs.project(session_id)["stage"]} |
| 80 | except (ValueError, FileNotFoundError, KeyError): |
| 81 | return {"status": "not_found", "message": "No matching job or project in this workspace."} |
| 82 | if state["session_id"]: |
| 83 | try: |
| 84 | state.update(jobs.artifacts(state["session_id"])) |
| 85 | except (ValueError, KeyError): |
| 86 | state.update(artifacts=[], artifacts_truncated=False) |
| 87 | return state |
| 88 | |
| 89 | return server |
| 90 |