图像压缩工具 MCP云托管添加福利群:解决AI开发者的「MCP实战痛点」一个用于获取图像尺寸和压缩图像的模型上下文协议(MCP)服务,支持 URL 和本地文件来源。


从 Figma URL 下载并压缩

该服务提供五个工具函数:
get_image_size - 获取远程图像的尺寸get_local_image_size - 获取本地图像的尺寸compress_image_from_url - 使用 TinyPNG API 压缩远程图像compress_local_image - 使用 TinyPNG API 压缩本地图像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>"
}
}
}
}

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
}
}

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

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

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

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

该项目基于以下库构建:
TINIFY_API_KEY - 图像压缩功能所需。从 TinyPNG 获取您的 API 密钥
compress_image_from_url 和 compress_local_image)将不会被注册FIGMA_API_TOKEN - 从 Figma API 获取图像链接所需。从 Figma 获取您的 API 令牌
figma)将不会被注册注意:基本的图像尺寸工具(get_image_size 和 get_local_image_size)无论 API 密钥如何都始终可用。
MIT