Skip to content

fix(#1084): plugin MCP server no longer lets a local sequant shadow the pin - #1085

Merged
admarble merged 11 commits into
mainfrom
feature/1084-fix-mcp-plugin-mcp-server-dies-with-connection-clo
Sep 19, 2026
Merged

admarble merged 11 commits into
mainfrom
feature/1084-fix-mcp-plugin-mcp-server-dies-with-connection-clo

Conversation

@admarble

@admarble admarble commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • The plugin's .mcp.json now launches sequant serve through a dependency-free launcher (scripts/mcp-launch.mjs) that spawns npx from an isolated mkdtemp cwd, so npx can never resolve a shadowing node_modules/sequant in the open project (the root cause of CONNECTION_CLOSED).
  • The launcher passes the real project path through SEQUANT_PROJECT_DIR; sequant serve now chdirs there before doing anything else, and rejects a non-directory value with a named stderr message + non-zero exit.
  • sequant doctor gains a Local sequant shadow warning: it walks cwd's ancestor chain and $HOME for a node_modules/sequant at a version different from the running CLI, and names the fix.
  • docs/troubleshooting.md and docs/features/mcp-server.md document the new failure mode, SEQUANT_PROJECT_DIR, and how the launcher avoids it.
  • The shipped .mcp.json launches the server as node -e <inline launcher> sequant@<pin> with no ${...} anywhere in command/args/env. ${CLAUDE_PLUGIN_ROOT:-.} was proven (QA run 2 probe) to resolve to the open project, and Claude Code's generic env expander runs over every arg, so the launcher source uses string concatenation, never template literals. scripts/generate-mcp-launch-inline.mjs derives the inline form from scripts/mcp-launch.mjs, and a gate test keeps the two byte-identical. The same file therefore works unchanged as the plugin config and as the dev repo's own project config.
  • QA follow-ups closed in 68db25fd: the launcher sweeps empty sequant-mcp-launch-* dirs older than an hour on each start (a SIGKILL of the launcher skips its own cleanup); sequant://install is read through a real StdioClientTransport with cwd ≠ project as the AC-3 regression test; the pin test asserts args[2] and exactly three args; npm run mcp-launch:inline regenerates the inline launcher in place and runs first in prepare:marketplace; an Execution Verification record is on the issue.
  • Run 6 QA follow-ups closed in cd0347fd: on Windows the launch goes through cmd.exe (Node refuses to spawn npx.cmd without a shell — CVE-2024-27980 hardening, inside the engine floor), gated by isSafeSpec() which rejects any spec a shell could interpret before spawn; each launch dir carries launcher.pid, and the sweep keeps any dir whose launcher is alive (however old), removes dead ones outright, and age-gates only pid-less dirs — so a session open for hours keeps its launch dir even while a pre-2.16 serve is still using it as cwd; the integration test's cleanup assertion is unconditional; the generator uses replaceAll for the argv index rewrite. Branch carries origin/main through 52cd790e (fix(#1075): timeout the codex+opencode CLI probes; add the missing templates/codex routing test #1091, fix(#1071): preserve .sequant/settings.json on re-init #1092) with no conflicts.

Pre-PR AC Verification

AC Description Status Evidence
AC-1 scripts/mcp-launch.mjs launcher ✅ Implemented scripts/mcp-launch.mjs; mutation-verified by scripts/mcp-launch.integration.test.ts
AC-2 Shipped .mcp.json is an inline node -e launcher, no placeholders, pin in args[2], byte-identical to the generated form ✅ Implemented .mcp.json; scripts/generate-mcp-launch-inline.mjs; scripts/plugin-mcp-pin.test.ts (see AC-2 Mutation-verified marker)
AC-3 serve chdirs to SEQUANT_PROJECT_DIR, rejects non-directory; sequant://install resolves against it ✅ Implemented src/commands/serve.ts (applyProjectDir); src/commands/serve.integration.test.ts — stderr warning test, non-directory test, and a stdio readResource("sequant://install") test asserting the project's 0.0.1 manifest
AC-4 Integration test reproduces the shadow, mutation-verified ✅ Implemented scripts/mcp-launch.integration.test.ts (see Mutation-verified marker below)
AC-5 doctor warns on a shadowing local sequant ✅ Implemented src/commands/doctor.ts (checkShadowingLocalSequant); src/commands/doctor.test.ts
AC-6 Docs updated ✅ Implemented docs/troubleshooting.md, docs/features/mcp-server.md
AC-7 Server starts through .mcp.json as resolved by Claude Code, in the dev-repo context and in a project with an older local sequant ✅ Verified through the host Test plan: two claude -p --mcp-config probes + one initialize probe, recorded verbatim
AC-8 (derived) Launcher logs its resolved npx-launch cwd to stderr ✅ Implemented scripts/mcp-launch.mjs (stderr line on every start)

Mutation-verified: AC-4 — commented out the launcher's isolated-cwd spawn (cwd: launchCwd → cwd: projectDir); scripts/mcp-launch.integration.test.ts failed (recorded npx cwd equalled the shadowing project dir); restored.

Mutation-verified: AC-2 — (a) appended one space to .mcp.json args[1]; scripts/plugin-mcp-pin.test.ts > the inline launcher source (args[1]) is byte-identical to the generated form of scripts/mcp-launch.mjs failed (1 failed / 6 passed); restored. (b) set args[0] to ${CLAUDE_PLUGIN_ROOT}/-e; both the invokes node -e and the carries no ${...} tests failed (2 failed / 5 passed); restored. Baseline 7/7 green.

Mutation-verified: AC-1 (sweep) — removed the age check in sweepStaleLaunchDirs (now - st.mtimeMs < maxAgeMs); scripts/mcp-launch.test.ts > leaves a fresh sibling launcher's directory alone failed (1 failed / 6 passed); restored, 7/7 green.

Mutation-verified: AC-1 (pid ownership) — removed if (alive(pid)) continue; in sweepStaleLaunchDirs; scripts/mcp-launch.test.ts > never removes a directory whose launcher pid is still alive, however old and > consults the injected liveness check with the recorded pid failed (2 failed / 12 passed); restored.

Mutation-verified: AC-1 (Windows spawn) — made spawnShellFor return false on win32; scripts/mcp-launch.test.ts > uses a shell only on win32 failed (1 failed / 13 passed); restored. Loosened isSafeSpec to spec.length > 0; > rejects anything a shell could interpret failed (1 failed / 13 passed); restored. Baseline 14/14 green.

Also mutation-verified along the way (not separately AC-gated, but load-bearing for AC-3/AC-5):

  • serve.ts's applyProjectDir() call — disabling it left both new serve.integration.test.ts AC-3 tests timing out/failing.
  • doctor.ts's checkShadowingLocalSequant push — disabling it dropped both "warns" tests to [].

Test plan

  • npm run build — exit 0 (worktree, head cd0347fd)
  • npm run lint — exit 0, --max-warnings 0 (head cd0347fd)
  • npm test (full suite, worktree, head cd0347fd, includes origin/main through 52cd790e) — Test Files 321 passed (321); Tests 6508 passed | 1 expected fail | 3 skipped (6512), 0 failures.
  • Smoke on cd0347fd from ~/Projects/sequant-landing (shadowed): serverInfo.version 2.15.1, launch dir removed on exit; node -e <launcher> '[email protected]; echo pwned' → mcp-launch: refusing package spec with shell-significant characters, exit 1.
  • Execution Verification record posted on fix(mcp): plugin MCP server dies with CONNECTION_CLOSED when the open project has an older sequant in node_modules — npx ignores the pin and runs the local copy #1084 (launcher from the shadowed project → serverInfo.version 2.15.1; claude -p --mcp-config connected in both contexts; mcp-launch:inline idempotent).
  • AC-7, dev-repo context, resolved by Claude Code's own MCP loader (cwd = this branch's worktree): claude -p "reply with the single word ok" --mcp-config .mcp.json --strict-mcp-config --output-format stream-json --verbose --max-turns 1 → init message "mcp_servers":[{"name":"sequant","status":"connected","source":"dynamic"}].
  • AC-7, shadowed project, same command with cwd = ~/Projects/sequant-landing (has [email protected] in node_modules, the original CONNECTION_CLOSED reproducer) → "mcp_servers":[{"name":"sequant","status":"connected","source":"dynamic"}].
  • AC-7, exact shipped args from the shadowed project: node -e "$(jq -r '.mcpServers.sequant.args[1]' .mcp.json)" [email protected] fed an MCP initialize → stderr mcp-launch: project dir /Users/tony/Projects/sequant-landing; launching npx from /var/folders/.../sequant-mcp-launch-WOvIvN then Sequant MCP server started (stdio); reply serverInfo: {name: "sequant", version: "2.15.1"}. The 1.20.1 local copy never ran.
  • claude mcp list from the worktree lists the project server as node -e … [email protected] — Pending approval; project-scope .mcp.json servers need a one-time interactive approval per path, which is why the host-resolution probes above use --mcp-config (same loader, same expansion, no approval gate).

