Spaces:
Running

File size: 28,053 Bytes
6ed91fb
 
 
 
 
 
 
 
 
 
270f46d
 
950662b
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed91fb
 
 
270f46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed91fb
 
950662b
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Optional, paid sprite generation with FLUX.1-dev through Hugging Face Inference Providers.
// Everything else in this server is free and offline. sharp and @huggingface/inference are
// loaded lazily so the map tools keep working even if those packages fail to install.
// In hosted mode the call is billed to the caller's own token and the sprite stays in their
// session (src/sprites.ts) instead of the server's disk.

import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { buildBuildingPrompt, buildPrompt, NEGATIVE_PROMPT, type ThemeKey } from "../themes/prompts.js";
import { BUILDINGS, BUILDING_TYPES, buildingSpriteFile } from "../buildings.js";
import { themeAssetDir, themeKeySchema } from "../themes/index.js";
import { THEME_ROLES } from "../types.js";
import { getMap, overrideTileImage } from "../builder.js";
import { assetsRoot, resolveUserPath } from "../paths.js";
import { ASSET_PREFIX } from "../tmj.js";
import { currentScope, isHosted } from "../context.js";
import { loadSharp, type Sharp } from "../sharp.js";
import { saveSprite, spriteExists } from "../sprites.js";
import { asText } from "./util.js";

/**
 * Magenta chroma key. FLUX is asked for a pure #FF00FF background, a colour no gothic sprite
 * contains, so removing it keeps the subject intact (deep blacks, shadows, iron included).
 * Uses R/G and B/G ratios to also catch the desaturated magenta FLUX often produces
 * (e.g. R142 G41 B100). Fully magenta pixels become transparent neutral grey (no halo after
 * resize); partially magenta edge pixels fade out and are desaturated.
 */
