8000 GitHub - DarioPanada/mfp-tool · GitHub
[go: up one dir, main page]

Skip to content
DarioPanadaPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

23 Commits

Folders and files

Repository files navigation

mfp-tool

MyFitnessPal from the command line. Built so a Claude Code session can read and log the food diary on a machine where a browser login is not possible.

MFP's login needs a browser (reCAPTCHA), so the tool runs on a session cookie copied from one. No dependencies beyond the Python standard library.

Install

uv tool install git+https://github.com/DarioPanada/mfp-tool

Claude Code skill

The package ships a skill that tells Claude Code when and how to use mfp (and never to touch the cookie). Install it for every session on the machine:

mkdir -p ~/.claude/skills/mfp && mfp skill > ~/.claude/skills/mfp/SKILL.md

Rerun it after upgrading the tool.

The session cookie

The tool reads the cookie __Secure-next-auth.session-token from ~/.config/mfp/session-token (or --token-file, or $MFP_TOKEN_FILE).

MFP refreshes the cookie on most responses, and its refresh token is single-use: once MFP has issued a new cookie the old one stops working. So the tool writes every cookie MFP sends back to that file, and the machine running the tool needs a session of its own. Copying the cookie of the browser you keep using fails as soon as the browser refreshes it.

To give a machine its own session:

  1. In the browser, on myfitnesspal.com, open DevTools → Application → Cookies and copy the value of __Secure-next-auth.session-token.
  2. Save it on the machine without echoing it:
    mkdir -p ~/.config/mfp && chmod 700 ~/.config/mfp
    read -rs T && printf %s "$T" > ~/.config/mfp/session-token && chmod 600 ~/.config/mfp/session-token && unset T
  3. Back in DevTools, Application → Storage → Clear site data (not "Log out", which may end the session you just copied), then log in again in the browser. The browser now has a new session and the machine keeps the old one.

The session lasts 30 days from its last use. When it has expired the tool exits with status 2.

One run at a time

Two runs at once could spend the same token and lose the session, so every command that talks to MFP holds an exclusive lock on session-token.lock, beside the cookie file, for its whole run, and reads the cookie only once it holds the lock. A run waits up to 200 s for the lock, above the 180 s of the heaviest command at 30 s per request, and then exits with status 3 having sent nothing. It never runs without the lock. The kernel frees the lock when a process ends, however it ends, so there is nothing to clean up: leave the lock file in place.

The lock can't protect a run that is killed. Once MFP has received a request it may have spent the old cookie, and if the process dies before the new one arrives, that cookie is gone and the session needs a fresh one from a browser. The cookie is saved as soon as a response's headers arrive, so only that gap is exposed, not the reading of the body.

Use

mfp session              # check the login, refresh the cookie
mfp diary [YYYY-MM-DD]   # food entries for a day (default today), as JSON
mfp search <text>        # foods: id, version, description, brand, kcal, serving sizes
mfp log <food id> <food version> --meal Lunch --servings 1 --value 100 --unit g --multiplier 1
mfp delete <entry id>    # remove a diary entry (food, quick add or exercise)
mfp quick-add --kcal 450 --meal Lunch [--carbs G --fat G --protein G]   # calories without a food
mfp exercises [YYYY-MM-DD]              # exercise entries for a day
mfp exercise-search <text>              # exercises: id, description
mfp exercise-log <exercise id> --minutes 45 --kcal 400 [--time HH:MM]   # calories burned
mfp meals                               # saved meals with their foods' nutrition
mfp foods [text]                        # foods you created
mfp food-create "Chili (homemade)" --value 1 --unit serving --kcal 520 [--carbs G ...]  # e.g. a recipe
mfp food-delete <food id>
mfp weights [--from YYYY-MM-DD] [--to YYYY-MM-DD] [--limit N]   # weigh-ins, newest first
mfp weight-log 82.4 --unit kg [--date YYYY-MM-DD]              # save a weigh-in (kg or lb)
mfp weight-delete <measurement id>
mfp skill                # the Claude Code skill (SKILL.md), see above
mfp manifest             # this tool described for a host such as the cockpit, as JSON

--meal is a meal name looked up in today's diary (and the target day's), or a meal position (0 is Breakfast). The serving size flags come from one of the food's serving_sizes.

Exit status: 0 ok, 1 error, 2 the session has expired, 3 another run held the lock for too long.

mfp manifest needs no login. It prints manifest (format version, 1), name, version, description, commands (each with name, usage, summary, writes, true for the commands that change the diary, and label, what a conversation shows for a step that ran it when there's no AI summary), app and skill (the text of mfp skill). The cockpit reads it to list the tool and to set up the conversations of a project that uses it. writes is about the diary only: every command that logs in can rotate the cookie.

app is {"label": "Food", "private": true}. It makes mfp one of the tools of a host's daily chat, under that label. private says its data is personal, for a host to keep out of anything it shares or summarises. There is no summary command: the host's daily page is a chat, in which Claude runs the commands above.

Development

uv sync
uv run ruff check && uv run ruff format --check && uv run mypy && uv run pytest

Tests never call MFP; they use a fake opener. Never commit a cookie or diary data.

API notes

The client uses the website's own JSON routes (https://www.myfitnesspal.com/api/...), which proxy MFP API v2. Authentication is the session cookie plus, for /api/services/*, an X-CSRF-Token header and the CSRF cookie from GET /api/auth/csrf. Weigh-ins are under /api/user-measurements/measurements (GET with types=Weight, PUT {"items": [...]}, DELETE /{id}).

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0