An MCP server that lets AI agents play NetHack. It spawns NetHack under a pseudo-terminal, parses the rendered screen into an LLM-friendly structured state, and exposes a small action DSL so the agent can emit action chunks instead of single keystrokes.
stdio (JSON-RPC)
Agent <-------------------> nethack-mcp (Rust)
|
| fork+execvp + PTY
v
nethack process
|
| ANSI bytes
v
vte parser ──► virtual screen
│
▼
structured GameState (JSON)
A human can watch the game by tail -f-ing the mirror file (see
--nethack-mirror).
brew install nethack cargo build --release
# Let NetHack prompt for character, or set --player to pass `nethack -u <name>`: ./target/release/nethack-mcp --nethack-cmd "nethack" --player Agent # With a mirror file a human can watch: ./target/release/nethack-mcp --mirror /tmp/nh.log tail -f /tmp/nh.log
Every option can be set either as a CLI flag or as an environment variable. CLI flags take precedence over environment variables, and environment variables take precedence over defaults.
| CLI flag | Environment variable | Default | Meaning |
|---|---|---|---|
--nethack-cmd |
NETHACK_CMD |
nethack |
Command used to launch NetHack. The value is split on whitespace, so "nethack -u Agent" runs nethack with -u Agent. |
--cols |
NETHACK_COLS |
80 |
Terminal width, in columns, for the PTY that NetHack renders into. Keep this at least 80 unless you know your frontend can parse narrower screens. |
--rows |
NETHACK_ROWS |
24 |
Terminal height, in rows, for the PTY that NetHack renders into. NetHack's standard tty layout expects 24 rows. |
--mirror |
NETHACK_MIRROR |
unset | Optional path for a raw terminal mirror. If set, the server writes NetHack's terminal stream to this file so a human can watch with tail -f. |
--settle-ms |
NETHACK_SETTLE_MS |
250 |
Delay after sending actions before snapshotting the screen. Increase this if NetHack output arrives slowly or state snapshots look stale. |
--max-chunk |
NETHACK_MAX_CHUNK |
64 |
Maximum number of DSL actions accepted in one execute_actions call. This prevents accidental runaway action chunks. |
--player |
NETHACK_PLAYER |
unset | NetHack player name passed as -u <name>. Leave unset to let NetHack ask interactively. |
You can also source config.example.env and then run the server:
source config.example.env
./target/release/nethack-mcp| tool | description |
|---|---|
pick_character |
Drive NetHack's character selection menus (role/race/gender/alignment), then confirm. |
execute_actions |
Parse and run an action chunk; returns the new state. |
get_state |
Snapshot the LLM-friendly state (message, vitals, nearby window, monsters/items/features). |
get_map |
Full ASCII screen (use sparingly). |
get_inventory |
Open the inventory menu and return its text. |
send_keys |
Escape hatch — send raw keystrokes. |
dsl_reference |
Full DSL verb list + glyph legend. |
Call pick_character exactly once at the start of a fresh game (before any
execute_actions / get_state). It reads each NetHack menu off the screen and
sends the matching selector key, then confirms with y to enter the dungeon.
{"name":"pick_character","arguments":{
"role":"Wizard","race":"human","gender":"male","alignment":"neutral"}}Set "auto": true to let NetHack roll a random character instead.
A chunk is actions separated by ; or newlines; semicolons inside quoted raw
strings are preserved. Directions are n s e w ne nw se sw. Item slots are
single inventory letters.
move n; move n; wait 3; wield a; quaff b; fight e; descend
run ne; attack s; fire n; throw b e; zap z n; cast a n; pray; more
farlook nw 2; inspect at 47 7; look offset -1 -1
Verb categories:
- Movement:
move <dir>,run <dir>,wait [n],descend,ascend,search [n] - Combat:
fight <dir>,fire <dir>,throw <item> <dir>,zap <item> [dir],cast <spell> [dir],apply <item> [dir],kick <dir>,force <dir> - Items:
wield [slot],wear <slot>,takeoff [slot],remove <slot>,puton <slot>,drop <slots>,dropgold <n>,pickup,quaff <slot>,read <slot>,eat <slot>,open <dir>,close <dir> - Extended:
pray,offer,pay,chat [dir],turn [dir],name <what> <name>,dip <item>,wipe,conduct - Meta / prompts:
inventory,discoveries,attributes,save,quit,confirm,cancel,more,look,farlook [target],whatis <glyph>,raw "<keys>"
Call the dsl_reference tool for the authoritative list.
get_state returns JSON shaped like:
{
"message": "You see here a newt corpse.",
"needs_more": false,
"hero": { "x": 12, "y": 9 },
"vitals": {
"role": "Archeologist", "race": "human", "gender": "male",
"level": 1, "hp": 16, "hp_max": 16, "pw": 5, "pw_max": 5,
"ac": 7, "gold": 0, "turn": 42
},
"visible_monsters": [{ "glyph": "n", "x": 15, "y": 9, "dir_from_hero": "E", "manhattan": 3 }],
"visible_items": [],
"features": [{ "kind": "stairs_down", "glyph": ">", "x": 40, "y": 5, "dir_from_hero": "NE", "color": "bright_yellow", "color_role": "interactable" }],
"nearby": {
"radius": 6, "cols": 13, "rows": 13,
"hero_col": 6, "hero_row": 6,
"rows_text": [" ......", "[email protected].....", ".........>"],
"cells": [{ "x": 12, "y": 9, "glyph": "@", "kind": "hero", "walkable": false, "color": "bright_white", "color_role": "terrain_or_friendly" }]
},
"raw_status_lines": ["...", "..."]
}Color-aware fields should be preferred over inferring topology or attitude from ASCII alone. NetHack tty colors are mapped coarsely as: red = hostile creature, yellow = interactable object/feature, white/default = wall/floor/player-side glyphs, and blue-gray = remembered floor/corridor that was seen before but is outside current line of sight.
MIT.
