Skip to main content
Glama
nwn-dm-mcp
by tim4net
README.md
# nwn-dm-mcp

A protocol-level Neverwinter Nights: Enhanced Edition DM client,
exposed as an MCP server for DM presence, chat, and
(server-side bridged) DM actions.

**Status: login solved; MCP server v1 built and verified.** The
session layer (key exchange, encrypted transport, login, frame
layer) is reversed and reproduced live by an independent Python
client - the full login construction is cracked and gate-verified
(see [AUTH.md](AUTH.md); [SPIKE.md](SPIKE.md) is the original
dissection). The MCP server below wraps that session core and was
tested end-to-end against a disposable test server: connect,
in-world hold, status/who, chat send, bridge writes, disconnect,
with the server log corroborating the join and both auth factors.

## Layout

- `SPIKE.md` - protocol findings from the first spike.
- `AUTH.md` - the login construction and its gate evidence.
- `src/nwn_dm_mcp/` - the package: `protocol.py` (wire
  constructions), `session.py` (managed DM session), `poll.py`
  (cleartext server polling), `bridge.py` (SQLite action queue),
  `server.py` (the MCP server), `hydrogen.py` (LibHydrogen
  wrapper).
- `tests/` - a bridge row-shape test and a JSON-RPC test client
  that drives the server over stdio.
- `prototype/` - the spike code the package was distilled from.
- `captures/` - pcap of a real DM login against a test server
  (kept out of the public tree; may contain account identifiers).

## MCP server

The server speaks MCP as newline-delimited JSON-RPC 2.0,
hand-rolled - zero runtime dependencies beyond Python 3.10+
and the LibHydrogen build below. Two transports share one tool
surface:

- **stdio** (default) - for a client on the same host.
- **HTTP** (`--http`) - POST-based JSON-RPC for hosting the
  server next to the game server and reaching it over a private
  network (for example a tailnet). Every request must carry a
  bearer token from the `NWN_MCP_TOKEN` environment variable
  (`Authorization: Bearer <token>`, constant-time comparison);
  with no token configured the HTTP transport refuses to start.
  Bind only a private-network interface (`--bind` /
  `NWN_MCP_BIND`, default 127.0.0.1; `NWN_MCP_PORT`, default
  8765). `GET /health` is the only unauthenticated route.

### Run (stdio)

    pip install .
    HYDROGEN_LIB=/path/to/libhydrogen-old.so nwn-dm-mcp

or, from a checkout without installing:

    PYTHONPATH=src HYDROGEN_LIB=... python3 -m nwn_dm_mcp

### Run (HTTP, hosted)

    NWN_MCP_TOKEN=<token> NWN_MCP_BIND=<private-interface-ip> \
    HYDROGEN_LIB=... PYTHONPATH=src \
    python3 -m nwn_dm_mcp --http --http-port 8765

Clients POST JSON-RPC messages to `/` or `/mcp` with the
`Authorization: Bearer <token>` header; notifications are
accepted with an empty 202 response.

### Configuration (arguments to dm_connect, or environment)

| Setting | Env var | Notes |
|---|---|---|
| Server host | `NWN_DM_HOST` | no default |
| Server port | `NWN_DM_PORT` | default 5122; any port the operator configures |
| Account name | `NWN_ACCOUNT` | default `dm` |
| Public CD key id | `NWN_PUBLIC_ID` | public info (cleartext polls) |
| DM password file | `NWN_DM_PASS_FILE` | default `~/.nwn-dm-pass`; value never logged |
| CD key file | `NWN_CDKEY_INI` | default `~/.local/share/Neverwinter Nights/cdkey.ini` |
| Settings-flood file | `NWN_FLOOD_FILE` | **required** - see below |
| Bridge DB | `NWN_BRIDGE_DB` | SQLite world DB for bridge tools |
| LibHydrogen | `HYDROGEN_LIB` | pre-overhaul build (see Build note) |
| HTTP bearer token | `NWN_MCP_TOKEN` | **required for `--http`**; never logged |
| HTTP bind address | `NWN_MCP_BIND` | default 127.0.0.1; private interface only |
| HTTP port | `NWN_MCP_PORT` | default 8765 |

