Architecture

rpg-engine is a set of commands over a campaign folder of markdown and YAML files, served as a command line (rpg) and as an MCP server (rpg mcp), with a dashboard for the player. This page maps how the parts work together; structure.md says where they are and what each depends on, the code says the rest. AGENTS.md holds the development rules.

Comments and tests cite rule codes such as INH-4 or CON-1: grep -r "INH-4" src test finds the code and the tests of a rule.

1. A command

A command is defined once with defineCommand (core/registry.ts): name, a description written for an AI, a zod input, flags (readOnly, alwaysLoad, needsPlayer, slow, positionals), run(input, context) and format(result).

  • One list: commands/index.ts; the CLI (cli/command-line.ts) and the MCP server (server/mcp.ts) are generated from it, and the dashboard calls view through /api/view.
  • Placeholders: a description names a game’s thing as {{counter}} (core/examples.ts); the MCP server fills in the system’s names (commands/shared/examples.ts), the CLI neutral ones.
  • Context (core/context.ts): everything from outside, injected: folder, randomness, time, ids, system folders. Tests use testContext() from test/support.ts.
  • Store (core/store.ts): a call reads each file once, keeps it deep-frozen, writes through it. The MCP server keeps a SessionStore (core/session-store.ts) across calls and tells the master what changed outside (commands/shared/campaign/outside.ts).
  • Log: a command that changes the game appends public or hidden lines to log.jsonl (core/log.ts).
  • Chats and book: the MCP server copies the hosts’ chats into sessions/ as text (chats/); book writes chapters of prose over closed scenes (commands/shared/campaign/book.ts). Both leave out what a load undid.
  • Output: an AI reads it. Errors say what went wrong and what to call next; a refused write says “Nothing changed”. Everything is checked before the first write.

2. Data on disk

A campaign

campaign.yaml   settings
state.yaml      the present: mode, date, open scene, journey, the master's mood
log.jsonl       one JSON line per change
world/          the pages, one folder per type
rules/ images/  house rules and pictures (optional)
.saves/         saves, one zip each
.imports/       documents being made adventures
inbox/          files the player attached, for the master
layers/<system>/   the campaign's changes to the system's pages
extensions/<name>/state.yaml   a package's own state
.dashboard-port the dashboard's port
exports/ sessions/ book/   exported adventures, chats, the story as a book (not saved)
.player/        the player's notes
AGENTS.md, CLAUDE.md, .mcp.json, .claude/, .codex/, .agents/, opencode.json, .opencode/   host files
  • init creates it (campaign/create-campaign.ts); on an existing campaign it only refreshes the host files (campaign/host-files.ts), as the MCP server does as it starts.
  • A campaign is the nearest folder upwards with a campaign.yaml.
  • The router: outside a campaign, rpg mcp is a router for Antigravity (server/route/): one child server per campaign, a call sent to a campaign’s child only when rpg --approve noted its conversation as playing there, never to another campaign’s.

Pages

A page is a markdown file under world/ (pages/world.ts): its name is the file name, its type the first folder, its fields the YAML frontmatter. The format is Obsidian’s: [[links]], headings make sections, > [!secret] Name blocks. Standard fields (pages/standard-fields.ts) have fixed meanings; others are free or declared by the system. Commands write with writePage (pages/write-page.ts).

An adventure

A folder or .zip with adventure.yaml and only world/, images/, rules/ and layers/<system>/world/ (adventures/).

  • package install copies it whole or not at all, and the log records it so it goes back with a save; package export writes the world as it was at the start (commands/shared/books/adventures.ts).
  • import makes a document an adventure: one outside the campaign needs the player’s yes; the engine reads it (documents/), keeps it in pieces in .imports/ (importer/), and a helper outside the chat, fenced to that folder, sets the world (commands/import/run.ts).

A game system

A folder with system.yaml, world/, rules/, images/, LICENSE.md and versions/<name>/, named by the campaign’s system; its ruleset setting picks a version. See game-systems.md.

  • The engine ships systems/free/ alone; any other is an npm package in the player’s data folder, with its own repository. The MCP server installs one of ours a campaign lacks (commands/package/install.ts).
  • systems/read.ts validates it into a frozen GameSystem; systems/code.ts loads its code into the Context.

