food-tracker
# food-tracker
A small MCP server that lets Claude log what I eat and drink. I send Claude a photo of my plate, a menu screenshot, or a sentence like "two eggs and a bagel." Claude estimates the macros, saves the meal here, and tells me where I am for the day. A private web dashboard shows the totals and the last two weeks.
I built it because I already pay for Claude Pro. Food-tracking apps that estimate meals from photos are mostly an AI model behind a subscription, and I didn't want to pay for a second one while in college. Claude can already look at a photo and estimate a meal, so all it was missing was somewhere to keep the numbers.
By [Maximus Desir](https://github.com/maximusdesir).
## How it works
Claude does all the estimating. The server never guesses nutrition: every item arrives with calories, protein, carbs, and fat already filled in, plus where the numbers came from (`photo`, `menu`, `manual`, `template`) and how confident Claude was. The server stores, edits, and adds up. That keeps it small and testable, and the source field shows later which entries were guesses.
Claude gets these tools:
| Tool | What it does |
|---|---|
| `log_meal` | Save a meal as a list of items |
| `log_water` | Save water in fl oz |
| `log_template`, `save_template`, `list_templates` | Saved meals for plates I eat often, with optional scaling ("1.5x the usual") |
| `edit_meal`, `delete_entry` | Fix mistakes ("that was two chicken breasts") |
| `get_day`, `get_range` | A day's entries and totals, or per-day totals and averages for a check-in |
| `set_targets` | Daily targets, dated so past days keep the targets they had |
| `find_menu_items` | Look up the dining hall's published nutrition for something I ate |
Every write returns the updated day totals, so Claude can answer in one message. Writes that add rows take a `request_id`, so a retried tool call doesn't log the same meal twice.
Optional fields cover portion weight and eight micronutrients (fiber, saturated fat, sodium, potassium, calcium, iron, magnesium, vitamin D). Totals for those report how many items had data, so a missing label doesn't read as a low intake.
Days follow local time (`America/New_York` by default), so an 11:30 pm snack counts toward that day. Timestamps are stored in UTC.
### Dining hall menus
My dining hall publishes nutrition labels through Dine On Campus. `POST /menu-import` accepts the raw menu JSON the site loads in my own browser and stores the published numbers, so Claude can use real label values instead of estimating. The server never fetches the site itself. Screenshots of a nutrition panel work too: Claude copies the label and logs it as `source="menu"`.
### Dashboard
`/dash` is a read-only page: today's totals against targets, calorie and protein bars for the last 14 days, 7-day averages, today's meals, and micronutrients. It sits behind the same Google sign-in as Claude and doesn't refresh on its own, so an open tab doesn't keep the server awake.
## Security
- Claude connects with MCP OAuth (dynamic client registration and PKCE). Signing in goes through Google, and only the one email in `FOOD_ALLOWED_EMAIL` gets in.
- Sign-in codes can only be sent to claude.ai's callback or to a loopback address, and tokens are issued only for this server's `/mcp`.
- Access tokens last an hour. Refresh tokens last 90 days and rotate on every use. Reusing an old refresh token revokes the whole grant.
- Codes, tokens, and dashboard sessions are stored as SHA-256 hashes. The dashboard cookie is `__Host-`, `HttpOnly`, `Secure`, and `SameSite=Lax`.
- Open endpoints (registration, sign-in starts) are capped so strangers can't fill the database.
- Request logs are off, so one-time codes never land in logs.
- The container drops from root to an unprivileged user after taking ownership of the data volume.
- Deleted entries are hidden immediately and erased for good after 30 days.
## Running it
You need Python 3.12 and [uv](https://docs.astral.sh/uv/).
```bash
uv sync
uv run ruff check . && uv run ruff format --check .
uv run pyright
uv run python -m food_mcp.server # stdio, e.g. for Claude Code or the MCP Inspector
```
### Deploying to Fly.io
It runs on one Fly machine with a 1 GB volume for the SQLite file. The machine stops when idle and starts on the next request (about a second), which keeps the cost near the price of the disk. Fly snapshots the volume daily and keeps 14 days of snapshots. SQLite needs a single writer, so never scale past one machine.
1. Change `app` and `FOOD_PUBLIC_URL` in `fly.toml`, then `fly apps create <name>` and `fly volumes create food_data --size 1 --region iad`.
2. In Google Cloud, create an OAuth client of type "Web application" with the redirect URI `https://<app>.fly.dev/auth/google/callback`. Leave the consent screen in Testing with yourself as a test user.
3. Set the secrets:
```bash
fly secrets set GOOGLE_CLIENT_ID=... GOOGLE_CLIENT_SECRET=... \
FOOD_ALLOWED_EMAIL=you@gmail.com FOOD_IMPORT_TOKEN=$(python3 -c "import secrets; print(secrets.token_urlsafe(32))")
```
4. `fly deploy --ha=false`
5. In claude.ai, open Settings, then Connectors, and add a custom connector with `https://<app>.fly.dev/mcp`. Sign in with Google when asked.
| Variable | Purpose |
|---|---|
| `FOOD_PUBLIC_URL` | Public base URL; the OAuth issuer |
| `FOOD_ALLOWED_EMAIL` | The one Google account allowed to sign in |
| `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET` | Google OAuth client |
| `FOOD_IMPORT_TOKEN` | Bearer token for `/menu-import`, 32+ characters |
| `FOOD_DB_PATH` | SQLite file (default `data/food.db`; `/data/food.db` on Fly) |
| `FOOD_TZ` | Time zone for day boundaries (default `America/New_York`) |
| `FOOD_GOAL` | Optional goal Claude checks progress against, e.g. `lean bulk to 180 lb by 2027-06-01, about 0.25 lb/week` |
| `FOOD_DINING_HALL` | Optional name of your dining hall, so "I ate at North" triggers a menu lookup |
| `FOOD_BREAKFAST_END`, `FOOD_LUNCH_END`, `FOOD_DINNER_END` | Cutoffs for guessing the meal type (defaults 10:30, 15:30, 21:00) |
## Layout
```
src/food_mcp/
server.py MCP tools; thin wrappers over repo and summary
web.py HTTP app: /mcp, /dash, /auth/google/callback, /menu-import, /health
auth.py OAuth provider backed by SQLite, Google sign-in check
repo.py all SQL
summary.py day and range totals, remaining vs. targets
dashboard.py the dashboard page
menu_import.py Dine On Campus JSON to menu items
timeutil.py local day and UTC conversion
migrations/ numbered SQL files, applied in order
```
## Limits
Photo estimates are rough, especially for big or mixed plates. Treat the numbers as ballpark figures: steady logging and a weekly look at the trend matter more than any single entry. It's built for one person: one allowed Google account and one database. The browser bookmarklet that sends the dining hall menu isn't written yet; for now, menu data comes from screenshots.
TDQS
Scored across 11 tools
Each tool has a clearly distinct purpose, and the descriptions explicitly resolve the riskiest overlaps: log_meal vs log_template (itemized vs named saved meal), log_template vs save_template vs list_templates, and get_day vs get_range (single day vs trend window). Even edge tools like find_menu_items and set_targets sit in unambiguous territory.
Every tool follows a strict verb_noun snake_case pattern (log_meal, get_range, save_template, edit_meal, delete_entry, set_targets, find_menu_items). Verbs are used consistently for their action class (log/save/list/get/set/edit/delete/find).
11 tools is well within the ideal range, and each one earns its place: meal logging, water logging, template management, retrieval, targets, and menu lookup. No redundant or filller tools.
Meal lifecycle (log/edit/delete/get), water logging with delete, template create/list/apply, target setting, and range queries are all covered. Minor gaps remain: no way to delete or rename a saved template, no editing of a water entry (only delete), and no standalone get_targets, but these are workable around.