edit_image
Modify existing images using text prompts to apply changes, adjustments, or transformations based on your specific requirements.
Instructions
Edit an existing image based on a text prompt
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | The text prompt describing the edits to make | |
| file | Yes | The filename of the input image to edit | |
| preview | No | Automatically open generated images in default viewer |
Implementation Reference
- mcp-server/src/imageGenerator.ts:800-891 (handler)Core implementation of the edit_image tool. Loads the input image as base64 data URL, sends it along with the edit prompt to the Gemini model via OpenRouter API, parses the response image, saves it as PNG, and handles preview.async editImage( request: ImageGenerationRequest ): Promise<ImageGenerationResponse> { try { if (!request.inputImage) { return { success: false, message: "Input image file is required for editing", error: "Missing inputImage parameter", }; } const fileResult = FileHandler.findInputFile(request.inputImage); if (!fileResult.found) { return { success: false, message: `Input image not found: ${request.inputImage}`, error: `Searched in: ${fileResult.searchedPaths.join(", ")}`, }; } const outputPath = FileHandler.ensureOutputDirectory(); const imageBase64 = await FileHandler.readImageAsBase64( fileResult.filePath! ); const fileName = path.basename(fileResult.filePath!); const mimeType = this.detectMimeType(fileName); const dataUrl = `data:${mimeType};base64,${imageBase64}`; const payload: Record<string, unknown> = { model: this.modelName, input: [ { role: "user", content: [ { type: "input_text", text: request.prompt, }, ], }, ], images: [dataUrl], }; if (request.seed !== undefined) { payload.seed = request.seed; } const response = await this.postJson<OpenRouterImageResponse>( this.generationPath, payload ); const imageBase64Result = this.parseImageFromResponse(response); if (!imageBase64Result) { return { success: false, message: `Failed to ${request.mode} image`, error: "No image data returned in OpenRouter response", }; } const filename = FileHandler.generateFilename( `${request.mode}_${request.prompt}`, "png", 0 ); const fullPath = await FileHandler.saveImageFromBase64( imageBase64Result, outputPath, filename ); await this.handlePreview([fullPath], request); return { success: true, message: `Successfully ${request.mode}d image`, generatedFiles: [fullPath], }; } catch (error: unknown) { logger.error(`Error in ${request.mode}Image:`, error); return { success: false, message: `Failed to ${request.mode} image`, error: this.handleApiError(error), }; } }
- mcp-server/src/server.ts:173-196 (registration)Registration of the 'edit_image' tool in the MCP server's listTools handler, including name, description, and input schema definition.{ name: "edit_image", description: "Edit an existing image based on a text prompt", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "The text prompt describing the edits to make", }, file: { type: "string", description: "The filename of the input image to edit", }, preview: { type: "boolean", description: "Automatically open generated images in default viewer", default: false, }, }, required: ["prompt", "file"], }, },
- mcp-server/src/server.ts:514-529 (handler)Dispatch handler in the MCP server's CallToolRequestSchema that maps 'edit_image' tool calls to ImageGenerator.editImage method.case "edit_image": case "restore_image": { const mode = name === "edit_image" ? "edit" : "restore"; const imageRequest: ImageGenerationRequest = { prompt: args?.prompt as string, inputImage: args?.file as string, mode, seed: args?.seed as number, preview: args?.preview as boolean, noPreview: (args?.noPreview as boolean) || (args?.["no-preview"] as boolean), }; response = await this.imageGenerator.editImage(imageRequest); break; }