返回市场
图像工具MCP

图像工具MCP

作者:kshern6 星标更新:2025-06-26

项目介绍

Image Tools MCP

smithery 徽章

这是一个用于获取图像尺寸和压缩图像的Model Context Protocol (MCP)服务,支持URL和本地文件源。

中文文档

功能

  • 从URL中获取图像尺寸
  • 从本地文件中获取图像尺寸
  • 使用TinyPNG API压缩URL中的图像
  • 使用TinyPNG API压缩本地图像
  • 将图像转换为不同的格式(webp、jpeg/jpg、png)
  • 返回宽度、高度、类型、MIME类型和压缩信息

示例结果

示例结果1 示例结果2

示例结果1 示例结果2

从Figma URL下载并压缩 示例结果3

使用方法

作为MCP服务使用

此服务提供五个工具功能:

  1. get_image_size - 获取远程图像的尺寸
  2. get_local_image_size - 获取本地图像的尺寸
  3. compress_image_from_url - 使用TinyPNG API压缩远程图像
  4. compress_local_image - 使用TinyPNG API压缩本地图像
  5. figma - 从Figma API获取图像链接并使用TinyPNG API进行压缩

客户端集成

要使用此MCP服务,您需要从MCP客户端连接到它。这里有一些如何与不同客户端集成的例子:

使用方法

{
  "mcpServers": {
    "image-tools": {
      "command": "npx",
      "args": ["image-tools-mcp"],
      "env": {
        "TINIFY_API_KEY": "<YOUR_TINIFY_API_KEY>",
        "FIGMA_API_TOKEN": "<YOUR_FIGMA_API_TOKEN>"
      }
    }
  }
}

使用MCP客户端库

import { McpClient } from "@modelcontextprotocol/client";

// 初始化客户端
const client = new McpClient({
  transport: "stdio" // 或其他传输选项
});

// 连接到服务器
await client.connect();

// 从URL获取图像尺寸
const urlResult = await client.callTool("get_image_size", {
  options: {
    imageUrl: "https://example.com/image.jpg"
  }
});
console.log(JSON.parse(urlResult.content[0].text));
// 输出: { width: 800, height: 600, type: "jpg", mime: "image/jpeg" }

// 从本地文件获取图像尺寸
const localResult = await client.callTool("get_local_image_size", {
  options: {
    imagePath: "D:/path/to/image.png"
  }
});
console.log(JSON.parse(localResult.content[0].text));
// 输出: { width: 1024, height: 768, type: "png", mime: "image/png", path: "D:/path/to/image.png" }

// 压缩URL中的图像
const compressUrlResult = await client.callTool("compress_image_from_url", {
  options: {
    imageUrl: "https://example.com/image.jpg",
    outputFormat: "webp" // 可选:转换为webp、jpeg/jpg或png
  }
});
console.log(JSON.parse(compressUrlResult.content[0].text));
// 输出: { originalSize: 102400, compressedSize: 51200, compressionRatio: "50.00%", tempFilePath: "/tmp/compressed_1615456789.webp", format: "webp" }

// 压缩本地图像
const compressLocalResult = await client.callTool("compress_local_image", {
  options: {
    imagePath: "D:/path/to/image.png",
    outputPath: "D:/path/to/compressed.webp", // 可选
    outputFormat: "image/webp" // 可选:转换为image/webp、image/jpeg或image/png
  }
});
console.log(JSON.parse(compressLocalResult.content[0].text));
// 输出: { originalSize: 102400, compressedSize: 51200, compressionRatio: "50.00%", outputPath: "D:/path/to/compressed.webp", format: "webp" }

// 从Figma API获取图像链接

const figmaResult = await client.callTool("figma", {
  options: {
    figmaUrl: "https://www.figma.com/file/XXXXXXX"
  }
});
console.log(JSON.parse(figmaResult.content[0].text));
// 输出: { imageLinks: ["https://example.com/image1.jpg", "https://example.com/image2.jpg"] }

### 工具模式

#### get_image_size

```typescript
{
  options: {
    imageUrl: string // 要获取尺寸的图像URL
  }
}

get_local_image_size

{
  options: {
    imagePath: string; // 本地图像文件的绝对路径
  }
}

compress_image_from_url

{
  options: {
    imageUrl: string // 要压缩的图像URL
    outputFormat?: "image/webp" | "image/jpeg" | "image/jpg" | "image/png" // 可选输出格式
  }
}

compress_local_image

{
  options: {
    imagePath: string // 本地图像文件的绝对路径
    outputPath?: string // 可选的压缩输出图像的绝对路径
    outputFormat?: "image/webp" | "image/jpeg" | "image/jpg" | "image/png" // 可选输出格式
  }
}

figma

{
  options: {
    figmaUrl: string; // 要从中获取图像链接的Figma文件URL
  }
}

更新日志

  • 2025-05-12: 更新了Figma API以支持额外参数,包括2倍图像缩放。

技术实现

该项目基于以下库构建:

环境变量

  • TINIFY_API_KEY - 图像压缩功能所需。从TinyPNG获取您的API密钥
    • 当未提供时,压缩工具(compress_image_from_urlcompress_local_image)将不会注册
  • FIGMA_API_TOKEN - 从Figma API获取图像链接所需。从Figma获取您的API令牌
    • 当未提供时,Figma工具(figma)将不会注册

注意:基本的图像尺寸工具(get_image_sizeget_local_image_size)始终可用,无论是否有API密钥。

许可证

MIT