Fantasy Gamebook Engine
An MCP server that provides 18 tools for dice rolling, luck tests, character management, world state, combat, and save/load, enabling an AI game master to run a solo-play gamebook entirely through deterministic game logic.
README
Fantasy Gamebook Engine
A solo-play gamebook (Fighting Fantasy–style) engine where an AI acts as the game master and narrator. The whole design turns on one hard rule:
The AI never invents numbers or rolls dice in prose. All randomness, state, and combat math go through an MCP server. The AI only narrates and offers choices.
Everything numeric — dice, attribute generation, luck tests, combat rounds, persistence — is owned by a deterministic Python engine and exposed to the narrator as MCP tools. The narrator (Phase 1: Claude Code) reads real state, calls tools, and writes the story around the results.
Status: Phase-1 MVP — implemented and green. Python engine under src/gamebook/, an MCP
server exposing 18 tools, 158 passing tests at 96% coverage across tests/engine,
tests/server, and tests/qa. The narrator harness lives as Claude Code skills and commands
under .claude/.
Why it's built this way
The system decomposes into 8 modules, and the golden rule of the design is that dependency arrows point only at interfaces/contracts, never at concrete implementations. That discipline is what makes three things swappable without touching the rest:
- Storage —
JSONStoragetoday,PostgresStoragetomorrow. Injected at server startup. - Adventure — swap the lore module without touching the engine. Today a
SKILL.md. - Harness — swap who narrates (terminal → web) while reusing the same MCP tool contract.
07 harness ───────► 05 mcp ◄──────── 08 commands
│
┌─────────────┼─────────────┐
▼ ▼ ▼
01 rules 04 combat 03 storage (interface)
│ │ ▲
└──────►──────┘ │ implements
│ │
▼ │
02 domain ◄─────────────┘ (shared data contracts)
06 adventure-module ──(lore consumed by)──► 07 harness
| # | Module | Responsibility | Pluggable? |
|---|---|---|---|
| 01 | rules |
Pure rules engine — dice, attributes, luck test, one combat round. No I/O, RNG injected. | — (stable) |
| 02 | domain |
Shared data contracts + invariant validation. Depends on nothing. | — (stable) |
| 03 | storage |
Persistence behind the StorageBackend interface. |
✅ JSON ↔ Postgres |
| 04 | combat |
Combat lifecycle (start → rounds → flee → end). | — |
| 05 | mcp |
MCP server exposing tools to the harness. No game rules of its own. | — (stable contract) |
| 06 | adventure-module |
Pluggable static lore (zones, bestiary, victory). Debut: Ignarok. | ✅ Ignarok ↔ others |
| 07 | harness |
The narrator/master that talks to the player and calls the MCP. | ✅ Claude Code ↔ agent |
| 08 | commands |
System commands (/hero, /backpack, /map, /save). |
✅ add new ones |
Project layout
src/gamebook/
domain/ # data contracts: CharacterSheet, World, Event, Combat, ArchiveRecord
rules/ # pure rules engine (interfaces + implementation), injectable RNG
storage/ # StorageBackend interface + JSONStorage + in-memory impl
combat/ # combat lifecycle (interfaces + implementation)
mcp/ # FastMCP server over stdio — orchestrates the modules
docs/
00-index.md … 08-commands.md # specs (requirements)
CONTRACTS.md # authoritative English code contract
adrs/ # architecture decision records
learning-lessons/ # captured gotchas
.claude/
skills/ # game-master, combat-sub-agent, ignarok (the Phase-1 harness)
commands/ # /hero, /backpack, /map, /save
tests/
engine/ server/ qa/
Requirements
- Python 3.13 (
.python-version),requires-python >= 3.12 uvfor dependency management and running
Quickstart
# Run the full test suite
uv run pytest -q
# Scope it
uv run pytest tests/engine -q # pure rules (seeded RNG, in-memory storage)
uv run pytest tests/server -q # storage + MCP server
uv run pytest tests/qa -q # plugability / isolation / e2e
# The golden-rule plugability audit (catches any module reaching past an interface)
uv run pytest tests/qa/test_dependencies.py tests/qa/test_isolation.py -q
# Start the MCP server over stdio (exits cleanly on EOF)
uv run python -m gamebook.mcp.server
The server is registered for Claude Code in .mcp.json, so the narrator skills can call it
directly.
MCP tools (18)
The authoritative contract is docs/CONTRACTS.md §6. Grouped by purpose:
| Group | Tools |
|---|---|
| Dice & luck | roll_dice, test_luck |
| Character | create_character, read_character_sheet, update_character_sheet, archive_character |
| World | read_world, update_world |
| Chronicle | register_event, read_events, read_summary, update_summary |
| Combat | start_combat, resolve_combat_round, flee_combat, end_combat |
| Saves | save_progress, load_progress |
Game rules (reference)
- Attributes:
skill= 1d6+6,stamina= 2d6+12,luck= 1d6+6 — each tracksinitial/current. - Luck test: success if roll ≤ current luck; luck always decrements by exactly 1 afterward.
- Combat round: each side's attack strength =
skill+ 2d6; higher AS hits for base damage 2, a tie deals 0. - Luck on a hit: won+lucky → 4, won+unlucky → 1, lost+lucky → 1, lost+unlucky → 3.
- Death / flee: hero at 0 stamina →
alive: false; fleeing costs 2 stamina and only if allowed.
rules and combat are tested in full isolation with a seeded RNG and in-memory storage —
deterministic, no disk, no AI.
Playing a session (Phase-1 harness)
When you sit down to play rather than develop, Claude Code becomes the Game Master.
Session-opening rule: before narrating anything, the master reads real engine state via
MCP (read_character_sheet, read_world, read_events, read_summary). No living character
→ it offers create_character and starts the adventure's opening. A living character → it
resumes from the exact recorded point (never restarts, re-rolls, or contradicts recorded
facts). Every number and state change routes through MCP tools.
- Skills (
.claude/skills/):game-master(narrator),combat-sub-agent(runs one fight),ignarok(the debut adventure — swap this file to swap adventures). - Commands (
.claude/commands/):/hero,/backpack,/map,/save— read-outs and checkpoints that reflect real MCP state and don't advance the story.
Documentation
docs/00-index.md— start here; maps every module.docs/CONTRACTS.md— the authoritative English code contract (cross-module interfaces, domain schema §2, MCP tool contract §6). When code and a spec disagree, CONTRACTS.md governs.docs/adrs/— architecture decision records (ADR-001 … ADR-010).docs/learning-lessons/— captured gotchas worth not relearning.CLAUDE.md— guidance for Claude Code working in this repo.
Roadmap
- Phase 1 (current): Claude Code as harness,
JSONStorage, adventure asSKILL.md. - Phase 2: a PydanticAI/FastAPI harness with structured
Sceneoutput for a web frontend, plusPostgresStorage— reusing the same MCP tool contract and adventure module unchanged. That reuse is the entire point of the architecture.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。