Skip to main content
Glama
README.md
# game-loop-mcp

Let an AI coding agent **run your game and look at it**: launch a development build, ask it questions, drive
it, wait for a state, screenshot it, read its log and close it. The agent can then check its own change in
the real game before handing the work back, instead of editing code and hoping.

```
agent ──MCP──▶ game-loop-mcp (Node, zero dependencies) ──TCP 127.0.0.1──▶ listener inside your game
                launch · exec · wait · screenshot · log · close            runs commands on the main thread
```

It grew out of three games we develop with [Claude Code](https://code.claude.com), two online games and a
Unity game. Each had a hand-written "drive the running client" MCP server, and they turned out to be the
same tool. This is that tool, made generic, with the rules those games taught us built in.

## The two halves

**Inside the game, a listener.** A few hundred lines, one file:

- runs text commands **on the game's own thread**, so handlers can touch game state;
- exists only in **development builds**, and listens only when started with `GAME_RPC=1`;
- answers `help`, `ping` (`pid=`, `scene=`, `fps=`...), `arm`, optionally `shot`, plus **whatever commands
  you register**: `hp`, `inventory`, `teleport 10 0 5`, `load Level2`, `spawn goblin`, `give gold 100`...

Ready-made listeners:

| | file | notes |
|---|---|---|
| Unity | [`listeners/unity/GameLoopRpc.cs`](listeners/unity/GameLoopRpc.cs) | drop into `Assets/`. Compiled only into the Editor and Development Builds. Built in: `scene`, `objects`, `find`, `timescale`, `load`, `quit`, and `shot` (game view with UI, works covered or minimised) |
| Python | [`listeners/python/gameloop_rpc.py`](listeners/python/gameloop_rpc.py) | stdlib only. Call `rpc.pump()` once per frame |
| anything else | [`PROTOCOL.md`](PROTOCOL.md) | one command per line, replies end with a `.` line. Port it to Godot, Unreal, C++, Rust... |

**Outside, the MCP server** [`server/game-loop-mcp.js`](server/game-loop-mcp.js). It's pure Node, configured by
one `game.json`, and gives the agent seven tools, prefixed with your game's name:

| tool | what it does |
|---|---|
| `<prefix>_status` | is the game running, and what does `ping` say |
| `<prefix>_launch` | start the dev build with `GAME_RPC=1`. **Refuses while an instance already runs** (it may be yours, or another agent's) |
| `<prefix>_exec` | run commands, get their output |
| `<prefix>_wait` | poll a command until its output matches a regex (`scene=Level2`, `hp=0`...) |
| `<prefix>_screenshot` | returns the **image** to the agent. Uses the game's `shot` if it has one, else captures the window (Windows) |
| `<prefix>_log` | tail the game's log, optionally filtered |
| `<prefix>_close` | ask the game to close and wait. **Never force-kills**: a killed online client can stay logged in server-side |

Commands that change the game are **write** commands. They answer an error until the agent sends `arm write`,
and every one is logged. Looking around is free; changing things is a deliberate step you can see in the
transcript.

## Set up

1. **Add a listener** to your game (Unity: copy `GameLoopRpc.cs` into `Assets/`) and register a few commands
   worth asking about:

   ```csharp
   #if UNITY_EDITOR || DEVELOPMENT_BUILD
   GameLoopRpc.Register("hp", args => $"{player.hp}/{player.maxHp}", "player hit points");
   GameLoopRpc.Register("teleport", args => { player.Teleport(args); return "ok"; }, "teleport <x> <y> <z>", write: true);
   #endif
   ```

2. **Make a development build.**

3. **Write a `game.json`** next to your project (paths are relative to it; `%VAR%`, `${VAR}` and `~` expand):

   ```json
   {
     "prefix": "mygame",
     "title": "My Game",
     "exe": "Builds/Dev/MyGame.exe",
     "args": ["-screen-fullscreen", "0", "-screen-width", "1280", "-screen-height", "720"],
     "processName": "MyGame",
     "port": 47817,
     "log": "%USERPROFILE%/AppData/LocalLow/MyCompany/MyGame/Player.log"
   }
   ```

   | key | |
   |---|---|
   | `prefix` | tool name prefix (`mygame_launch`...) |
   | `exe`, `args`, `cwd`, `env` | how to start the dev build. `env` is merged in; `GAME_RPC=1` and `GAME_RPC_PORT` are always set |
   | `processName` | optional: launch also refuses while a process of that name runs, even without a listener |
   | `port` | listener port (default 47817). Give each game its own |
   | `log` | file `<prefix>_log` tails |
   | `launchTimeoutSeconds`, `commandTimeoutMs` | defaults 90 s and 15 s |

4. **Register the server** with your MCP client. For Claude Code, in the project's `.mcp.json`:

   ```json
   { "mcpServers": { "mygame": { "command": "node", "args": ["C:/tools/game-loop-mcp/server/game-loop-mcp.js", "C:/work/mygame/game.json"] } } }
   ```

Then ask: *"launch the game, load Level2, teleport to the boss door and show me a screenshot"*.

## Try it in one minute

The repo has a tiny tkinter demo game wired to the Python listener, and an end-to-end test that drives it
through the MCP server exactly as an agent would:

```sh
node test/selftest.js            # needs Node 16+ and Python 3 with tkinter
```

`test/unity-e2e.ps1 -Unity <path to Unity.exe>` does the same for Unity. It creates a throwaway project,
adds the listener and a one-scene demo, makes a Development Build, and runs the self-test against it.

## Lessons baked in

- **Drive the game from inside.** Sending keys or clicks from outside fails in practice: background
  processes can't take focus, games read raw input, and you can't read game state through pixels. The
  listener runs inside the game, and the outside half only carries text.
- **Answer on the main thread or not at all.** A command waits for the next frame. During a load or a
  modal dialog it times out with a clear message instead of hanging the agent.
- **Arm from the environment, never from a file.** A flag file left behind changes the next normal run.
- **One instance, never killed.** Shared accounts, shared save folders, other agents' sessions: refuse
  instead of guessing, and close politely.
- **Return pictures, not paths.** The screenshot comes back as an image the agent can see. Downscaled to
  1280 px by default, which is plenty to judge a scene and cheap on tokens.

## Platforms

The server runs anywhere Node does. Window capture (the fallback when a listener has no `shot`) and the
polite close via `CloseMainWindow` are Windows features. On macOS and Linux, close sends `SIGTERM`, and
screenshots need the listener's `shot` (the Unity one has it).

## License

MIT