Files
ruflo/plugins/ruflo-core/scripts/mcp-launch.cjs
T
rUvandr 657a4ff81c feat(funnel): count plugin-only users via the ruflo-core MCP launch (#4872)
* feat(funnel): count plugin-only users via the ruflo-core MCP launch

Plugin-only users never run init, so no SessionStart hook refreshes the
funnel cache and they are invisible in the usage numbers. The ruflo-core
launcher now sets RUFLO_LAUNCH_SURFACE=plugin; mcp start (stdio) makes the
existing 6h-cached /v1/messages fetch off the start path, gated on
resolveFunnelEnabled(), and the User-Agent gains ' surface/plugin' from a
closed allowlist. Nothing is written to stdout. LIA updated.

Co-Authored-By: RuFlo <ruv@ruv.net>

* fix(ruflo-core): keep RUFLO_MCP_SPAWN inside the env-precedence audit window

The four-line insertion pushed the existing RUFLO_MCP_SPAWN read past the
audit's 20-line function-signature heuristic. Collapse it to one line and
move the explanation to the header comment.

Co-Authored-By: RuFlo <ruv@ruv.net>

---------

Co-authored-by: r <r@x>
2026-11-06 15:00:01 -06:00

294 lines
12 KiB
JavaScript

#!/usr/bin/env node
'use strict';
/**
* .mcp.json's command/args are static strings, so they can't run
* resolution logic inline. This launcher exists to do that work before
* exec'ing the real server: it resolves a local @claude-flow/cli install
* and runs its MCP server directly (`<bin>/cli.js mcp start`), falling
* back to `npx -y @claude-flow/cli@latest mcp start` only when no local
* install resolves.
*
* Without this, plugins/ruflo-core/.mcp.json's `npx -y
* @claude-flow/cli@latest` always re-resolved to whatever was newest on
* the registry, independent of — and frequently diverging from — any
* version a user separately pinned via `npm install @claude-flow/cli`
* (ADR-382 Gap 3 / issue #2971).
*
* The dist/src/index.js existence guard mirrors
* .claude/helpers/hook-handler.cjs's resolveCliBinForHook(): a
* marketplace-plugin checkout is installed by `git clone`/`git pull`
* with no build step, so bin/cli.js can be present on disk while
* importing dist/src/index.js throws ERR_MODULE_NOT_FOUND on every real
* command. Checking for dist/ alongside bin/ prevents that doomed
* candidate from winning ahead of a real local install or the npx
* fallback. RUFLO_MCP_CLI_OVERRIDE can pin a built bin/cli.js when
* an install lives outside the standard project or global npm layouts;
* RUFLO_MCP_SKIP_NPX=1 rejects an unpinned fallback.
*
* A resolved local CLI is run in THIS process (argv rewritten to the exact
* `<node> <bin>/cli.js mcp start` the child used to receive) rather than in
* a spawned child. The child-process hop cost every Claude Code session a
* second, otherwise idle node.exe (~21 MB private / ~60 MB working set on
* Windows) plus a second node startup. Stdio, env, cwd and signals are the
* same process's, so the server sees an identical environment.
* RUFLO_MCP_SPAWN=1 restores the old child-process behaviour.
*
* RUFLO_LAUNCH_SURFACE=plugin is set (unless already set) so the CLI's funnel
* fetch can count plugin-only users; the CLI only accepts values from a closed
* allowlist (funnel/message-transport.ts). Inherited by every launch path.
*/
const { existsSync, statSync } = require('fs');
const { join, dirname, resolve, isAbsolute, delimiter, parse } = require('path');
const { spawn } = require('child_process');
const os = require('os');
const { pathToFileURL } = require('url');
const MCP_ARGS = ['mcp', 'start'];
function comparablePath(p) {
// resolve() normalises separators and drops trailing slashes (except on a root).
const r = resolve(p);
return process.platform === 'win32' ? r.toLowerCase() : r;
}
/**
* A directory that cannot be a project root: a filesystem root ('/', 'C:\')
* or the user's home directory. Home is compared against os.homedir() as well
* as $HOME/$USERPROFILE because $HOME is normally unset on Windows, which made
* a $HOME-only guard a no-op there.
*/
function isNonProjectDir(dir, home, env = process.env) {
const target = comparablePath(dir);
if (target === comparablePath(parse(target).root)) return true;
for (const h of [home, env.HOME, env.USERPROFILE]) {
if (h && comparablePath(h) === target) return true;
}
return false;
}
function isDirectory(dir) {
try {
return statSync(dir).isDirectory();
} catch {
return false;
}
}
/**
* The directory the MCP server should treat as the project. Claude Code
* exposes the session's project as CLAUDE_PROJECT_DIR; an explicit
* CLAUDE_FLOW_CWD is honoured next; the launcher's own cwd is the
* backwards-compatible fallback. Home/root/missing values are skipped so a
* stale global variable cannot redirect every session into ~/.claude-flow.
*/
function resolveProjectDir(env = process.env, cwd = process.cwd(), home = os.homedir()) {
for (const candidate of [env.CLAUDE_PROJECT_DIR, env.CLAUDE_FLOW_CWD]) {
if (!candidate) continue;
const dir = resolve(cwd, candidate);
if (isDirectory(dir) && !isNonProjectDir(dir, home, env)) return dir;
}
return resolve(cwd);
}
function isRunnableCli(candidate) {
try {
const distEntry = join(dirname(candidate), '..', 'dist', 'src', 'index.js');
return existsSync(candidate) && existsSync(distEntry);
} catch {
return false;
}
}
function resolveLocalCliBin(cwd, home, env = process.env) {
// An explicit pin must fail closed if it disappears; silently running
// @latest instead would defeat the override's purpose.
if (env.RUFLO_MCP_CLI_OVERRIDE) {
const override = resolve(cwd, env.RUFLO_MCP_CLI_OVERRIDE);
if (!isRunnableCli(override)) {
throw new Error(`RUFLO_MCP_CLI_OVERRIDE is not a built @claude-flow/cli bin: ${override}`);
}
return override;
}
const candidates = [join(home, '.claude', 'plugins', 'marketplaces', 'ruflo', 'bin', 'cli.js')];
// Node resolves node_modules from the current directory and its ancestors.
// A literal cwd-only probe misses installs from every nested project dir.
for (let dir = resolve(cwd); ; dir = dirname(dir)) {
candidates.push(
join(dir, 'node_modules', '@claude-flow', 'cli', 'bin', 'cli.js'),
join(dir, 'node_modules', 'ruflo', 'node_modules', '@claude-flow', 'cli', 'bin', 'cli.js'),
join(dir, 'node_modules', 'ruflo', 'bin', 'cli.js'),
join(dir, 'v3', '@claude-flow', 'cli', 'bin', 'cli.js'),
);
if (dirname(dir) === dir) break;
}
// npm global installs live next to bin/ on POSIX and under the shim
// directory on Windows. Probe PATH roots without spawning npm or npx.
for (const binDir of String(env.PATH || env.Path || '').split(delimiter)) {
if (!isAbsolute(binDir)) continue;
const modulesDir = process.platform === 'win32'
? join(binDir, 'node_modules')
: join(binDir, '..', 'lib', 'node_modules');
candidates.push(
join(modulesDir, '@claude-flow', 'cli', 'bin', 'cli.js'),
join(modulesDir, 'ruflo', 'node_modules', '@claude-flow', 'cli', 'bin', 'cli.js'),
join(modulesDir, 'claude-flow', 'node_modules', '@claude-flow', 'cli', 'bin', 'cli.js'),
);
}
for (const candidate of candidates) {
if (isRunnableCli(candidate)) return candidate;
}
return null;
}
function buildLaunchSpec(localBin, env = process.env, projectDir = null) {
// Start the server *in* the project and say so explicitly: tools built on
// getProjectCwd() read CLAUDE_FLOW_CWD, tools that call process.cwd() read
// the spawn cwd. Both must match the folder the CLI writes state to.
const placement = projectDir
? { cwd: projectDir, env: { ...env, CLAUDE_FLOW_CWD: projectDir } }
: {};
if (localBin) {
return { command: process.execPath, args: [localBin, ...MCP_ARGS], shell: false, ...placement };
}
if (env.RUFLO_MCP_SKIP_NPX === '1') {
throw new Error('No built local CLI found and RUFLO_MCP_SKIP_NPX=1 forbids the @latest fallback');
}
const npxCmd = process.platform === 'win32' ? 'npx.cmd' : 'npx';
// Windows .cmd shims aren't directly executable via CreateProcess — they
// need shell:true (or an explicit cmd.exe /c wrapper) or spawn throws
// EINVAL/ENOENT. Args here are hardcoded constants, not user input, so
// shell:true carries no injection risk on this branch.
return {
command: npxCmd,
args: ['-y', '@claude-flow/cli@latest', ...MCP_ARGS],
shell: process.platform === 'win32',
...placement,
};
}
function runInProcess(localBin, projectDir = null) {
// Same placement the spawned child got from buildLaunchSpec(): run in the
// project and advertise it via CLAUDE_FLOW_CWD, before the CLI is imported
// (it reads both at import/start time).
if (projectDir) {
process.chdir(projectDir);
process.env.CLAUDE_FLOW_CWD = projectDir;
}
// Same argv shape the spawned child saw: [node, <bin>/cli.js, 'mcp', 'start'].
// cli.js decides MCP mode from process.argv and stdin at import time.
process.argv = [process.execPath, localBin, ...MCP_ARGS];
return import(pathToFileURL(localBin).href);
}
function isMissingModuleError(error) {
return Boolean(error) && (error.code === 'ERR_MODULE_NOT_FOUND' || error.code === 'MODULE_NOT_FOUND');
}
/**
* isRunnableCli() only proves bin/cli.js and dist/src/index.js exist; it
* cannot know every file cli.js imports. A stale dist/ (source newer than the
* last build) therefore passes the guard and then throws on import. The text
* below is what the person sees instead of a bare module path.
*/
function describeUnloadableCli(localBin, error, willFallBack) {
const bin = resolve(localBin);
const inRepo = /[\\/]v3[\\/]@claude-flow[\\/]cli[\\/]bin[\\/]cli\.js$/i.test(bin);
const lines = [`[mcp-launch] could not load ${bin}: ${error && error.message}`];
if (inRepo) {
lines.push(
'You are running inside a ruflo checkout, so its v3/@claude-flow/cli was picked as the local CLI,',
'but its dist/ build is missing files (it is older than src/). To use it, rebuild:',
' cd v3/@claude-flow/cli && pnpm install && pnpm run build',
'or start Claude Code outside the ruflo repo to use an installed @claude-flow/cli instead.',
);
} else {
lines.push('That local @claude-flow/cli install is incomplete; reinstall or rebuild it.');
}
lines.push(willFallBack
? 'Falling back to `npx -y @claude-flow/cli@latest mcp start` for this session.'
: 'Not falling back to @latest.');
return lines.join('\n') + '\n';
}
/**
* Called when the in-process import of a resolved local CLI throws. A pinned
* RUFLO_MCP_CLI_OVERRIDE still fails closed, as does RUFLO_MCP_SKIP_NPX=1;
* otherwise a missing-module failure (stale or partial build) degrades to the
* npx fallback so the MCP server still comes up. The import fails before the
* CLI touches stdio, so starting a second server cannot interleave output.
*/
function recoverFromLoadFailure(localBin, error, projectDir, env = process.env, deps = {}) {
const run = deps.run || launch;
const exit = deps.exit || ((code) => process.exit(code));
const canFallBack = isMissingModuleError(error)
&& !env.RUFLO_MCP_CLI_OVERRIDE
&& env.RUFLO_MCP_SKIP_NPX !== '1';
process.stderr.write(describeUnloadableCli(localBin, error, canFallBack));
if (!canFallBack) return exit(1);
return run(buildLaunchSpec(null, env, projectDir));
}
function launch({ command, args, shell, cwd, env }) {
const child = spawn(command, args, { stdio: 'inherit', env: env || process.env, shell, cwd });
// Forward termination signals so Claude Code can stop the MCP server the
// same way it would if it had spawned the real binary directly.
const forward = (signal) => child.kill(signal);
for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, forward);
}
child.on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal);
} else {
process.exit(code === null ? 1 : code);
}
});
child.on('error', (err) => {
process.stderr.write(`[mcp-launch] failed to start ${command}: ${err.message}\n`);
process.exit(1);
});
}
if (require.main === module) {
try {
if (!process.env.RUFLO_LAUNCH_SURFACE) process.env.RUFLO_LAUNCH_SURFACE = 'plugin'; // counts plugin users in the funnel (see header)
const home = os.homedir();
const projectDir = resolveProjectDir(process.env, process.cwd(), home);
if (isNonProjectDir(projectDir, home)) {
// stderr only: stdout is the MCP stdio channel.
process.stderr.write(
`[mcp-launch] warning: no project directory (session started in ${projectDir}); ` +
'MCP state will be kept there, not in folders CLI commands run from. ' +
'Start Claude Code inside the project or set CLAUDE_FLOW_CWD.\n',
);
}
const localBin = resolveLocalCliBin(projectDir, home);
if (localBin && process.env.RUFLO_MCP_SPAWN !== '1') {
runInProcess(localBin, projectDir).catch((error) => {
try {
recoverFromLoadFailure(localBin, error, projectDir);
} catch (recoveryError) {
process.stderr.write(`[mcp-launch] ${recoveryError.message}\n`);
process.exit(1);
}
});
} else {
launch(buildLaunchSpec(localBin, process.env, projectDir));
}
} catch (error) {
process.stderr.write(`[mcp-launch] ${error.message}\n`);
process.exitCode = 1;
}
}
module.exports = {
resolveLocalCliBin, resolveProjectDir, isNonProjectDir, buildLaunchSpec, runInProcess,
isMissingModuleError, describeUnloadableCli, recoverFromLoadFailure, MCP_ARGS,
};