Skip to main content
Glama

img-mcp-server

npm version CI

一站式 MCP 图片工具箱。把 AI 生成/修改 和常用的 本地图片处理 放进同一套工具里,方便 Agent 连续完成"生成 → 编辑 → 裁剪 → 压缩 → 转格式 → 提取色板"等工作流。

所有工具都把图片保存到本地,只返回绝对路径和元数据,不返回图片二进制。

当前版本 0.0.1。

发布包名为 img-mcp-server,安装后的命令行名称也是 img-mcp-server。

工具总览

工具

能力

依赖

generate_image

调用 OpenAI Images API 生成图片

OPENAI_API_KEY

edit_image

调用 OpenAI Images API 修改本地图片(本地绝对路径)

OPENAI_API_KEY

crop_image

按像素区域裁剪

sharp

resize_image

调整尺寸,支持 cover/contain/fill/inside/outside

sharp

rotate_image

任意角度旋转、EXIF 自动校正

sharp

convert_image

PNG/JPEG/WebP/AVIF/TIFF/GIF 互转

sharp

compress_image

按质量压缩,或压缩到指定文件大小

sharp

get_image_info

获取格式、尺寸、色彩空间、动画信息、主色/平均色等

sharp

extract_palette

k-means 提取色板(hex/RGB/占比)

sharp

remove_background

纯色/近纯色背景去除,输出透明 PNG/WebP

sharp

html_to_image

用无头 Chromium 渲染 HTML/URL 截图

Chrome/Chromium

echarts_to_image

服务端渲染 Apache ECharts(无需浏览器)

echarts + sharp

Related MCP server: roundcut-mcp

要求

  • Node.js >= 22(OpenAI SDK 7.x 的要求)。

  • html_to_image 需要本机有 Chrome/Chromium。Server 会自动查找:

    1. IMG_CHROME_PATH

    2. playwright-core 已安装的浏览器缓存

    3. 系统常见路径中的 Google Chrome / Chromium / Edge

快速开始

MCP 客户端配置

Claude Desktop、Cursor 等使用 command / args / env 的客户端:

{
  "mcpServers": {
    "img": {
      "command": "npx",
      "args": ["-y", "img-mcp-server@0.0.1"],
      "env": {
        "OPENAI_API_KEY": "sk-...",
        "OPENAI_BASE_URL": "https://api.openai.com/v1",
        "IMG_MODEL": "gpt-image-2.6-sunburst",
        "IMG_OUTPUT_DIR": "/absolute/path/to/img-output"
      }
    }
  }
}

只使用本地图片工具时可以完全省略 OPENAI_API_KEY,例如:

{
  "mcpServers": {
    "img": {
      "command": "npx",
      "args": ["-y", "img-mcp-server@0.0.1"],
      "env": {
        "IMG_OUTPUT_DIR": "/absolute/path/to/img-output"
      }
    }
  }
}

使用兼容网关(示例):

{
  "mcpServers": {
    "img": {
      "command": "npx",
      "args": ["-y", "img-mcp-server@0.0.1"],
      "env": {
        "OPENAI_API_KEY": "your-api-key",
        "OPENAI_BASE_URL": "https://wukong.support/v1",
        "IMG_MODEL": "gpt-image-2.6-sunburst",
        "IMG_OUTPUT_DIR": "/absolute/path/to/img-output"
      }
    }
  }
}

OPENAI_BASE_URL 需要包含版本路径(通常是 /v1)。使用官方 OpenAI 时可以省略,默认 https://api.openai.com/v1。

环境变量

变量

必填

默认值

说明

OPENAI_API_KEY

仅生成/修改需要

—

OpenAI 或兼容网关 API Key

OPENAI_BASE_URL

否

https://api.openai.com/v1

API 基础地址

OPENAI_ORG_ID

否

—

OpenAI Organization ID

OPENAI_PROJECT_ID

否

—

OpenAI Project ID

IMG_MODEL

否

gpt-image-2.6-sunburst

生成/修改未指定 model 时使用

IMG_OUTPUT_DIR

否

~/img-output

默认输出目录

IMG_TIMEOUT_MS

否

600000

生成/修改请求超时(毫秒)

IMG_MAX_RETRIES

否

2

OpenAI SDK 重试次数

IMG_DOWNLOAD_TIMEOUT_MS

否

120000

下载 API 返回图片 URL 的超时(毫秒)

IMG_CHROME_PATH

否

自动检测

html_to_image 使用的 Chrome/Chromium 可执行文件

通用输入输出约定

  • 所有本地图片输入都是运行 MCP Server 的机器上的绝对路径;相对路径直接报错。

  • 所有生成/处理结果都写入本地文件,返回 path 等元数据。

  • 同名文件自动追加 -1、-2,绝不覆盖已有文件。

  • 默认文件名格式:img-<时间戳>-<随机串>.<扩展名>。

  • 扩展名以实际编码结果为准;例如请求 JPEG 但 API 返回 PNG 时,会保存为 .png 并在 format 中如实标明。

  • 图片处理工具默认应用 EXIF 自动校正(rotate_image 可关闭)。

  • 动图(GIF/WebP)按首帧处理;convert_image 输出 GIF 时也是单帧。

生成与修改

generate_image

参数

类型

必填

说明

prompt

string

是

图片描述,最长 32000 字符

model

string

否

默认取 IMG_MODEL

n

integer (1–10)

否

生成数量,默认 1

size

string

否

auto、1024x1024、1536x1024、1024x1536;GPT Image 2.x 支持任意 宽x高(宽高被 16 整除、宽高比 1:3–3:1、最大 3840x2160)

quality

string

否

low / medium / high / xhigh / max / auto

background

string

否

transparent / opaque / auto

output_format

string

否

png / webp / jpeg / gif / svg

output_compression

integer (0–100)

否

webp / jpeg 压缩级别

moderation

string

否

low / auto

complexity

string

否

low / medium / high

frame_count

integer (2–16)

否

GIF 帧数,仅 output_format=gif

frame_duration_ms

integer

否

GIF 帧延迟(毫秒)

safety_identifier

string

否

终端用户稳定标识

user

string

否

已废弃,建议用 safety_identifier

svg

object

否

{ "model": string, "reasoning"?: { "effort": ... } },需 SVG 输出

output_dir

string

否

绝对输出目录

file_name

string

否

文件名(不含目录分隔符);n > 1 时追加序号

edit_image

参数

类型

必填

说明

image

string | string[]

是

1–16 张图片的绝对路径;PNG/JPEG/WebP,每张 < 50 MB

prompt

string

是

修改要求,最长 32000 字符

mask

string

否

PNG 遮罩绝对路径,< 4 MB;不能与 markup 同用

markup

string

否

透明 PNG 叠加层绝对路径;不能与 mask 同用

model

string

否

默认取 IMG_MODEL

n

integer (1–10)

否

生成数量

size

string

否

同 generate_image

quality

string

否

同 generate_image

background

string

否

同 generate_image

input_fidelity

string

否

high / low;gpt-image-1.5 使用,gpt-image-2 系列应省略

output_format

string

否

同 generate_image

output_compression

integer (0–100)

否

同 generate_image

complexity

string

否

low / medium / high

frame_count

integer (2–16)

否

仅 GIF 输出

frame_duration_ms

integer

否

仅 GIF 输出

safety_identifier

string

否

终端用户稳定标识

user

string

否

已废弃,建议用 safety_identifier

svg

object

否

自定义 SVG 作者模型/推理强度

output_dir

string

否

绝对输出目录

file_name

string

否

文件名

未暴露的 OpenAI 参数:stream、partial_images(工具只返回最终文件),以及 GPT Image 模型不支持的旧版 DALL·E 参数 response_format、style。

本地处理工具

除特别说明外,以下通用输出参数均可用于各处理工具:

参数

类型

说明

output_format

string

png / jpeg / webp / avif / tiff / gif;省略时保持输入格式(SVG 输入输出 PNG)

quality

integer (1–100)

JPEG/WebP/AVIF/TIFF 质量

lossless

boolean

WebP/AVIF 无损编码

compression_level

integer (0–9)

PNG 压缩级别

background

string

CSS 颜色;用于 letterbox、旋转留白或 JPEG 压平背景

output_dir

string

绝对输出目录

file_name

string

输出文件名

crop_image

image, left, top, width, height(均必填,像素,零基坐标)。

resize_image

image 必填;width / height 至少一个。可选 fit(cover/contain/fill/inside/outside)、position、without_enlargement、kernel。

rotate_image

image 必填;angle(正数顺时针,可省略表示只做 EXIF 校正)、auto_orient(默认 true)。

convert_image

image + output_format(必填)。JPEG 输出会用 background(默认白色)压平透明通道。

compress_image

image;可选 output_format、quality、lossless、compression_level、target_size_bytes。

  • target_size_bytes 仅支持 JPEG/WebP/AVIF,会二分搜索能塞进目标大小的最高质量。

  • 如果即使质量 1 也达不到目标,会输出最小结果,并在 details.target_reached=false 中说明。

  • 返回 input_bytes、output_bytes、saved_bytes、ratio。

get_image_info

image;可选 include_stats(默认 true)。返回:

  • 格式、宽高、色彩空间、通道数、位深、DPI、alpha、ICC profile 存在性、EXIF orientation

  • 多帧信息(pages、page_height、is_animated、loop、delay_ms)

  • 统计信息:is_opaque、entropy、sharpness、dominant_color、average_color

不会返回原始 EXIF(避免泄露 GPS 等隐私)。

extract_palette

image;color_count(2–32,默认 8)。返回示例:

{
  "operation": "extract_palette",
  "source": { "path": "/abs/palette.png", "..." : "..." },
  "colors": [
    { "hex": "#ff0000", "rgb": { "r": 255, "g": 0, "b": 0 }, "percentage": 25 },
    { "hex": "#00ff00", "rgb": { "r": 0, "g": 255, "b": 0 }, "percentage": 25 }
  ],
  "details": { "method": "k-means", "sampled_pixels": 40000, "iterations": 3 }
}

透明像素会被忽略;采样上限 256×256,保证大图也很快。

remove_background

参数

说明

image

必填,本地绝对路径

background_color

可选,十六进制背景色;省略时从图片边缘采样

tolerance

0–100 颜色距离容差,默认 12

feather

0–50 羽化范围,默认 1.5;0 为硬边

despill

是否去除半透明边缘的背景色溢出,默认 false

output_format

png / webp,默认 png

quality / lossless

WebP 编码选项

这是颜色键控算法,适合纯色/近纯色背景的商品图、图标、截图;不是 AI 语义分割。复杂照片背景建议使用专门的抠图服务。

渲染工具

html_to_image

html 或 url 二选一;可选:

  • selector:只截取第一个匹配元素

  • width(默认 1280)/ height(默认 800)

  • full_page(默认 false)

  • device_scale_factor(1–4)

  • wait_until(load/domcontentloaded/networkidle)

  • wait_ms、timeout_ms

  • base_url:为 HTML 字符串中的相对资源提供 base URL

  • background:页面背景色

  • output_format / quality / lossless / compression_level

示例:

{
  "name": "html_to_image",
  "arguments": {
    "html": "<div style=\"width:400px;padding:24px;background:#4f46e5;color:white\">Hello IMG</div>",
    "width": 640,
    "height": 360,
    "device_scale_factor": 2,
    "output_format": "png"
  }
}

没有浏览器时,Server 会返回明确错误。安装 Chrome/Chromium,或设置 IMG_CHROME_PATH。也可以安装与内置 playwright-core 匹配的 Chromium:

npx playwright@1.65.2 install chromium

echarts_to_image

option 必填,ECharts setOption 的配置对象(也接受 JSON 字符串)。服务端用 SVG renderer 渲染,无需浏览器。

其他参数:

参数

默认

说明

width

1000

图表宽度(CSS 像素)

height

600

图表高度

theme

默认主题

内置主题名(如 dark)或主题对象

background

option.backgroundColor 或白色

背景色,可传 transparent

scale

1

光栅倍率;2 输出 2 倍分辨率(SVG 输出忽略此参数)

