Spellbook MCP Server
README.md
# Spellbook
A licence-aware rules library for tabletop game sources. Point it at the PDFs, markdown and SRD checkouts you
own. It extracts them page by page, records the licence each page is under, and gives you full-text search,
random tables, stat blocks and generators. It also keeps a provenance log of what went where.
It also runs as an [MCP](https://modelcontextprotocol.io) server, so an AI assistant can look rules up in your
books instead of answering from memory, and is told every time which material it may reuse.

A real Claude Code session against a store holding SRD 5.2.1 and one page of table notes, replayed at 1.5x, then
a CLI search on the same store.
<sub>This work includes material from the System Reference Document 5.2.1 ("SRD 5.2.1") by Wizards of the Coast LLC,
available at https://www.dndbeyond.com/srd. The SRD 5.2.1 is licensed under the Creative Commons Attribution 4.0
International License, available at https://creativecommons.org/licenses/by/4.0/legalcode.</sub>
**The software is MIT. Your books are not.** Spellbook never ships, downloads or shares source material. The
store it builds stays on your machine, and it holds copyrighted text. Do not commit it, publish it or serve it to
anyone else.
## Licence classes
Every page is in one of three classes. The class decides what you may do with it:
| Class | What it is | What you may take |
|---|---|---|
| `copy` | Open licence text (OGL Open Game Content, CC BY, an SRD, public domain) | The text, verbatim, with the credit the licence asks for |
| `facts` | Pages whose numbers you may use but whose prose you may not | Numbers and mechanics, never the wording |
| `reference` | Everything else: books you own for reading | Ideas only. Every result starts with the marker `FOR REFERENCE ONLY! FORBIDDEN ILLEGAL` |
Classes come from a licence reading of each source. A source nobody has read is `reference` until someone does.
The reading lives in `overrides.json`, plus an optional catalog. `overrides.json` here is a one-entry template:
replace it with your own readings, including the page ranges a mixed book splits into.
## Install
- Python 3.10 or newer.
- `pip install -r requirements.txt` (pypdf for PDF bookmarks; pytest for the tests).
- Poppler's `pdftotext` and `pdfinfo` on your PATH, for PDFs. On Windows use the poppler build; on Linux and
macOS use the `poppler-utils` or `poppler` package.
## Quick start
```
python spellbook.py ingest ~/books --no-catalog # extract a folder (local paths only; URLs are refused)
python spellbook.py status # what the store holds, by class
python spellbook.py search "retainer morale" # full text: words ANDed, "a phrase", prefix*
python spellbook.py show basic-fantasy 50 # one page, with its licence history
python spellbook.py tables && python spellbook.py tables --list
python spellbook.py roll "wandering monster" # roll a detected table
python spellbook.py blocks && python spellbook.py blocks --list --kind monster
python spellbook.py generate room --level 2 # a generator procedure from procedures.json
python spellbook.py cite --doc basic-fantasy --page 50 --into data/hirelings.yaml --use facts
python spellbook.py lint ./my-game # banned names (from overrides.json) in your files
```
The store defaults to `$SPELLBOOK_STORE`, else `~/.spellbook/store`. Pass `--store` to use another. Run
`python spellbook.py <command> --help` for every option.
## The store
```
docs/<slug>/meta.json the document record and its licence history
docs/<slug>/raw/0001.txt one file per page, reading order
docs/<slug>/layout/0001.txt one file per page, pdftotext -layout (PDF only)
docs/<slug>/outline.json PDF bookmarks, when present
library.sqlite documents and an FTS5 page index
provenance.ndjson every ingest, reclass and cite, append-only
tables.ndjson / blocks.ndjson detected random tables and stat blocks
```
## The MCP server
`spellbook_mcp.py` serves the read commands over stdio:
`spellbook_search`, `spellbook_show`, `spellbook_topics`, `spellbook_tables`, `spellbook_table`,
`spellbook_roll`, `spellbook_blocks`, `spellbook_block`, `spellbook_generate`, `spellbook_lint` and
`spellbook_status`.
- Output passes through unchanged, so the class and the reference marker always reach the model.
- Arguments are validated, and none can add a command-line flag.
- `tables` and `blocks` only ever list; they never rebuild an index.
One write tool, `spellbook_cite`, records provenance. It is offered only when `SPELLBOOK_MCP_WRITE=1`, and
`spellbook cite` itself enforces the licence:
- `copy` needs a copy page;
- `facts` needs a copy or facts page;
- a reference page takes `idea` only, with a note;
- a quote must occur verbatim on the page.
Claude Code, read and write:
```
claude mcp add spellbook --scope user -e SPELLBOOK_MCP_WRITE=1 -- python /path/to/spellbook_mcp.py
```
Any MCP client, read-only:
```json
{"mcpServers": {"spellbook": {"command": "python", "args": ["/path/to/spellbook_mcp.py"],
"env": {"SPELLBOOK_STORE": "/path/to/store"}}}}
```
## Contributing
Issues and pull requests are welcome. Good first areas:
- importers for more formats;
- table and stat-block detection for more systems;
- generator procedures.
- Run the tests before you open a pull request: `python -m pytest -q`. They build their own fixtures and need
no books.
- Never commit extracted text, a store, or a page of any book. Fixtures are written by hand.
- Keep the licence classes strict: a change that could let reference text reach a `copy` or `facts` result needs
a test that proves it cannot.
## Licence
MIT, credited to Mattlock of Schedule 3 Wizards. See `LICENSE`. The licence covers this software only. It grants
nothing in any book you ingest.