lanmail
by zAcherttp
README.md
# lanmail
A file mailbox for AI agents and scripts: an MCP server (stdio), a CLI and a wake server over a plain folder.
Any MCP client (Claude Code, Cursor, Codex) and any language can use it, and mail survives restarts.
- One folder per box, one file per message, readable by humans and by any language.
- Atomic delivery and exactly-once reads, even with several readers (Windows included).
- `cc` and multi-recipient sends, threads, replies, read receipts, expiry, attachments by reference, relays.
- A wake watcher that turns bursts of mail into one wake per agent, under rules that stop ping-pong.
- One shared wake server per mail root, started on demand and restarted if it dies.
- Claude Code hooks: mark a session busy while it works, hand it new mail when it stops.
Built with [Effect](https://effect.website) 4.
## Install
```sh
npx lanmail --help # or: pnpm dlx lanmail --help
npm i -g lanmail # puts `lanmail` on PATH
```
Node 20 or newer.
## Where the mail lives, and who you are
| Setting | First of | Default |
|---|---|---|
| Root (folder of boxes) | `--root`, `LANMAIL_ROOT`, `root` in `.lanmail.json` | `~/.lanmail` |
| Your box | `--box`, `LANMAIL_BOX`, `box` in `.lanmail.json` | none (needed to read, wait or send) |
A box is a stable name such as `bob` or `lane-a`, never a session id: 1–64 characters (letters, digits, `.`, `_`,
`-`) starting with a letter or digit.
**Opting a project in.** Put `.lanmail.json` in the project (it is found from any subfolder):
```json
{ "root": "../lanes/mail", "box": "lane-a" }
```
A relative `root` is resolved against the file's folder. Every project that names the same root shares that root's
one wake server. With no root named (the `~/.lanmail` default), nothing runs in the background: `watch` watches
its own box in-process.
## CLI
```sh
lanmail send bob "build #12 is up" --subject "build 12" --thread release
lanmail send lane-a,lane-b "rule change" --cc bob # one message, a copy in every box
echo "long text" | lanmail send bob - # text from stdin
lanmail send bob "slot1 is yours" --urgent --expires 10m --receipt
lanmail send bob "trace attached" --attach trace.json,shot.png
lanmail reply <id> "on it" # to reply_to (or the sender), same thread, reference chain; --all copies everyone
lanmail forward <id> phat # relay unchanged, adding a "received" line
lanmail read # new mail, clean view, filed under read/
lanmail peek # new mail, left unread
lanmail ls # one line per unread message
lanmail show <id> # one message with every header
lanmail history -n 5 # the last 5 filed messages (after a lost wake, say)
lanmail boxes # every box: unread count, oldest unread's age, expired count
lanmail wait --timeout 60 # block until mail arrives; exit 2 on timeout
lanmail tail [--keep] # every new message as it arrives, no wake rules
lanmail watch [--json] # wakes, by the wake rules (below)
lanmail serve # the shared wake server (watch starts it for you)
lanmail server status
lanmail mcp # the MCP server on stdio
lanmail hook prompt | hook stop # Claude Code hooks
```
`read`, `peek`, `history`, `wait`, `tail` and `watch` show a **clean view** by default: one summary line (id,
sender, subject, and only the flags that matter) and the text. `--full` or `show <id>` prints every header. Most
commands take `--json`.
## MCP tools
| Tool | Arguments | Does |
|---|---|---|
| `send` | `to` (box or list), `cc?`, `text`, `subject?`, `thread?`, `urgent?`, `ack?`, `receipt?`, `expires?`, `attach?`, `reply_to?` | Send from your box; returns the id and any warnings |
| `reply` | `id`, `text`, `all?`, and the optional fields above | Reply in the same thread |
| `forward` | `id`, `to` | Relay a message to other boxes |
| `read` | | New mail (clean view), filed |
| `peek` | | New mail, left unread |
| `show` | `id` | One message with every header |
| `history` | `last?` | Already-read mail |
| `list_boxes` | | Boxes with unread counts |
| `wait` | `timeout_seconds?` (default 60, max 3600) | Block until mail arrives, then return and file it |
The agent gives only what it knows; the tool fills `id`, `from`, `at` and the origin trace.
### Claude Code
```sh
claude mcp add lanmail --env LANMAIL_BOX=lane-a --env LANMAIL_ROOT=/path/to/mail -- npx -y lanmail mcp
```
Or in `.mcp.json`:
```json
{
"mcpServers": {
"lanmail": {
"command": "npx",
"args": ["-y", "lanmail", "mcp"],
"env": { "LANMAIL_BOX": "lane-a", "LANMAIL_ROOT": "/path/to/mail" }
}
}
}
```
### Cursor
`~/.cursor/mcp.json` (or `.cursor/mcp.json` in a project) takes the same `mcpServers` block.
### Codex
`~/.codex/config.toml`:
```toml
[mcp_servers.lanmail]
command = "npx"
args = ["-y", "lanmail", "mcp"]
env = { LANMAIL_BOX = "lane-a", LANMAIL_ROOT = "/path/to/mail" }
```
On Windows, if a client can't start `npx` directly, use `"command": "cmd", "args": ["/c", "npx", "-y", "lanmail", "mcp"]`.
## Claude Code hooks
```json
{
"env": { "LANMAIL_BOX": "lane-a", "LANMAIL_ROOT": "/path/to/mail" },
"hooks": {
"UserPromptSubmit": [{ "hooks": [{ "type": "command", "command": "npx -y lanmail hook prompt" }] }],
"Stop": [{ "hooks": [{ "type": "command", "command": "npx -y lanmail hook stop" }] }]
}
}
```
- `hook prompt` marks the box busy, so the watcher never interrupts a turn, and records the session id, transcript and
turn count for the origin trace.
- `hook stop` hands over new mail: it files it and prints `{"decision": "block", "reason": "<the mail>"}`, so the
session handles it without a separate turn. It blocks at most 3 times in a row (`LANMAIL_STOP_BLOCKS`); after
that it lets the session stop and leaves the mail to the watcher. With no mail it marks the box idle, then checks once
more for mail that landed meanwhile.
## Waking agents
`lanmail watch` prints one wake per burst of mail, with the mail in it, for a Monitor or any script that turns
output lines into turns. `--json` gives one object per wake on one line.
| Rule | Behaviour | Default |
|---|---|---|
| 1. Burst | Wake once no new mail has arrived for the quiet time, never later than the cap | 3 s quiet, 30 s cap |
| 2. Busy | A busy box (between `hook prompt` and `hook stop`) is never woken; a busy mark expires | 10 min |
| 3. Size | Messages in full up to a cap (filed); the rest stay unread ("N more, read your box") | 10 messages, 8,000 chars |
| 4. Urgent | `priority: urgent` skips rules 1 and 7 (not 2 or 5) | |
| 5. Ping-pong | `kind: ack` never wakes; at most N wakes per pair of boxes in a window | 4 per 60 s |
| 6. Repeats | Each message is claimed once, so nothing is delivered twice | |
| 7. Gap | At least this long between wakes for one box | 15 s |
One watcher per box: a second `watch` on the same box is refused (`tail --keep` gives a read-only view). A crashed
watcher's lock is taken over at once on the same machine, or after 90 s without a heartbeat elsewhere. `send` warns
when the recipient has shown no sign of life (no hooks, no watcher) for 10 minutes.
**The shared server.** For a named root, `watch` subscribes to that root's one wake server (`lanmail serve`),
starting it if needed. The server watches the whole root once and applies the rules centrally (the pair limit sees every
box). It listens on a named pipe (Windows) or Unix socket derived from the root, writes `<root>/.server`, and exits 5
minutes after its last subscriber leaves. If it dies, watchers restart it; if it can't start, they watch their own box
directly.
| Variable | Default |
|---|---|
| `LANMAIL_WAKE_QUIET_MS` / `LANMAIL_WAKE_BURST_CAP_MS` | 3000 / 30000 |
| `LANMAIL_WAKE_MIN_GAP_MS` | 15000 |
| `LANMAIL_WAKE_PAIR_MAX` / `LANMAIL_WAKE_PAIR_WINDOW_MS` | 4 / 60000 |
| `LANMAIL_WAKE_MAX_MESSAGES` / `LANMAIL_WAKE_MAX_CHARS` | 10 / 8000 |
| `LANMAIL_BUSY_EXPIRY_MS` | 600000 |
| `LANMAIL_HEARTBEAT_MS` / `LANMAIL_LOCK_EXPIRY_MS` | 30000 / 90000 |
| `LANMAIL_UNWATCHED_MS` | 600000 |
| `LANMAIL_STOP_BLOCKS` | 3 |
| `LANMAIL_POLL_MS` (file-event fallback) | 500 |
| `LANMAIL_SERVER_IDLE_MS` | 300000 |
| `LANMAIL_NO_SERVER=1` | never start a server |
The server takes its settings from the environment of the watcher that started it.
## LAN mail (off by default)
Mail boxes on other machines as `box@node`. Each machine runs one node (the wake server of its root); nodes talk over
plain HTTP, but every message is signed (Ed25519) and sealed (X25519 + HKDF + ChaCha20-Poly1305) for one paired node.
```sh
lanmail lan enable --alias pc # on each machine (prints the firewall rule on Windows)
lanmail lan nearby # machines heard on the LAN
lanmail lan pair mac # on pc (or by address: lan pair 192.168.1.20): prints a 6-digit code and waits
lanmail lan pending # on mac: shows pc with the same code
lanmail lan accept pc # on mac: "same on pc? [y/N]" (Match and click)
lanmail send lane@mac "build is up" # queued in <root>/.lan/outbox/mac, delivered by pc's node
lanmail lan status # peers, outbox counts
```
- Mail from an unpaired node is refused and never stored. The sender's outbox retries (2 s doubling to 5 min, jittered)
and bounces back to the sender after 3 days, or at the message's own `expires`.
- Only a person pairs: no MCP tool pairs, and `accept` needs a terminal or `--yes --i-am-human`.
- Received mail shows `from bob@pc` and `[remote]`, with a `received:` line and `lan_verified:` (the checked key).
- Only loopback, private, link-local and tailnet addresses may connect. Identity keys live outside the root
(`%APPDATA%/lanmail` or `~/.config/lanmail`, or `LANMAIL_IDENTITY_DIR`).
- Discovery: nodes announce alias, port and key fingerprint (never box names) on multicast 239.255.77.77, UDP 53320,
every 60 s. `lan nearby` lists them and `lan pair <name>` uses the address it heard; signed announcements let a paired
machine that changed IP be followed. Where multicast is blocked (guest Wi-Fi, VPN), pair by address.
- `lan doctor` checks the node, the port, interfaces, discovery, each peer, and prints the firewall commands.
## Folder format
Other tools can read and write the folder directly:
```
<root>/
.server the wake server's pid and heartbeat, while one runs
<box>/
<id>.md new mail
<id>.md.tmp being written: ignore
read/<id>.md filed mail
expired/<id>.md mail that expired before anyone read it
.state .session .watch.lock .stop-blocks small JSON files for hooks and watchers
```
A message file is `key: value` header lines, one blank line, then the text (UTF-8):
```
id: 20261128T004905123Z-01a3f9c2-bob
from: bob
to: lane-a, lane-b
cc: phat
at: 2026-11-28T00:49:05.123Z
subject: slot1 is yours
thread: slots
priority: urgent
receipt: read
expires: 2026-11-28T01:00:00.000Z
attach: 9f86d081884c7d65... 1024 /path/to/trace.json
origin-via: claude-code via lanmail-cli/0.1.0
origin-host: build-pc
origin-repo: https://github.com/you/game
The text, any length, any lines.
```
| Field | Filled by | Meaning |
|---|---|---|
| `id` | tool | File name without `.md`: UTC time, counter + random hex, sender. Sorting names sorts by time. The same id lands in every recipient's box. |
| `from`, `at` | tool | Sender box, ISO 8601 time |
| `to`, `cc` | sender | Comma-separated boxes; each gets its own copy |
| `subject` | sender | Wake/list line; the text's first line when absent |
| `thread` | sender | Groups related messages |
| `in_reply_to`, `references` | tool on reply | The message replied to; the chain, oldest first, space-separated |
| `reply_to` | sender | Where replies and receipts go instead of `from` |
| `priority` | sender | `urgent` or `normal` |
| `kind` | sender | `ack` never wakes anyone |
| `receipt` | sender | `read`: an ack goes back when the message is read |
| `expires` | sender | Read after this, the message goes to `expired/` instead of being delivered |
| `attach` | tool | `<sha256> <size> <path>`, one line per file; the file is referenced, not copied |
| `received` | tool on relay | `relayed by <box>, <time>`, one line per relay |
| `read_at` | tool | When it was filed |
| `origin-*` | tool | Where it was sent from: `via` (harness and tool version), `host`, `pid`, `cwd`, `repo`, `branch`, `commit`, and `session`, `transcript`, `turn`, `model` only where the harness exposes them (or `LANMAIL_ORIGIN_*` sets them). Never guessed. |
Unknown header keys are ignored. Files with only `from` and `at` also parse: `id` then comes from the file name and
`to` from the folder.
- **To send:** write `<box>/<id>.md.tmp`, then rename it to `<box>/<id>.md` (in every recipient's box).
- **To read:** create `<box>/read/<id>.md` exclusively (O_CREAT|O_EXCL; give up if it exists), then rename
`<box>/<id>.md` over it. Exactly one reader wins. A plain rename is not enough on Windows, where two renames of the
same file can both succeed. An empty placeholder older than 30 s whose message is still in the box was left by a
crashed reader and can be taken over.
## Library
```ts
import { Mailbox, MailConfig, Presence, WakeHub, watch } from "lanmail"
```
Everything is an Effect service (`Mailbox`, `Presence`, `Origin`, `WakeHub`, `Host`). Provide the Node
platform once, as `src/bin.ts` does: `NodeServices.layer`, `NodeCrypto.layerDigest` and the Node `Host` layer.
## Development
```sh
pnpm install
pnpm test # unit tests: in-memory files and TestClock, no real waiting (~3 s)
pnpm test:all # unit + integration: real files, child processes, the wake server (~30 s)
pnpm check # tsc with Effect diagnostics as errors + scripts/effect-gate.ts + unit tests
pnpm check:all # the same with integration tests (prepublishOnly and CI)
bash scripts/demo.sh
```
The code follows the Effect rules in [AGENTS.md](AGENTS.md); `scripts/effect-gate.ts` enforces what the type checker
can't see, and counts `// effect-ok:` escapes (there are none).
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues