generate_image
Generate images from text prompts using AI models. Specify prompts, models, aspect ratios, and output formats to create visual content.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | grok-imagine-image | |
| image_path | No | ||
| image_url | No | ||
| n | No | ||
| image_format | No | url | |
| aspect_ratio | No |
Implementation Reference
- src/server.py:35-68 (handler)The generate_image handler function that executes the image generation logic using the xai_sdk Client. It accepts prompt, model, image_path, image_url, n, image_format, and aspect_ratio parameters, encodes local images to base64 if needed, calls the API, and returns formatted results with URLs.@mcp.tool() async def generate_image( prompt: str, model: str = "grok-imagine-image", image_path: Optional[str] = None, image_url: Optional[str] = None, n: int = 1, image_format: str = "url", aspect_ratio: Optional[str] = None ): client = Client(api_key=XAI_API_KEY) params = {"model": model, "prompt": prompt, "n": n, "image_format": image_format} 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 aspect_ratio: params["aspect_ratio"] = aspect_ratio images = client.image.sample_batch(**params) client.close() result = ["## Generated Image(s)"] for i, img in enumerate(images, 1): result.append(f"\n**Image {i}:** {img.url}") if img.prompt and img.prompt != prompt: result.append(f"*Revised prompt:* {img.prompt}") return "\n".join(result)
- src/utils.py:13-18 (helper)Helper function that encodes an image file to base64 string format for API transmission. Used by generate_image to handle local image files.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")