内容
# Aseprite MCP 服务器
A [模型上下文协议](https://modelcontextprotocol.io)(MCP)服务器,旨在向Claude等AI助手暴露Aseprite的像素艺术功能。设计用于为2D游戏创建像素艺术风格的精灵、角色和背景。
## 工作原理
服务器在**无头批处理模式**下运行Aseprite - 不需要GUI。对于每个MCP工具调用,它会:
1. 使用Aseprite的脚本API生成Lua脚本
2. 执行 `aseprite --batch --script <file>`
3. 将结果返回给MCP客户端(Claude等)
```
Claude ──工具调用──▶ aseprite-mcp ──Lua脚本──▶ aseprite --batch
◀──结果文本──────────────────────────
```
## 需求
- [Go 1.22+](https://go.dev/dl/)
- [Aseprite](https://www.aseprite.org) v1.2.10 或更高版本(必须使用有效的许可证安装)
## 安装
```bash
git clone https://github.com/rfsbsb/aseprite-mcp
cd aseprite-mcp
go build -o aseprite-mcp .
```
## 配置
| 环境变量 | 默认值 | 描述 |
| --- | --- | --- |
| `ASEPRITE_PATH` | `aseprite` | Aseprite二进制文件的完整路径 |
### 定位Aseprite二进制文件
| 平台 | 典型路径 |
| --- | --- |
| macOS | `/Applications/Aseprite.app/Contents/MacOS/aseprite` |
| Linux | `/usr/bin/aseprite` 或 `~/.local/bin/aseprite` |
| Windows | `C:\Program Files\Aseprite\aseprite.exe` |
## Claude 代码集成
使用CLI将服务器添加到Claude Code MCP配置中:
```bash
claude mcp add aseprite -- /绝对路径/to/aseprite-mcp
```
传递Aseprite二进制文件路径作为环境变量:
```bash
claude mcp add aseprite \
-e ASEPRITE_PATH=/Applications/Aseprite.app/Contents/MacOS/aseprite \
-- /绝对路径/to/aseprite-mcp
```
这会将服务器添加到您项目局部的 `.mcp.json` 文件中。要全局添加(在所有项目中可用):
```bash
claude mcp add aseprite --scope user \
-e ASEPRITE_PATH=/Applications/Aseprite.app/Contents/MacOS/aseprite \
-- /绝对路径/to/aseprite-mcp
```
### 验证连接
在Claude Code会话中,询问:
> “有哪些Aseprite工具可用?”
如果服务器运行正确,Claude将列出所有37种工具。
## 使用指南
### 工作流程:创建游戏角色精灵
这是为2D游戏创建新角色精灵的推荐工作流程:
**步骤 1 — 创建画布**
> “创建一个32x32的RGB精灵并保存到 `/sprites/hero.aseprite`”
像素艺术游戏精灵的典型尺寸:
- **8×8** — 微型精灵,子弹,小瓷砖
- **16×16** — 角色,物品(NES/Game Boy风格)
- **32×32** — 详细的角色(SNES风格)
- **48×48 或 64×64** — 大型角色/ Boss
**步骤 2 — 设置图层**
> “在 `/sprites/hero.aseprite` 中添加一个名为 'outline' 的图层”
> “在 `/sprites/hero.aseprite` 中添加一个名为 'fill' 的图层”
> “在 `/sprites/hero.aseprite` 中添加一个名为 'shadow' 的图层”
**步骤 3 — 应用调色板**
> “将 `/sprites/hero.aseprite` 的调色板设置为以下颜色: `[{"r":34,"g":32,"b":52},{"r":69,"g":40,"b":60},{"r":102,"g":57,"b":49}]`"
**步骤 4 — 绘图**
> “在 `/sprites/hero.aseprite` 的 'fill' 图层中绘制一个填充的矩形,位于 (10,5) 处,大小为 12x20,颜色为 r=102 g=57 b=49”
> “在 `/sprites/hero.aseprite` 的 'outline' 图层中绘制一条从 (10,5) 到 (22,5) 的线,颜色为 r=34 g=32 b=52"
**步骤 5 — 动画**
> “在 `/sprites/hero.aseprite` 中添加 3 个更多帧”
> “在 `/sprites/hero.aseprite` 中创建一个动画标签 'idle',涵盖帧 1-2”
> “在 `/sprites/hero.aseprite` 中创建一个动画标签 'walk',涵盖帧 3-4”
**步骤 6 — 导出**
> “将 `/sprites/hero.aseprite` 导出为水平精灵表 PNG 至 `/sprites/hero_sheet.png`”
> “将 `/sprites/hero.aseprite` 导出为 GIF 至 `/sprites/hero_preview.gif`"
---
### 工作流程:创建 tileset / 背景
> “在 `/sprites/tileset.aseprite` 处创建一个 128x128 的 RGB 精灵”
> “在 `/sprites/tileset.aseprite` 中添加一个名为 'grass' 的图层”
> “在 '/sprites/tileset.aseprite' 的 'grass' 图层中,用 r=106 g=190 b=48 颜色绘制一个填充的矩形,位于 (0,0) 处,大小为 16x16”
> “从 `/sprites/tileset.aseprite` 导出一个精灵表,包含 8 列至 `/sprites/tiles.png`”
---
### 工作流程:检查现有的精灵
> “获取 `/sprites/hero.aseprite` 的信息”
> “列出 `/sprites/hero.aseprite` 中的所有图层”
> “列出 `/sprites/hero.aseprite` 中的所有动画标签”
> “获取 `/sprites/hero.aseprite` 中位于 (5, 10) 处的像素的颜色”
---
### 颜色格式
所有颜色均使用RGBA(每个通道0–255)。Alpha默认为255(完全不透明)。
```
r=255, g=0, b=0, a=255 → 不透明的红色
r=0, g=255, b=0, a=128 → 半透明的绿色
r=0, g=0, b=0, a=0 → 完全透明(擦除)
```
### 坐标系统
坐标从精灵画布的**左上角**为基准,**0-based**。
```
(0,0) ──────── (width-1, 0)
│ │
│ │
(0, height-1) ─ (width-1, height-1)
```
### 帧编号
帧为**1-based**(第一帧 = 1)。这与Aseprite的用户界面显示一致。
---
## 可用工具
### 精灵/画布管理
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `create_sprite` | `width`, `height`, `output_path` | 创建一个新的精灵 |
| `open_sprite` | `file_path` | 验证精灵文件是否可读 |
| `save_sprite` | `file_path` | 就地保存精灵或保存到新路径 |
| `save_sprite_copy` | `file_path`, `output_path` | 导出副本(文件名不变) |
| `get_sprite_info` | `file_path` | 获取大小,颜色模式,图层/帧数 |
| `resize_sprite` | `file_path`, `width`, `height` | 调整画布大小 |
| `crop_sprite` | `file_path`, `x`, `y`, `width`, `height` | 裁剪到矩形 |
| `flatten_layers` | `file_path` | 合并所有图层到一个 |
可选:`color_mode` (rgb/灰度/索引,默认:rgb)。
### 图层管理
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `add_layer` | `file_path`, `name` | 添加图层(可选:`type`:normal/group) |
| `delete_layer` | `file_path`, `name` | 按名称删除图层 |
| `set_layer_opacity` | `file_path`, `name`, `opacity` | 不透明度 0–255 |
| `set_layer_blend_mode` | `file_path`, `name`, `blend_mode` | 参见下面的混合模式 |
| `set_layer_visibility` | `file_path`, `name`, `visible` | true/false |
| `get_layers` | `file_path` | 列出所有图层 |
混合模式:`normal`, `multiply`, `screen`, `overlay`, `darken`, `lighten`, `colordodge`, `colorburn`, `hardlight`, `softlight`, `difference`, `exclusion`, `hue`, `saturation`, `color`, `luminosity`, `addition`, `subtract`, `divide`.
### 绘图
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `draw_pixel` | `file_path`, `x`, `y`, `r`, `g`, `b` | 单个像素 |
| `draw_pixels` | `file_path`, `pixels` | 批量像素为JSON数组 |
| `draw_line` | `file_path`, `x1`, `y1`, `x2`, `y2`, `r`, `g`, `b` | Bresenham线 |
| `draw_rectangle` | `file_path`, `x`, `y`, `width`, `height`, `r`, `g`, `b` | 可选:`filled` |
| `draw_circle` | `file_path`, `x`, `y`, `width`, `height`, `r`, `g`, `b` | 可选:`filled` |
| `fill_area` | `file_path`, `x`, `y`, `r`, `g`, `b` | 洪水填充 |
| `clear_layer` | `file_path` | 擦除整个图层单元格 |
所有绘图工具均接受可选参数:`a` (alpha,默认255),`layer` (名称),`frame` (1-based,默认1)。
`draw_pixels` 格式:
```json
[{"x": 0, "y": 0, "r": 255, "g": 0, "b": 0, "a": 255}, ...]
```
### 颜色和调色板
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `get_palette` | `file_path` | 列出所有调色板颜色 |
| `set_palette_color` | `file_path`, `index`, `r`, `g`, `b` | 设置一个条目 |
| `set_palette` | `file_path`, `colors` | 替换整个调色板(JSON数组) |
| `load_palette` | `file_path`, `palette_path` | 从.ase/.gpl/.png文件加载 |
`set_palette` 颜色格式:
```json
[{"r": 0, "g": 0, "b": 0, "a": 255}, {"r": 255, "g": 255, "b": 255}]
```
### 帧和动画
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `add_frame` | `file_path` | 添加空帧(可选:`at` 位置) |
| `duplicate_frame` | `file_path`, `frame` | 复制帧内容 |
| `delete_frame` | `file_path`, `frame` | 按索引删除帧 |
| `set_frame_duration` | `file_path`, `frame`, `duration_ms` | 毫秒为单位的持续时间 |
| `create_tag` | `file_path`, `name`, `from_frame`, `to_frame` | 可选:`ani_dir` |
| `delete_tag` | `file_path`, `name` | 删除标签 |
| `get_frames` | `file_path` | 列出帧及其持续时间 |
| `get_tags` | `file_path` | 列出动画标签 |
动画方向(`ani_dir`):`forward` (默认),`reverse`,`pingpong`.
### 选择
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `select_rectangle` | `file_path`, `x`, `y`, `width`, `height` | 可选:`mode` |
| `select_all` | `file_path` | 选择整个画布 |
| `deselect` | `file_path` | 清除选择 |
| `invert_selection` | `file_path` | 反转选择 |
| `fill_selection` | `file_path`, `r`, `g`, `b` | 用颜色填充 |
选择模式:`replace` (默认),`add`,`subtract`,`intersect`.
### 变换
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `flip_sprite` | `file_path`, `direction` | `horizontal` 或 `vertical` |
| `rotate_sprite` | `file_path`, `degrees` | `90`,`180` 或 `270` |
| `scale_sprite` | `file_path`, `width`, `height` | 最近邻(像素完美) |
### 导出
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `export_png` | `file_path`, `output_path` | 可选:`frame` 索引 |
| `export_gif` | `file_path`, `output_path` | 动画GIF |
| `export_spritesheet` | `file_path`, `output_path` | 可选:`layout`,`columns` |
| `export_layer_as_png` | `file_path`, `output_path`, `layer` | 可选:`frame` |
精灵表布局:`horizontal` (默认),`vertical`,`packed`.
### 像素检查
| 工具 | 必需参数 | 描述 |
| --- | --- | --- |
| `get_pixel` | `file_path`, `x`, `y` | 一个像素的RGBA |
| `get_pixels_region` | `file_path`, `x`, `y`, `width`, `height` | 区域最多4096个像素 |
---
## 测试
### 单元测试(不需要Aseprite)
```bash
go test ./...
```
运行 **61个单元测试**,涵盖:
- Lua脚本构建器(`lua/`)
- 执行器临时文件生命周期和错误处理(`executor/`)
- 参数验证助手 - `requireString`,`requireInt`,`optString`,`optInt`,`optBool` (`tools/`)
- 每个工具处理器的输入验证(无效尺寸,未知颜色模式,错误JSON等)
- 像素艺术几何 - Bresenham线算法和椭圆绘制器(`tools/`)
### 集成测试(需要Aseprite)
```bash
ASEPRITE_PATH=/Applications/Aseprite.app/Contents/MacOS/aseprite \
go test ./tools/... -run Integration -v
```
集成测试验证整个循环:创建磁盘上的精灵,修改它,检查它,然后导出它。它们涵盖:
| 测试 | 验证内容 |
| --- | --- |
| `TestIntegration_CreateSprite` | 精灵文件在磁盘上创建 |
| `TestIntegration_GetSpriteInfo` | 元数据(大小,颜色模式)正确循环 |
| `TestIntegration_AddAndGetLayers` | 图层被持久化和列出 |
| `TestIntegration_AnimationTagWorkflow` | 添加帧,创建并列出标签 |
| `TestIntegration_ExportPNG` | 生成PNG文件 |
| `TestIntegration_DrawPixelAndReadBack` | 像素颜色在写入→读取后存活 |
| `TestIntegration_PaletteRoundTrip` | 调色板颜色在设置→获取后存活 |
| `TestIntegration_ResizeAndCrop` | 尺寸反映resize+crop |
| `TestIntegration_FlattenLayers` | 扁平化后保留单个图层 |
如果未设置`ASEPRITE_PATH`,集成测试会自动**跳过**,因此它们不会在没有Aseprite的情况下阻塞CI/CD管道。
### 运行覆盖率
```bash
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out
```
---
## 架构
```
main.go → 入口点:加载配置,创建StdioServer,启动监听
config/config.go → 读取ASEPRITE_PATH环境变量
executor/
executor.go → 通过CLI运行Lua脚本,处理结果JSON文件
lua/
templates.go → 类型安全的Lua脚本构建器(EscapeLua, ColorRGBA等)
server/server.go → 创建MCPServer,注册所有工具组
tools/
sprite.go → 精灵/画布工具 + 共享参数助手
layer.go → 图层管理工具
drawing.go → 像素绘图工具 + Bresenham/椭圆算法
palette.go → 颜色调色板工具
animation.go → 帧和动画标签工具
selection.go → 选择工具
transform.go → 翻转/旋转/缩放工具
export.go → PNG/GIF/精灵表导出工具
pixels.go → 像素读取工具
export_test_helpers.go → 导出内部函数测试(仅测试)
```
## 许可证
MIT
连接信息
猜你喜欢
rea
用于编码代理的逆向工程:一个 CLI 和 MCP 服务器,用于检查任何应用程序,了解其功能,并按照您的方式构建它们。
everything-claude-code
完整的 Claude Code 配置集合 - agents、skills、hooks、commands、rules、MCPs。来自 Anthropic...
Firecrawl
Firecrawl MCP Server 实现了网页抓取、爬虫和内容提取功能。
MarkItDown MCP
MarkItDown-MCP是一个轻量级服务器,用于将URI转换为Markdown。
cc-switch
跨平台的Claude Code、Codex和Gemini CLI一体化助手。
Agent-Reach
赋予你的AI Agent观察整个互联网的眼睛。读取并搜索...