handwriting-mcp
by NorkelL
README.md
# handwriting-mcp
An MCP server that writes text in **your own handwriting**: a neural network (Graves-style LSTM with attention)
that you fine-tune on a few dozen lines of your handwriting. Claude (or any MCP client) can then produce
handwritten notes as PNG/SVG/PDF, handwritten diagrams with hand-drawn shapes, and even **real Goodnotes files
with editable pen strokes**.
- 13 built-in base styles work out of the box (English alphabet only).
- After fine-tuning: your own style, with umlauts/ß, capital Q/X/Z and more symbols.
- Two ways to collect training data: write lines on a **recording website** (tablet + stylus), and/or reuse
your existing **Goodnotes notebooks** (with an AI-assisted review step).
- Runs locally (stdio, e.g. Claude Code / Claude Desktop) or as an HTTP server (Docker, optional Cloudflare
Tunnel for claude.ai).
> **Using Claude Code?** Open this folder in Claude Code and say *"set this up for me"*. `AGENTS.md` guides it
> through the whole setup, including the review of notebook data.
## Requirements
- Linux (macOS/WSL should work but are untested). Python 3.10-3.13 or [uv](https://docs.astral.sh/uv/).
- **A GPU for training**: NVIDIA (CUDA) or AMD (ROCm). Serving runs fine on the CPU.
Developed and tested on an AMD RX 7800 XT with ROCm; the CUDA path uses the standard PyTorch CUDA build but
has not been tested by the author.
- System packages: `libcairo` (PNG export) and `poppler-utils` (`pdftoppm`, Goodnotes previews).
- For recording: any tablet with a stylus and a browser in the same network (iPad + Apple Pencil works well).
- For the notebook route: Goodnotes notebooks with lots of handwritten running text.
## 1. Setup
```bash
git clone https://github.com/NorkelL/handwriting-mcp && cd handwriting-mcp
scripts/setup.sh # detects CUDA / ROCm / CPU; or: scripts/setup.sh --gpu cuda
.venv/bin/python scripts/smoke_test.py
```
`setup.sh` creates `.venv`, installs the matching PyTorch build and downloads the pretrained base model from
[sjvasquez/handwriting-synthesis](https://github.com/sjvasquez/handwriting-synthesis) (converted locally, no
TensorFlow needed). The smoke test talks to the MCP server like a client would and writes previews to `out/smoke/`.
## 2. Collect training data
You need at least one of the two routes. Combining them works best: recordings teach the rare characters, notebooks
add volume and your natural everyday writing.
### Route A: recording website (recommended start)
```bash
.venv/bin/python scripts/recorder/server.py --lang en # or --lang de (German prompts with umlauts)
```
Open the printed address (`http://<pc-ip>:8765`) on the tablet. You may need to allow port 8765 in your firewall.
Write each prompt in one line, the way you normally write, then *Save & next*. The first ~30 prompts contain the
rare characters (Q, X, Z, digits, symbols, umlauts), so do those first.
- **40-60 lines** give a clearly recognizable style (the author's first model used 23 recordings plus notebook
data; the final one 48 recordings plus notebook data).
- *Edit text* if you wrote something different from the prompt, *Split* if a line doesn't fit.
- Recordings are saved to `data/recordings/`. The server has no login: only run it in your home network.
### Route B: Goodnotes notebooks
Goodnotes already recognizes your handwriting for its search. That recognition is stored inside the `.goodnotes`
file together with the pen strokes, so notebooks can become training data **without writing anything new**.
**But the recognition is wrong very often** (in testing, ~40 % of the segments that looked fine still had errors:
wrong words, swapped order, words from neighboring lines). Training on that teaches the model wrong letters.
So every segment must be **checked against an image of its strokes**: by an AI with image input (Claude Code does
this automatically when following `AGENTS.md`) or by yourself. Without this review, this route does not work.
1. In Goodnotes: open the notebook -> Share -> Export -> format **Goodnotes** -> save to Files. Use notebooks with
lots of running text (lecture notes, essays); formulas and drawings are filtered out or dropped in the review.
Put the file into `data/notebooks/` (or upload it on the `/files` page of the recording server).
2. Extract the recognized words:
`.venv/bin/python scripts/notebook_extract.py data/notebooks/Biology.goodnotes`
3. Build candidate segments (geometric filters drop words from other lines, wrong order, big gaps):
`.venv/bin/python scripts/notebook_candidates.py --name Biology`
If you already trained a model (e.g. on recordings), add `--model <name>`: it scores how well each text fits
its strokes, tries the alternative recognition candidates and drops bad segments before the review (strict:
keeps roughly a fifth, but those need fewer corrections).
4. **Review** in sheets of 20 segments:
`.venv/bin/python scripts/notebook_review.py sheet Biology 0` -> `out/review/Biology_0.png`
Write corrections into `data/notebook_units/Biology.review.json` (format in
[`scripts/notebook_review.py`](scripts/notebook_review.py)), then
`.venv/bin/python scripts/notebook_review.py apply Biology` -> `data/notebook_segments/Biology.json`.
## 3. Train
```bash
.venv/bin/python scripts/finetune.py --name my-hand
```
Uses everything in `data/recordings/` and the reviewed `data/notebook_segments/*.json`. Defaults: 1500 steps,
validation every 200 steps, only the best checkpoint is kept (`models/my-hand.pt`). For reference, on an RX 7800 XT
a step takes ~1.5 s (1500 steps ≈ 40 min). With little data (< 50 lines) the validation loss usually stops improving
early (around step 200-800), that's expected; the best checkpoint is what counts. Keep the PC awake while training.
Then pick the style that looks best and make it the default:
```bash
.venv/bin/python scripts/pick_styles.py grid my-hand # -> out/styles_my-hand.png, numbered candidates
.venv/bin/python scripts/pick_styles.py save my-hand 7 me --default
.venv/bin/python scripts/smoke_test.py # default style should now be "me"
```
## 4. Use it with Claude
**Claude Code / Claude Desktop on the same machine (stdio):**
```bash
claude mcp add --scope user handwriting -- "$PWD/bin/handwriting-mcp"
```
For Claude Desktop add `{"mcpServers": {"handwriting": {"command": "/abs/path/to/bin/handwriting-mcp"}}}` to its config.
**HTTP (claude.ai, other machines):**
```bash
cp .env.example .env # set HANDWRITING_MCP_SECRET (random) and, for public use, HANDWRITING_PUBLIC_URL
docker compose up -d --build # or without Docker: scripts/serve_http.sh
docker compose --profile tunnel up -d --build # + Cloudflare Tunnel (TUNNEL_TOKEN in .env)
.venv/bin/python scripts/smoke_test.py --http http://localhost:8770/mcp/<secret>
```
claude.ai needs a public HTTPS URL: add `https://<your-domain>/mcp/<secret>` as a custom connector
(Settings -> Connectors). The secret in the path is the only protection, so keep it long and private.
Generated files are served at `/f/<random-name>` and deleted after `HANDWRITING_KEEP_DAYS`.
**Allow your server's domain in the client.** Tool calls go through MCP, but the generated files come back as
download links. When Claude fetches one of them (to save it, put it into a document or another connector), that
is a normal web request from Claude's environment, so your domain must be allowed there: on claude.ai under
Settings -> Capabilities (network access for code execution / allowed domains), in Claude Code via the web-fetch
permission / sandbox network allowlist. Otherwise the tool works and shows a preview, but the file download fails.
### Goodnotes output
`goodnotes_write` needs a paper template: in Goodnotes create a one-page notebook with the paper you want, scribble
anything on it (removed automatically), export it as **Goodnotes** and put it into `data/goodnotes_templates/`
(or upload it on the recorder's `/files` page). Open the generated `.goodnotes` file on the iPad to import it.
## MCP tools
| tool | what it does |
|---|---|
| `list_styles`, `preview_styles` | available styles, side-by-side preview |
| `handwrite` | text -> PNG/SVG/PDF (transparent, lined, grid, ... background) |
| `measure_handwriting` | size of a text before placing it (returns a `layout_id`) |
| `compose` | pages/graphics from handwritten texts + hand-drawn shapes (arrows, boxes, brackets, ...) |
| `goodnotes_templates`, `goodnotes_write` | real Goodnotes files with editable pen strokes |
## Project layout
```
src/handwriting/ model (PyTorch), engine (generation, layout), MCP server, Goodnotes reader/writer
scripts/ setup, base-model download, recorder, notebook pipeline, finetune, pick_styles, smoke_test
prompts/ recording prompts (en, de); check coverage with scripts/check_prompts.py
data/, models/, out/ your data, trained models and output (git-ignored)
```
## Known limitations
- Rare characters need examples: if a character never appears in your data, the model can't learn it.
`€ % &` are spelled out by default for own styles (`HANDWRITING_WEAK_CHARS`; language of the words:
`HANDWRITING_SPELLOUT=en|de`), missing umlaut dots are added.
- Long or unusual words are sometimes hard to read; generate again (different `seed`) or raise `neatness`.
- Base styles 0-12 only know the English alphabet without capital Q/X/Z.
## Credits
- Model architecture: Alex Graves, *Generating Sequences With Recurrent Neural Networks* (2013).
- Pretrained weights and base styles: [sjvasquez/handwriting-synthesis](https://github.com/sjvasquez/handwriting-synthesis)
(TensorFlow), downloaded at setup and ported to PyTorch here.
- Goodnotes format: reverse-engineered; thanks to the open-source Goodnotes parsers that documented parts of it.
## License
Code: MIT (see [LICENSE](LICENSE)). The base model is not included and comes with its own terms: it was trained
on IAM-OnDB, which is for non-commercial research only, so fine-tuned models are for personal, non-commercial use.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues