convert_format
Convert images between formats like JPEG, PNG, WebP, AVIF, TIFF, and GIF with configurable quality settings for different use cases.
Instructions
Convert an image to a different format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Path to the input image file | |
| output | Yes | Path to save the converted image | |
| format | Yes | Target format: jpeg, png, webp, avif, tiff, etc. | |
| 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 validates input parameters, ensures the input file exists, creates the output directory, uses Sharp to process the image conversion based on the target format with quality settings, saves the converted image, retrieves its metadata, and returns a success message with details or an error message.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:283-308 (schema)The input schema definition for the 'convert_format' tool, specifying required parameters (input, output, format) and optional quality, listed in the ListTools response.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"] } },