Skip to main content
Glama
zhixiaoqiang

Desktop Image Manager MCP Server

compress-image

Reduces image file size by compressing images with adjustable quality settings, enabling efficient storage and sharing of desktop images.

Instructions

压缩图片

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileNameYes要压缩的图片文件名
outputNameNo输出文件名 (可选)
qualityNo压缩质量 (1-100)

Implementation Reference

  • Executes the image compression logic: loads image from desktop using Sharp, applies quality-based compression depending on format (JPEG, PNG, WebP, or fallback to JPEG), saves to new file, computes and reports size savings in bytes and percentage, handles file existence, format validation, and errors.
    async ({ fileName, quality, outputName }) => {
      try {
        const desktopPath = getDesktopPath();
        const inputPath = path.join(desktopPath, fileName);
        
        // 检查文件是否存在
        if (!await fs.pathExists(inputPath)) {
          return {
            content: [{ type: "text", text: `文件 "${fileName}" 不存在。` }],
            isError: true
          };
        }
        
        // 检查是否为图片文件
        if (!isImageFile(inputPath)) {
          return {
            content: [{ type: "text", text: `文件 "${fileName}" 不是支持的图片格式。` }],
            isError: true
          };
        }
        
        // 确定输出文件名
        const ext = path.extname(fileName);
        const baseName = path.basename(fileName, ext);
        const finalOutputName = outputName || `${baseName}-compressed${ext}`;
        const outputNameFilled = isImageFile(finalOutputName) ? finalOutputName : `${finalOutputName}${ext}`;
        const outputPath = path.join(desktopPath, outputNameFilled);
        
        // 根据文件扩展名确定压缩方法
        const lowerExt = ext.toLowerCase();
        
        if (['.jpg', '.jpeg'].includes(lowerExt)) {
          await sharp(inputPath)
            .jpeg({ quality })
            .toFile(outputPath);
        } else if (lowerExt === '.png') {
          await sharp(inputPath)
            .png({ quality })
            .toFile(outputPath);
        } else if (lowerExt === '.webp') {
          await sharp(inputPath)
            .webp({ quality })
            .toFile(outputPath);
        } else {
          // 对于其他格式,先转换为 JPEG 再压缩
          await sharp(inputPath)
            .jpeg({ quality })
            .toFile(outputPath);
        }
        
        // 获取原始文件和压缩后文件的大小
        const originalSize = (await fs.stat(inputPath)).size;
        const compressedSize = (await fs.stat(outputPath)).size;
        const savingsPercent = ((originalSize - compressedSize) / originalSize * 100).toFixed(2);
        
        return {
          content: [{ 
            type: "text", 
            text: `图片压缩成功!\n原始文件: ${fileName} (${originalSize} 字节)\n压缩后文件: ${outputNameFilled} (${compressedSize} 字节)\n节省空间: ${savingsPercent}%` 
          }]
        };
      } catch (error) {
        return {
          content: [{ 
            type: "text", 
            text: `压缩图片时出错: ${error instanceof Error ? error.message : String(error)}` 
          }],
          isError: true
        };
      }
    }
  • Zod schema for 'compress-image' tool inputs: fileName (string, required), quality (number 1-100, default 80), outputName (optional string). Used for validation in the tool registration.
    {
      fileName: z.string().describe("要压缩的图片文件名"),
      quality: z.number().min(1).max(100).default(80).describe("压缩质量 (1-100)"),
      outputName: z.string().optional().describe("输出文件名 (可选)")
    },
  • server.ts:108-187 (registration)
    Registers the 'compress-image' tool on the MCP server with name, description, input schema, and handler function.
    server.tool(
      "compress-image",
      '压缩图片',
      {
        fileName: z.string().describe("要压缩的图片文件名"),
        quality: z.number().min(1).max(100).default(80).describe("压缩质量 (1-100)"),
        outputName: z.string().optional().describe("输出文件名 (可选)")
      },
      async ({ fileName, quality, outputName }) => {
        try {
          const desktopPath = getDesktopPath();
          const inputPath = path.join(desktopPath, fileName);
          
          // 检查文件是否存在
          if (!await fs.pathExists(inputPath)) {
            return {
              content: [{ type: "text", text: `文件 "${fileName}" 不存在。` }],
              isError: true
            };
          }
          
          // 检查是否为图片文件
          if (!isImageFile(inputPath)) {
            return {
              content: [{ type: "text", text: `文件 "${fileName}" 不是支持的图片格式。` }],
              isError: true
            };
          }
          
          // 确定输出文件名
          const ext = path.extname(fileName);
          const baseName = path.basename(fileName, ext);
          const finalOutputName = outputName || `${baseName}-compressed${ext}`;
          const outputNameFilled = isImageFile(finalOutputName) ? finalOutputName : `${finalOutputName}${ext}`;
          const outputPath = path.join(desktopPath, outputNameFilled);
          
          // 根据文件扩展名确定压缩方法
          const lowerExt = ext.toLowerCase();
          
          if (['.jpg', '.jpeg'].includes(lowerExt)) {
            await sharp(inputPath)
              .jpeg({ quality })
              .toFile(outputPath);
          } else if (lowerExt === '.png') {
            await sharp(inputPath)
              .png({ quality })
              .toFile(outputPath);
          } else if (lowerExt === '.webp') {
            await sharp(inputPath)
              .webp({ quality })
              .toFile(outputPath);
          } else {
            // 对于其他格式,先转换为 JPEG 再压缩
            await sharp(inputPath)
              .jpeg({ quality })
              .toFile(outputPath);
          }
          
          // 获取原始文件和压缩后文件的大小
          const originalSize = (await fs.stat(inputPath)).size;
          const compressedSize = (await fs.stat(outputPath)).size;
          const savingsPercent = ((originalSize - compressedSize) / originalSize * 100).toFixed(2);
          
          return {
            content: [{ 
              type: "text", 
              text: `图片压缩成功!\n原始文件: ${fileName} (${originalSize} 字节)\n压缩后文件: ${outputNameFilled} (${compressedSize} 字节)\n节省空间: ${savingsPercent}%` 
            }]
          };
        } catch (error) {
          return {
            content: [{ 
              type: "text", 
              text: `压缩图片时出错: ${error instanceof Error ? error.message : String(error)}` 
            }],
            isError: true
          };
        }
      }
    );
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the action ('compress image') but doesn't describe key behaviors like whether the original file is modified or replaced, if a new file is created, error handling, or performance considerations. This leaves significant gaps for a mutation tool.

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 extremely concise with just two characters ('压缩图片'), which is front-loaded and wastes no words. For a simple tool, this brevity is efficient, though it may sacrifice clarity. Every element earns its place by directly stating the core function.

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?

Given the tool's complexity (a mutation operation with 3 parameters) and lack of annotations and output schema, the description is incomplete. It doesn't cover behavioral aspects, usage context, or output details, leaving the agent with insufficient information to invoke the tool correctly without guesswork.

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%, with clear descriptions for fileName, outputName, and quality parameters. The description adds no additional meaning beyond the schema, such as explaining parameter interactions or constraints. Baseline score of 3 is appropriate since the schema adequately documents parameters.

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

Purpose3/5

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

The description '压缩图片' (compress image) states the basic verb and resource but is vague about scope and implementation details. It doesn't specify what type of compression (e.g., lossy/lossless), supported formats, or how it differs from sibling tools like count-desktop-images and list-desktop-images. The purpose is understandable but lacks specificity and differentiation.

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?

No guidance is provided on when to use this tool versus alternatives or any prerequisites. The description doesn't mention context for usage, such as when compression is needed or what happens to the original file. Without annotations or explicit instructions, the agent must infer usage from the tool name alone.

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/zhixiaoqiang/desktop-image-manager-mcp'

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