generate_video
Create videos from text prompts using AI, with options to customize duration, resolution, aspect ratio, and incorporate images or existing video content.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| image_path | No | ||
| image_url | No | ||
| video_path | No | ||
| video_url | No | ||
| duration | No | ||
| aspect_ratio | No | ||
| resolution | No | ||
| model | No | grok-imagine-video |
Implementation Reference
- src/server.py:64-111 (handler)The generate_video tool handler - generates videos using the XAI SDK. Accepts prompt, model, optional image/video inputs (from path or URL), and optional parameters for duration, aspect_ratio, and resolution. Encodes local files to base64, calls client.video.generate(), and returns the video URL and duration.@mcp.tool() async def generate_video( prompt: str, model: str = "grok-imagine-video", image_path: Optional[str] = None, image_url: Optional[str] = None, video_path: Optional[str] = None, video_url: Optional[str] = None, duration: Optional[int] = None, aspect_ratio: Optional[str] = None, resolution: Optional[str] = None ): client = Client(api_key=XAI_API_KEY) params = { "model": model, "prompt": prompt } if image_path: base64_string = encode_image_to_base64(image_path) ext = Path(image_path).suffix.lower().replace('.', '') params["image_url"] = f"data:image/{ext};base64,{base64_string}" elif image_url: params["image_url"] = image_url if video_path: base64_string = encode_video_to_base64(video_path) ext = Path(video_path).suffix.lower().replace('.', '') params["video_url"] = f"data:video/{ext};base64,{base64_string}" elif video_url: params["video_url"] = video_url if duration: params["duration"] = duration if aspect_ratio: params["aspect_ratio"] = aspect_ratio if resolution: params["resolution"] = resolution response = client.video.generate(**params) client.close() return { "url": response.url, "duration": response.duration if hasattr(response, 'duration') else None }
- src/server.py:64-64 (registration)Tool registration via @mcp.tool() decorator at line 64, which registers the generate_video function as an available MCP tool.@mcp.tool()
- src/utils.py:13-18 (helper)encode_image_to_base64 helper function - reads an image file from disk and converts it to base64 string for transmission to the XAI API.def encode_image_to_base64(image_path: str): path = Path(image_path) if not path.exists(): raise FileNotFoundError(f"Image file not found: {image_path}") with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8")
- src/utils.py:21-26 (helper)encode_video_to_base64 helper function - reads a video file from disk and converts it to base64 string for transmission to the XAI API.def encode_video_to_base64(video_path: str): path = Path(video_path) if not path.exists(): raise FileNotFoundError(f"Video file not found: {video_path}") with open(video_path, "rb") as video_file: return base64.b64encode(video_file.read()).decode("utf-8")