A package’s code

  • One contract for extensions and systems: core/packages/api.ts, exported by index.ts. PackageEntry gives PackageCode: commands, what happens as time passes, widget hooks, hooks after the engine’s writes, the system’s first (commands/shared/packages/hooks.ts).
  • Declarations of extension.yaml and system.yaml alike: core/packages/declarations.ts; page.ts passes the page side to page code.
  • The API number (rpg-engine.api in package.json) goes up with every addition an older engine would refuse or misread, a new key too; not for one it ignores unharmed. The engine reads a range.

3. Mechanisms

Mechanism What it does Where
World Campaign and system pages in a frozen World; system pages read-only, with version, translation and the campaign’s layer merged over them; only write with layer writes a layer pages/world.ts, pages/layers.ts
Inheritance A character derives from its creature, an item from its item; a page keeps only its differences pages/inheritance.ts
Containers A page never records where it is: its container lists it; locate climbs the chain pages/containers.ts
Stacks Goods counted where they are held, [[Torch]] 7 pages/stacks/
Links Links in fields and text, backlinks, rename that follows them pages/markdown.ts
Secrets Only characters know, in knows; reveal adds commands/reveal.ts
Counters and money current/max within limits; coins with change counters/, money/
Effects Lines in effects over a page’s fields, ending at a date, turn, scene or rest effects/
Formulas and sheet Computed fields and limits; the character sheet as data, the same in chat and dashboard core/formula.ts, commands/shared/sheet/
Progression Levels, features, uses a rest refills, experience progression/
Rolls and asking Dice, hidden rolls with proofs, tables, the oracle; questions with buttons dice/, commands/roll/, commands/ask.ts
Scenes The open scene in state.yaml: who, turns, hostility scene/
Saves A zip at each scene’s end; load as the campaign allows saves/
Git Where the campaign asks: commits at start, save, load and scene end, pull with rebase, push; never asks for credentials or keeps a token; a conflict pushes nothing git/
Pictures A page’s image, made by the chat, Codex, Antigravity’s agy or the master’s own image tool, as pictures-by says images/, helpers/
Inbox The player’s files in inbox/, listed and read by inbox; only files really in the folder commands/inbox.ts
Themes A dashboard theme from a system or a data package, checked and turned into CSS custom properties themes/
Moods, diaries Moods from a closed list; a character’s thoughts kept in the log, never on their page core/mood.ts, commands/thoughts.ts
The player’s view What the player may see, never a secret; with as, what another character knows, for the character agent alone commands/view/
Time and events The game date and calendar; advance stops at the first event due (by date, clock or condition) and ends effects time/, triggers/
Environment A place’s environment holds for everything inside it pages/environment.ts
Travel Roads as places with stops, positions, maps map/, grid/
Rules and library House rules, the system’s chapters, the player’s books (indexed in their data folder, never written into a campaign), precedents rules/, library/
Problems and checks What validate lists, mostly blocking nothing; contradictions in party and scene told to the master commands/shared/campaign/problems.ts, commands/shared/campaign/checks.ts
Instructions core.md at every chat, then parts per mode and situation, with the system’s and extensions’; the character agent may call only view with as instructions/, commands/shared/campaign/parts.ts
Extensions The packages campaign.yaml lists: parts, calendar, additions to the system, commands, widgets, words extensions/, packages/, commands/package/
Languages and translation Hand-made translations as packages; what lacks one is translated outside the chat by a helper with every tool denied languages/, commands/translate.ts
Consent The player’s yes, and the folder picker, in a window of the operating system confirm/
Last choices The player’s last setup choices, suggested to a new campaign, never chosen campaign/last-choices.ts

The character agent’s fence. Where a host gives the character agent every tool, the server refuses its calls but view (server/subagent.ts) and a hook refuses the host’s tools (server/approver.ts). In Codex that hook must be trusted in Codex’s config.toml, or the role is taken away (campaign/codex.ts).

The dashboard

The MCP server serves it on 127.0.0.1 as the chat starts (server/dashboard/index.ts); the page is dashboard/app.tsx.

  • Port: the campaign’s own, kept in .dashboard-port (server/dashboard/ports.ts); another chat, or the table, takes the page over at the same port.
  • API: /api/view, the player’s few actions, /events when files change (no data: the page reads view again), /api/widgets, pictures by name only.
  • The doorbell: what the player’s character does reaches the master at the head of its next call (server/player-acts.ts, server/bell/), rung by rpg --listen, a host’s Stop hook or plugins/opencode.js.
  • The engine’s update: /api/engine gives versions and how the engine was installed; a POST, with the player’s yes, installs npm’s latest (server/dashboard/engine.ts).
  • Words: t() from dashboard/i18n/. A refusal travels as code and values beside the engine’s words (core/refusal.ts), from the table and the launcher too. Help’s guide is dashboard/guide/.
  • No game: a game’s cards and panels are its packages’ widgets and page modules, mounted where the page keeps a place (dashboard/packages/page-code.tsx).
  • Preferences are the player’s on every device, in page-prefs.json in their data folder, never in a campaign (server/dashboard/prefs.ts).
  • The phone: a second door on the home network (server/dashboard/phone/), opened only from the computer’s page; a device pairs once by QR code and gets a key in a cookie. Plain HTTP, home network alone; a device never keeps the table alive.

