MCP: read-only stdio server for LLM apps to query workout history

A Model Context Protocol bridge that lets an external LLM application (Claude Desktop,
Cursor, Cline, Continue, …) read a user's openGym profile directly from ./data — no new
container, no extra auth, no third-party service. The LLM client spawns the server
locally as a stdio process (per the official MCP model); reads the same state-<uid>.json
the api server writes; reuses the pure training helpers from frontend/src/lib/*.js so
the numbers it answers with match what the Stats screen shows exactly.

Eight tools in v1, all read-only:
 · list_routines, get_routine — names + exercise counts + per-exercise targets
 · get_week_plan — Mo/We/Fr assignments and today's routine, accounting for any
   date-specific overrides (a "rest" override cancels the day)
 · list_workouts, get_workout — newest-first session summaries + per-set breakdown
 · get_bodyweight — weigh-ins with the goal line and signed deltas vs goal
 · estimate_1rm — Epley/Brzycki/Lombardi formulas, all-time best + trend per exercise,
   or a PR table across all reps-mode exercises
 · muscle_balance — which muscles are trained vs neglected over a week/month/all window

State is read with fs.watch (mtime fallback for platforms where watchers are flaky) so
the server reflects a session the web UI just finished without a restart. No passkey
material, VAPID keys, or session secrets ever cross the boundary — handlers only ever
serialize fields from the state object plus the user's name from db.json.

The MCP server has exactly one runtime dependency beyond the SDK: none. zod is the SDK's
transitive; vitest and Stryker are devDeps only. Tests run against the demo seed fixture
(demoSeed.js) — 32 cases pinning the JSON shape and the most user-facing edge cases
(empty state, missing routine, rest-day override, zero-workout muscle_balance, three
1RM formulas, superset links, timed/cardio set labels).
This commit is contained in:
Pengboi committed 2026-07-26 19:34:09 +01:00
1 parent fadeb17b6c
commit 92914dea43
9 files changed
+5641

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
// Formatting glue between pure lib helpers and the JSON the MCP tools return to an LLM.
//
// The lib modules speak in compact codes (e.g. `nextPrescription.why` is [template, ...args])
// because the UI also needs to translate them. An LLM doesn't read templates, so the tools
// format each value into final English text before serialising. Keeping this in one place
// means a fix to a phrasing lands in every tool at once.
import { MUSCLE_NAME, MUSCLES } from '../../frontend/src/lib/muscles.js'
import { modeOf, fmtSec, setLabel } from '../../frontend/src/lib/history.js'
import { POLICY_NAME } from '../../frontend/src/lib/progression.js'
import { fmtDate, fmtNum, fmtDur } from '../../frontend/src/lib/format.js'
/** Apply {0},{1},… substitutions to the template strings the lib returns. */
export function fmt(template, args) {
let v = template
for (let i = 0; i < (args || []).length; i++) v = v.replaceAll('{' + i + '}', String(args[i]))
return v
}
/** Pre-formatted label for one set, e.g. "5 @ 60 kg", "1:30 · 20 kg", "20 min @ 8 km/h". */
export { setLabel }
/** A one-line description of a routine entry, e.g. "3 × 10 · 60 kg". Accepts a cfg + unit. */
export function exLine(cfg, unit) {
const mode = modeOf(cfg)
const n = cfg.sets || 1
const load = cfg.weight ? ' · ' + fmtNum(cfg.weight) + ' ' + unit : ''
if (mode === 'cardio') return `${n} × ${cfg.min || 20} min @ ${fmtNum(cfg.speed || 8)} km/h`
if (mode === 'time') return `${n} × ${fmtSec(cfg.sec || 45)}${load}`
return `${n} × ${cfg.reps}${load}`
}
/** A human name for a muscle slug, falling back to the slug if unmapped. */
export function muscleName(slug) {
return MUSCLE_NAME[slug] || slug
}
/** English name of a progression policy, falling back to the slug. */
export function policyName(policy) {
return POLICY_NAME[policy] || policy
}
/** Friendly month-day for an ISO date ("3 Jul"); used in summaries so the LLM reads natural. */
export function friendlyDate(iso) {
if (!iso) return null
return fmtDate(iso)
}
/** Duration in minutes ("46 min", "1h 14m"). */
export function friendlyDuration(ms) {
return ms ? fmtDur(ms) : null
}
/** Sums vs planned and returns a "{done}/{total}" label. */
export function ratio(done, total) {
return `${done}/${total}`
}
/** Returns the muscle slugs in head-to-toe order (the same order UI lists them). */
export function muscleOrder() { return MUSCLES.slice() }