Skip to main content
Glama
Game Table

Game Table

by danpillay87
README.md
# Game Table

Friendly browser board games for a child and an AI friend.

A child plays on a cheap laptop or Chromebook. An AI agent or a desk robot sits at the other side of the table: it sees the live board, makes its own moves and chats about the game in a speech bubble. Agents join over **MCP**, so any agent, voice model or maker robot can play.

- **Play:** https://danpillay87.github.io/game-table/
- **MCP endpoint:** `https://game-table.danpillay87.workers.dev/mcp` (Streamable HTTP, no auth)

| Game | Status |
| --- | --- |
| Battleships | Ready |
| Chess | Ready |
| Word Tiles | Coming next |

![Playing Battleships](docs/screenshots/04-playing.png)

Works from phone portrait (375 px) through tablet to desktop. On narrow screens the boards stack and size themselves to fit, with no sideways scrolling. More screenshots are in [`docs/screenshots/responsive`](docs/screenshots/responsive).

## How a game works

1. The child opens the site, picks Battleships and an animal (Otter, Panda, Fox…). Nobody types a name.
2. They tap **Play my robot friend**. The screen shows a room code such as `OTTER-37`.
3. The child reads the code out loud. The agent calls `join_room("otter 37")`. Spaces, case and dashes don't matter.
4. Both sides hide their ships, then take turns. The agent loops `wait_for_turn` → `make_move`, and uses `say` to chat.

It also works the other way round: the agent calls `create_room` and tells the child the code, and the child types it into **Join**. There's also **Practice with Pebble**, a built-in friend for playing with no agent at all.

## Connect an agent

Any MCP client that supports remote servers over Streamable HTTP will work.

**Claude Desktop / Claude Code / Cursor style config**

```json
{
  "mcpServers": {
    "game-table": {
      "type": "http",
      "url": "https://game-table.danpillay87.workers.dev/mcp"
    }
  }
}
```

For clients that only speak stdio, use a bridge such as `npx mcp-remote https://game-table.danpillay87.workers.dev/mcp`.

**OpenAI Realtime / Responses API (remote MCP tool)**

```json
{
  "type": "mcp",
  "server_label": "game_table",
  "server_url": "https://game-table.danpillay87.workers.dev/mcp",
  "require_approval": "never"
}
```

**Raw JSON-RPC** (handy for robots without an MCP library):

```bash
curl -s https://game-table.danpillay87.workers.dev/mcp \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"join_room","arguments":{"code":"OTTER-37","name":"Reachy"}}}'
```

The server is stateless: no MCP session needed. Your seat is identified by the `token` that `join_room` or `create_room` returns. Pass it to every other tool.

## Tools

| Tool | What it does |
| --- | --- |
| `list_games` | Games on the table and whether they're ready. |
| `get_rules(game)` | Short rules and the move format. |
| `create_room(game, name)` | New room; you take the first seat. Returns `code` and `token`. |
| `join_room(code, name)` | Sit down in an existing room. Returns your `token`. |
| `get_state(code, token?)` | The live board from your side (see below). Without a token you get a spectator view. |
| `wait_for_turn(code, token, timeout_seconds?)` | Waits up to 25 s until it's your turn or the game ends. Call again if `timed_out` is true. |
| `make_move(code, token, move)` | Make a move. Returns the new state, or an `error` with a reason. |
| `say(code, token, text)` | Speech bubble next to your avatar. Max 140 characters. |
| `resign(code, token)` | Give up this game. |
| `rematch(code, token)` | New game in the same room once one has finished. |

### State shape (same for every game)

```jsonc
{
  "room": "OTTER-37",
  "game": "battleships",
  "status": "waiting_for_players | playing | finished",
  "phase": "placing | playing | finished",   // game-specific
  "you": { "seat": 1, "name": "Reachy", "kind": "agent" },
  "your_turn": true,
  "turn": 1, "turn_name": "Reachy",
  "last_move": { "seat": 0, "type": "fire", "cell": "E5", "result": "hit" },
  "winner": null,
  "players": [{ "seat": 0, "name": "Otter", "kind": "human", "connected": true, "score": 0 }, "..."],
  "summary": "Otter fired at E5 and hit a ship. Your turn to fire. ...",
  "legal_moves": ["A1", "A2", "..."],
  "board": { /* game-specific, see below */ },
  "recent": ["Otter fired at E5: hit!", "..."],
  "speech": [{ "seat": 1, "text": "Nice shot!", "at": 1764750000000 }]
}
```

`summary` is plain English written from your side of the table. It's meant for a voice model to read and talk about.

### Battleships

- 10×10 sea. Rows `A`–`J` top to bottom, columns `1`–`10` left to right. `A1` is top-left.
- Fleet: Carrier 5, Battleship 4, Cruiser 3, Submarine 3, Destroyer 2. Ships can touch but can't overlap.
- Place: `make_move("random")`, or `"carrier A1 across, battleship C3 down, cruiser E5 across, submarine G1 down, destroyer J9 across"`, or `{"ships":[{"ship":"carrier","start":"A1","dir":"across"}, ...]}`.
- Fire: `make_move("B7")`. One shot per turn. The child goes first.
- `board.target` is your view of their sea, as 10 strings: `.` not fired yet, `o` miss, `X` hit, `#` sunk ship.
- `board.my_ocean` is your own sea: `S` your ship, `X` hit on your ship, `o` their miss.
- `board.enemy_sunk`, `board.enemy_remaining`, `board.my_ships`, `board.my_shots` and `board.incoming_shots` give the same information in structured form.

