convert-image
Transform image files into different formats using the MCP Media Processing Server. Specify input path, desired output format, and optional output filename or path for efficient conversion.
Instructions
Convert image to different format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Absolute path to input image file | |
| outputFilename | No | Output filename (only used if outputPath is not provided) | |
| outputFormat | Yes | Desired output format (e.g., jpg, png, webp, gif) | |
| outputPath | No | Optional absolute path for output file. If not provided, file will be saved in Downloads folder |
Implementation Reference
- src/index.ts:332-362 (handler)The asynchronous handler function that executes the image conversion logic using ImageMagick's 'convert' command. It validates paths, constructs the output filename, runs the conversion, and returns success or error messages.async ({ inputPath, outputFormat, outputPath, outputFilename }) => { try { await checkImageMagick(); const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_converted.${outputFormat}`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = `convert "${absoluteInputPath}" "${finalOutputPath}"`; await execSync(command); return { content: [ { type: "text", text: `Image successfully converted and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error converting image: ${errorMessage}`, }, ], }; } }
- src/index.ts:326-331 (schema)Zod schema defining the input parameters for the convert-image tool.{ inputPath: z.string().describe("Absolute path to input image file"), outputFormat: z.string().describe("Desired output format (e.g., jpg, png, webp, gif)"), outputPath: z.string().optional().describe("Optional absolute path for output file. If not provided, file will be saved in Downloads folder"), outputFilename: z.string().optional().describe("Output filename (only used if outputPath is not provided)") },
- src/index.ts:323-363 (registration)The server.tool registration call that defines the tool name, description, input schema, and handler function for 'convert-image'. Note: excerpt abbreviated for handler.server.tool( "convert-image", "Convert image to different format", { inputPath: z.string().describe("Absolute path to input image file"), outputFormat: z.string().describe("Desired output format (e.g., jpg, png, webp, gif)"), outputPath: z.string().optional().describe("Optional absolute path for output file. If not provided, file will be saved in Downloads folder"), outputFilename: z.string().optional().describe("Output filename (only used if outputPath is not provided)") }, async ({ inputPath, outputFormat, outputPath, outputFilename }) => { try { await checkImageMagick(); const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_converted.${outputFormat}`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = `convert "${absoluteInputPath}" "${finalOutputPath}"`; await execSync(command); return { content: [ { type: "text", text: `Image successfully converted and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error converting image: ${errorMessage}`, }, ], }; } } );