Skip to main content
Glama
Garoth
by Garoth

edit_image

Modify existing images using text prompts with DALL-E AI, allowing users to add, remove, or alter elements while preserving specified areas through masking.

Instructions

Edit an existing image using DALL-E based on a text prompt

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYesText description of the desired edits
imagePathYesPath to the image to edit
maskNoPath to the mask image (white areas will be edited, black areas preserved)
modelNoDALL-E model to use (currently only dall-e-2 supports editing)
sizeNoSize of the generated image
nNoNumber of images to generate (1-10)
saveDirNoDirectory to save the edited images
fileNameNoBase filename for the edited images (without extension)

Implementation Reference

  • The main tool handler function for 'edit_image'. Resolves image and mask paths to absolute, calls dalleService.editImage with parameters, handles success/error responses, and formats the ToolResponse with saved image paths.
    handler: async (args: EditImageArgs): Promise<ToolResponse> => {
      // Resolve relative paths to absolute paths
      const imagePath = path.isAbsolute(args.imagePath) 
        ? args.imagePath 
        : path.resolve(process.cwd(), args.imagePath);
      
      const mask = args.mask && !path.isAbsolute(args.mask)
        ? path.resolve(process.cwd(), args.mask)
        : args.mask;
    
      const result = await dalleService.editImage(args.prompt, imagePath, {
        mask,
        model: args.model,
        size: args.size,
        n: args.n,
        saveDir: args.saveDir,
        fileName: args.fileName
      });
    
      if (!result.success) {
        return {
          content: [{
            type: "text",
            text: `Error editing image: ${result.error}`
          }]
        };
      }
    
      const imagePaths = result.imagePaths || [];
      const imageCount = imagePaths.length;
      const model = result.model || 'dall-e-2';
    
      let responseText = `Successfully edited image and generated ${imageCount} variation${imageCount !== 1 ? 's' : ''} using ${model}.\n\n`;
      responseText += `Original image: ${imagePath}\n`;
      if (mask) {
        responseText += `Mask: ${mask}\n`;
      }
      responseText += `Prompt: "${result.prompt}"\n\n`;
      responseText += `Edited image${imageCount !== 1 ? 's' : ''} saved to:\n`;
      
      imagePaths.forEach(imagePath => {
        responseText += `- ${imagePath}\n`;
      });
    
      return {
        content: [{
          type: "text",
          text: responseText
        }]
      };
    }
  • TypeScript interface EditImageArgs defining the input schema for the edit_image tool, including required prompt and imagePath, optional mask, model, size, n, saveDir, fileName.
    export interface EditImageArgs {
      prompt: string;
      imagePath: string;
      mask?: string;
      model?: string;
      size?: string;
      n?: number;
      saveDir?: string;
      fileName?: string;
    }
  • Full tool object definition and registration in the exported tools array, specifying name 'edit_image', description, inputSchema (JSON schema mirroring EditImageArgs), and handler reference.
    {
      name: "edit_image",
      description: "Edit an existing image using DALL-E based on a text prompt",
      inputSchema: {
        type: "object",
        properties: {
          prompt: {
            type: "string",
            description: "Text description of the desired edits"
          },
          imagePath: {
            type: "string",
            description: "Path to the image to edit"
          },
          mask: {
            type: "string",
            description: "Path to the mask image (white areas will be edited, black areas preserved)"
          },
          model: {
            type: "string",
            description: "DALL-E model to use (currently only dall-e-2 supports editing)",
            enum: ["dall-e-2"]
          },
          size: {
            type: "string",
            description: "Size of the generated image",
            enum: ["256x256", "512x512", "1024x1024"]
          },
          n: {
            type: "number",
            description: "Number of images to generate (1-10)",
            minimum: 1,
            maximum: 10
          },
          saveDir: {
            type: "string",
            description: "Directory to save the edited images"
          },
          fileName: {
            type: "string",
            description: "Base filename for the edited images (without extension)"
          }
        },
        required: ["prompt", "imagePath"]
      },
      handler: async (args: EditImageArgs): Promise<ToolResponse> => {
        // Resolve relative paths to absolute paths
        const imagePath = path.isAbsolute(args.imagePath) 
          ? args.imagePath 
          : path.resolve(process.cwd(), args.imagePath);
        
        const mask = args.mask && !path.isAbsolute(args.mask)
          ? path.resolve(process.cwd(), args.mask)
          : args.mask;
    
        const result = await dalleService.editImage(args.prompt, imagePath, {
          mask,
          model: args.model,
          size: args.size,
          n: args.n,
          saveDir: args.saveDir,
          fileName: args.fileName
        });
    
        if (!result.success) {
          return {
            content: [{
              type: "text",
              text: `Error editing image: ${result.error}`
            }]
          };
        }
    
        const imagePaths = result.imagePaths || [];
        const imageCount = imagePaths.length;
        const model = result.model || 'dall-e-2';
    
        let responseText = `Successfully edited image and generated ${imageCount} variation${imageCount !== 1 ? 's' : ''} using ${model}.\n\n`;
        responseText += `Original image: ${imagePath}\n`;
        if (mask) {
          responseText += `Mask: ${mask}\n`;
        }
        responseText += `Prompt: "${result.prompt}"\n\n`;
        responseText += `Edited image${imageCount !== 1 ? 's' : ''} saved to:\n`;
        
        imagePaths.forEach(imagePath => {
          responseText += `- ${imagePath}\n`;
        });
    
        return {
          content: [{
            type: "text",
            text: responseText
          }]
        };
      }
  • DalleService.editImage helper method implementing the core logic: validates files, prepares multipart form data with image/mask, calls OpenAI /images/edits API, decodes b64 images, saves to disk, returns ImageGenerationResult.
    async editImage(
      prompt: string,
      imagePath: string,
      options: {
        mask?: string;
        model?: string;
        size?: string;
        n?: number;
        saveDir?: string;
        fileName?: string;
      } = {}
    ): Promise<ImageGenerationResult> {
      try {
        // Set default options
        const model = options.model || 'dall-e-2'; // DALL-E 3 doesn't support image edits yet
        const size = options.size || '1024x1024';
        const n = options.n || 1;
        const saveDir = options.saveDir || this.config.defaultSaveDir || process.cwd();
        const fileName = options.fileName || `dalle-edit-${Date.now()}`;
    
        // Ensure save directory exists
        await fs.ensureDir(saveDir);
    
        // Check if image exists
        if (!await fs.pathExists(imagePath)) {
          return {
            success: false,
            error: `Image file not found: ${imagePath}`
          };
        }
    
        // Check if mask exists if provided
        if (options.mask && !await fs.pathExists(options.mask)) {
          return {
            success: false,
            error: `Mask file not found: ${options.mask}`
          };
        }
    
        // Create form data
        const formData = new FormData();
        formData.append('prompt', prompt);
        formData.append('n', n.toString());
        formData.append('size', size);
        formData.append('response_format', 'b64_json');
    
        // Read image file and append to form
        const imageBuffer = await fs.readFile(imagePath);
        formData.append('image', imageBuffer, {
          filename: path.basename(imagePath),
          contentType: 'image/png'
        });
    
        // Add mask if provided
        if (options.mask) {
          const maskBuffer = await fs.readFile(options.mask);
          formData.append('mask', maskBuffer, {
            filename: path.basename(options.mask),
            contentType: 'image/png'
          });
        }
    
        // Make request to OpenAI API
        const response = await axios.post(
          `${this.baseUrl}/images/edits`,
          formData,
          {
            headers: {
              'Content-Type': 'multipart/form-data',
              'Authorization': `Bearer ${this.config.apiKey}`
            }
          }
        );
    
        // Process response
        const data = response.data;
        const imagePaths: string[] = [];
    
        // Save each image
        for (let i = 0; i < data.data.length; i++) {
          const item = data.data[i];
          const resultBuffer = Buffer.from(item.b64_json, 'base64');
          let resultPath = path.join(saveDir, `${fileName}${n > 1 ? `-${i + 1}` : ''}.png`);
          
          // Ensure the path is absolute
          if (!path.isAbsolute(resultPath)) {
            resultPath = path.resolve(process.cwd(), resultPath);
          }
          
          await fs.writeFile(resultPath, resultBuffer);
          imagePaths.push(resultPath);
        }
    
        return {
          success: true,
          imagePaths,
          model,
          prompt
        };
      } catch (error) {
        console.log("DALL-E API Error:", error);
        
        let errorMessage = 'Failed to edit image';
        
        if (axios.isAxiosError(error) && error.response?.data?.error) {
          errorMessage = `DALL-E API Error: ${error.response.data.error.message}`;
        } else if (error instanceof Error) {
          errorMessage = error.message;
        }
        
        return {
          success: false,
          error: errorMessage
        };
      }
    }
  • src/index.ts:85-86 (registration)
    Dispatch logic in MCP server's CallToolRequestSchema handler: switch case for 'edit_image' that casts arguments and invokes the tool handler.
    case 'edit_image':
      response = await (tool as Tool<EditImageArgs>).handler(args as unknown as EditImageArgs);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions the tool edits images using DALL-E but omits critical details: whether this is a read/write operation (implied mutation from 'edit'), authentication needs, rate limits, file format requirements, or what happens to the original image. For a mutation tool with zero annotation coverage, this leaves significant gaps in understanding its behavior and constraints.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core action ('Edit an existing image') and includes essential details (technology and input). There is zero waste or redundancy, making it appropriately sized and well-structured for quick comprehension.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 parameters, mutation operation, no output schema, and no annotations), the description is incomplete. It lacks information on behavioral traits (e.g., file handling, error cases), output format (what is returned?), and usage context. For a DALL-E editing tool with multiple parameters, this minimal description fails to provide sufficient context for effective agent use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all 8 parameters. The description adds no parameter-specific information beyond implying 'prompt' is used for editing. Since the schema does the heavy lifting, the baseline score of 3 is appropriate, as the description doesn't compensate with additional semantic context for parameters.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('Edit') and resource ('an existing image'), specifying the technology ('using DALL-E') and input mechanism ('based on a text prompt'). It distinguishes from sibling 'create_variation' and 'generate_image' by focusing on editing existing images rather than creating new ones or variations, though it doesn't explicitly contrast with these alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'create_variation' or 'generate_image'. It doesn't mention prerequisites (e.g., needing an existing image file), exclusions, or specific contexts where editing is preferred over generation. Usage is implied through the action 'edit an existing image' but lacks explicit direction.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Garoth/dalle-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server