compress_remote_image
Reduce the size of a remote image by providing its URL, specify the output format, and save it to an absolute path for efficient storage and faster loading.
Instructions
Compress a remote image file by giving the URL of the image
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imageUrl | Yes | The URL of 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 |
Input Schema (JSON Schema)
{
"properties": {
"imageUrl": {
"description": "The URL of the image file to compress",
"example": "https://example.com/image.jpg",
"type": "string"
},
"outputFormat": {
"description": "The format to save the compressed image file",
"enum": [
"image/jpeg",
"image/png",
"image/webp",
"image/jpg",
"image/avif"
],
"example": "image/jpeg",
"type": "string"
},
"outputPath": {
"description": "The ABSOLUTE path to save the compressed image file",
"example": "/Users/user/Downloads/image_compressed.jpg",
"type": "string"
}
},
"required": [
"imageUrl"
],
"type": "object"
}
Implementation Reference
- src/tools.ts:262-279 (handler)The MCP tool handler function that processes the incoming request for compress_remote_image, extracts parameters, calls the core handler, and formats the response.compress_remote_image: async (request) => { const result = await handleCompressRemoteImageTool( request.params.arguments as { imageUrl: string; outputPath?: string; outputFormat?: SupportedImageTypes; }, ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], metadata: {}, }; },
- src/tools.ts:52-77 (schema)Tool definition including the name, description, and input schema for validating parameters like imageUrl, outputPath, and outputFormat.const COMPRESS_REMOTE_IMAGE_TOOL: Tool = { name: 'compress_remote_image', description: 'Compress a remote image file by giving the URL of the image', inputSchema: { type: 'object', properties: { imageUrl: { type: 'string', description: 'The URL of the image file to compress', example: 'https://example.com/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', }, }, required: ['imageUrl'], }, };
- src/tools.ts:173-211 (helper)Core helper function that performs the actual compression of the remote image using tinify.fromUrl, handles output format conversion, determines output path, saves the file, and computes compression statistics.async function handleCompressRemoteImageTool({ imageUrl, outputPath, outputFormat, }: { imageUrl: string; outputPath?: string; outputFormat?: SupportedImageTypes; }) { tinify.key = config.apiKey!; const source = tinify.fromUrl(imageUrl); let ext = path.extname(imageUrl).slice(1); if (outputFormat) { source.convert({ type: outputFormat, }); ext = outputFormat.split('/')[1]; } let dest = outputPath; if (!dest) { const dir = path.dirname(imageUrl); const basename = path.basename(imageUrl, path.extname(imageUrl)); dest = path.join(dir, `${basename}.${ext}`); } await source.toFile(dest); const originalSize = (await fetch(imageUrl).then((res) => res.arrayBuffer())).byteLength; const compressedSize = fs.statSync(dest).size; const compressionRatio = (originalSize - compressedSize) / originalSize; return { originalSize, compressedSize, compressionRatio, }; }
- src/tools.ts:117-117 (registration)Exports the array of Tool objects including COMPRESS_REMOTE_IMAGE_TOOL for registration with the MCP server.export const TOOLS = [COMPRESS_LOCAL_IMAGE_TOOL, COMPRESS_REMOTE_IMAGE_TOOL, RESIZE_IMAGE_TOOL];