### Chess

- Standard rules, including castling, en passant, promotion, checkmate, stalemate, threefold repetition, the fifty-move rule and insufficient material. Rules come from [chess.js](https://github.com/jhlywa/chess.js) (BSD-2-Clause), running on the server.
- The child plays White and moves first. If two agents play, seat 0 is White.
- Move with SAN (`"e4"`, `"Nf3"`, `"O-O"`, `"exd5"`, `"e8=Q"`), from-to squares (`"e2e4"`, `"e7e8q"`) or `{"from":"e2","to":"e4"}`. Promotion defaults to a queen.
- `legal_moves` lists every legal move in SAN when it's your turn, and `board.legal` gives the same moves as `{from, to, san}`.
- `board.squares` is 8 rows from rank 8 (Black's side) down to rank 1, with cells like `"wP"` or `"bK"`, or `null` when empty. `board.fen` is there too if your agent prefers FEN.
- `board.in_check`, `board.last_move`, `board.captured_white` / `captured_black`, `board.material` and `board.history` (SAN) give the agent plenty to talk about.
- There's no built-in engine: agents bring their own play. Pebble, the practice friend, is deliberately gentle. It grabs free pieces and spots mate in one, but often plays a random sensible move so a child can win.

## Safety (it's a kids' product)

- **No accounts and no personal data.** Children pick a preset animal; they never type a name or a message. No IP addresses or analytics are stored.
- **No human-to-human chat.** The only free text that ever appears on the board comes from an agent's `say` tool. It's capped at 140 characters, and links, emails, phone numbers and a list of rude words are stripped. The browser always renders it as plain text.
- **Agent names** are capped at 20 characters and filtered the same way.
- **Rooms expire** after 2 hours of quiet, and always within 24 hours. Then everything in the room is deleted.
- **Two seats per room.** Once both seats are taken nobody else can play. Anyone who knows a code can still *look* at the board with `get_state`, and that view never shows hidden ships. Treat room codes as casual, not secret.
- `say` is limited to one bubble per second.

If you connect your own agent, give it a system prompt that keeps it kind, brief and about the game. The server's MCP `instructions` say the same.

## Run it yourself

Needs Node 22+.

```bash
npm install
npm run dev:worker      # Worker + Durable Objects on http://127.0.0.1:8799
npm run dev:site        # site on http://127.0.0.1:5178 (talks to the local worker)
npm run test:local      # full scripted game against the local worker
```

Deploy your own copy:

```bash
npx wrangler login
npx wrangler deploy     # prints your https://game-table.<you>.workers.dev URL
```

Then set `API` in `web/js/config.js` and `SITE_URL` in `wrangler.toml`. The site is plain static files (`node scripts/build-site.mjs` writes `dist/site`); the included GitHub Action publishes it to GitHub Pages. You can also add `?api=https://your-worker` to the site URL to point it at a different backend.

### Tests

```bash
npm run test:rules     # fast rules tests, no network
npm run test:e2e       # both end-to-end games against the live deployment
npm run test:local     # the same against the local worker
```

`tests/e2e-chess.mjs` works the other way round from the Battleships test. The agent creates the room, the child joins it and plays Scholar's Mate, and then the agent resigns a rematch.

`tests/e2e-battleships.mjs` plays a whole game. A scripted "human" uses the browser's REST + websocket API, and a scripted bot uses only MCP tools. The test checks the handshake, joining by spoken code, full-room refusal, text filtering, placement, every shot, agreement on the winner, speech reaching the child's screen, and rematch.

```bash
npm run test:e2e                              # against the live deployment
BASE=http://127.0.0.1:8799 npm run test:e2e   # against local
```

`tests/perf-check.mjs` is an optional frame-rate check that slows the CPU down 4x as a stand-in for a cheap Chromebook. It needs a local Chromium and `playwright-core`; see the comment at the top of the file.

## Layout

```
shared/        game rules (pure JS), used by both the Worker and the site
  games/battleships.js, games/chess.js
worker/src/    Cloudflare Worker: REST + websocket API, MCP endpoint, Room Durable Object
web/           static site (no build step, no framework); web/js/table.js is the shared table around every game
tests/         end-to-end test
```

## Adding a game

Write a rules module in `shared/games/` with `create`, `applyMove`, `view`, `needsAction`, `isOver` and `resign` (and optionally `botMove` / `botLine` for the practice friend). Then register it in `shared/games/index.js` and add a UI in `web/js/games/`. The room, MCP tools and state envelope are shared.

## Credits and licence

MIT, © 2026 Dan Pillay. All artwork is original and drawn in code. The font is [Fredoka](https://github.com/hafontia/Fredoka-One) (SIL Open Font License, see `web/fonts/OFL.txt`). "Battleships" here means the traditional pencil-and-paper game.