Skip to main content
Glama
README.md
# Hanabi Table

A Hanabi referee, an MCP server and a live web viewer, built to study how teams of LLM agents learn and evolve conventions in a cooperative game, and what that says about teamwork.

The table knows the rules, deals the deck, validates every move, shows each player only what they are allowed to know, tracks hint and strike tokens, and logs the whole game. Agents (for example 3-5 Claude Code sessions) sit at the table through MCP. You watch the games in the browser.

## Features

- **Rules engine**: standard 5-colour game (50 cards, 8 hints, 3 strikes), 2-5 players, deterministic seeded decks, configurable tokens and hand size. Pure TypeScript, no I/O, fully tested.
- **Information hiding**: every player sees the other hands and the public log, never their own cards. Each card carries what its owner has learned from hints.
- **Private reasoning**: each move requires a `reasoning` string. It is saved for observers and hidden from the other players (optionally revealed after the game).
- **MCP server** (Streamable HTTP, per-player token): lobby, turns (`wait_for_turn`), moves, history, retrospective chat.
- **Series of games**: several tables in parallel, games played back to back with the same players, optional `maxGames`, organizer "close table".
- **Retrospective chat**: communication is closed during play (hints only) and opens after each game.
- **Web app**: live board (SSE), per-player view, step-by-step replay, statistics chart, JSON export.
- **Persistence**: SQLite event log; state is rebuilt by replaying events.

## Quick start

Requires Node.js 20+.

```bash
npm install
npm run dev          # http://localhost:3000
npm test             # engine and table service tests
npm run bots -- 4 2  # smoke test: 4 random bots play 2 games through MCP
```

Data is stored in `data/hanabi.db` (override with `HANABI_DB`).

## Connect agents

```bash
claude mcp add --transport http -s user hanabi http://localhost:3000/api/mcp
```

Open one terminal per agent, each in its own directory (so each keeps its own notes):

```bash
mkdir -p ~/hanabi-agents/alice && cd ~/hanabi-agents/alice
claude --allowedTools "mcp__hanabi"
```

Then paste the prompt from [`docs/agent-prompt.md`](docs/agent-prompt.md). The agent flow is:

`get_rules` → `register_player` (keep the token) → `create_table` / `join_table` → `start_game` → loop `wait_for_turn` + `give_hint` / `play_card` / `discard_card` until `gameOver` → `post_retro` → `next_game`.

Any MCP client works; the token can be passed as a tool argument or as an `Authorization: Bearer` header.

## MCP tools

| Tool | Purpose |
|---|---|
| `get_rules`, resource `hanabi://rules` | Rules and workflow |
| `register_player` | Get a secret token |
| `list_tables`, `create_table`, `join_table` | Lobby (`create_table` takes `seed`, `maxGames`, `maxHints`, `maxStrikes`, `handSize`, `revealReasoningAfterGame`) |
| `start_game`, `next_game` | Start the first / next game with the same players |
| `wait_for_turn` | Blocks until your turn, game end, or timeout; returns your view and new events |
| `get_state`, `get_history` | Your filtered view and the event log |
| `give_hint`, `play_card`, `discard_card` | Moves; each requires `reasoning` |
| `post_retro`, `get_retro` | Retrospective chat, open only after a game ends |

## Study design

- No communication during play except hints.
- A `seed` on the table makes game N use deck `seed-N`, so strategies can be compared on identical decks.
- The simulator provides no memory between games: agents keep their own notes.
- `maxGames` ends a series; the organizer can press **Close table** in the web app to stop everything (agents get `table_closed` / `tableClosed`).
- Export a game (`/api/games/[id]/export`) to analyse moves, reasoning and retrospectives offline.

## Project layout

```
src/engine/    pure rules engine, seeded deck, per-viewer projection
src/table/     tables, series, wait_for_turn, retro chat, observer queries
src/store/     SQLite connection and schema
src/mcp/       MCP tool definitions
src/app/       Next.js pages and API routes (/api/mcp, /api/tables, /api/stats, ...)
scripts/       bots.mts, random-bot smoke test
tests/         vitest suites
```

## Security note

The web app and the MCP endpoint have no authentication beyond per-player tokens. They are designed for local use; do not expose them to the internet as is.

## License

MIT