# AGENTS.md — Calends Guide for AI Agents

This file helps autonomous coding and assistant agents (Claude Code, Codex, Cursor, Gemini CLI, etc.) deploy, configure, and interact with **Calends** (`https://github.com/imetn/calends`, v1.84.0) safely and accurately.

## Machine-Readable Discovery Index

- **Read-only Docs MCP server (zero auth)**: `https://docs.calends.date/mcp` (`claude mcp add --transport http calends-docs https://docs.calends.date/mcp`)
- **Docs index (with direct per-page `.md` links)**: `https://docs.calends.date/llms.txt`
- **Per-page docs Markdown (all 14 languages)**: replace trailing `/` with `.md` on any `docs.calends.date` URL (e.g. `https://docs.calends.date/en/ai/tools.md`, `https://docs.calends.date/ai/tools.md`), or send `Accept: text/markdown`.
- **Full English docs bundle**: `https://docs.calends.date/llms-full.txt`
- **Full Simplified Chinese docs bundle**: `https://docs.calends.date/llms-zh.txt`
- **Docs AI Agent Guide**: `https://docs.calends.date/AGENTS.md`
- **Product summary & full LLM reference**: `https://calends.date/llms.txt` | `https://calends.date/llms-full.txt`
- **FAQ & Calends vs Wallos comparison**: `https://calends.date/en/faq.md` | `https://calends.date/en/compare.md`
- **Complete changelog (Markdown)**: `https://calends.date/en/releases.md` | `https://calends.date/releases.md`

---

## 1. Deploying Calends for a User

Always ask the user which runtime they prefer before deploying:

### Option A: Cloudflare Workers + D1
```bash
git clone https://github.com/imetn/calends && cd calends
npm ci
npx wrangler login
node scripts/wrangler.mjs secret put SETUP_TOKEN
npm run deploy
```
- Generate a long random `SETUP_TOKEN` (never hardcode or commit it).
- If the user wants a custom domain, add `"routes": [{ "pattern": "<domain>", "custom_domain": true }]` and `"vars": { "SITE_URL": "https://<domain>" }` to `local/wrangler.jsonc` (or `wrangler.jsonc`) before `npm run deploy`.

### Option B: Docker (Recommended for VPS / NAS / Local)
Prefer the prebuilt multi-arch image (`xizozhe/calends:1.84.0` or `ghcr.io/imetn/calends:1.84.0`, `linux/amd64` + `linux/arm64`) rather than building from source unless `BASE_PATH` is required:
```bash
docker run -d --name calends --restart unless-stopped \
  -p 127.0.0.1:8787:8787 -v calends-data:/data \
  xizozhe/calends:1.84.0
docker logs calends
```
- Bind to `127.0.0.1:8787` on remote servers; place behind Caddy, Nginx (`proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;`), or Traefik with HTTPS, and set `SITE_URL=https://<domain>`.
- Leave `TRUST_PROXY` unset unless running without a proxy (`TRUST_PROXY=off`) or behind a non-Cloudflare CDN (comma-separated IPs/CIDRs).
- Passkeys require HTTPS. Never transmit `SETUP_TOKEN` over plain HTTP on the public internet; suggest `ssh -L 8787:localhost:8787 user@host` for initial setup if HTTPS is not yet configured.

### Option C: Linux systemd (Debian / Ubuntu One-Liner)
```bash
curl -fsSL https://calends.date/install.sh | sudo bash
```
- Bundles Node, listens on `127.0.0.1:8787`, stores data in `/var/lib/calends`, and supports re-running the same command to upgrade with automatic backup and rollback.

### Post-Deploy Handback Checklist
1. Verify `GET /` redirects to `/login` (and `/healthz` returns 200 on Docker/systemd).
2. Share the URL and `SETUP_TOKEN` with the user so they can set their password at `/login`.
3. Remind the user to bind a Passkey under **Settings → Security**, configure currency/timezone under **Settings → General**, and (optionally) disable anonymous telemetry in Settings or via `CALENDS_TELEMETRY=0` / `DO_NOT_TRACK=1`.

---

## 2. Operating a User's Ledger via MCP

Endpoint: `POST https://<user-host>/mcp` (Streamable HTTP, `Authorization: Bearer <token>`). Tokens are created at `/connect`. Full 41-tool specification: `https://docs.calends.date/en/ai/tools.md`.

### Mandatory Safety Rules
1. **Always preview writes first**: Every one of the 23 write tools supports `dry_run: true`. Call with `dry_run: true`, show the user the exact computed summary, and only re-call without `dry_run` after explicit confirmation (especially for `delete_subscription`, `save_subscription`, and `record_receivable`).
2. **Start with context**: Call `get_overview`, `list_categories`, and `list_payment_methods` before creating or updating subscriptions so you reuse existing category and wallet names/IDs instead of failing on unknown categories.
3. **Use `search_services` / omit `icon`**: Creating a subscription without `icon` automatically fills icon, website, and category when the name uniquely matches Calends' built-in service catalog. Pass `icon: ""` only to suppress auto-fill.
4. **Respect specialized write tools**:
   - Price change within same plan → `change_price` (not `update_subscription`).
   - Region / currency / cycle / payment-method switch or prorated upgrade → `change_plan`.
   - Pause / cancel / terminate / reactivate → `set_status`.
   - Separate bills & payments → `update_subscription` (`set: { settlement: "separate", settledBefore }`), then `record_payment` with `periods`.
   - Linked perks / add-ons → `bundle: { parent, follow? }` on `create_subscription`, `update_subscription`, `change_plan`, or `save_subscription`.
   - Shared plans & proxy repayments → `set_share`, `save_share_member`, `set_proxy`, `record_receivable`.
   - Private invoices, contracts & receipts → `list_files`, `get_file`, `upload_file`, `update_file`.
5. **Never bypass security boundaries**: Never set `DEV_BYPASS_AUTH=1` in production, never print or commit user `SETUP_TOKEN`, MCP Bearer tokens, AI provider API keys, or backup passphrases.
