Spaces:
Running

File size: 19,716 Bytes
70e6684
 
6ed91fb
 
 
70e6684
6ed91fb
bdfb0e8
6ed91fb
bdfb0e8
 
 
6ed91fb
 
 
 
bdfb0e8
 
 
6ed91fb
 
bdfb0e8
 
6ed91fb
 
 
 
bdfb0e8
70e6684
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
6ed91fb
 
e183811
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
 
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
 
 
 
 
 
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950662b
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270f46d
6ed91fb
270f46d
6ed91fb
 
 
 
 
 
 
950662b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
950662b
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950662b
 
6ed91fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950662b
 
6ed91fb
 
 
 
270f46d
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
---
title: Tiled Map Builder
emoji: 🗺️
colorFrom: indigo
colorTo: gray
sdk: docker
app_port: 7860
pinned: true
license: mit
short_description: 'MCP server: describe a dungeon, get a Tiled map'
thumbnail: >-
  https://huggingface.co/spaces/4math/tiled-map-builder/resolve/main/docs/images/preview-crypt-preset.png
hf_oauth: true
hf_oauth_expiration_minutes: 480
hf_oauth_scopes:
  - inference-api
models:
  - openai/gpt-oss-120b
  - black-forest-labs/FLUX.1-dev
tags:
  - mcp-server
  - model-context-protocol
  - ai-agents
  - tiled
  - map-generator
  - isometric
  - procedural-generation
  - game-development
---

<p align="center">
  <img src="docs/assets/banner.svg" alt="Tiled Map Builder MCP: an isometric crypt room built from the project's sprites" width="100%">
</p>

<p align="center">
  <a href="https://nodejs.org"><img alt="Node.js 20.9+" src="https://img.shields.io/badge/Node.js-%E2%89%A520.9-5FA04E?logo=nodedotjs&logoColor=white"></a>
  <a href="https://www.typescriptlang.org"><img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-5-3178C6?logo=typescript&logoColor=white"></a>
  <a href="https://modelcontextprotocol.io"><img alt="Model Context Protocol" src="https://img.shields.io/badge/MCP-stdio%20%2B%20HTTP-8B5CF6?logo=modelcontextprotocol&logoColor=white"></a>
  <a href="https://www.mapeditor.org"><img alt="Tiled 1.10+" src="https://img.shields.io/badge/Tiled-.tmj%20maps-5DA9E9"></a>
  <a href="https://zod.dev"><img alt="Zod 4" src="https://img.shields.io/badge/Zod-4-3E67B1?logo=zod&logoColor=white"></a>
  <a href="docs/sprite-generation.md"><img alt="Hugging Face FLUX (optional)" src="https://img.shields.io/badge/Hugging%20Face-FLUX%20(optional)-FFD21E?logo=huggingface&logoColor=black"></a>
  <a href="docs/nemotron.md"><img alt="Works with NVIDIA Nemotron" src="https://img.shields.io/badge/NVIDIA-Nemotron%20ready-76B900?logo=nvidia&logoColor=white"></a>
  <a href="LICENSE"><img alt="MIT license" src="https://img.shields.io/badge/license-MIT-blue"></a>
</p>

<p align="center">
  <b>An MCP server that lets AI agents build isometric <a href="https://www.mapeditor.org">Tiled</a> maps</b><br>
  rooms, corridors, doors, traps, chests and spawns, validated and saved as <code>.tmj</code> files you open in Tiled.
</p>

<p align="center">
  <a href="https://huggingface.co/spaces/4math/tiled-map-builder"><img alt="Open in Hugging Face Spaces" src="https://huggingface.co/datasets/huggingface/badges/resolve/main/open-in-hf-spaces-md.svg"></a>
</p>

<p align="center">
  <a href="#-try-it-online">Try it online</a> ·
  <a href="#-quick-start">Quick start</a> ·
  <a href="docs/client-setup.md">Connect a client</a> ·
  <a href="docs/tools.md">Tool reference</a> ·
  <a href="docs/map-format.md">Map format</a> ·
  <a href="docs/themes.md">Themes</a> ·
  <a href="docs/troubleshooting.md">Troubleshooting</a>
</p>

---

## ✨ Features

- 🗺️ **35 MCP tools** to create maps up to 256x256, paint tiles, generate rooms and corridors, and place doors, chests, traps, levers, stairs and spawns.
- 🏘️ **Buildings you can enter**: put houses, taverns, temples, towers or castles on an outdoor map; each gets its own interior map, linked both ways, ready to load in a game ([guide with a Godot loader](docs/worlds-and-buildings.md)).
- 🏰 **3 layout presets**: a crypt, a castle floor and a werewolf den, each built in one call.
- 🎨 **20 themes**, 12 of them with bundled isometric sprites ready to use.
- 🛡️ **Guard rails for agents**: placing a chest on a wall, outside a room or on another object fails with a message that says how to fix it, and `validate_map` checks the map before export, including that the player can walk to every room, chest and doorway.
- 📐 **Correct Tiled output**: object positions follow Tiled's isometric convention, and saved files point at their sprites with relative paths, so they open directly in Tiled.
- 🧠 **Agent skill and MCP prompt** (`map_builder`): a step-by-step workflow with an ASCII sketch confirmation, theme heuristics and tactical rules.
- 🖌️ **Optional sprite generation** with FLUX.1-dev through Hugging Face. It is paid, opt-in, and never runs by accident.
- 🔌 Works with **any MCP client**: Claude Code, Claude Desktop, Cursor, VS Code, Zed, OpenCode, and agents running NVIDIA Nemotron.
- 🤗 **Hosted on Hugging Face**: a web page where an open AI model builds a map from your description, and a remote MCP endpoint. Paid calls always run on the visitor's own Hugging Face account.

## 🤗 Try it online

