AyyoSalli MCP Server
by XTECLK
README.md
# AyyoSalli
අයියෝ සල්ලි 💸 SMS-powered expense tracker: bank alerts are forwarded to a Cloudflare Worker, parsed into transactions, and shown in a PWA.
## How it works
One Cloudflare Worker serves both the **API** (TypeScript + Hono, **D1** database) and the **web app** (React PWA in `web/`, served as static assets).
1. MacroDroid on your phone forwards each SMS to your private webhook URL.
2. An AI model (Workers AI by default; Claude optional) classifies it as `debit | credit | otp | declined | promotional | other`. For debits it extracts amount, currency, merchant, category, bank, card type (credit/debit/account/wallet) and card number.
3. A server-side check records an expense only for a `debit` with a positive amount, and blocks OTP-looking messages unless the model is ≥ 85% sure. This catches messages like *"OTP for LKR 5,000 at DARAZ is 482913"*.
4. Every SMS is kept in the message log with the decision and the reason. Expenses link back to their SMS.
**Web app:** Dashboard (category donut, by bank, by card type, totals), Expenses (by day, filterable, each with a link to its SMS and an editable category), Messages (every SMS with an expense / not-expense indicator), Settings (webhook URLs, MacroDroid guide, sign out). Sign-in is with Google.
## Setup
```bash
npm install
npx wrangler d1 create ayyosalli # paste database_id into wrangler.toml
npm run db:migrate:local
```
In `wrangler.toml` set:
- `GOOGLE_CLIENT_ID`: Google Cloud Console → APIs & Services → Credentials → *Create OAuth client ID* → *Web application*. Add your site URL and `http://localhost:8787` under *Authorized JavaScript origins*.
- `OPERATOR_NAME`, `CONTACT_EMAIL` (and optionally `CONTACT_WHATSAPP`, `PRO_PRICE`): shown on the legal pages and on "Request Pro".
Anyone with a Google account can sign up, on the Free plan (no AI, so no cost to you). Give someone Pro, or block them, in the `subscriptions` table. After creating the database, make yourself Pro:
```sql
INSERT INTO subscriptions (email, plan) VALUES ('you@gmail.com', 'pro')
ON CONFLICT(email) DO UPDATE SET plan = 'pro', expires_at = NULL;
```
- `AI_PROVIDER` / `WORKERS_AI_MODEL`: the Workers AI model (default), or `"claude"` plus the `ANTHROPIC_API_KEY` secret.
Run locally (Workers AI always runs on Cloudflare, even in dev, so you need `wrangler login`):
```bash
npm run build:web && npm run dev # app + API on http://localhost:8787
# or, for hot reload of the UI: `npm run dev` in one terminal and `npm run dev:web` in another (http://localhost:5173)
```
Deploy:
```bash
npm run db:migrate:remote
npm run deploy # builds the web app, then deploys the Worker
```
## Gemini (MCP)
The Worker is also a remote MCP server. In the app, go to **Settings → Gemini → Connect Gemini** and follow the steps. They create a private connector URL (`https://<your-domain>/mcp/<key>`, shown once) to add in Gemini as a custom connector with **Authentication: None**. The last step turns green when Gemini first uses it.
The key is part of the URL, so treat it like a password. It only works for the MCP tools, only sees its owner's data, and can be reset or disconnected in Settings. Any MCP client that accepts a URL works the same way.
| Tool | What it does |
|---|---|
| `get_spending_summary` | Totals by category, bank, card type and day (per currency) for a month or date range |
| `list_expenses` | Filter by period, category, bank, card type, text, or `added_since`; includes the original SMS |
| `get_expense` | One expense with its SMS and the AI's confidence |
| `list_messages` | SMS the AI read and what it decided (`recorded=false` shows ignored ones). Filtered SMS are never shown |
| `list_categories` / `add_category` | Categories |
| `add_expense` | Cash or anything the SMS missed |
| `update_expense` | Correct any field: amount, currency, date, category, merchant, bank, card type, card/account, note |
| `get_sms_filter` / `set_sms_filter` | Read the filter, switch "only with an amount" / "only tracked senders" |
| `track_sender` / `untrack_sender` | Manage tracked sender patterns (untracking keeps recorded data) |
| `preview_backfill` / `run_backfill` | Counts of earlier filtered SMS that now match; process them only after the user agrees |
There's no delete tool on purpose. Deleting is done in the app.
## API
Public:
| Method | Path | Notes |
|---|---|---|
| GET | `/auth/config` | Google client ID for the sign-in button |
| POST | `/auth/google` | `{ "credential": "<Google ID token>" }` → `{ token, expires_at, user }` |
| POST | `/hooks/sms/<secret>?sender=COMBANK` | Webhook for SMS forwarders. Body: the raw SMS text (`text/plain`), or JSON / form with `body`, `sender`, `received_at`. → 201 new, 200 duplicate, 502 AI failure (resend to retry) |
Everything under `/api/*` needs `Authorization: Bearer <session token>` and is scoped to the signed-in user:
| Method | Path | Notes |
|---|---|---|
| GET | `/api/me` | Current user |
| POST | `/api/auth/logout` | Revokes the current session |
| GET/POST/DELETE | `/api/tokens[/:id]` | Keys: `?type=device` (webhook secret) or `?type=mcp` (Claude API key). Create with `{ "name", "type" }`; the key is returned once |
| POST | `/api/sms` | Same as the webhook, with a bearer token |
| GET | `/api/sms?kind=otp&expense=yes\|no&before=<id>` | Message log with the decision and linked expense |
| GET | `/api/sms/:id` | One message |
| GET | `/api/expenses?from=&to=&category=&bank=&card_type=` | Expenses, with linked SMS sender |
| GET | `/api/expenses/summary?from=&to=` (or `?month=YYYY-MM`) | Totals plus breakdowns by category, bank, card type and day (per currency) |
| GET/PATCH/DELETE | `/api/expenses/:id` | PATCH `{ category, bank, card_type, amount, merchant, ... }` |
| POST | `/api/expenses` | Manual expense |
| GET/POST | `/api/categories` | Built-in plus your own categories |
## Tests
- `npm test`: offline API and unit tests against an in-memory D1 (Google and the AI are faked).
- `npm run eval`: scores real Workers AI models on 18 sample SMS (`evals/sms-cases.ts`). Uses your Cloudflare account. Pick models with `MODELS="@cf/a,@cf/b"`; if you have several accounts, set `CLOUDFLARE_ACCOUNT_ID`.
## Demo data
`scripts/demo-data.mjs` creates a realistic month of expenses, messages (OTPs, refunds, a failed one, filtered ones) and filter rules for one user, who must have signed in once:
```bash
node scripts/demo-data.mjs you@gmail.com > demo.sql && npx wrangler d1 execute ayyosalli --remote --file demo.sql
node scripts/demo-data.mjs you@gmail.com --cleanup > clean.sql && npx wrangler d1 execute ayyosalli --remote --file clean.sql # remove it again
```
## CI/CD
GitHub Actions (`.github/workflows/ci.yml`):
- **Every push and pull request:** `npm ci`, typecheck, tests, web build.
- **Push to `main`:** after the checks pass, applies D1 migrations, deploys the Worker and web app, then runs a quick smoke test. Deployments run one at a time.
It needs one repository secret, `CLOUDFLARE_API_TOKEN`: a Cloudflare API token from the **Edit Cloudflare Workers** template, plus **D1: Edit**, for the XTECLK account and the `xtec.lk` zone.
## Plans: Free and Pro
- **Free**: forwarded SMS still go through the sender filter, then wait in **Messages → To review** with a "tap to add" push notification. The user adds the expense with the same form used for editing (pre-filled with simple patterns, no AI).
- **Pro**: the AI reads SMS that pass the filter and records expenses automatically, and the Gemini connector works.
There's no admin panel: manage the `subscriptions` and `invoices` tables with SQL in the Cloudflare D1 console. Ready-to-use statements are at the top of `migrations/0004_plans.sql`. People without a `subscriptions` row are on Free. Users see their plan, expiry date and invoices on the **Profile** tab.
## Privacy policy and terms
The Worker serves public `/privacy` and `/terms` pages, linked from the sign-in screen and usable as the links on Google's OAuth consent screen. Set `OPERATOR_NAME` and `CONTACT_EMAIL` in `wrangler.toml` so they name who runs your server and where to send data requests. The texts are a sensible starting point, not legal advice.
## License
[MIT](LICENSE)