output_format

png

png / jpeg / webp / avif / svg

示例:

{
  "name": "echarts_to_image",
  "arguments": {
    "option": {
      "xAxis": { "type": "category", "data": ["A", "B", "C"] },
      "yAxis": { "type": "value" },
      "series": [{ "type": "bar", "data": [3, 5, 2] }]
    },
    "width": 800,
    "height": 500,
    "scale": 2,
    "output_format": "png"
  }
}

返回格式

生成/修改

{
  "model": "gpt-image-2.6-sunburst",
  "created": 1796364512,
  "size": "1024x1024",
  "quality": "low",
  "output_format": "png",
  "images": [
    {
      "path": "/home/user/img-output/generate-smoke.png",
      "file_name": "generate-smoke.png",
      "format": "png",
      "mime_type": "image/png",
      "width": 1024,
      "height": 1024,
      "bytes": 760034,
      "revised_prompt": "A simple flat vector icon ...",
      "source": "url",
      "url": "https://example.com/generated.png"
    }
  ],
  "usage": { "input_tokens": 19, "output_tokens": 272, "total_tokens": 291 }
}

本地处理

{
  "operation": "crop_image",
  "source": {
    "path": "/abs/input.png",
    "file_name": "input.png",
    "format": "png",
    "mime_type": "image/png",
    "width": 200,
    "height": 200,
    "bytes": 1874
  },
  "image": {
    "path": "/abs/img-output/crop-blue.png",
    "file_name": "crop-blue.png",
    "format": "png",
    "mime_type": "image/png",
    "width": 100,
    "height": 100,
    "bytes": 423
  },
  "details": {
    "left": 0,
    "top": 100,
    "width": 100,
    "height": 100,
    "source_width": 200,
    "source_height": 200
  }
}

渲染工具返回 { "operation", "image", "details" };get_image_info 返回 { "operation", "source", "metadata" };extract_palette 返回 { "operation", "source", "colors", "details" }。

本地开发

npm install
npm run build

# 单元测试(无需 API Key、无需浏览器)
npm test

# 本地工具全量测试(sharp + ECharts + Chromium)
npm run test:local

# 真实 API 测试(会消耗额度)
OPENAI_API_KEY=... OPENAI_BASE_URL=https://wukong.support/v1 npm run smoke

# 启动 server(stdio)
OPENAI_API_KEY=... node dist/index.js

作为本地 MCP Server 调试时,把客户端配置中的 command 改为 node,args 改为 ["/绝对路径/dist/index.js"]。

License

MIT

Available Tools

12 tools
compress_imageCompress imageA

Re-encode a local image with a chosen quality/compression level, or binary-search the highest quality that fits target_size_bytes (JPEG, WebP, AVIF). Returns before/after sizes.

Inputs: image (required), quality, lossless, file_name, +5 more. Returns: image, source, details, operation

convert_imageConvert image formatA

Convert a local image to PNG, JPEG, WebP, AVIF, TIFF or GIF. JPEG output is flattened onto the given background color (white by default).

Inputs: image (required), output_format (required), quality, lossless, +4 more. Returns: image, source, details, operation

crop_imageCrop imageA

Crop a local image to a pixel region and save the result. Coordinates are zero-based and apply after EXIF auto-orientation. Returns the source and output paths plus metadata.

Inputs: top (required), left (required), image (required), width (required), +8 more. Returns: image, source, details, operation

echarts_to_imageRender ECharts to imageA

Render an Apache ECharts option object server-side (SVG renderer, no browser) and save it as PNG, JPEG, WebP, AVIF or SVG. Supports themes, background color and raster scale.

Inputs: option (required), scale, theme, width, +8 more. Returns: image, details, operation

edit_imageEdit imageA

Edit or extend one or more local images with the OpenAI Images API (POST /v1/images/edits), optionally using a mask or markup, then save the results to local disk and return absolute file paths plus metadata. Source images must be local absolute paths (PNG, JPEG or WebP, under 50 MB each, up to 16). Requires OPENAI_API_KEY. Image bytes are never returned.

