generate_image
Generate images from text descriptions, with automatic local saving and customizable aspect ratios, powered by MiniMax's image-01 model.
Instructions
使用 MiniMax image-01 生成图片,默认保存到本地
图片自动保存到配置的输出目录(默认 ~/Pictures/MiniMax)。
Args: prompt: 图片描述文本(英文效果最佳,中文也可) model: 模型名称,默认 image-01 aspect_ratio: 图片比例:1:1 / 16:9 / 9:16 / 3:4 / 4:3,默认 1:1 response_format: 默认 base64(自动存本地),改 url 则仅返回24h临时链接 n: 生成数量 1-3,默认 1 prompt_optimizer: 是否启用提示词优化,默认 true save_to_disk: 是否保存到本地(仅 base64 模式有效),默认 true
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | image-01 | |
| aspect_ratio | No | 1:1 | |
| response_format | No | base64 | |
| n | No | ||
| prompt_optimizer | No | ||
| save_to_disk | No |
Implementation Reference
- Core implementation of the generate_image tool. Handles parameter validation, calls the MiniMax API via MiniMaxClient.generate_image(), processes responses (URL or base64), and optionally saves base64 images to disk.
def generate_image( client: MiniMaxClient, prompt: str, model: str = "image-01", aspect_ratio: str = "1:1", response_format: str = "url", n: int = 1, prompt_optimizer: bool = True, save_to_disk: bool = True, ) -> dict: """使用 MiniMax image-01 模型生成图片 Args: client: MiniMax API 客户端 prompt: 图片描述文本(英文效果最佳,中文也可) model: 模型名称,默认 image-01 aspect_ratio: 图片比例:1:1 / 16:9 / 9:16 / 3:4 / 4:3 response_format: 返回格式:url(24h有效)或 base64 n: 生成数量 1-3 prompt_optimizer: 是否启用提示词优化 save_to_disk: base64 模式下是否自动保存到本地 Returns: { success: bool, images: [{url, saved_path, base64_preview}], model: str, request_id: str, } """ # 参数校验 if n not in (1, 2, 3): return {"success": False, "error": "n 必须为 1、2 或 3"} if aspect_ratio not in VALID_ASPECT_RATIOS: return {"success": False, "error": f"aspect_ratio 必须是: {', '.join(VALID_ASPECT_RATIOS)}"} if response_format not in VALID_FORMATS: return {"success": False, "error": f"response_format 必须是 'url' 或 'base64'"} if not prompt or not prompt.strip(): return {"success": False, "error": "prompt 不能为空"} # 调用 API result = client.generate_image( prompt=prompt.strip(), model=model, aspect_ratio=aspect_ratio, n=n, prompt_optimizer=prompt_optimizer, response_format=response_format, ) if not result.get("success"): return { "success": False, "error": result.get("error", "Image generation failed"), "detail": result.get("detail", ""), } # 解析响应 data = result.get("data", result) images_out = [] # 获取图片列表 if response_format == "url": img_list = data.get("image_urls", []) if isinstance(data, dict) else [] else: img_list = data.get("image_base64", []) if isinstance(data, dict) else [] if isinstance(img_list, str): img_list = [img_list] for img in img_list: item = {} if response_format == "url": url = img.get("url", img) if isinstance(img, dict) else str(img) item["url"] = url item["saved_path"] = "" else: b64 = img if isinstance(img, str) else (img.get("image_base64", "") or img.get("b64_img", "")) item["base64_preview"] = (b64[:40] + "...") if b64 else "" if save_to_disk and b64: try: raw = base64.b64decode(b64) filename = f"minimax_{uuid.uuid4().hex[:8]}.jpg" out_path = IMAGE_OUTPUT_DIR / filename out_path.write_bytes(raw) item["saved_path"] = str(out_path) except Exception as e: item["saved_path"] = "" item["save_error"] = str(e) images_out.append(item) return { "success": True, "images": images_out, "count": len(images_out), "model": result.get("model", model), "request_id": result.get("request_id", ""), } - Schema constants: VALID_ASPECT_RATIOS and VALID_FORMATS define the allowed input values for aspect_ratio and response_format parameters.
# 支持的参数范围 VALID_ASPECT_RATIOS = ("1:1", "16:9", "9:16", "3:4", "4:3") VALID_FORMATS = ("url", "base64") - src/minimax_mcp/server.py:111-144 (registration)MCP tool registration via @mcp.tool() decorator. The async function 'generate_image' on the server is decorated and delegates to the core handler in tools/image_generate.py.
@mcp.tool() def generate_image( prompt: str, model: str = "image-01", aspect_ratio: str = "1:1", response_format: str = "base64", n: int = 1, prompt_optimizer: bool = True, save_to_disk: bool = True, ) -> dict: """使用 MiniMax image-01 生成图片,默认保存到本地 图片自动保存到配置的输出目录(默认 ~/Pictures/MiniMax)。 Args: prompt: 图片描述文本(英文效果最佳,中文也可) model: 模型名称,默认 image-01 aspect_ratio: 图片比例:1:1 / 16:9 / 9:16 / 3:4 / 4:3,默认 1:1 response_format: 默认 base64(自动存本地),改 url 则仅返回24h临时链接 n: 生成数量 1-3,默认 1 prompt_optimizer: 是否启用提示词优化,默认 true save_to_disk: 是否保存到本地(仅 base64 模式有效),默认 true """ from minimax_mcp.tools.image_generate import generate_image as _run return _run( get_client(), prompt, model=model, aspect_ratio=aspect_ratio, response_format=response_format, n=n, prompt_optimizer=prompt_optimizer, save_to_disk=save_to_disk, ) - src/minimax_mcp/client.py:74-99 (handler)API client method generate_image on MiniMaxClient. Sends a POST request to {host}/v1/image_generation with model, prompt, aspect_ratio, n, prompt_optimizer, and response_format.
def generate_image( self, prompt: str, model: str = "image-01", aspect_ratio: str = "1:1", n: int = 1, prompt_optimizer: bool = True, response_format: str = "url", ) -> dict: """图片生成 POST {host}/v1/image_generation Body: {"model": "image-01", "prompt": "...", ...} """ return self._request( "POST", "/v1/image_generation", body={ "model": model, "prompt": prompt, "aspect_ratio": aspect_ratio, "n": n, "prompt_optimizer": prompt_optimizer, "response_format": response_format, }, ) - src/minimax_mcp/config.py:46-48 (helper)Configuration helper: IMAGE_OUTPUT_DIR defines where generated images are saved (default ~/Pictures/MiniMax).
# 输出目录 IMAGE_OUTPUT_DIR: Path = Path(_get("MINIMAX_IMAGE_OUTPUT_DIR", str(Path.home() / "Pictures" / "MiniMax"))).expanduser() IMAGE_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)