From e007f76d432ce5ca1ba3f12aa0e4e56928545d9d Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Tue, 4 Aug 2026 16:40:03 +0200 Subject: [PATCH] MCP: CI laeuft ueberhaupt erstmal, und der Server ueberlebt seinen Client nicht MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fuer mcp/ gab es keinen CI-Job — weder die 33 Tests noch sonst irgendwas lief je auf GitHub. Jetzt laeuft beides bei jedem PR, der mcp/ oder frontend/ anfasst. Dazu der Check, den die Testsuite prinzipiell nicht machen kann: vitest loest ueber Vite auf, also gehen dort import.meta.glob, ?raw und .jsx anstandslos durch. Genau so kam ein Vite-only-Import in ein gemeinsames Lib-Modul und liess 33 gruene Tests ueber einem Server stehen, der beim Start starb. check-node-loadable.mjs laeuft deshalb unter nacktem node — dass es ausserhalb von vitest laeuft, IST der Test. Gegengeprueft: Bug wieder eingebaut → Exit 1 und history.js/progression.js/tools.js namentlich gemeldet; sauber → Exit 0. Und der fs.watch-Handle haelt die Event-Loop offen, sobald es eine State-Datei zu beobachten gibt — der Prozess ueberlebt damit den Client, der ihn gestartet hat, obwohl die mcp/README genau das Gegenteil verspricht. Ein unref() reicht; nachgemessen: mit State-Datei vorher Exit 124 (haengt), jetzt Exit 0, und der Watcher invalidiert den Cache unveraendert weiter. --- .github/workflows/test.yml | 22 ++++++++++++ CHANGELOG.md | 3 ++ mcp/package.json | 5 +-- mcp/scripts/check-node-loadable.mjs | 55 +++++++++++++++++++++++++++++ mcp/src/state.js | 6 ++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 mcp/scripts/check-node-loadable.mjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4e3af34..e8632b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,11 +6,13 @@ on: pull_request: paths: - 'frontend/**' + - 'mcp/**' - '.github/workflows/test.yml' push: branches: [main] paths: - 'frontend/**' + - 'mcp/**' - '.github/workflows/test.yml' workflow_dispatch: @@ -41,3 +43,23 @@ jobs: working-directory: frontend - run: node scripts/check-locales.mjs working-directory: frontend + + mcp: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 22 + cache: npm + cache-dependency-path: mcp/package-lock.json + - run: npm ci + working-directory: mcp + - run: npm test + working-directory: mcp + # The one check the vitest suite structurally cannot make. The MCP server is a bare node + # process, but vitest runs its tests through Vite — import.meta.glob, .jsx imports and + # ?raw all resolve happily there. That is how a Vite-only import reached a shared lib + # module with 33 green tests standing over a server that died on startup. + - run: npm run check:node-loadable + working-directory: mcp diff --git a/CHANGELOG.md b/CHANGELOG.md index 58486ab..78539f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,9 @@ release is invisible to you. import inside a shared module is invisible under vitest, which transforms it, and fatal to the MCP server, which does not — so the shared half stays clear of the bundler half by construction rather than by remembering to. +- `npm run check:node-loadable` in `mcp/` walks the server's import graph under a bare `node`, + which is the one thing the test suite cannot do from inside Vite. CI runs it, alongside the + MCP tests — neither had ever run there before. The MCP server was contributed by [@Pengboi](https://github.com/Pengboi) — the first feature in openGym written by someone other than me. Thank you. diff --git a/mcp/package.json b/mcp/package.json index 62afb79..92e3de6 100644 --- a/mcp/package.json +++ b/mcp/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "type": "module", - "description": "MCP (Model Context Protocol) bridge for openGym — lets an external LLM read your routines, workouts, body-weight log, estimated 1RMs and muscle balance from your self-hosted data. Read-only, stdio transport; no extra container.", + "description": "MCP (Model Context Protocol) bridge for openGym \u2014 lets an external LLM read your routines, workouts, body-weight log, estimated 1RMs and muscle balance from your self-hosted data. Read-only, stdio transport; no extra container.", "license": "AGPL-3.0-or-later", "main": "src/index.js", "bin": { @@ -11,7 +11,8 @@ }, "scripts": { "start": "node src/index.js", - "test": "vitest run" + "test": "vitest run", + "check:node-loadable": "node scripts/check-node-loadable.mjs" }, "dependencies": { "@modelcontextprotocol/sdk": "^1.0.4", diff --git a/mcp/scripts/check-node-loadable.mjs b/mcp/scripts/check-node-loadable.mjs new file mode 100644 index 0000000..4400c6e --- /dev/null +++ b/mcp/scripts/check-node-loadable.mjs @@ -0,0 +1,55 @@ +#!/usr/bin/env node +/* Does the server's import graph still load under plain node? + * + * This exists because the test suite cannot answer that question. `npm test` runs vitest, and + * vitest resolves modules through Vite — `import.meta.glob`, `?raw`, a stray `.jsx` import all + * work there. The server does not run under Vite. So a Vite-only import can land in a shared + * lib module, leave all 33 tests green, and still kill the server at startup. That is exactly + * what happened once: `history.js` began importing `t` from `i18n.js` (import.meta.glob over + * the locale packs, plus React), and the server died on `import` with the suite none the wiser. + * + * Run by bare `node` on purpose — being outside vitest IS the check. Do not port this to a + * vitest test file; that would make it pass unconditionally. + * + * Importing the two entry modules is enough on its own: everything the server touches hangs off + * them, so a lib module that grows a browser-only dependency next year fails here without this + * list being updated. The named list below only exists to point at the culprit instead of at a + * stack trace ten frames deep. + */ +const LIB = new URL('../../frontend/src/lib/', import.meta.url) + +// Every frontend/src/lib module the server reaches today. Named individually so a failure says +// which one, rather than which import chain. +const MODULES = [ + 'i18n-core.js', 'format.js', 'exercises.js', 'exercises-data.js', + 'history.js', 'muscles.js', 'onerm.js', 'progression.js' +] + +let failed = 0 +for (const m of MODULES) { + try { + await import(new URL(m, LIB)) + console.log(` ok ${m}`) + } catch (e) { + failed++ + console.error(` FAIL ${m} — ${e.message}`) + } +} + +// The real assertion: the server's own entry points, which pull the whole graph transitively. +// A module added to the graph later is covered by this even if the list above never learns it. +for (const entry of ['../src/state.js', '../src/tools.js']) { + try { + await import(new URL(entry, import.meta.url)) + console.log(` ok mcp/${entry.replace('../', '')}`) + } catch (e) { + failed++ + console.error(` FAIL mcp/${entry.replace('../', '')} — ${e.message}`) + } +} + +if (failed) { + console.error(`\n${failed} module(s) do not load under plain node — the MCP server would not start.`) + process.exit(1) +} +console.log('\nthe whole MCP import graph loads under plain node.') diff --git a/mcp/src/state.js b/mcp/src/state.js index 5f67c47..72c2b03 100644 --- a/mcp/src/state.js +++ b/mcp/src/state.js @@ -75,6 +75,12 @@ export function init() { _state = undefined _loadedMtime = 0 }) + // Unref'd, or the watcher alone holds the event loop open and the process outlives the + // client that spawned it — "exits when the LLM client disconnects" stops being true the + // moment there is a state file to watch. Most clients kill the child anyway, but one that + // merely closes the pipe would leak a process per session. Watching is unaffected: the + // stdio transport is what keeps the loop alive while a client is actually attached. + _watcher.unref() } catch { /* fs.watch unsupported on this platform; tools will re-read on mtime change */ } }