Inputs: image (required), prompt (required), n, svg, +16 more. Returns: size, model, usage, images, +5 more

extract_paletteExtract color paletteA
Read-onlyIdempotent

Extract the dominant color palette from a local image using k-means clustering. Returns hex/RGB colors with coverage percentages; transparent pixels are ignored.

Inputs: image (required), color_count. Returns: colors, source, details, operation

generate_imageGenerate imageA

Generate one or more images with the OpenAI Images API (POST /v1/images/generations), save them to local disk, and return absolute file paths plus metadata. Image bytes are never returned. Requires OPENAI_API_KEY. The model defaults to IMG_MODEL (gpt-image-2.6-sunburst); set OPENAI_BASE_URL for OpenAI-compatible gateways.

Inputs: prompt (required), n, svg, size, +13 more. Returns: size, model, usage, images, +5 more

get_image_infoGet image infoA
Read-onlyIdempotent

Read local image metadata and optional pixel statistics: format, dimensions, color space, channels, alpha, density, animation info, dominant/average color, opacity, entropy and sharpness.

Inputs: image (required), include_stats. Returns: source, metadata, operation

html_to_imageRender HTML to imageA

Render an HTML string or HTTP(S) URL with headless Chromium and save a screenshot. Supports viewport size, full-page capture, element selectors, device scale factor and multiple output formats. Requires a local Chrome/Chromium (auto-detected or IMG_CHROME_PATH).

Inputs: url, html, width, height, +14 more. Returns: image, details, operation

remove_backgroundRemove backgroundA

Remove a solid or near-solid background from a local image using color-distance keying, with optional auto border sampling, tolerance, feathering and despill. Outputs a transparent PNG or WebP. This is not ML/AI segmentation.

Inputs: image (required), despill, feather, quality, +6 more. Returns: image, source, details, operation

resize_imageResize imageA

Resize a local image with sharp's cover/contain/fill/inside/outside fitting modes and save the result. At least one of width or height is required.

Inputs: image (required), fit, width, height, +10 more. Returns: image, source, details, operation

rotate_imageRotate imageB

Rotate a local image by an arbitrary angle (clockwise for positive values), optionally applying EXIF auto-orientation first, and save the result.

Inputs: image (required), angle, quality, lossless, +6 more. Returns: image, source, details, operation

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.9/5.0

    Scored across 12 tools

    Disambiguation4/5

    Most tools have clearly distinct purposes: crop, resize, rotate, convert, and compress each target a specific operation, and generate vs edit are clearly separated. A few boundaries blur slightly: get_image_info already reports dominant/average color and opacity, which overlaps with extract_palette, and html_to_image vs echarts_to_image both render markup to an image though from different sources.

    Naming Consistency5/5

    All twelve tools follow a consistent snake_case verb_noun pattern (crop_image, remove_background, generate_image, convert_image, get_image_info, extract_palette, etc.). The two rendering tools (html_to_image, echarts_to_image) consistently share the same noun_to_image form.

    Tool Count5/5

    Twelve tools sits comfortably in the well-scoped 3-15 range for an image processing server. Each tool covers a distinct capability (geometry ops, format/quality, AI generation, metadata, rendering) with no redundant entries.

    Completeness4/5

    Coverage is strong for a local image pipeline: geometric transforms, format/quality conversion, background removal, AI generate/edit, metadata, palette extraction, and two rendering paths. Minor gaps remain—no flip/mirror, no overlay/watermark/text compositing, and no batch operation across multiple files—but agents can work around these with existing tools.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      C
      maintenance
      Enables AI agents to perform local image operations such as circle cropping, cropping, resizing, compressing, converting, and inspecting images without uploading files or making network calls.
      7
      89 npm
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to generate and edit local PNG images from text prompts or reference images through an OpenAI-compatible Images API, with selectable style templates and offline reference examples.
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables coding agents to generate and edit images from text prompts, reference images, or saved files through any OpenAI-images-compatible backend, returning each result inline as a downscaled preview (full-res on disk) so the agent can inspect and iterate. Also supports prompt expansion, exact-pixel resizing/recompression, and listing or re-serving saved images.
      AGPL 3.0