generate_image
Create images from text prompts with customizable styles, variations, and output formats for visual content generation.
Instructions
Generate single or multiple images from text prompts with style and variation options
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | The text prompt describing the image to generate | |
| outputCount | No | Number of variations to generate (1-8, default: 1) | |
| styles | No | Array of artistic styles: photorealistic, watercolor, oil-painting, sketch, pixel-art, anime, vintage, modern, abstract, minimalist | |
| variations | No | Array of variation types: lighting, angle, color-palette, composition, mood, season, time-of-day | |
| format | No | Output format: separate files or single grid image | separate |
| seed | No | Seed for reproducible variations | |
| preview | No | Automatically open generated images in default viewer |
Implementation Reference
- mcp-server/src/server.ts:121-172 (schema)Tool schema definition including input parameters for generate_image tool (prompt, outputCount, styles, variations, etc.) used in ListTools response.{ name: "generate_image", description: "Generate single or multiple images from text prompts with style and variation options", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "The text prompt describing the image to generate", }, outputCount: { type: "number", description: "Number of variations to generate (1-8, default: 1)", minimum: 1, maximum: 8, default: 1, }, styles: { type: "array", items: { type: "string" }, description: "Array of artistic styles: photorealistic, watercolor, oil-painting, sketch, pixel-art, anime, vintage, modern, abstract, minimalist", }, variations: { type: "array", items: { type: "string" }, description: "Array of variation types: lighting, angle, color-palette, composition, mood, season, time-of-day", }, format: { type: "string", enum: ["grid", "separate"], description: "Output format: separate files or single grid image", default: "separate", }, seed: { type: "number", description: "Seed for reproducible variations", }, preview: { type: "boolean", description: "Automatically open generated images in default viewer", default: false, }, }, required: ["prompt"], }, },
- mcp-server/src/server.ts:494-512 (handler)MCP CallToolRequest handler case for 'generate_image': parses arguments, builds ImageGenerationRequest, and delegates execution to ImageGenerator.generateTextToImage().case "generate_image": { const imageRequest: ImageGenerationRequest = { prompt: args?.prompt as string, outputCount: (args?.outputCount as number) || 1, mode: "generate", styles: args?.styles as string[], variations: args?.variations as string[], format: (args?.format as "grid" | "separate") || "separate", seed: args?.seed as number, preview: args?.preview as boolean, fileFormat: (args?.fileFormat as "png" | "jpeg") || "png", noPreview: (args?.noPreview as boolean) || (args?.["no-preview"] as boolean), }; response = await this.imageGenerator.generateTextToImage(imageRequest); break; }
- mcp-server/src/imageGenerator.ts:532-638 (handler)Core handler function implementing generate_image tool logic: builds batch prompts, calls OpenRouter API via postJson, parses base64 image data, saves files using FileHandler, handles multiple variations, previews, and returns generated files list.async generateTextToImage( request: ImageGenerationRequest ): Promise<ImageGenerationResponse> { try { const outputPath = FileHandler.ensureOutputDirectory(); const generatedFiles: string[] = []; const prompts = this.buildBatchPrompts(request); let firstError: string | null = null; const fileFormat = this.resolveFileFormat(request); logger.debug(`Generating ${prompts.length} image variation(s)`); for (let i = 0; i < prompts.length; i++) { const currentPrompt = prompts[i]; logger.debug( `Generating variation ${i + 1}/${prompts.length}:`, currentPrompt ); try { const payload: Record<string, unknown> = { model: this.modelName, input: [ { role: "user", content: [ { type: "input_text", text: currentPrompt, }, ], }, ], }; if (request.seed !== undefined) { payload.seed = request.seed; } const response = await this.postJson<OpenRouterImageResponse>( this.generationPath, payload ); const imageBase64 = this.parseImageFromResponse(response); if (imageBase64) { const filename = FileHandler.generateFilename( request.styles || request.variations ? currentPrompt : request.prompt, fileFormat, i ); const fullPath = await FileHandler.saveImageFromBase64( imageBase64, outputPath, filename ); generatedFiles.push(fullPath); logger.debug("Image saved to:", fullPath); } else { logger.warn("No valid image data found in OpenRouter response"); } } catch (error: unknown) { const errorMessage = this.handleApiError(error); if (!firstError) { firstError = errorMessage; } logger.warn(`Error generating variation ${i + 1}:`, errorMessage); if (errorMessage.toLowerCase().includes("authentication failed")) { return { success: false, message: "Image generation failed", error: errorMessage, }; } } } if (generatedFiles.length === 0) { return { success: false, message: "Failed to generate any images", error: firstError || "No image data returned from OpenRouter. Try adjusting your prompt.", }; } await this.handlePreview(generatedFiles, request); return { success: true, message: `Successfully generated ${generatedFiles.length} image variation(s)`, generatedFiles, }; } catch (error: unknown) { logger.error("Error in generateTextToImage:", error); return { success: false, message: "Failed to generate image", error: this.handleApiError(error), }; } }
- Helper utility to generate multiple prompt variations from styles, variations parameters, and outputCount for batch image generation.private buildBatchPrompts(request: ImageGenerationRequest): string[] { const prompts: string[] = []; const basePrompt = request.prompt; if (!request.styles && !request.variations && !request.outputCount) { return [basePrompt]; } if (request.styles && request.styles.length > 0) { for (const style of request.styles) { prompts.push(`${basePrompt}, ${style} style`); } } if (request.variations && request.variations.length > 0) { const basePrompts = prompts.length > 0 ? prompts : [basePrompt]; const variationPrompts: string[] = []; for (const baseP of basePrompts) { for (const variation of request.variations) { switch (variation) { case "lighting": variationPrompts.push(`${baseP}, dramatic lighting`); variationPrompts.push(`${baseP}, soft lighting`); break; case "angle": variationPrompts.push(`${baseP}, from above`); variationPrompts.push(`${baseP}, close-up view`); break; case "color-palette": variationPrompts.push(`${baseP}, warm color palette`); variationPrompts.push(`${baseP}, cool color palette`); break; case "composition": variationPrompts.push(`${baseP}, centered composition`); variationPrompts.push(`${baseP}, rule of thirds composition`); break; case "mood": variationPrompts.push(`${baseP}, cheerful mood`); variationPrompts.push(`${baseP}, dramatic mood`); break; case "season": variationPrompts.push(`${baseP}, in spring`); variationPrompts.push(`${baseP}, in winter`); break; case "time-of-day": variationPrompts.push(`${baseP}, at sunrise`); variationPrompts.push(`${baseP}, at sunset`); break; default: variationPrompts.push(`${baseP}, ${variation}`); break; } } } if (variationPrompts.length > 0) { prompts.splice(0, prompts.length, ...variationPrompts); } } if ( prompts.length === 0 && request.outputCount && request.outputCount > 1 ) { for (let i = 0; i < request.outputCount; i++) { prompts.push(basePrompt); } } if (request.outputCount && prompts.length > request.outputCount) { prompts.splice(request.outputCount); } return prompts.length > 0 ? prompts : [basePrompt]; }