No install: open the **[Tiled Map Builder Space](https://huggingface.co/spaces/4math/tiled-map-builder)**,
sign in with Hugging Face, describe a map in any language and download a `.zip` that opens in
Tiled. The AI model (default `openai/gpt-oss-120b`) runs on **your** Hugging Face inference
credits, usually a few cents per map; the Space owner pays nothing for your use and never sees
your token.

New: try the **Village (enter buildings)** example. The model builds an outdoor map with houses,
a tavern, a chapel, a tower and a castle; each building gets its own interior map, and the
`.zip` holds them all, linked both ways.

AI agents can use the same Space as a remote MCP server with their own token:

```bash
claude mcp add --transport http tiled-map-builder https://4math-tiled-map-builder.hf.space/mcp \
  --header "Authorization: Bearer hf_YOUR_TOKEN"
```

How it works, costs, other clients, and running your own copy: **[docs/hugging-face.md](docs/hugging-face.md)**.

## 🖼️ Preview

<p align="center">
  <img src="docs/images/preview-crypt-preset.png" alt="Crypt built with generate_crypt_layout: entrance, nave, two side chambers and a deep crypt, with player, enemy, chest and trap markers" width="85%">
  <br><sub>A 28x18 crypt from <code>generate_crypt_layout</code> plus a few <code>place_*</code> calls.
  Markers: <b>P</b> player, <b>E</b> enemy, <b>D</b> door, <b>C</b> chest, <b>T</b> trap. Rendered with <code>npm run preview</code>.</sub>
</p>

<p align="center">
  <img src="docs/images/preview-ice-fortress.png" alt="64x64 ice fortress sample map" width="49%">
  <img src="docs/images/preview-aquatic-cavern.png" alt="64x64 flooded cavern sample map" width="49%">
  <br><sub>Sample maps in <a href="maps/"><code>maps/</code></a>: an ice fortress and a flooded cavern, both 64x64.</sub>
</p>

<p align="center">
  <img src="docs/images/preview-crossroads-village.png" alt="40x40 village with a house, a shop, a tavern, a chapel, a watchtower and a castle, each with an entrance and an arrival point" width="85%">
  <br><sub>The sample world <a href="maps/crossroads-village/"><code>maps/crossroads-village/</code></a>: six buildings from <code>place_building</code>.
  Walking onto an entrance (<b>I</b>) loads the building's interior map; its exit brings you back to the arrival point (<b>A</b>).
  See <a href="docs/worlds-and-buildings.md">worlds and buildings</a>.</sub>
</p>

## 🧱 Tech stack

| | Technology | Role in this project |
|---|---|---|
| <img src="https://cdn.simpleicons.org/modelcontextprotocol/8B5CF6" width="22" alt=""> | [Model Context Protocol](https://modelcontextprotocol.io) | How agents discover and call the map tools (stdio locally, Streamable HTTP when hosted) |
| <img src="https://cdn.simpleicons.org/typescript/3178C6" width="22" alt=""> | [TypeScript](https://www.typescriptlang.org) | Server source in `tiled-mcp/src` |
| <img src="https://cdn.simpleicons.org/nodedotjs/5FA04E" width="22" alt=""> | [Node.js](https://nodejs.org) 20.9+ | Runtime for the server and the scripts |
| <img src="https://cdn.simpleicons.org/zod/3E67B1" width="22" alt=""> | [Zod](https://zod.dev) | Validates every tool input and generates the JSON schemas agents see |
| 🗺️ | [Tiled](https://www.mapeditor.org) | Map editor that opens the generated `.tmj` files |
| <img src="https://cdn.simpleicons.org/huggingface/FFD21E" width="22" alt=""> | [Hugging Face Inference Providers](https://huggingface.co/docs/inference-providers) | The hosted page's AI model (open LLMs with tool calling) and optional FLUX.1-dev sprites, billed to each user |
| <img src="https://cdn.simpleicons.org/huggingface/FFD21E" width="22" alt=""> | [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces) | Hosts the web page and remote MCP endpoint (Docker Space with "Sign in with Hugging Face") |
| <img src="https://cdn.simpleicons.org/docker/2496ED" width="22" alt=""> | [Docker](https://www.docker.com) | Image of the hosted mode, used by the Space and for self-hosting |
| 🖼️ | [sharp](https://sharp.pixelplumbing.com) | Background removal and resizing of generated sprites, PNG previews |
| <img src="https://cdn.simpleicons.org/nvidia/76B900" width="22" alt=""> | [NVIDIA Nemotron](https://build.nvidia.com) | One of the agent models this server was designed and tested with |

## 🚀 Quick start

**Requirements**: [Node.js](https://nodejs.org) 20.9 or newer and git. [Tiled](https://www.mapeditor.org) is optional, to view and edit the maps.

```bash
git clone https://github.com/4math2379/Tiled-MCP-Server.git
cd Tiled-MCP-Server
npm install        # installs the server workspace
npm run build      # compiles tiled-mcp/src to tiled-mcp/dist
npm test           # optional: every offline test suite (96 + 96 + 39 checks)
```

Then register the server with your MCP client. With **Claude Code**, from the repository folder:

```bash
claude mcp add tiled-map-builder -- node "$(pwd)/tiled-mcp/dist/index.js"
```

Most other clients take the same JSON block (replace the path with your absolute path):

```json
{
  "mcpServers": {
    "tiled-map-builder": {
      "command": "node",
      "args": ["/absolute/path/to/Tiled-MCP-Server/tiled-mcp/dist/index.js"]
    }
  }
}
```

👉 Per-client instructions for Claude Desktop, Cursor, VS Code, Zed, OpenCode, Windsurf and the MCP Inspector are in **[docs/client-setup.md](docs/client-setup.md)**.

Now ask your agent:

> Build a 28x18 crypt under an abandoned chapel with two skeletons in the nave, a locked chest in the north chamber and a dart trap before the deep crypt. Save it as crypt-chapel.tmj.

The map lands in `maps/crypt-chapel.tmj`. Open it in Tiled, or render a preview without Tiled:

```bash
npm run preview -- maps/crypt-chapel.tmj
```

## 🧭 How it works

```mermaid
flowchart LR
    A["AI agent<br/>(Claude, Nemotron, ...)"] -- MCP tools --> S["tiled-map-builder<br/>server"]
    S --> M[("Maps in memory<br/>ground · walls · decorations<br/>objects · spawns")]
    M --> V{"validate_map"}
    V --> F["save_to_disk<br/>maps/*.tmj"]
    F --> T["Tiled editor<br/>or a game engine"]
```

1. `create_map` makes an empty map in a **theme** and returns a `mapId`.
2. Tiles are painted by **role** (`walkable_floor`, `wall`, `hazard_primary`, `obstacle_decor`, ...) and the theme picks the sprite.
3. Each map has three tile layers, **ground**, **walls** and **decorations**, and two object layers, **objects** (doors, chests, traps, levers, stairs) and **spawns** (player, enemy, NPC).
4. Coordinates are grid cells: `x` is the column and `y` the row; (0,0) is the top corner of an isometric map.
5. Maps live in server memory until you call `save_to_disk` or `export_json`.

Details, properties and importer formulas: **[docs/map-format.md](docs/map-format.md)**.

## 🧰 Tools

| Group | Tools |
|---|---|
| Map lifecycle | `list_themes` · `create_map` · `list_maps` · `describe_map` · `discard_map` |
| Painting | `paint_tile` · `paint_rect` · `paint_wall_border` · `erase_tile` · `get_tile_info` |
| Rooms and corridors | `generate_room` · `generate_corridor` |
| Layout presets | `generate_crypt_layout` (27x18+) · `generate_castle_floor` (27x20+) · `generate_werewolf_den` (22x16+) |
| Objects | `place_door` · `place_chest` · `place_stairs_object` · `place_trap` · `place_lever` |
| Spawns | `place_spawn_player` · `place_spawn_enemy` · `place_spawn_npc` |
| Buildings and linked maps | `place_building` · `link_maps` · `validate_world` · `save_world` |
| Validation and export | `validate_map` · `export_json` · `save_to_disk` |
| Sprites | `preview_sprite_prompt` (free) · `generate_tile_sprite` 💸 · `generate_theme_sprite_set` 💸 · `generate_building_sprite` 💸 · `override_tile_image` |

Full parameters, generated from the server itself: **[docs/tools.md](docs/tools.md)**.

## 🎨 Themes

| Theme key | Display name | Setting | Sprites |
|---|---|---|---|
| `CRYPT` | Mournevald Crypt | Burial vaults, cracked flagstones, poison vents | ✅ |
| `CASTLE` | Castle Von Drak | Black marble halls, throne room, grand stairs | ✅ |
| `WEREWOLF_DEN` | Werewolf Den | Damp caves, bones, blood pools | ✅ |
| `TAVERN_BLOOD_PRINCE` | Blood Prince Inn | Tavern interior, oak tables, hearth | ✅ |
| `VILLAGE` | Mournevald Village | Wet cobblestones, half-timbered houses, well | ✅ |
| `HAUNTED_FOREST` | Drakenmark Forest | Dark undergrowth, brambles, parasitic mist | ✅ |
| `MAUSOLEUM` | Mausoleum | Veined marble, fallen-angel statues, urns | ✅ |
| `BLOOD_TOMB` | Blood Tomb | Ritual sanctuary, bleeding runes, altar | ✅ |
| `AQUATIC` | Stagnant Waters | Stagnant water, water lilies, sunken ruins | ✅ |
| `GLACIER` | Frozen Tundra | Black ice, frozen statues, thin ice | ✅ |
| `FLOODED` | Flooded Coast | Drowned city, kelp, deep currents | ✅ |
| `ELECTRONIC` | Electronic Network | Circuit boards, cables, mainframe cores | ✅ |
| `VOLCANO` | Volcanic Slope | Lava flows, obsidian, ash | ➖ |
| `DARK_FOREST` | Dark Forest | Twisted woods, quicksand, standing stones | ➖ |
| `WINTER` | Snowy Plain | Snowfields, frozen lakes, cairns | ➖ |
| `SPRING` | Spring Meadow | Flowering meadows, streams, maypole | ➖ |
| `SUMMER` | Summer Plain | Golden wheat, cracked earth, scarecrows | ➖ |
| `AUTUMN` | Autumn Forest | Russet leaves, bog mire, mushrooms | ➖ |
| `DESOLATION` | Desolate Land | Ash pits, charred trees, ruins | ➖ |
| `FUTURISTIC` | Futuristic City | Chrome, neon, plasma leaks, server racks | ➖ |

✅ sprites bundled in `assets/` · ➖ no sprites yet: the maps are valid and Tiled shows placeholders until you [generate the sprites](docs/sprite-generation.md).

<p align="center">
  <img src="assets/crypt/walkable_floor.png" alt="crypt floor" height="40">
  <img src="assets/crypt/wall.png" alt="crypt wall" height="64">
  <img src="assets/crypt/obstacle_decor.png" alt="sarcophagus" height="64">
  <img src="assets/castle/obstacle_decor.png" alt="throne" height="64">
  <img src="assets/castle/stairs.png" alt="castle stairs" height="64">
  <img src="assets/werewolf_den/obstacle_decor.png" alt="boulder" height="64">
  <img src="assets/aquatic/hazard_primary.png" alt="deep water" height="64">
  <img src="assets/glacier/obstacle_decor.png" alt="frozen statue" height="64">
  <img src="assets/electronic/obstacle_decor.png" alt="mainframe core" height="64">
  <img src="assets/mausoleum/obstacle_decor.png" alt="fallen angel statue" height="64">
</p>

More about each theme's roles and hazards: **[docs/themes.md](docs/themes.md)**.

## 🤖 Agent skill and prompt

[`skills/map-builder/SKILL.md`](skills/map-builder/SKILL.md) teaches an agent the whole workflow: infer the theme, sketch the layout in ASCII and ask for confirmation, build, validate, save.

- **Claude Code**: copy the folder to `~/.claude/skills/map-builder/` (all projects) or `.claude/skills/map-builder/` (one project), then type `/map-builder crypt 28x18 ...`.
- **Any MCP client**: the server exposes the same workflow as the prompt **`map_builder`**, which takes a free-form `description`.

## ⚙️ Configuration

Nothing is required for map building. Optional settings go in `tiled-mcp/.env` (copy [`tiled-mcp/.env.example`](tiled-mcp/.env.example)) or in the `env` block of your MCP client:

| Variable | Default | Purpose |
|---|---|---|
| `TILED_MAPS_DIR` | `maps/` | Where `save_to_disk` writes relative paths |
| `TILED_ASSETS_DIR` | `assets/` | Sprite folder (`<theme>/<role>.png`) |
| `TILED_OBJECT_COORDS` | `tiled` | `screen` restores the legacy object encoding for older importers |
| `HUGGINGFACE_API_KEY` or `HF_TOKEN` | none | Only for paid sprite generation |

All variables, the hosted-mode settings, the `.env` lookup order and security notes: **[docs/configuration.md](docs/configuration.md)**.

## 💸 Sprite generation (optional, paid)

Map building is free and offline. Generating new sprites calls FLUX.1-dev through Hugging Face Inference Providers and **costs money per sprite**. The tools never overwrite an existing sprite unless you pass `force`, and the batch script asks for `--yes`:

```bash
node scripts/gen-theme-set.mjs VOLCANO --dry-run   # free: shows what would be generated
node scripts/gen-theme-set.mjs VOLCANO --yes       # paid: about 10 sprites
```

Read **[docs/sprite-generation.md](docs/sprite-generation.md)** before you start.

## 🟩 Using it with NVIDIA Nemotron

The server works with any model that supports tool calling. It was built alongside agents running **Nemotron 3.5 Super-VL** on the NVIDIA API free tier, for example through OpenCode. The NVIDIA key belongs to your agent, not to this server. Setup: **[docs/nemotron.md](docs/nemotron.md)**.

## 🗂️ Repository layout

```
tiled-mcp/          MCP server (TypeScript): src/, package.json, tsconfig.json, .env.example
  src/tools/        tool groups: lifecycle, paint, procedural, layout-presets, objects, export, generation
  src/themes/       20 theme definitions and the FLUX prompt catalog
  src/http.ts       hosted mode: web page, web AI agent, MCP over HTTP, Hugging Face sign-in
  web/              the hosted web page (HTML, CSS, JS)
huggingface/        Space card and the script that publishes this repository to the Space
Dockerfile          image of the hosted mode (used by the Space)
skills/map-builder/ agent skill, also served as the map_builder MCP prompt
maps/               sample .tmj maps (default output folder of save_to_disk)
assets/             isometric sprites per theme: <theme>/<role>.png
scripts/            example map scripts, preview renderer, sprite batch, docs generator, smoke test
docs/               documentation, banner and preview images
```

## 🛠️ Scripts

| Command | What it does |
|---|---|
| `npm run build` | Compile the server |
| `npm test` | Run every offline test suite once; `npm run test:loop` runs them 5 times in a row |
| `npm run smoke` | Start the server and run 96 offline end-to-end checks, including one loop over all 20 themes |
| `npm run start:web` | Start the hosted mode (web page and MCP over HTTP) on port 7860 |
| `npm run smoke:http` | Test the hosted mode offline, with a stand-in for the AI model |
| `npm run inspect` | Open the server in the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) |
| `npm run preview -- <map.tmj>` | Render a map to PNG without Tiled |
| `npm run examples` | Rebuild the scripted sample maps and the sample world in `maps/` |
| `npm run docs` | Regenerate `docs/tools.md` and `docs/themes.md` from the server |
| `node scripts/gen-theme-set.mjs <THEME> --yes` | Generate a theme's missing sprites (paid) |
| `node scripts/gen-building-sprites.mjs --yes` | Generate missing building sprites (paid); `--clean` re-cuts existing ones for free |

## 🤝 Contributing

Issues and pull requests are welcome. See **[CONTRIBUTING.md](CONTRIBUTING.md)** for the development loop and **[CHANGELOG.md](CHANGELOG.md)** for what changed.

## 📄 License

[MIT](LICENSE). Tiled is an independent project by Thorbjørn Lindeijer. Some theme names evoke classic gothic-horror settings; this project is not affiliated with or endorsed by their rights holders.