The table

rpg table (server/table/) serves the campaign’s page with no chat open, and can hold a chat of its own with the master.

  • Where: a local socket (a named pipe on Windows) and a token, in the player’s data folder (tables/, readable by them alone).
  • Seating: a campaign’s rpg mcp knocks with the token; one chat at a time sits and only passes its lines to a server the table makes for it. Others only look, so one engine writes the campaign.
  • Ending: a few seconds after its last page went with no chat sitting, or once a loaded package’s code changed on disk; rpg table --stop asks it with its token, never killing it from outside.
  • Its own chat: in the host’s own program: Claude Code by its protocol, Codex by codex app-server, OpenCode and Antigravity by the Agent Client Protocol. Each message is appended whole as a JSON line to sessions/table-chats/, so a cut leaves at most one torn line.
  • Hosts: the page lists every host with its account, install state and models (helpers/access.ts); a host the player is signed out of is never started.
  • The master’s tools: rpg commands, web search and page reading, the character subagent; tools that touch the computer are taken away where the host allows, and one run anyway stops the turn.
  • Marks: [roll <id>], [clock <page>] and [turns] in the master’s words become data once the message is complete (server/table/marks.ts).
  • Log: tables/<key>.log (server/table/log.ts), never a word of the chat.

The launcher

rpg launcher (server/launcher/) is the page of this computer’s campaigns: it lists them, creates new ones and opens them.

  • Where: 127.0.0.1, first free port from 5750 to 5759, else any, behind the gate it shares with the dashboard (server/page/); address and token in launcher.json in the player’s data folder. It ends a few seconds after its last page went.
  • The list: campaigns it remembers, those in ~/RPG Engine and those with a table’s place; each with its system’s colours and the player character’s portrait. One it cannot read stays listed with why.
  • Opening: it starts the campaign’s table apart and opens its page in an app window (desktop/window/); a table of another engine is never ended from here.
  • rpg open: what the desktop icon runs: a waiting window, then the launcher found or started (desktop/).
  • AI apps: installs and signs in Claude Code, Codex and OpenCode, one at a time per computer (helpers/access.ts); on Windows it also finds the programs inside the desktop apps (helpers/desktop.ts). A status it cannot read never counts as signed out.
  • Separate: the table and the dashboard never import the launcher; the CLI gives rpg table the way to it.

4. Tests

  • npm test runs vitest on test/; npm run test:downstream runs our packages’ own tests against this engine.
  • Commands are tested through runCommandLine(args, testContext(folder)); *-laws.test.ts check rules with fast-check on random worlds.
  • Tests play test/systems/fake, never published, and free; madeSystem in test/support.ts makes a system that lacks something.
  • test/system-packages.test.ts checks every installed system with the kit’s systemChecks, which a system’s repository runs on itself.

5. How to add things

A command. Create src/commands/<name>.ts, or the file in its topic’s folder, with defineCommand. In run: requireCampaign, campaignWorld, read with effectiveFields, check everything, write through the modules, log. Return a result; format tells the AI what happened and what next. Add it to commands/index.ts, test it through runCommandLine, run npm run docs.

A field kind. Add it to fieldKinds in core/field-kind.ts, give it a meaning where kinds are read (declaredKind in pages/system-pages.ts and its callers), and its check to fits in testing/system-checks.ts.

A key of system.yaml. Add it to systemSchema in systems/schema.ts and to readSystem in systems/read.ts, use it from a command helper, check it in testing/system-checks.ts when a value can be wrong, have the fake system use it, describe it in game-systems.md.

A page of a system. Add world/<type>/<Name>.md in the system’s own repository; the kit’s systemChecks, in its tests, checks its name, fields and links. Only open-licensed content, with its attribution.


© 2026 Stefano Caronia · code under the MIT license