Skip to main content
Glama

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
NameRequiredDescriptionDefault
imagePathYesThe ABSOLUTE path to the image file to compress
outputFormatNoThe format to save the compressed image file
outputPathNoThe ABSOLUTE path to save the compressed image file
preserveMetadataNoThe metadata to preserve in the image file

Implementation Reference

  • 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,
      };
    }
  • 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: {},
      };
    },
  • 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',
    ];
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool compresses an image but lacks details on critical behaviors: whether it overwrites files, requires specific permissions, handles errors (e.g., invalid paths), or has performance implications (e.g., processing time). This is inadequate for a mutation tool with zero annotation coverage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with no wasted words. It front-loads the core purpose ('Compress a local image file') without unnecessary elaboration, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 4 parameters, no annotations, and no output schema, the description is incomplete. It doesn't address behavioral aspects like file handling, error conditions, or output details (e.g., what the tool returns), leaving significant gaps for an agent to understand and invoke it correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all four parameters. The description adds no parameter-specific information beyond implying 'local image file' relates to 'imagePath'. It doesn't explain interactions between parameters (e.g., how 'outputFormat' affects compression) or usage nuances, meeting the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('compress') and resource ('a local image file'), distinguishing it from sibling tools like 'compress_remote_image' (local vs. remote) and 'resize_image' (compress vs. resize). However, it doesn't specify what compression entails (e.g., quality reduction, format conversion) beyond the basic verb.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention when to choose 'compress_local_image' over 'compress_remote_image' (e.g., for local vs. remote files) or 'resize_image' (e.g., for size reduction vs. dimension changes), nor does it specify prerequisites like file accessibility or format support.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/zhendi/tinypng-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server