The **settings flood** is a one-time capture of the real client's
post-login settings-sync frames (hex, one frame per line). It is
operator-supplied runtime data, not shipped: without the replay
the server never sends the character message and the session
never reaches in-world state (AUTH.md SS6). Which flood elements
are load-bearing has not been bisected yet.

### Tools

Wire-side (the held DM session):

- `dm_connect` - log in fresh and hold the in-world session.
- `dm_disconnect` - end the session (the server reaps it after
  ~75-95 s; an immediate same-key reconnect may be refused).
- `dm_status` - state, uptime, server name/module, frame counts.
- `dm_who` - fresh cleartext poll (server name, module, build,
  BNXR count byte: 1 idle / 2 with one session in-world) plus
  this session's own presence. A named player roster is not yet
  exposed by the reversed protocol surface.
- `dm_say` - DM chat (wire message 9/14). **Caveat:** server-side
  delivery is not yet witness-verified - the test server logs no
  client chat, and CD-key auth prevents a fabricated witness
  client (AUTH.md SS7).

Bridge-side (rows in the world DB's `bridge_action_queue`,
executed or refused by a server-side consumer on its heartbeat):

- `dm_jump` (target_tag or location `area_tag:x:y:z`),
  `dm_spawn` (resref), `dm_freeze` (target_tag),
  `dm_inspect` (target_tag).
- With no bridge DB configured, these tools say so plainly and
  queue nothing.
- The queue contract is the in-module one: rows are inserted
  with status `pending`; the consumer moves them to `done` or
  `refused` with a reason in `result`. The currently deployed
  consumer implements inhabitant verbs (speak/move/follow/wait/
  attack); DM verbs ride the same queue and await a DM-action
  consumer module-side.

### Tests

    python3 tests/test_bridge.py                    # row shape
    python3 tests/test_http.py                      # HTTP auth + surface
    python3 tests/test_session_messages.py          # frame message walk
    PYTHONPATH=src python3 tests/mcp_test_client.py smoke
    PYTHONPATH=src NWN_BRIDGE_DB=/tmp/x.sqlite3 \
        python3 tests/mcp_test_client.py bridge
    # full wire flow (needs a test server + operator config):
    PYTHONPATH=src HYDROGEN_LIB=... NWN_TEST_HOST=... \
        NWN_TEST_ACCOUNT=... NWN_TEST_PUBLIC_ID=... \
        NWN_TEST_PASS_FILE=... NWN_TEST_CDKEY_INI=... \
        NWN_TEST_FLOOD_FILE=... \
        python3 tests/mcp_test_client.py e2e

## Build note

The protocol needs LibHydrogen's **pre-overhaul** `hydro_kx_XX`
API (commit `850c6bd`, 2019-11-22); current libhydrogen is not
wire-compatible. `src/nwn_dm_mcp/hydrogen.py` documents the build.

TDQS

A3.6/5.0

Scored across 9 tools

Disambiguation4/5

Each tool maps to a distinct action (connect, disconnect, say, jump, spawn, freeze, inspect), and the four bridge-queue tools are differentiated by their action payloads. The only mild overlap is dm_status vs dm_who, since both report server/session information, though the descriptions clarify that one is session state and the other is a presence poll.

Naming Consistency5/5

All nine tools use the same dm_ prefix followed by a single lowercase snake_case verb/noun, giving a fully predictable pattern. Query-style names (dm_status, dm_who) sit naturally alongside action names without breaking the convention.

Tool Count5/5

Nine tools is well within the sweet spot and each one earns its place: session lifecycle, queries, chat, and four distinct bridge actions. Nothing appears redundant or padded.

Completeness3/5

Session lifecycle and basic DM actions are covered, but the bridge-queue tools are write-only — there is no tool to read the queue, poll a pending action's result (dm_inspect explicitly writes results to a row the agent cannot fetch), or cancel an action. Common DM operations like kick/kill, give item, or possess target are also absent.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues