rotate-image
Rotate an image by a specified angle in degrees using the MCP Media Processing Server. Input the image path and rotation angle, and optionally set an output path or filename for the rotated image.
Instructions
Rotate image by specified degrees
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| degrees | Yes | Rotation angle in degrees | |
| inputPath | Yes | Absolute path to input image file | |
| outputFilename | No | Output filename (only used if outputPath is not provided) | |
| outputPath | No | Optional absolute path for output file. If not provided, file will be saved in Downloads folder |
Implementation Reference
- src/index.ts:430-461 (handler)Executes image rotation using ImageMagick 'convert' command with -rotate option. Handles path resolution, output path generation, command execution, and error handling.async ({ inputPath, degrees, outputPath, outputFilename }) => { try { await checkImageMagick(); const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const extension = absoluteInputPath.split('.').pop() || 'png'; const defaultFilename = outputFilename || `${inputFileName}_rotated.${extension}`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = `convert "${absoluteInputPath}" -rotate ${degrees} "${finalOutputPath}"`; await execSync(command); return { content: [ { type: "text", text: `Image successfully rotated and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error rotating image: ${errorMessage}`, }, ], }; } }
- src/index.ts:424-429 (schema)Zod schema defining input parameters for the rotate-image tool: inputPath, degrees, optional outputPath and outputFilename.{ inputPath: z.string().describe("Absolute path to input image file"), degrees: z.number().describe("Rotation angle in degrees"), 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:421-462 (registration)Registers the 'rotate-image' tool with the MCP server, providing name, description, input schema, and handler function.server.tool( "rotate-image", "Rotate image by specified degrees", { inputPath: z.string().describe("Absolute path to input image file"), degrees: z.number().describe("Rotation angle in degrees"), 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, degrees, outputPath, outputFilename }) => { try { await checkImageMagick(); const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const extension = absoluteInputPath.split('.').pop() || 'png'; const defaultFilename = outputFilename || `${inputFileName}_rotated.${extension}`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = `convert "${absoluteInputPath}" -rotate ${degrees} "${finalOutputPath}"`; await execSync(command); return { content: [ { type: "text", text: `Image successfully rotated and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error rotating image: ${errorMessage}`, }, ], }; } } );
- src/index.ts:69-76 (helper)Helper function to verify ImageMagick installation by executing 'convert -version'.async function checkImageMagick() { try { execSync('convert -version'); return true; } catch (error) { throw new Error('ImageMagick is not installed. Please install it first.'); } }
- src/index.ts:30-44 (helper)Helper to resolve relative input paths to absolute paths using process.cwd() and verify file accessibility.async function getAbsolutePath(inputPath: string): Promise<string> { if (isAbsolute(inputPath)) { return inputPath; } // FIXME: But it's not working, because the server is running in a different directory const absolutePath = resolve(process.cwd(), inputPath); try { await fs.access(absolutePath); return absolutePath; } catch (error) { throw new Error(`Input file not found: ${inputPath}`); } }