@ikijs/mcp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/chunk-WAWSLTKU.mjs +535 -0
- package/dist/chunk-WAWSLTKU.mjs.map +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +554 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +16 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.mts +96 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +564 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zeikar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @ikijs/mcp
|
|
2
|
+
|
|
3
|
+
A stdio [MCP](https://modelcontextprotocol.io/) server that exposes `.iki` model tools to AI agents (Claude, LLMs, and any MCP-compatible client): read/validate a model, and auto-rig one from role-named PNG layers.
|
|
4
|
+
|
|
5
|
+
## Tools
|
|
6
|
+
|
|
7
|
+
| Tool | Description |
|
|
8
|
+
| -------------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
9
|
+
| `validate_iki` | Validate a raw `.iki` model — accepts an object or a JSON string — fail-fast, one error at a time. |
|
|
10
|
+
| `describe_iki` | Return a structured summary of a valid model's canvas, parameters, parts, and deformers. |
|
|
11
|
+
| `list_standard_parameters` | List the recommended standard parameter ids (e.g. `ParamAngleX`, `ParamMouthOpenY`) with descriptions. |
|
|
12
|
+
| `auto_rig_from_layers` | Auto-rig role-named PNG layers into a renderable `.iki` written to disk; returns the output path. |
|
|
13
|
+
|
|
14
|
+
The `model` input for `validate_iki` and `describe_iki` accepts either a plain JSON object or a JSON string — the server normalises both.
|
|
15
|
+
|
|
16
|
+
### `auto_rig_from_layers`
|
|
17
|
+
|
|
18
|
+
Turns a set of role-named, full-canvas transparent PNG layers into a renderable, validated `.iki` model with the textures atlased and embedded as a base64 `data:image/png` URI.
|
|
19
|
+
|
|
20
|
+
- **`layers`** — array of `{ path, fileName? }`. `path` is a PNG file path (resolved against the server's working directory); the role is derived from `fileName ?? basename(path)`. Required roles: `face`, `eye_L`, `eye_R`, `mouth`. Optional roles include `iris_L`/`iris_R`, `brow_L`/`brow_R`, `hair_front`/`hair_back`, `lash_L`/`lash_R`, etc. All layers must share the same canvas size (taken from the first layer).
|
|
21
|
+
- **`outputPath`** — optional `.iki` output path (resolved against the working directory; the parent directory must already exist, and the path must end in `.iki`). Defaults to `auto-rigged-model.iki`.
|
|
22
|
+
- **Result** — on success the result text is the written file path and `structuredContent` carries `{ ok: true, path, canvas, partCount, atlasBytes }`. The (potentially multi-MB) model is written to disk, not inlined. Invalid input (unknown/missing role, empty layer, mismatched sizes, bad path, oversized atlas) returns `{ ok: false, error }` with a `INVALID: …` text — not a protocol error.
|
|
23
|
+
|
|
24
|
+
The decode/atlas pipeline runs in Node via `sharp` (a native dependency confined to this package); it mirrors the browser editor app's import flow and reuses the pure `@ikijs/editor` model + atlas math, so both paths produce the same rig.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Run directly with npx (no install needed):
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx -y @ikijs/mcp
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or install globally and use the `iki-mcp` bin:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install -g @ikijs/mcp
|
|
38
|
+
iki-mcp
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Claude Desktop / Claude Code config
|
|
42
|
+
|
|
43
|
+
Add to your MCP server config:
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{ "mcpServers": { "iki": { "command": "npx", "args": ["-y", "@ikijs/mcp"] } } }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
For Claude Desktop this goes in `claude_desktop_config.json`; for Claude Code it goes in `.claude/mcp.json` (or the equivalent per-project config).
|
|
50
|
+
|
|
51
|
+
## Scope note
|
|
52
|
+
|
|
53
|
+
Current tools cover read/validate plus auto-rigging a model from PNG layers (`auto_rig_from_layers`). PSD input and granular model-mutation primitives (add part, bind parameter, export) are deferred to future slices.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT © Zeikar
|
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
// src/tools.ts
|
|
2
|
+
import fs2 from "fs";
|
|
3
|
+
import path2 from "path";
|
|
4
|
+
import {
|
|
5
|
+
parseIkiModel,
|
|
6
|
+
loadIkiModel,
|
|
7
|
+
IkiFormatError,
|
|
8
|
+
StandardParameter
|
|
9
|
+
} from "@ikijs/format";
|
|
10
|
+
import {
|
|
11
|
+
EditorDocument,
|
|
12
|
+
packAtlas,
|
|
13
|
+
uvRectFor,
|
|
14
|
+
generateIkiFromLayerSet,
|
|
15
|
+
parseLayerRoles
|
|
16
|
+
} from "@ikijs/editor";
|
|
17
|
+
|
|
18
|
+
// src/node-images.ts
|
|
19
|
+
import sharp from "sharp";
|
|
20
|
+
import { detectAlphaBbox as scanAlphaBbox } from "@ikijs/editor";
|
|
21
|
+
|
|
22
|
+
// src/limits.ts
|
|
23
|
+
import fs from "fs";
|
|
24
|
+
import path from "path";
|
|
25
|
+
var MAX_LAYERS = 64;
|
|
26
|
+
var MAX_LAYER_DIM = 4096;
|
|
27
|
+
var MAX_CANVAS_DIM = 4096;
|
|
28
|
+
var MAX_ATLAS_AREA = 4096 * 4096;
|
|
29
|
+
var MAX_INPUT_PIXELS = 4096 * 4096;
|
|
30
|
+
var MAX_TOTAL_PIXELS = 64 * 1024 * 1024;
|
|
31
|
+
var MAX_OUTPUT_BYTES = 5 * 1024 * 1024;
|
|
32
|
+
var AutoRigInputError = class extends Error {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "AutoRigInputError";
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
function resolveInputPath(inputPath) {
|
|
39
|
+
if (typeof inputPath !== "string" || inputPath.trim() === "") {
|
|
40
|
+
throw new AutoRigInputError("layer path is empty");
|
|
41
|
+
}
|
|
42
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(inputPath)) {
|
|
43
|
+
throw new AutoRigInputError(
|
|
44
|
+
`layer path must be a file path, not a URL: ${inputPath}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const resolved = path.resolve(process.cwd(), inputPath);
|
|
48
|
+
let stat;
|
|
49
|
+
try {
|
|
50
|
+
stat = fs.statSync(resolved);
|
|
51
|
+
} catch {
|
|
52
|
+
throw new AutoRigInputError(`layer file not found: ${resolved}`);
|
|
53
|
+
}
|
|
54
|
+
if (stat.isDirectory()) {
|
|
55
|
+
throw new AutoRigInputError(
|
|
56
|
+
`layer path is a directory, not a file: ${resolved}`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return resolved;
|
|
60
|
+
}
|
|
61
|
+
function resolveOutputPath(outputPath) {
|
|
62
|
+
if (typeof outputPath !== "string" || outputPath.trim() === "") {
|
|
63
|
+
throw new AutoRigInputError("output path is empty");
|
|
64
|
+
}
|
|
65
|
+
if (!outputPath.toLowerCase().endsWith(".iki")) {
|
|
66
|
+
throw new AutoRigInputError(`output path must end in .iki: ${outputPath}`);
|
|
67
|
+
}
|
|
68
|
+
const root = process.cwd();
|
|
69
|
+
const resolved = path.resolve(root, outputPath);
|
|
70
|
+
const dir = path.dirname(resolved);
|
|
71
|
+
let stat;
|
|
72
|
+
try {
|
|
73
|
+
stat = fs.statSync(dir);
|
|
74
|
+
} catch {
|
|
75
|
+
throw new AutoRigInputError(`output directory does not exist: ${dir}`);
|
|
76
|
+
}
|
|
77
|
+
if (!stat.isDirectory()) {
|
|
78
|
+
throw new AutoRigInputError(`output parent is not a directory: ${dir}`);
|
|
79
|
+
}
|
|
80
|
+
const realRoot = fs.realpathSync(root);
|
|
81
|
+
const realDir = fs.realpathSync(dir);
|
|
82
|
+
if (realDir !== realRoot && !realDir.startsWith(realRoot + path.sep)) {
|
|
83
|
+
throw new AutoRigInputError(
|
|
84
|
+
`output path escapes the working directory: ${outputPath}`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return path.join(realDir, path.basename(resolved));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/node-images.ts
|
|
91
|
+
async function decodePng(filePath) {
|
|
92
|
+
try {
|
|
93
|
+
const pipeline = sharp(filePath, { limitInputPixels: MAX_INPUT_PIXELS });
|
|
94
|
+
const metadata = await pipeline.metadata();
|
|
95
|
+
if (metadata.format !== "png") {
|
|
96
|
+
throw new AutoRigInputError(
|
|
97
|
+
`non-PNG image (${metadata.format ?? "unknown"}): ${filePath}`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
const { data, info } = await pipeline.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
101
|
+
return { width: info.width, height: info.height, rgba: data };
|
|
102
|
+
} catch (e) {
|
|
103
|
+
if (e instanceof AutoRigInputError) throw e;
|
|
104
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
105
|
+
throw new AutoRigInputError(`failed to decode PNG ${filePath}: ${msg}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function detectAlphaBbox(rgba, width, height) {
|
|
109
|
+
const bbox = scanAlphaBbox(rgba, width, height);
|
|
110
|
+
if (bbox === null) {
|
|
111
|
+
throw new AutoRigInputError("layer is empty after alpha threshold");
|
|
112
|
+
}
|
|
113
|
+
return bbox;
|
|
114
|
+
}
|
|
115
|
+
function cropToBuffer(rgba, width, height, bbox) {
|
|
116
|
+
return sharp(rgba, { raw: { width, height, channels: 4 } }).extract({ left: bbox.x, top: bbox.y, width: bbox.w, height: bbox.h }).png().toBuffer();
|
|
117
|
+
}
|
|
118
|
+
async function renderAtlasToDataUri(crops, layout) {
|
|
119
|
+
const byId = /* @__PURE__ */ new Map();
|
|
120
|
+
for (const crop of crops) byId.set(crop.id, crop);
|
|
121
|
+
const pad = layout.padding;
|
|
122
|
+
const composites = await Promise.all(
|
|
123
|
+
layout.placements.map(async (placement) => {
|
|
124
|
+
const crop = byId.get(placement.id);
|
|
125
|
+
if (crop === void 0) {
|
|
126
|
+
throw new AutoRigInputError(
|
|
127
|
+
`no crop for atlas placement "${placement.id}"`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const input = pad > 0 ? await sharp(crop.buffer).extend({ right: pad, bottom: pad, extendWith: "copy" }).png().toBuffer() : crop.buffer;
|
|
131
|
+
return { input, left: placement.x, top: placement.y };
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
const page = await sharp({
|
|
135
|
+
create: {
|
|
136
|
+
width: layout.pageWidth,
|
|
137
|
+
height: layout.pageHeight,
|
|
138
|
+
channels: 4,
|
|
139
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
|
140
|
+
}
|
|
141
|
+
}).composite(composites).png().toBuffer();
|
|
142
|
+
return `data:image/png;base64,${page.toString("base64")}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/tools.ts
|
|
146
|
+
function coerceModel(model) {
|
|
147
|
+
try {
|
|
148
|
+
const parsed = typeof model === "string" ? loadIkiModel(model) : parseIkiModel(model);
|
|
149
|
+
return { ok: true, model: parsed };
|
|
150
|
+
} catch (e) {
|
|
151
|
+
if (e instanceof IkiFormatError) return { ok: false, error: e.message };
|
|
152
|
+
throw e;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function validateIki(model) {
|
|
156
|
+
const result = coerceModel(model);
|
|
157
|
+
if (!result.ok) return { ok: false, error: result.error };
|
|
158
|
+
return { ok: true };
|
|
159
|
+
}
|
|
160
|
+
function describeIki(model) {
|
|
161
|
+
const result = coerceModel(model);
|
|
162
|
+
if (!result.ok) return { ok: false, error: result.error };
|
|
163
|
+
const m = result.model;
|
|
164
|
+
const parameters = m.parameters.map((p) => ({
|
|
165
|
+
id: p.id,
|
|
166
|
+
min: p.min,
|
|
167
|
+
max: p.max,
|
|
168
|
+
default: p.default
|
|
169
|
+
}));
|
|
170
|
+
const parts = m.parts.map((p) => {
|
|
171
|
+
const entry = {
|
|
172
|
+
id: p.id,
|
|
173
|
+
order: p.order
|
|
174
|
+
};
|
|
175
|
+
if (p.deformer !== void 0) entry.deformer = p.deformer;
|
|
176
|
+
return entry;
|
|
177
|
+
});
|
|
178
|
+
const deformers = (m.deformers ?? []).map(
|
|
179
|
+
(d) => {
|
|
180
|
+
const kind = d.kind === "warp" ? "warp" : "matrix";
|
|
181
|
+
const entry = { id: d.id, kind };
|
|
182
|
+
if (d.parent !== void 0) entry.parent = d.parent;
|
|
183
|
+
if (d.kind === "warp") {
|
|
184
|
+
if (d.warp2d !== void 0) {
|
|
185
|
+
entry.warp = {
|
|
186
|
+
mode: "2d",
|
|
187
|
+
parameterX: d.warp2d.parameter,
|
|
188
|
+
parameterY: d.warp2d.parameterY,
|
|
189
|
+
gridX: d.grid.cols,
|
|
190
|
+
gridY: d.grid.rows
|
|
191
|
+
};
|
|
192
|
+
} else if (d.warps !== void 0 && d.warps.length > 0) {
|
|
193
|
+
entry.warp = {
|
|
194
|
+
mode: "1d",
|
|
195
|
+
parameters: d.warps.map((w) => w.parameter)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return entry;
|
|
200
|
+
}
|
|
201
|
+
);
|
|
202
|
+
const summary = {
|
|
203
|
+
name: m.name,
|
|
204
|
+
canvas: { width: m.canvas.width, height: m.canvas.height },
|
|
205
|
+
parameters,
|
|
206
|
+
parts,
|
|
207
|
+
deformers
|
|
208
|
+
};
|
|
209
|
+
return { ok: true, summary };
|
|
210
|
+
}
|
|
211
|
+
var STANDARD_PARAMETER_INFO = [
|
|
212
|
+
{
|
|
213
|
+
id: StandardParameter.MouthOpen,
|
|
214
|
+
description: "Mouth open amount for lip-sync (0 closed .. 1 open)."
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
id: StandardParameter.MouthForm,
|
|
218
|
+
description: "Mouth form / smile (-1 .. 1)."
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
id: StandardParameter.EyeOpenLeft,
|
|
222
|
+
description: "Left eye open (0 closed .. 1 open). Drive with the right eye for a blink."
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
id: StandardParameter.EyeOpenRight,
|
|
226
|
+
description: "Right eye open (0 closed .. 1 open)."
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
id: StandardParameter.EyeballX,
|
|
230
|
+
description: "Eyeball gaze, horizontal (-1 .. 1)."
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
id: StandardParameter.EyeballY,
|
|
234
|
+
description: "Eyeball gaze, vertical (-1 .. 1)."
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: StandardParameter.AngleX,
|
|
238
|
+
description: "Head angle, horizontal degrees."
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
id: StandardParameter.AngleY,
|
|
242
|
+
description: "Head angle, vertical degrees."
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
id: StandardParameter.AngleZ,
|
|
246
|
+
description: "Head tilt / roll degrees."
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
id: StandardParameter.Breath,
|
|
250
|
+
description: "Idle breath (0 .. 1), cycled by the host."
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
id: StandardParameter.BrowLeftY,
|
|
254
|
+
description: "Left brow raise/lower (-1 .. 1)."
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
id: StandardParameter.BrowRightY,
|
|
258
|
+
description: "Right brow raise/lower (-1 .. 1)."
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: StandardParameter.BrowLeftAngle,
|
|
262
|
+
description: "Left brow tilt/angle (-1 .. 1)."
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
id: StandardParameter.BrowRightAngle,
|
|
266
|
+
description: "Right brow tilt/angle (-1 .. 1)."
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
id: StandardParameter.HairSwayX,
|
|
270
|
+
description: "Horizontal hair-sway driver. Physics OUTPUT \u2014 driven by the spring, hosts should not set it directly."
|
|
271
|
+
}
|
|
272
|
+
];
|
|
273
|
+
function listStandardParameters() {
|
|
274
|
+
return STANDARD_PARAMETER_INFO.map((p) => ({ ...p }));
|
|
275
|
+
}
|
|
276
|
+
function expectInput(label, fn) {
|
|
277
|
+
try {
|
|
278
|
+
return fn();
|
|
279
|
+
} catch (e) {
|
|
280
|
+
throw new AutoRigInputError(
|
|
281
|
+
`${label}: ${e instanceof Error ? e.message : String(e)}`
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async function autoRigFromLayers(input) {
|
|
286
|
+
try {
|
|
287
|
+
const layers = input.layers;
|
|
288
|
+
if (!Array.isArray(layers) || layers.length === 0) {
|
|
289
|
+
throw new AutoRigInputError("layers must be a non-empty array");
|
|
290
|
+
}
|
|
291
|
+
if (layers.length > MAX_LAYERS) {
|
|
292
|
+
throw new AutoRigInputError(
|
|
293
|
+
`too many layers: ${layers.length} > ${MAX_LAYERS}`
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
const outPath = resolveOutputPath(
|
|
297
|
+
input.outputPath ?? "auto-rigged-model.iki"
|
|
298
|
+
);
|
|
299
|
+
const resolvedLayers = layers.map((layer) => {
|
|
300
|
+
const resolved = resolveInputPath(layer.path);
|
|
301
|
+
return { resolved, fileName: layer.fileName ?? path2.basename(resolved) };
|
|
302
|
+
});
|
|
303
|
+
const rolePairs = expectInput(
|
|
304
|
+
"role parsing",
|
|
305
|
+
() => parseLayerRoles(resolvedLayers.map((r) => r.fileName))
|
|
306
|
+
);
|
|
307
|
+
const roleByFileName = new Map(rolePairs.map((p) => [p.fileName, p.role]));
|
|
308
|
+
let canvasW = 0;
|
|
309
|
+
let canvasH = 0;
|
|
310
|
+
let totalPixels = 0;
|
|
311
|
+
const layerInputs = [];
|
|
312
|
+
const crops = [];
|
|
313
|
+
for (let i = 0; i < resolvedLayers.length; i++) {
|
|
314
|
+
const { resolved, fileName } = resolvedLayers[i];
|
|
315
|
+
const png = await decodePng(resolved);
|
|
316
|
+
if (png.width > MAX_LAYER_DIM || png.height > MAX_LAYER_DIM) {
|
|
317
|
+
throw new AutoRigInputError(
|
|
318
|
+
`layer ${resolved} dimension ${png.width}x${png.height} exceeds ${MAX_LAYER_DIM}`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
totalPixels += png.width * png.height;
|
|
322
|
+
if (totalPixels > MAX_TOTAL_PIXELS) {
|
|
323
|
+
throw new AutoRigInputError(
|
|
324
|
+
`total decoded pixels exceed ${MAX_TOTAL_PIXELS}`
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
if (i === 0) {
|
|
328
|
+
canvasW = png.width;
|
|
329
|
+
canvasH = png.height;
|
|
330
|
+
if (canvasW > MAX_CANVAS_DIM || canvasH > MAX_CANVAS_DIM) {
|
|
331
|
+
throw new AutoRigInputError(
|
|
332
|
+
`canvas ${canvasW}x${canvasH} exceeds ${MAX_CANVAS_DIM}`
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
} else if (png.width !== canvasW || png.height !== canvasH) {
|
|
336
|
+
throw new AutoRigInputError(
|
|
337
|
+
`layer "${fileName}" size ${png.width}x${png.height} differs from canvas ${canvasW}x${canvasH}`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
const role = roleByFileName.get(fileName);
|
|
341
|
+
if (role === void 0) {
|
|
342
|
+
throw new AutoRigInputError(`no role resolved for "${fileName}"`);
|
|
343
|
+
}
|
|
344
|
+
let bbox;
|
|
345
|
+
try {
|
|
346
|
+
bbox = detectAlphaBbox(png.rgba, png.width, png.height);
|
|
347
|
+
} catch (e) {
|
|
348
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
349
|
+
throw new AutoRigInputError(
|
|
350
|
+
`role "${role}" file "${fileName}": ${msg}`
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
|
|
354
|
+
layerInputs.push({
|
|
355
|
+
role,
|
|
356
|
+
fileName,
|
|
357
|
+
canvasW,
|
|
358
|
+
canvasH,
|
|
359
|
+
bbox,
|
|
360
|
+
cropW: bbox.w,
|
|
361
|
+
cropH: bbox.h
|
|
362
|
+
});
|
|
363
|
+
crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
|
|
364
|
+
}
|
|
365
|
+
const model = generateIkiFromLayerSet(layerInputs, {
|
|
366
|
+
width: canvasW,
|
|
367
|
+
height: canvasH
|
|
368
|
+
});
|
|
369
|
+
const doc = new EditorDocument(model);
|
|
370
|
+
const layout = packAtlas(
|
|
371
|
+
crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
|
|
372
|
+
);
|
|
373
|
+
if (layout.pageWidth * layout.pageHeight > MAX_ATLAS_AREA) {
|
|
374
|
+
throw new AutoRigInputError(
|
|
375
|
+
`atlas page ${layout.pageWidth}x${layout.pageHeight} exceeds max area ${MAX_ATLAS_AREA}`
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
const dataUri = await renderAtlasToDataUri(crops, layout);
|
|
379
|
+
if (dataUri.length > MAX_OUTPUT_BYTES) {
|
|
380
|
+
throw new AutoRigInputError(
|
|
381
|
+
`atlas data URI ${dataUri.length} bytes exceeds ${MAX_OUTPUT_BYTES}`
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
const partTextureAssignments = crops.map((crop) => {
|
|
385
|
+
const placement = layout.placements.find((p) => p.id === crop.id);
|
|
386
|
+
if (placement === void 0) {
|
|
387
|
+
throw new Error(`auto-rig: no atlas placement for "${crop.id}"`);
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
partId: crop.id,
|
|
391
|
+
uv: uvRectFor(placement, {
|
|
392
|
+
width: layout.pageWidth,
|
|
393
|
+
height: layout.pageHeight
|
|
394
|
+
})
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
doc.applyAtlas({ textures: [{ source: dataUri }], partTextureAssignments });
|
|
398
|
+
const finalModel = parseIkiModel(doc.getModel());
|
|
399
|
+
expectInput("write", () => {
|
|
400
|
+
const tmp = `${outPath}.${process.pid}.${Date.now()}.tmp`;
|
|
401
|
+
fs2.writeFileSync(tmp, JSON.stringify(finalModel));
|
|
402
|
+
try {
|
|
403
|
+
fs2.renameSync(tmp, outPath);
|
|
404
|
+
} catch (e) {
|
|
405
|
+
try {
|
|
406
|
+
fs2.rmSync(tmp, { force: true });
|
|
407
|
+
} catch {
|
|
408
|
+
}
|
|
409
|
+
throw e;
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
return {
|
|
413
|
+
ok: true,
|
|
414
|
+
path: outPath,
|
|
415
|
+
canvas: { width: canvasW, height: canvasH },
|
|
416
|
+
partCount: finalModel.parts.length,
|
|
417
|
+
atlasBytes: dataUri.length
|
|
418
|
+
};
|
|
419
|
+
} catch (err) {
|
|
420
|
+
if (err instanceof AutoRigInputError)
|
|
421
|
+
return { ok: false, error: err.message };
|
|
422
|
+
throw err;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// src/server.ts
|
|
427
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
428
|
+
import { z } from "zod";
|
|
429
|
+
function createIkiMcpServer() {
|
|
430
|
+
const server = new McpServer({ name: "iki", version: "0.1.0" });
|
|
431
|
+
server.registerTool(
|
|
432
|
+
"validate_iki",
|
|
433
|
+
{
|
|
434
|
+
description: "Validates a raw .iki model (object or JSON string); fail-fast \u2014 returns at most ONE error, so fix one, re-run.",
|
|
435
|
+
inputSchema: {
|
|
436
|
+
model: z.unknown().describe("Raw .iki model JSON (object or JSON string)")
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
async ({ model }) => {
|
|
440
|
+
try {
|
|
441
|
+
const r = validateIki(model);
|
|
442
|
+
const text = r.ok ? "OK" : `INVALID: ${r.error}`;
|
|
443
|
+
return { content: [{ type: "text", text }], structuredContent: r };
|
|
444
|
+
} catch (err) {
|
|
445
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
446
|
+
return {
|
|
447
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
448
|
+
structuredContent: { ok: false, error },
|
|
449
|
+
isError: true
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
);
|
|
454
|
+
server.registerTool(
|
|
455
|
+
"describe_iki",
|
|
456
|
+
{
|
|
457
|
+
description: "Summarizes a valid model's canvas/params/parts/deformers; returns an error for an invalid model.",
|
|
458
|
+
inputSchema: {
|
|
459
|
+
model: z.unknown().describe("Raw .iki model JSON (object or JSON string)")
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
async ({ model }) => {
|
|
463
|
+
try {
|
|
464
|
+
const r = describeIki(model);
|
|
465
|
+
const text = r.ok ? JSON.stringify(r.summary, null, 2) : `INVALID: ${r.error}`;
|
|
466
|
+
return { content: [{ type: "text", text }], structuredContent: r };
|
|
467
|
+
} catch (err) {
|
|
468
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
469
|
+
return {
|
|
470
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
471
|
+
structuredContent: { ok: false, error },
|
|
472
|
+
isError: true
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
);
|
|
477
|
+
server.registerTool(
|
|
478
|
+
"list_standard_parameters",
|
|
479
|
+
{
|
|
480
|
+
description: "Lists the recommended standard parameter ids a host can drive without per-model wiring.",
|
|
481
|
+
inputSchema: {}
|
|
482
|
+
},
|
|
483
|
+
async () => {
|
|
484
|
+
const params = listStandardParameters();
|
|
485
|
+
const text = params.map((p) => `${p.id} \u2014 ${p.description}`).join("\n");
|
|
486
|
+
return {
|
|
487
|
+
content: [{ type: "text", text }],
|
|
488
|
+
structuredContent: { parameters: params }
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
);
|
|
492
|
+
server.registerTool(
|
|
493
|
+
"auto_rig_from_layers",
|
|
494
|
+
{
|
|
495
|
+
description: "Auto-rigs role-named PNG layers (face, eye_L/eye_R, mouth required; iris_L/R, brow_L/R, hair_front, etc. optional) into a renderable .iki written to disk. Pass full-canvas PNG file paths; returns the output path + summary (the model is NOT inlined). Filenames map to roles unless `fileName` is given.",
|
|
496
|
+
inputSchema: {
|
|
497
|
+
layers: z.array(
|
|
498
|
+
z.object({
|
|
499
|
+
fileName: z.string().optional().describe(
|
|
500
|
+
"Role-bearing filename; defaults to the basename of `path`."
|
|
501
|
+
),
|
|
502
|
+
path: z.string().describe("PNG file path (resolved against cwd).")
|
|
503
|
+
})
|
|
504
|
+
).describe("Full-canvas PNG layers; all must share the same size."),
|
|
505
|
+
outputPath: z.string().optional().describe(
|
|
506
|
+
"Output .iki path (resolved against cwd; parent dir must exist)."
|
|
507
|
+
)
|
|
508
|
+
}
|
|
509
|
+
},
|
|
510
|
+
async (args) => {
|
|
511
|
+
try {
|
|
512
|
+
const r = await autoRigFromLayers(args);
|
|
513
|
+
const text = r.ok ? r.path : `INVALID: ${r.error}`;
|
|
514
|
+
return { content: [{ type: "text", text }], structuredContent: r };
|
|
515
|
+
} catch (err) {
|
|
516
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
517
|
+
return {
|
|
518
|
+
content: [{ type: "text", text: `Unexpected error: ${error}` }],
|
|
519
|
+
structuredContent: { ok: false, error },
|
|
520
|
+
isError: true
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
);
|
|
525
|
+
return server;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
export {
|
|
529
|
+
validateIki,
|
|
530
|
+
describeIki,
|
|
531
|
+
listStandardParameters,
|
|
532
|
+
autoRigFromLayers,
|
|
533
|
+
createIkiMcpServer
|
|
534
|
+
};
|
|
535
|
+
//# sourceMappingURL=chunk-WAWSLTKU.mjs.map
|