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:
1 parent
fadeb17b6c
commit
92914dea43
9 files changed
+5641
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env node
|
||||
// openGym MCP server — stdio transport, read-only tools over a single user's state file.
|
||||
//
|
||||
// Spawning model (per the official MCP design): the LLM client (Claude Desktop, Cursor,
|
||||
// Cline, …) starts this process locally, talks JSON-RPC over stdin/stdout, and tears it down
|
||||
// when the session ends. That matches openGym's "your data stays in a folder you control"
|
||||
// ethos — no extra container, no new outbound network, no third-party service.
|
||||
//
|
||||
// Env:
|
||||
// OPENGYM_DATA directory containing db.json + state-<uid>.json (default: ./data)
|
||||
// OPENGYM_UID which profile to answer for (default: auto-detect the single-user case)
|
||||
// OPENGYM_LANG reserved for future label-localisation (default: en)
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
||||
import { TOOLS } from './tools.js'
|
||||
import { init, getUser } from './state.js'
|
||||
|
||||
const server = new McpServer({
|
||||
name: 'opengym',
|
||||
version: '0.1.0'
|
||||
})
|
||||
|
||||
// Initialise the state view up-front so a bad config (missing data dir, ambiguous uid) fails
|
||||
// fast with a clear message on stderr rather than silently answering every tool call with the
|
||||
// "no synced state" fallback. init() is idempotent — the LLM client restart is cheap.
|
||||
try {
|
||||
init()
|
||||
// Always register every tool, regardless of whether state resolved — an LLM client that
|
||||
// learns the tool list at handshake should see all eight. The handlers themselves degrade
|
||||
// gracefully if state is null (returns the no-state sentinel object).
|
||||
const u = getUser()
|
||||
console.error(`[opengym-mcp] serving profile ${u.name} (${u.id})`)
|
||||
} catch (e) {
|
||||
console.error(`[opengym-mcp] ${e.message}`)
|
||||
// Don't exit — keep the server up so the LLM client still gets tool listings and a useful
|
||||
// error if it tries to call one. Most issues are env-shaped (wrong OPENGYM_DATA path) and
|
||||
// a user will restart once they fix it; failing at handshake is more annoying than a clear
|
||||
// runtime error.
|
||||
}
|
||||
|
||||
for (const t of TOOLS) {
|
||||
server.tool(
|
||||
t.name,
|
||||
t.description,
|
||||
t.schema,
|
||||
async (params) => {
|
||||
try {
|
||||
const result = t.handler(params || {})
|
||||
return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }
|
||||
} catch (err) {
|
||||
const code = err.code || 'ERROR'
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: 'text', text: `${code}: ${err.message}` }]
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const transport = new StdioServerTransport()
|
||||
await server.connect(transport)
|
||||
// Process stays alive serving JSON-RPC over stdio until the LLM client disconnects.
|
||||
Reference in new issue
Block a user