Known trade-off (flagged, not fixed here)

Previously, the dev repo's own .mcp.json happened to run the local checkout's dist/bin/cli.js (because npx resolved the repo's own package.json bin when cwd == repo root — the same shadowing behavior this issue fixes for plugin users). With the isolated-cwd launcher, the dev repo's own MCP server now always fetches the pinned published sequant@<pin> via npx, same as any other project. This is the intended effect of removing the shadow, but it means local, uncommitted CLI changes are no longer reflected when testing sequant serve through .mcp.json in this repo — contributors iterating on MCP-server code should invoke dist/bin/cli.js serve directly instead.

Also out of scope (per the issue's own scope note): templates/mcp.json (the separate official-marketplace dist copy, not yet catalogued) and sequant init --mcp's project-level config (src/lib/mcp-config.ts) still invoke npx directly and have the same shadowing exposure — AC-5's doctor check covers users of either path, but neither routes through the new launcher.

Review history

  • Run 2 QA proved ${CLAUDE_PLUGIN_ROOT:-.} resolves against the open project (three-row probe on a throwaway plugin). Owner decision: inline launcher.
  • Run 3 (exec → qa) was a no-op exec on an unchanged tree; its AC_MET_BUT_NOT_A_PLUS verdict is void (noted on the issue).
  • Run 4 exec implemented the inline launcher; QA flagged the unpushed commit, the CHANGELOG conflict, stale PR text, and missing host-resolved AC-7 evidence. All four addressed in the commits after 56c33a0b, plus removal of the three template literals the inline source carried.
  • Run 5 QA: AC_MET_BUT_NOT_A_PLUS on aace6fc3. Its non-blocking notes (temp-dir leak on SIGKILL, AC-3 resource test, pin index, generator wiring, /verify record) are closed in 68db25fd; sibling shadowing sites stay in fix(mcp): sequant init --mcp and the official-plugin-directory dist .mcp.json still invoke npx directly, retaining the #1084 shadowing exposure #1089.
  • Run 6 QA: AC_MET_BUT_NOT_A_PLUS on 68db25fd; flagged the Windows .cmd spawn (EINVAL under CVE-2024-27980 hardening) and the sweep's blind spot for a live session older than an hour. Both fixed in cd0347fd, mutation-verified above.

Closes #1084

🤖 Generated with Claude Code

Entire-Checkpoint: 328010829d2c
- Fix a real infinite-recursion/swallowed-signal bug in the launcher's
  signal re-raise: process.kill(self, signal) with our own SIGINT/SIGTERM
  listener still attached routed back into that listener instead of
  terminating, silently reporting exit 0 for a process actually killed by
  a signal. removeAllListeners() before re-raising fixes it.
- Resolve npx.cmd on win32 (spawn("npx") raises ENOENT there with
  shell: false — npm ships npx as npx.cmd on Windows).
- doctor's shadow check now also compares against the project's own
  .mcp.json pin, not just the running CLI version, so it catches shadows
  that match the running CLI (including the self-shadowing case where
  doctor itself runs the shadow) but still defeat the pin.
- Add regression tests for exit-code/signal forwarding and the npx.cmd
  resolution, and a CHANGELOG entry.
- Scope .mjs files into real lint coverage instead of a blanket ignore.

Entire-Checkpoint: 4645960a34b9
Entire-Checkpoint: c1ec6e3e835f
…de Code's env expander cannot touch it

The generic ${VAR} expander runs over every arg in .mcp.json. Three stderr messages in mcp-launch.mjs used template literals, which the inline copy carried verbatim. Rewritten as concatenation; inline regenerated; the shipped config now contains no ${ at all.
…install-resource test over stdio, pinned arg index, generator --write wired into prepare:marketplace

- mcp-launch.mjs: sweepStaleLaunchDirs() removes empty sequant-mcp-launch-* dirs older than an hour on each launch (SIGKILL of the launcher skips cleanup()).
- generate-mcp-launch-inline.mjs: strips every 'export function', refuses other exports, and --write rewrites .mcp.json args[1] in place; npm run mcp-launch:inline, first step of prepare:marketplace.
- serve.integration.test.ts: reads sequant://install through a real StdioClientTransport with cwd != project and asserts the SEQUANT_PROJECT_DIR project's manifest.
- plugin-mcp-pin.test.ts: pin must sit at args[2] with exactly three args.
- docs: sweep + regeneration command.
…; pid-file ownership so the sweep never removes a live session's launch dir

- Node refuses to spawn npx.cmd without a shell (CVE-2024-27980 hardening, inside the engine floor), so shell: true on win32; isSafeSpec() rejects any spec a shell could interpret before spawn.
- Each launch dir carries launcher.pid; sweepStaleLaunchDirs() keeps dirs whose launcher is alive (however old), removes dead ones outright, and age-gates only pid-less dirs.
- Integration test: launch-dir cleanup assertion is now unconditional.
- Generator: replaceAll for the argv index rewrite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant