convert_format
Transform image files into various formats like JPEG, PNG, or WebP using specified paths and quality settings for compatible formats.
Instructions
Convert an image to a different format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | Yes | Target format: jpeg, png, webp, avif, tiff, etc. | |
| input | Yes | Path to the input image file | |
| output | Yes | Path to save the converted image | |
| quality | No | Quality level (1-100, for formats that support it) |
Implementation Reference
- src/index.ts:438-511 (handler)The handler for the 'convert_format' tool. It processes the input image using Sharp library to convert it to the specified format (jpeg, png, webp, etc.), applies quality settings, ensures the output directory exists, saves the converted image, retrieves its metadata, and returns a success message with details.case "convert_format": { const { input, output, format, quality = 80 } = request.params.arguments as { input: string, output: string, format: string, quality?: number }; if (!input || !output || !format) { throw new McpError(ErrorCode.InvalidParams, "Input path, output path, and format are required"); } if (!fs.existsSync(input)) { throw new McpError(ErrorCode.InvalidRequest, `Input image not found: ${input}`); } try { // Create output directory if it doesn't exist await fsExtra.ensureDir(path.dirname(output)); // Convert the image let processor = sharp(input); // Apply format-specific options switch (format.toLowerCase()) { case 'jpeg': case 'jpg': processor = processor.jpeg({ quality }); break; case 'png': processor = processor.png({ quality: Math.floor(quality / 100 * 9) }); break; case 'webp': processor = processor.webp({ quality }); break; case 'avif': processor = processor.avif({ quality }); break; case 'tiff': processor = processor.tiff({ quality }); break; case 'gif': processor = processor.gif(); break; default: throw new McpError(ErrorCode.InvalidParams, `Unsupported format: ${format}`); } await processor.toFile(output); // Get metadata of the converted image const metadata = await getImageMetadata(output); return { content: [ { type: "text", text: `Image converted successfully:\n` + `- Original: ${input}\n` + `- Converted: ${output}\n` + `- Format: ${metadata.format}\n` + `- Dimensions: ${metadata.width}x${metadata.height}\n` + `- Size: ${(metadata.size / 1024).toFixed(2)} KB` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error converting image: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/index.ts:282-308 (schema)The input schema definition for the 'convert_format' tool, specifying required parameters (input, output, format) and optional quality, along with enums for supported formats.{ name: "convert_format", description: "Convert an image to a different format", inputSchema: { type: "object", properties: { input: { type: "string", description: "Path to the input image file" }, output: { type: "string", description: "Path to save the converted image" }, format: { type: "string", description: "Target format: jpeg, png, webp, avif, tiff, etc.", enum: ["jpeg", "png", "webp", "avif", "tiff", "gif"] }, quality: { type: "number", description: "Quality level (1-100, for formats that support it)" } }, required: ["input", "output", "format"] } },