mesen-probe
by louranveil
README.md
# mesen-probe
Extension for [mesen-for-ai](https://github.com/paulomanrique/mesen-for-ai), the MCP server that drives Mesen headlessly. `mesen-probe` adds what a ROM hacking or translation workflow needs and the original does not offer, without copying the original: it runs on top of it.
## What it adds
| Tool | Purpose |
| --- | --- |
| `state.save`, `state.load` | SNES savestates as standard Mesen `.mss` files. Mesen only allows them inside an exec callback of the main CPU, so the state is taken on the next instruction, or exactly at the instruction when the session is held at an `exec` breakpoint hit. After a load the frame counter restarts at 0. |
| `snesVideoRam`, `snesCgRam`, `snesSpriteRam` | Read-only memory types for VRAM (64 KiB), palette (512 bytes, BGR555) and OAM (544 bytes). Addresses are byte offsets. Writes are rejected. |
| `memory.export` | Writes a memory region (default: all of it) to a binary file and returns its SHA-256. |
| `cdl.export_binary` | Writes the Code/Data Logger as raw bytes, one per ROM byte (flags `0x01` code, `0x02` data, `0x04` jump target, `0x08` sub entry point), and returns its SHA-256 and counts. The original `cdl.export` writes one JSON object per byte, hundreds of MB for a 4 MiB ROM. |
| `video.export_frame` with `format` | `ppm` (default, raw framebuffer, 256x239) or `png` (the visible frame as Mesen shows it, 256x224). |
| `probe.info` | Confirms that the extension is loaded in the session. |
| Windows launcher | Each session gets an isolated portable Mesen home (hardlinked `Mesen.exe` plus a deterministic `settings.json`), so the original, which only supports Linux, works on Windows. |
All tools of mesen-for-ai remain available. Savestates and video memory are implemented for SNES; other consoles return an error.
## How it works
mesen-for-ai has no extension points, so `patches/` holds two small patches against it: `bridge.lua` loads the Lua file named by `MESEN_BRIDGE_EXTENSION` and gives it an API table, and `SessionManager` starts Mesen through an overridable `_spawn`. The extension is `src/mesen_probe/probe.lua`; the Python side subclasses the original daemon and session manager. See [patches/README.md](patches/README.md).
## Setup
1. Get MesenCE (the emulator) and mesen-for-ai.
2. Check out mesen-for-ai at the commit in `patches/BASE` and apply the patches, leaving them uncommitted:
```sh
git -C /path/to/mesen-for-ai checkout "$(cat patches/BASE)"
git -C /path/to/mesen-for-ai apply --ignore-whitespace patches/*.patch
```
3. Install mesen-probe, preferably in a virtual environment (the sources in `src/` are installed as the package `mesen_probe`), point it at the patched mesen-for-ai, and start the server:
```sh
pip install -e .
export MESEN_FOR_AI_DIR=/path/to/mesen-for-ai
export MESEN_BIN=/path/to/Mesen # Mesen.exe on Windows
python -m mesen_probe.daemon
```
mesen-probe refuses to start if mesen-for-ai is missing or not patched. The server speaks MCP over stdio; an example `.mcp.json` entry is:
```json
{
"mcpServers": {
"mesen": {
"command": "/path/to/venv/bin/python",
"args": ["-m", "mesen_probe.daemon"],
"env": {
"MESEN_FOR_AI_DIR": "/path/to/mesen-for-ai",
"MESEN_BIN": "/path/to/Mesen"
}
}
}
}
```
## Tests
```sh
MESEN_FOR_AI_DIR=/path/to/mesen-for-ai MESEN_BIN=/path/to/Mesen python -m unittest discover -s tests -v
```
Emulator tests run on a synthetic SNES ROM generated in memory by `tests/support.py`, so no game is needed; they are skipped without `MESEN_BIN`. The tests that check the patches need `MESEN_FOR_AI_DIR`.
## Limitations
- Savestates are tied to one ROM build and one Mesen version.
- A frame captured right after a reset can still show boot output; step past the boot first.
- At maximum speed Mesen skips rendering frames based on wall-clock time (`SnesPpu.cpp`, `_skipRender`), so a capture can show the previous frame while the emulated state has moved on. The Windows launcher disables it (`Snes.DisableFrameSkipping` in the session `settings.json`); the Linux path uses mesen-for-ai's own shell runner, which does not, so captures taken there are not reproducible pixel for pixel. Memory dumps (WRAM, VRAM, CGRAM, OAM) are never affected.
- The patches are made against one upstream commit; a newer mesen-for-ai may need them adjusted.
## License
The code of mesen-probe is released under the MIT License; see [LICENSE](LICENSE). The two files in `patches/` are the exception: they are modifications of mesen-for-ai, which is licensed under GPL-3.0-only, so they stay under that license (see [patches/LICENSE](patches/LICENSE)). mesen-for-ai is by paulomanrique; see its repository for its license and credits.
mesen-probe imports and extends mesen-for-ai, which is GPL-3.0-only, and the MIT License is compatible with it. Whoever distributes mesen-probe together with, or combined with, mesen-for-ai distributes a combined work and has to meet the GPL for that combination.
Never commit ROMs, BIOS images, savestates or any other copyrighted game material.