compress_local_image
Compress local image files to reduce file size while maintaining quality. Specify input and output paths, choose formats like JPEG, PNG, or WebP, and optionally preserve metadata.
Instructions
Compress a local image file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imagePath | Yes | The ABSOLUTE path to the image file to compress | |
| outputPath | No | The ABSOLUTE path to save the compressed image file | |
| outputFormat | No | The format to save the compressed image file | |
| preserveMetadata | No | The metadata to preserve in the image file |
Implementation Reference
- src/tools.ts:17-50 (schema)Defines the tool schema including name, description, and inputSchema with properties for imagePath (required), outputPath, outputFormat, and preserveMetadata.const COMPRESS_LOCAL_IMAGE_TOOL: Tool = { name: 'compress_local_image', description: 'Compress a local image file', inputSchema: { type: 'object', properties: { imagePath: { type: 'string', description: 'The ABSOLUTE path to the image file to compress', example: '/Users/user/Downloads/image.jpg', }, outputPath: { type: 'string', description: 'The ABSOLUTE path to save the compressed image file', example: '/Users/user/Downloads/image_compressed.jpg', }, outputFormat: { type: 'string', description: 'The format to save the compressed image file', enum: SUPPORTED_IMAGE_TYPES, example: 'image/jpeg', }, preserveMetadata: { type: 'array', description: 'The metadata to preserve in the image file', items: { type: 'string', enum: ['copyright', 'creation', 'location'], }, }, }, required: ['imagePath'], }, };
- src/tools.ts:119-171 (handler)Core handler function that performs the image compression using the tinify library. Handles metadata preservation, format conversion, automatic output path generation with '_compressed' suffix, saves the file, and returns compression statistics (originalSize, compressedSize, compressionRatio).async function handleCompressLocalImageTool({ imagePath, outputPath, outputFormat, preserveMetadata, }: { imagePath: string; outputPath?: string; outputFormat?: SupportedImageTypes; preserveMetadata?: string[]; }) { const originalSize = fs.statSync(imagePath).size; tinify.key = config.apiKey!; let source = tinify.fromFile(imagePath); if (preserveMetadata?.length) { source = source.preserve(...preserveMetadata); } let ext = path.extname(imagePath).slice(1); if (outputFormat) { source.convert({ type: outputFormat, }); ext = outputFormat.split('/')[1]; } let dest = outputPath; if (!dest) { const dir = path.dirname(imagePath); const basename = path.basename(imagePath, path.extname(imagePath)); dest = path.join(dir, `${basename}.${ext}`); } // add _compressed to the filename const destDir = path.dirname(dest); const destBasename = path.basename(dest, path.extname(dest)); if (!destBasename.endsWith('_compressed')) { dest = path.join(destDir, `${destBasename}_compressed.${ext}`); } await source.toFile(dest); const compressedSize = fs.statSync(dest).size; const compressionRatio = (originalSize - compressedSize) / originalSize; return { originalSize, compressedSize, compressionRatio, }; }
- src/tools.ts:117-117 (registration)Registers the compress_local_image tool schema (via COMPRESS_LOCAL_IMAGE_TOOL) in the exported TOOLS array used for MCP tool discovery.export const TOOLS = [COMPRESS_LOCAL_IMAGE_TOOL, COMPRESS_REMOTE_IMAGE_TOOL, RESIZE_IMAGE_TOOL];
- src/tools.ts:244-261 (registration)Registers the handler for 'compress_local_image' in the TOOL_HANDLERS map, wrapping the core handler and formatting the MCP response with JSON-stringified results.compress_local_image: async (request) => { const result = await handleCompressLocalImageTool( request.params.arguments as { imagePath: string; outputPath?: string; outputFormat?: SupportedImageTypes; }, ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], metadata: {}, }; },