compress_local_image
Reduce file size of local images by compressing them, optionally preserving metadata, and saving in formats like JPEG, PNG, or WebP for optimized storage or sharing.
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 | |
| outputFormat | No | The format to save the compressed image file | |
| outputPath | No | The ABSOLUTE path to save the compressed image file | |
| preserveMetadata | No | The metadata to preserve in the image file |
Implementation Reference
- src/tools.ts:119-171 (handler)Primary handler function that executes the image compression logic using Tinify: loads image, preserves metadata if specified, converts format if needed, determines output path (auto-appending _compressed), saves file, and returns original/compressed sizes and ratio.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:17-50 (schema)Tool schema definition including name, description, and inputSchema with properties for imagePath (required), outputPath, outputFormat (enum from SUPPORTED_IMAGE_TYPES), 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:117-117 (registration)Registration of the compress_local_image tool (via COMPRESS_LOCAL_IMAGE_TOOL) in the exported TOOLS array for MCP tool discovery.export const TOOLS = [COMPRESS_LOCAL_IMAGE_TOOL, COMPRESS_REMOTE_IMAGE_TOOL, RESIZE_IMAGE_TOOL];
- src/tools.ts:244-261 (registration)Registration of the tool handler in TOOL_HANDLERS object, which extracts arguments, calls the core handler, and formats 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: {}, }; },
- src/tools.ts:9-15 (helper)Helper constant defining supported image MIME types used in inputSchema enum and output format handling.const SUPPORTED_IMAGE_TYPES: SupportedImageTypes[] = [ 'image/jpeg', 'image/png', 'image/webp', 'image/jpg', 'image/avif', ];