async function chromaKeyMagenta(sharp: Sharp, buf: Buffer): Promise<Buffer> {
  const { data, info } = await sharp(buf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
  const out = Buffer.from(data);
  for (let i = 0; i < data.length; i += 4) {
    const r = data[i];
    const g = data[i + 1];
    const b = data[i + 2];
    const gSafe = Math.max(g, 1);
    const rRatio = r / gSafe;
    const bRatio = b / gSafe;
    const isFullMagenta = g < 80 && rRatio > 1.4 && bRatio > 1.4;
    const isPartialMagenta = g < 130 && rRatio > 1.2 && bRatio > 1.2;
    if (isFullMagenta) {
      out[i] = 128;
      out[i + 1] = 128;
      out[i + 2] = 128;
      out[i + 3] = 0;
    } else if (isPartialMagenta) {
      const fade = (g - 80) / 50; // 0..1: the greener, the more we keep
      out[i + 3] = Math.round(fade * 255);
      const avg = Math.round((r + g + b) / 3);
      const ds = 1 - fade;
      out[i] = Math.round(r * fade + avg * ds);
      out[i + 1] = Math.round(g * fade + avg * ds);
      out[i + 2] = Math.round(b * fade + avg * ds);
    }
  }
  return sharp(out, { raw: { width: info.width, height: info.height, channels: 4 } }).png().toBuffer();
}

/**
 * Downsample to the final iso tile size, keeping alpha:
 * - surface (floors, minor decorations): centre 2:1 strip, resized to 64x32
 * - cube (walls, obstacles, doors, stairs): centre square, resized to 64x64
 */
async function resizeForTile(sharp: Sharp, buf: Buffer, geometry: "surface" | "cube"): Promise<Buffer> {
  const meta = await sharp(buf).metadata();
  const w = meta.width ?? 1024;
  const h = meta.height ?? 1024;
  if (geometry === "surface") {
    const cropH = Math.floor(w / 2);
    const top = Math.max(0, Math.floor((h - cropH) / 2));
    return sharp(buf)
      .extract({ left: 0, top, width: w, height: cropH })
      .resize(64, 32, { kernel: "lanczos3" })
      .png()
      .toBuffer();
  }
  const side = Math.min(w, h);
  return sharp(buf)
    .extract({ left: Math.floor((w - side) / 2), top: Math.floor((h - side) / 2), width: side, height: side })
    .resize(64, 64, { kernel: "lanczos3" })
    .png()
    .toBuffer();
}

/** The token paid calls use. Hosted mode only ever uses the caller's own token. */
function hfToken(): string | null {
  if (isHosted()) return currentScope().hfToken ?? null;
  return process.env.HUGGINGFACE_API_KEY || process.env.HF_TOKEN || null;
}

const FLUX_MODEL = "black-forest-labs/FLUX.1-dev";
/** How long the provider picked automatically is reused before asking the Hub again. */
const PROVIDER_TTL_MS = 60 * 60 * 1000;
let autoProvider: { name: string; at: number } | undefined;

/**
 * Providers routing FLUX.1-dev right now, from the Hub's public routing table. The list changes
 * over time (Replicate stopped serving the model in 2026), so it is never hard-coded.
 */
async function fluxProviders(): Promise<Array<{ provider: string; status: string }>> {
  const res = await fetch(`https://huggingface.co/api/models/${FLUX_MODEL}?expand[]=inferenceProviderMapping`);
  if (!res.ok) throw new Error(`Hugging Face did not return the providers of ${FLUX_MODEL} (HTTP ${res.status}).`);
  const raw = ((await res.json()) as { inferenceProviderMapping?: unknown }).inferenceProviderMapping;
  if (Array.isArray(raw)) {
    return raw.map((m: { provider?: unknown; status?: unknown }) => ({ provider: String(m.provider), status: String(m.status) }));
  }
  return Object.entries((raw ?? {}) as Record<string, { status?: unknown }>).map(([provider, m]) => ({
    provider,
    status: String(m?.status),
  }));
}

/** TILED_HF_PROVIDER when set (other than "auto"), otherwise the first provider live for FLUX.1-dev. */
export async function resolveFluxProvider(): Promise<string> {
  const configured = process.env.TILED_HF_PROVIDER?.trim();
  if (configured && configured.toLowerCase() !== "auto") return configured;
  if (!autoProvider || Date.now() - autoProvider.at > PROVIDER_TTL_MS) {
    const live = (await fluxProviders()).filter((p) => p.status === "live");
    if (!live.length) throw new Error(`No Inference Provider serves ${FLUX_MODEL} right now: see https://huggingface.co/${FLUX_MODEL}.`);
    autoProvider = { name: live[0].provider, at: Date.now() };
  }
  return autoProvider.name;
}

/** Image size in the parameter shape each provider reads (they pass parameters through as is). */
function sizeParameters(provider: string, d: { width: number; height: number }): Record<string, unknown> {
  switch (provider) {
    case "fal-ai":
      return { image_size: { width: d.width, height: d.height } };
    case "wavespeed":
      return { size: `${d.width}*${d.height}` };
    default:
      return { width: d.width, height: d.height };
  }
}

function missingToken() {
  return isHosted()
    ? {
        ok: false,
        error: "No Hugging Face token was sent with this request. Sprite generation is a paid Hugging Face service billed to your own account; map editing does not need it.",
        hint: "Configure your MCP client to send the header 'Authorization: Bearer hf_...' with a token that can make calls to Inference Providers. See docs/hugging-face.md.",
      }
    : {
        ok: false,
        error: "HUGGINGFACE_API_KEY (or HF_TOKEN) is not set. Sprite generation is a paid Hugging Face service; map editing does not need it.",
        hint: "Add the token to tiled-mcp/.env or to the env block of your MCP client configuration. See docs/sprite-generation.md.",
      };
}

/**
 * One FLUX.1-dev call; returns the image with its magenta background made transparent, and the
 * provider that served it. A provider error says which providers serve the model now.
 */
async function fluxKeyed(
  sharp: Sharp,
  prompt: string,
  dimensions: { width: number; height: number },
  seed?: number,
): Promise<{ keyed: Buffer; provider: string }> {
  const provider = await resolveFluxProvider();
  const parameters: Record<string, unknown> = {
    ...sizeParameters(provider, dimensions),
    negative_prompt: NEGATIVE_PROMPT,
    guidance_scale: 4.5,
    ...(seed !== undefined ? { seed } : {}),
  };
  const { InferenceClient } = await import("@huggingface/inference");
  const client = new InferenceClient(hfToken()!);
  let blob: Blob;
  try {
    blob = (await client.textToImage({
      model: FLUX_MODEL,
      provider: provider as never,
      inputs: prompt,
      parameters: parameters as never,
    })) as unknown as Blob;
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    if (!/provider/i.test(message)) throw err;
    const live = await fluxProviders()
      .then((list) => list.filter((p) => p.status === "live").map((p) => p.provider))
      .catch(() => [] as string[]);
    throw new Error(
      `${message} Provider used: ${provider}. Providers serving ${FLUX_MODEL} now: ${live.join(", ") || "unknown"}. Set TILED_HF_PROVIDER to one of them, or remove it to pick one automatically.`,
    );
  }
  return { keyed: await chromaKeyMagenta(sharp, Buffer.from(await blob.arrayBuffer())), provider };
}

/** Generate one sprite PNG and return the processed bytes. */
async function renderSprite(prompt: ReturnType<typeof buildPrompt>, seed?: number) {
  const sharp = await loadSharp();
  const { keyed } = await fluxKeyed(sharp, prompt.prompt, prompt.dimensions, seed);
  return resizeForTile(sharp, keyed, prompt.geometry);
}

/** Pixels with less alpha than this count as background when the key left a veil or specks. */
const SOLID_ALPHA = 250;
/** Pixels with less alpha than this count as background when the key left a clean border. */
const CLEAR_ALPHA = 32;

/**
 * Keep only the building of a keyed image, cropped to it. FLUX does not always paint a clean
 * magenta background (fal-ai often returns a pale pink or a grey noisy one), so the chroma key
 * can leave a translucent veil or specks; it can also half-erase purple-grey parts of the
 * building such as slate roofs. Here:
 * - background is what connects to the image border through see-through pixels: any pixel
 *   that is not fully opaque when the border holds a veil or specks, only nearly transparent
 *   ones when the border is already clean (so a half-keyed roof is not swept away);
 * - of the rest, only the largest connected shape is kept, which drops opaque specks;
 * - see-through pixels enclosed by the building (windows or roof tiles keyed out because they
 *   looked magenta) become opaque again.
 */
async function isolateSubject(sharp: Sharp, buf: Buffer): Promise<Buffer> {
  const { data, info } = await sharp(buf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
  const w = info.width;
  const h = info.height;
  const n = w * h;
  let borderPixels = 0;
  let clearBorder = 0;
  for (let i = 0; i < n; i++) {
    const x = i % w;
    if (x !== 0 && x !== w - 1 && i >= w && i < n - w) continue;
    borderPixels++;
    if (data[i * 4 + 3] < CLEAR_ALPHA) clearBorder++;
  }
  const threshold = clearBorder >= 0.9 * borderPixels ? CLEAR_ALPHA : SOLID_ALPHA;
  const background = new Uint8Array(n);
  const stack: number[] = [];
  const reach = (i: number) => {
    if (!background[i] && data[i * 4 + 3] < threshold) {
      background[i] = 1;
      stack.push(i);
    }
  };
  for (let x = 0; x < w; x++) {
    reach(x);
    reach((h - 1) * w + x);
  }
  for (let y = 0; y < h; y++) {
    reach(y * w);
    reach(y * w + w - 1);
  }
  while (stack.length) {
    const i = stack.pop()!;
    const x = i % w;
    if (x > 0) reach(i - 1);
    if (x < w - 1) reach(i + 1);
    if (i >= w) reach(i - w);
    if (i < n - w) reach(i + w);
  }

  // Largest 8-connected shape among the remaining pixels.
  const label = new Int32Array(n);
  let best = 0;
  let bestSize = 0;
  let next = 0;
  for (let s = 0; s < n; s++) {
    if (background[s] || label[s]) continue;
    label[s] = ++next;
    let size = 0;
    stack.push(s);
    while (stack.length) {
      const i = stack.pop()!;
      size++;
      const x = i % w;
      const y = (i - x) / w;
      for (let dy = -1; dy <= 1; dy++) {
        for (let dx = -1; dx <= 1; dx++) {
          const xx = x + dx;
          const yy = y + dy;
          if ((!dx && !dy) || xx < 0 || yy < 0 || xx >= w || yy >= h) continue;
          const j = yy * w + xx;
          if (!background[j] && !label[j]) {
            label[j] = next;
            stack.push(j);
          }
        }
      }
    }
    if (size > bestSize) {
      bestSize = size;
      best = next;
    }
  }
  if (!best) return buf;

  const out = Buffer.from(data);
  let minX = w;
  let minY = h;
  let maxX = -1;
  let maxY = -1;
  for (let i = 0; i < n; i++) {
    const p = i * 4;
    if (label[i] !== best) {
      out[p] = out[p + 1] = out[p + 2] = out[p + 3] = 0;
      continue;
    }
    const x = i % w;
    const y = (i - x) / w;
    if (data[p + 3] < 255) {
      const touchesBackground =
        (x > 0 && background[i - 1]) || (x < w - 1 && background[i + 1]) || (y > 0 && background[i - w]) || (y < h - 1 && background[i + w]);
      if (!touchesBackground) out[p + 3] = 255;
    }
    if (x < minX) minX = x;
    if (x > maxX) maxX = x;
    if (y < minY) minY = y;
    if (y > maxY) maxY = y;
  }
  return sharp(out, { raw: { width: w, height: h, channels: 4 } })
    .extract({ left: minX, top: minY, width: maxX - minX + 1, height: maxY - minY + 1 })
    .png()
    .toBuffer();
}

/**
 * Turn a generated (or hand-made) building image into its sprite: isolate the building from its
 * background, then fit it into width x height standing on the bottom edge. Exported for
 * scripts/gen-building-sprites.mjs --clean, which repairs existing sprites for free.
 */
export async function cleanBuildingSprite(buf: Buffer, width: number, height: number): Promise<Buffer> {
  const sharp = await loadSharp();
  return fitBuilding(sharp, await isolateSubject(sharp, buf), width, height);
}

/**
 * Fit a keyed building image into its sprite size: trim the transparent margin, then scale it
 * to fit, standing on the bottom edge (the tile object's anchor) and centred horizontally.
 */
async function fitBuilding(sharp: Sharp, buf: Buffer, width: number, height: number): Promise<Buffer> {
  let trimmed = buf;
  try {
    trimmed = await sharp(buf).trim().toBuffer();
  } catch {
    // Nothing to trim (uniform image): keep it whole.
  }
  return sharp(trimmed)
    .resize(width, height, {
      fit: "contain",
      position: "south",
      background: { r: 0, g: 0, b: 0, alpha: 0 },
      kernel: "lanczos3",
    })
    .png()
    .toBuffer();
}

const THEME = themeKeySchema().describe("Theme key from list_themes");
const ROLE = z.enum(THEME_ROLES).describe("Tile role, e.g. walkable_floor, wall, obstacle_decor");
const PAID = { readOnlyHint: false, destructiveHint: false, openWorldHint: true } as const;

export function registerGenerationTools(server: McpServer): void {
  server.registerTool(
    "generate_tile_sprite",
    {
      title: "Generate one tile sprite (paid)",
      description:
        isHosted()
          ? "PAID, billed to the Hugging Face token you send: generates one isometric sprite for (theme, role) with FLUX.1-dev via Hugging Face Inference Providers, removes the magenta background and resizes it to 64x32 (floors) or 64x64 (walls, props). The sprite is returned as an image and kept with your session as <theme>/<role>.png, or <role>__<variant>.png when variant is set; maps of this session use it in previews and save_to_disk downloads. Takes 20-40 s and costs money per call; preview the prompt with preview_sprite_prompt first. Every theme already ships its sprites, so an existing one is kept unless force is true."
          : "PAID: generates one isometric sprite for (theme, role) with FLUX.1-dev via Hugging Face Inference Providers, removes the magenta background and resizes it to 64x32 (floors) or 64x64 (walls, props). Saves <assets>/<theme>/<role>.png, or <role>__<variant>.png when variant is set. Takes 20-40 s and costs money per call; check that the file is missing first (list_themes shows missing sprites) and preview the prompt with preview_sprite_prompt. An existing file is kept unless force is true.",
      inputSchema: {
        theme: THEME,
        role: ROLE,
        variant: z
          .string()
          .regex(/^[a-z0-9_]+$/)
          .optional()
          .describe("Contextual variant slug, e.g. 'alembic' for a laboratory: saved as <role>__<variant>.png and bound to a map with override_tile_image"),
        material: z.string().optional().describe("Override of the material description, e.g. 'alchemical workbench with brass alembics'"),
        lighting: z.string().optional().describe("Override of the lighting description"),
        extraDetail: z.string().optional().describe("Free text appended to the prompt"),
        seed: z.number().int().optional().describe("Seed for reproducibility"),
        force: z.boolean().optional().describe("Regenerate even if the file already exists (paid again)"),
        outputDir: z
          .string()
          .optional()
          .describe("Output folder; relative paths resolve against the assets folder. Defaults to <assets>/<theme_lowercase>/"),
      },
      annotations: PAID,
    },
    async ({ theme, role, variant, material, lighting, extraDetail, seed, force, outputDir }) => {
      if (!hfToken()) return asText(missingToken());
      const hosted = isHosted();
      if (hosted && outputDir) {
        return asText({
          ok: false,
          error: "outputDir is not available on the hosted server: generated sprites stay with your session and are included in the map download (save_to_disk).",
        });
      }
      const key = theme as ThemeKey;
      let rendered;
      try {
        rendered = buildPrompt(key, role, { material, lighting, extraDetail });
      } catch (err) {
        return asText({ ok: false, error: err instanceof Error ? err.message : String(err) });
      }

      const fileName = variant ? `${role}__${variant}.png` : `${role}.png`;
      const relPath = `${themeAssetDir(key)}/${fileName}`;
      const dir = outputDir ? resolveUserPath(outputDir, assetsRoot()) : join(assetsRoot(), themeAssetDir(key));
      const outPath = hosted ? relPath : join(dir, fileName);
      if ((hosted ? spriteExists(relPath) : existsSync(outPath)) && !force) {
        return asText({
          ok: false,
          skipped: true,
          outputPath: outPath,
          error: "The sprite already exists; nothing was generated or billed. Pass force: true to regenerate it.",
        });
      }

      const start = Date.now();
      try {
        const buf = await renderSprite(rendered, seed);
        const saved = await saveSprite(relPath, buf, outPath);
        const result = asText({
          ok: true,
          theme,
          role,
          variant: variant ?? null,
          geometry: rendered.geometry,
          outputPath: saved,
          finalSize: rendered.geometry === "surface" ? "64x32" : "64x64",
          relativePath: `${ASSET_PREFIX}${relPath}`,
          bytes: buf.length,
          durationMs: Date.now() - start,
          promptUsed: rendered.prompt,
        });
        // Hosted callers cannot read the server's disk: send them the image itself as well.
        return hosted
          ? { content: [...result.content, { type: "image" as const, data: buf.toString("base64"), mimeType: "image/png" }] }
          : result;
      } catch (err) {
        return asText({ ok: false, error: err instanceof Error ? err.message : String(err), promptAttempted: rendered.prompt });
      }
    },
  );

  server.registerTool(
    "override_tile_image",
    {
      title: "Use a sprite variant on one map",
      description:
        "Points one role of a single map's tileset at a variant image, e.g. obstacle_decor__alembic.png instead of obstacle_decor.png. Only that map changes; other maps and the theme keep their defaults. Call after create_map and before saving or exporting.",
      inputSchema: {
        mapId: z.string(),
        role: ROLE,
        variant: z
          .string()
          .regex(/^[a-z0-9_]+$/)
          .describe("Variant slug: the file <assets>/<theme>/<role>__<variant>.png should exist"),
        imageWidth: z.number().int().optional().describe("Image width in pixels if it differs from the default"),
        imageHeight: z.number().int().optional().describe("Image height in pixels if it differs from the default"),
        customPath: z
          .string()
          .optional()
          .describe("Use this image path verbatim instead of the './assets/<theme>/<role>__<variant>.png' convention"),
      },
      annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
    },
    async ({ mapId, role, variant, imageWidth, imageHeight, customPath }) => {
      const state = getMap(mapId);
      const file = `${themeAssetDir(state.theme.key)}/${role}__${variant}.png`;
      const path = customPath ?? `${ASSET_PREFIX}${file}`;
      overrideTileImage(mapId, role, path, imageWidth, imageHeight);
      const fileExists = customPath ? undefined : spriteExists(file);
      const where = isHosted() ? file : join(assetsRoot(), file);
      return asText({
        ok: true,
        mapId,
        role,
        variant,
        imagePath: path,
        ...(fileExists === false
          ? { warning: `No file at ${where} yet: generate it with generate_tile_sprite (variant '${variant}') or Tiled will show a placeholder.` }
          : {}),
      });
    },
  );

  server.registerTool(
    "preview_sprite_prompt",
    {
      title: "Preview a sprite prompt (free)",
      description:
        "Free: returns the exact FLUX prompt, geometry and size generate_tile_sprite would use for (theme, role) with optional overrides, without calling any service. Use it to iterate on wording before paying for a generation.",
      inputSchema: {
        theme: THEME,
        role: ROLE,
        material: z.string().optional(),
        lighting: z.string().optional(),
        extraDetail: z.string().optional(),
      },
      annotations: { readOnlyHint: true, openWorldHint: false },
    },
    async ({ theme, role, material, lighting, extraDetail }) => {
      try {
        return asText({ ok: true, ...buildPrompt(theme as ThemeKey, role, { material, lighting, extraDetail }) });
      } catch (err) {
        return asText({ ok: false, error: err instanceof Error ? err.message : String(err) });
      }
    },
  );

  server.registerTool(
    "generate_building_sprite",
    {
      title: "Generate a building sprite (paid)",
      description:
        `PAID unless dryRun is true: generates the sprite place_building draws for a building type (${BUILDING_TYPES.join(", ")}) with FLUX.1-dev via Hugging Face Inference Providers, removes the magenta background and fits the building into its footprint size (house and shop 192x192, tavern and temple 256x256, tower 128x192, castle 384x384). ` +
        (isHosted()
          ? "The sprite is returned as an image and kept with your session as buildings/<type>.png (or <type>__<variant>.png); maps of this session use it in previews and downloads. "
          : "Saves <assets>/buildings/<type>.png, or <type>__<variant>.png when variant is set. ") +
        "Takes 20-40 s and costs money per call; dryRun returns the prompt for free. An existing sprite is kept unless force is true.",
      inputSchema: {
        buildingType: z.enum(BUILDING_TYPES).describe("Building type, as in place_building"),
        variant: z
          .string()
          .regex(/^[a-z0-9_]+$/)
          .optional()
          .describe("Variant slug, e.g. 'snowy': saved as <type>__<variant>.png and used with place_building spriteVariant"),
        material: z.string().optional().describe("Override of the building description"),
        lighting: z.string().optional().describe("Override of the lighting description"),
        extraDetail: z.string().optional().describe("Free text appended to the prompt"),
        seed: z.number().int().optional().describe("Seed for reproducibility"),
        force: z.boolean().optional().describe("Regenerate even if the sprite already exists (paid again)"),
        dryRun: z.boolean().optional().describe("Return the prompt and target size without generating anything (free)"),
      },
      annotations: PAID,
    },
    async ({ buildingType, variant, material, lighting, extraDetail, seed, force, dryRun }) => {
      const def = BUILDINGS[buildingType];
      const rendered = buildBuildingPrompt(material ?? def.material, { lighting, extraDetail });
      const relPath = buildingSpriteFile(buildingType, variant);
      const size = `${def.imageWidth}x${def.imageHeight}`;
      if (dryRun) return asText({ ok: true, dryRun: true, buildingType, file: relPath, size, prompt: rendered.prompt });
      if (!hfToken()) return asText(missingToken());
      const hosted = isHosted();
      const outPath = hosted ? relPath : join(assetsRoot(), relPath);
      if (spriteExists(relPath) && !force) {
        return asText({
          ok: false,
          skipped: true,
          outputPath: outPath,
          error: "The sprite already exists; nothing was generated or billed. Pass force: true to regenerate it.",
        });
      }
      const start = Date.now();
      try {
        const sharp = await loadSharp();
        const { keyed, provider } = await fluxKeyed(sharp, rendered.prompt, rendered.dimensions, seed);
        const buf = await fitBuilding(sharp, await isolateSubject(sharp, keyed), def.imageWidth, def.imageHeight);
        const saved = await saveSprite(relPath, buf, outPath);
        const result = asText({
          ok: true,
          buildingType,
          variant: variant ?? null,
          provider,
          outputPath: saved,
          finalSize: size,
          relativePath: `${ASSET_PREFIX}${relPath}`,
          bytes: buf.length,
          durationMs: Date.now() - start,
          promptUsed: rendered.prompt,
        });
        return hosted
          ? { content: [...result.content, { type: "image" as const, data: buf.toString("base64"), mimeType: "image/png" }] }
          : result;
      } catch (err) {
        return asText({ ok: false, error: err instanceof Error ? err.message : String(err), promptAttempted: rendered.prompt });
      }
    },
  );

  server.registerTool(
    "generate_theme_sprite_set",
    {
      title: "Generate a theme's missing sprites (paid)",
      description:
        "PAID and slow: generates every sprite of a theme that is missing on disk (about 30 s and one paid call per sprite, typically 8-10 sprites, so 4-5 minutes). Existing sprites are skipped unless force is true. Many MCP clients time out on calls this long; for a whole theme prefer the command line script: node scripts/gen-theme-set.mjs <THEME> --yes.",
      inputSchema: {
        theme: THEME,
        force: z.boolean().optional().describe("Regenerate sprites that already exist (paid again)"),
        seed: z.number().int().optional(),
      },
      annotations: PAID,
    },
    async ({ theme, force, seed }) => {
      if (!hfToken()) return asText(missingToken());
      const key = theme as ThemeKey;
      const hosted = isHosted();
      const dir = hosted ? themeAssetDir(key) : join(assetsRoot(), themeAssetDir(key));

      const results: Array<{ role: string; ok: boolean; path?: string; error?: string; durationMs?: number; skipped?: boolean }> = [];
      for (const role of THEME_ROLES) {
        const relPath = `${themeAssetDir(key)}/${role}.png`;
        let rendered;
        try {
          rendered = buildPrompt(key, role);
        } catch {
          continue; // no prompt for this role in this theme
        }
        if (spriteExists(relPath) && !force) {
          results.push({ role, ok: true, path: hosted ? relPath : join(assetsRoot(), relPath), skipped: true });
          continue;
        }
        const start = Date.now();
        try {
          const path = await saveSprite(relPath, await renderSprite(rendered, seed));
          results.push({ role, ok: true, path, durationMs: Date.now() - start });
        } catch (err) {
          results.push({ role, ok: false, error: err instanceof Error ? err.message : String(err), durationMs: Date.now() - start });
        }
      }
      return asText({ theme, dir, results });
    },
  );
}