Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

perf(ruflo-core): run resolved local CLI in-process in mcp-launch

Marius Carlssondb0c10a81922
diff --git a/plugins/ruflo-core/scripts/mcp-launch.cjs b/plugins/ruflo-core/scripts/mcp-launch.cjs
index 732a3cdeb4..8870cb03af 100644
--- a/plugins/ruflo-core/scripts/mcp-launch.cjs
+++ b/plugins/ruflo-core/scripts/mcp-launch.cjs
@@ -25,12 +25,21 @@
* 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.
*/
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'];
@@ -157,6 +166,20 @@ function buildLaunchSpec(localBin, env = process.env, projectDir = null) {
};
}
+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 launch({ command, args, shell, cwd, env }) {
const child = spawn(command, args, { stdio: 'inherit', env: env || process.env, shell, cwd });
@@ -193,11 +216,18 @@ if (require.main === module) {
);
}
const localBin = resolveLocalCliBin(projectDir, home);
- launch(buildLaunchSpec(localBin, process.env, projectDir));
+ if (localBin && process.env.RUFLO_MCP_SPAWN !== '1') {
+ runInProcess(localBin, projectDir).catch((error) => {
+ process.stderr.write(`[mcp-launch] failed to load ${localBin}: ${error && error.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, MCP_ARGS };
+module.exports = { resolveLocalCliBin, resolveProjectDir, isNonProjectDir, buildLaunchSpec, runInProcess, MCP_ARGS };
diff --git a/plugins/ruflo-core/scripts/mcp-launch.test.cjs b/plugins/ruflo-core/scripts/mcp-launch.test.cjs
index 5fce1dea6c..9262cf71bd 100644
--- a/plugins/ruflo-core/scripts/mcp-launch.test.cjs
+++ b/plugins/ruflo-core/scripts/mcp-launch.test.cjs
@@ -14,7 +14,7 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
-const { resolveLocalCliBin, buildLaunchSpec, MCP_ARGS } = require(
+const { resolveLocalCliBin, buildLaunchSpec, runInProcess, MCP_ARGS } = require(
path.join(__dirname, 'mcp-launch.cjs')
);
const NO_GLOBAL_PATH = { PATH: '' };
@@ -245,3 +245,43 @@ test('resolveProjectDir: CLAUDE_PROJECT_DIR > CLAUDE_FLOW_CWD > cwd; home, root
for (const d of [cwd, a, b, home]) fs.rmSync(d, { recursive: true, force: true });
}
});
+
+test('runInProcess loads the resolved bin in-process with the argv the child used to get', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-launch-inproc-'));
+ const bin = path.join(dir, 'cli.js');
+ fs.writeFileSync(bin, 'globalThis.__mcpLaunchArgv = process.argv.slice();\n');
+ const savedArgv = process.argv;
+ try {
+ await runInProcess(bin);
+ assert.deepEqual(globalThis.__mcpLaunchArgv, [process.execPath, bin, ...MCP_ARGS]);
+ } finally {
+ process.argv = savedArgv;
+ delete globalThis.__mcpLaunchArgv;
+ }
+});
+
+test('runInProcess runs the CLI in the project dir with CLAUDE_FLOW_CWD set before import', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-launch-inproc-proj-'));
+ const project = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-launch-project-'));
+ const bin = path.join(dir, 'cli.js');
+ fs.writeFileSync(
+ bin,
+ 'globalThis.__mcpLaunchSeen = { cwd: process.cwd(), flowCwd: process.env.CLAUDE_FLOW_CWD };\n',
+ );
+ const savedArgv = process.argv;
+ const savedCwd = process.cwd();
+ const hadFlowCwd = Object.prototype.hasOwnProperty.call(process.env, 'CLAUDE_FLOW_CWD');
+ const savedFlowCwd = process.env.CLAUDE_FLOW_CWD;
+ try {
+ await runInProcess(bin, project);
+ assert.ok(samePath(globalThis.__mcpLaunchSeen.cwd, project), `cwd ${globalThis.__mcpLaunchSeen.cwd} should be ${project}`);
+ assert.equal(globalThis.__mcpLaunchSeen.flowCwd, project);
+ } finally {
+ process.chdir(savedCwd);
+ process.argv = savedArgv;
+ if (hadFlowCwd) process.env.CLAUDE_FLOW_CWD = savedFlowCwd;
+ else delete process.env.CLAUDE_FLOW_CWD;
+ delete globalThis.__mcpLaunchSeen;
+ for (const d of [dir, project]) fs.rmSync(d, { recursive: true, force: true });
+ }
+});