From db0c10a819229dc18f0d68ac22ad3a9307cbaf6b Mon Sep 17 00:00:00 2001 From: Marius Carlsson <109006081+socrates6969@users.noreply.github.com> Date: Sat, 10 Oct 2026 16:04:25 +0200 Subject: [PATCH 1/5] perf(ruflo-core): run resolved local CLI in-process in mcp-launch mcp-launch.cjs spawned `node /cli.js mcp start` as a child even when a built local/global CLI resolved, so every Claude Code session kept a second, idle node.exe alive just to forward stdio and signals. When a local CLI resolves, import it in-process with process.argv rewritten to the exact argv the child used to get. The npx fallback still spawns. RUFLO_MCP_SPAWN=1 restores the child-process behaviour. Measured (Windows 11, Node 24.21, @claude-flow/cli 3.56.3, median of 3, post-GC, after initialize + tools/list): processes 2 -> 1, working set 149.3 -> 87.6 MB, private 74.0 -> 52.2 MB, spawn->initialize 1320 -> 729 ms; tools/list sha256 unchanged (8a723dc0...). Co-Authored-By: Claude Opus 5.5 --- plugins/ruflo-core/scripts/mcp-launch.cjs | 34 ++++++++++++++- .../ruflo-core/scripts/mcp-launch.test.cjs | 42 ++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) 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 + * ` /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, /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 }); + } +}); From a7992f06c711934a734ca54d036a9b6cfa694c56 Mon Sep 17 00:00:00 2001 From: Marius Carlsson <109006081+socrates6969@users.noreply.github.com> Date: Sat, 10 Oct 2026 16:04:25 +0200 Subject: [PATCH 2/5] perf(cli): keep node:http and the security barrel off the MCP startup path The MCP server eagerly imports services/policy-runtime.ts, which imported the '@claude-flow/security' barrel. The barrel evaluates the OAuth callback server, bcryptjs, keychain and credential helpers, and the callback server's ESM `import { createServer } from 'node:http'` materialises every export of the builtin, tripping http's lazy undici getters. browser-intent-tools.ts and agentbbs-federation.ts did the same with their own static node:http imports. - policy-runtime.ts imports from '@claude-flow/security/policy/index' and '@claude-flow/security/mcp-caller-identity' (same objects, via the package's "./*" export; '/safe-git' already uses this pattern). - browser-intent-tools.ts / agentbbs-federation.ts require('node:http') lazily at the point a server is created. Measured on a patched copy of the 3.56.3 dist (median of 3, post-GC, idle after tools/list): heapUsed 15.6 -> 13.2 MB, private 51.5 -> 47.7 MB, working set 87.2 -> 81.5 MB. tools/list byte-identical; system_*, config_list, policy_status, memory_store and memory_search results identical apart from timestamps/pids/timings. Not type-checked or built here (no deps in the sparse worktree). Co-Authored-By: Claude Opus 5.5 --- .../cli/src/mcp-tools/agentbbs-federation.ts | 11 +++++++++-- .../cli/src/mcp-tools/browser-intent-tools.ts | 10 ++++++++-- .../cli/src/services/policy-runtime.ts | 17 ++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts index 93e776c717..a591d76ed3 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts @@ -54,7 +54,14 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, chmodSync } from 'node:fs'; import { join } from 'node:path'; import { createHash, randomBytes, timingSafeEqual } from 'node:crypto'; -import { createServer, type Server, type IncomingMessage, type ServerResponse } from 'node:http'; +import { createRequire } from 'node:module'; +import type { Server, IncomingMessage, ServerResponse } from 'node:http'; + +// Lazy, CommonJS-style load of node:http: an ESM `import … from 'node:http'` +// materialises every export of the builtin, which trips http's lazy undici +// getters (~2.5 MB heap / ~6 MB RSS) at MCP startup even when no server is +// ever started. require() of the builtin does not touch those getters. +const loadHttp = (): typeof import('node:http') => createRequire(import.meta.url)('node:http'); /** Max envelopes accepted from one peer in one sync. */ export const MAX_ENVELOPES_PER_SYNC = 5_000; @@ -425,7 +432,7 @@ export function serveFederation( opts: { port?: number; bindHost?: string } = {}, ): Promise<{ server: Server; port: number; host: string }> { const host = opts.bindHost ?? '127.0.0.1'; - const server = createServer((req: IncomingMessage, res: ServerResponse) => { + const server = loadHttp().createServer((req: IncomingMessage, res: ServerResponse) => { const send = (code: number, obj: unknown) => { const buf = Buffer.from(JSON.stringify(obj)); res.writeHead(code, { 'content-type': 'application/json', 'content-length': buf.length }); diff --git a/v3/@claude-flow/cli/src/mcp-tools/browser-intent-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/browser-intent-tools.ts index c63d04bce2..8d9598ce83 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/browser-intent-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/browser-intent-tools.ts @@ -86,10 +86,16 @@ */ import { createRequire } from 'node:module'; -import { createServer, type Server } from 'node:http'; +import type { Server } from 'node:http'; import { readFileSync, existsSync } from 'node:fs'; import { dirname, join } from 'node:path'; import type { MCPTool, MCPToolResult } from './types.js'; + +// Lazy, CommonJS-style load of node:http: an ESM `import … from 'node:http'` +// materialises every export of the builtin, which trips http's lazy undici +// getters (~2.5 MB heap / ~6 MB RSS) at MCP startup even when no server is +// ever started. require() of the builtin does not touch those getters. +const loadHttp = (): typeof import('node:http') => createRequire(import.meta.url)('node:http'); import { validateText, validateIdentifier } from './validate-input.js'; import { execBrowserCommand } from './browser-tools.js'; import { storeEntry } from '../memory/memory-initializer.js'; @@ -219,7 +225,7 @@ export interface LLMProxyHandle { export function startLocalLLMProxy(real: { baseURL: string; apiKey: string }): Promise { return new Promise((resolve, reject) => { - const server: Server = createServer((req, res) => { + const server: Server = loadHttp().createServer((req, res) => { const target = `${real.baseURL.replace(/\/+$/, '')}${req.url ?? ''}`; const chunks: Buffer[] = []; req.on('data', (c: Buffer) => chunks.push(c)); diff --git a/v3/@claude-flow/cli/src/services/policy-runtime.ts b/v3/@claude-flow/cli/src/services/policy-runtime.ts index 2672cf9732..1cc9a8a699 100644 --- a/v3/@claude-flow/cli/src/services/policy-runtime.ts +++ b/v3/@claude-flow/cli/src/services/policy-runtime.ts @@ -1,10 +1,11 @@ +// Narrow subpath imports (not the '@claude-flow/security' barrel): this module +// is loaded eagerly by the MCP server, and the barrel also evaluates the OAuth +// callback server (an ESM `node:http` import that materialises http's lazy +// undici getters), bcryptjs, keychain and credential helpers that the policy +// runtime never uses. import { AgenticPolicyEngine, createLegacyCompatibleState, - isMcpCallerAuthEnabled, - decodeTokenEnvelope, - publicKeyFromHex, - verifyInvocationToken, type BudgetLimit, type CapabilityEnvelope, type PolicyApproval, @@ -13,7 +14,13 @@ import { type PolicyRequest, type PolicyRule, type PolicyState, -} from '@claude-flow/security'; +} from '@claude-flow/security/policy/index'; +import { + isMcpCallerAuthEnabled, + decodeTokenEnvelope, + publicKeyFromHex, + verifyInvocationToken, +} from '@claude-flow/security/mcp-caller-identity'; import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto'; import { execFileSync } from 'node:child_process'; import { From 187e695665be52a46c7c39d5932a84bf1dc44a82 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 14 Oct 2026 05:34:05 -0400 Subject: [PATCH 3/5] test(cli): alias the security subpaths policy-runtime now imports to workspace source (like safe-git), so the tests do not load a stale installed dist beside the source barrel Co-Authored-By: RuFlo --- v3/@claude-flow/cli/vitest.config.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/v3/@claude-flow/cli/vitest.config.ts b/v3/@claude-flow/cli/vitest.config.ts index 6dff721cba..934201efe4 100644 --- a/v3/@claude-flow/cli/vitest.config.ts +++ b/v3/@claude-flow/cli/vitest.config.ts @@ -14,6 +14,14 @@ export default defineConfig({ find: '@claude-flow/guidance/compiler', replacement: fileURLToPath(new URL('../guidance/src/compiler.ts', import.meta.url)), }, + { + find: '@claude-flow/security/policy/index', + replacement: fileURLToPath(new URL('../security/src/policy/index.ts', import.meta.url)), + }, + { + find: '@claude-flow/security/mcp-caller-identity', + replacement: fileURLToPath(new URL('../security/src/mcp-caller-identity.ts', import.meta.url)), + }, { find: '@claude-flow/security/safe-git', replacement: fileURLToPath(new URL('../security/src/safe-git.ts', import.meta.url)), From 658655c1d9a971cd216c2e595e88754dd20bcfc5 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 14 Oct 2026 05:34:13 -0400 Subject: [PATCH 4/5] chore(ruflo-core): 0.2.10 (in-process MCP launcher), changelog with measured launcher numbers Co-Authored-By: RuFlo --- plugins/ruflo-core/.claude-plugin/plugin.json | 2 +- plugins/ruflo-core/CHANGELOG.md | 3 +++ plugins/ruflo-core/scripts/smoke.sh | 8 ++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/ruflo-core/.claude-plugin/plugin.json b/plugins/ruflo-core/.claude-plugin/plugin.json index d1c9c09013..18c436cc8b 100644 --- a/plugins/ruflo-core/.claude-plugin/plugin.json +++ b/plugins/ruflo-core/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "ruflo-core", "description": "Foundation plugin — registers the ruflo MCP server (300+ tools across memory/agentdb/embeddings/hooks/aidefence/neural/autopilot/browser/agent/swarm), provides 4 generalist agents (coder/researcher/reviewer/witness-curator), 5 skills (discover-plugins/init-project/ruflo-doctor/ruflo-status/witness), and a curated plugin-discovery catalog", - "version": "0.2.9", + "version": "0.2.10", "author": { "name": "ruvnet", "url": "https://github.com/ruvnet" diff --git a/plugins/ruflo-core/CHANGELOG.md b/plugins/ruflo-core/CHANGELOG.md index 7c108b56ae..9f2d7f69c7 100644 --- a/plugins/ruflo-core/CHANGELOG.md +++ b/plugins/ruflo-core/CHANGELOG.md @@ -2,6 +2,9 @@ Newest first. One `## — ` heading per version, then `feat:`, `fix:`, `breaking:` and `chore:` bullets (ADR-478). Built from git history; this is the whole history. +## 0.2.10 — 2026-10-14 +- perf: the MCP launcher runs the resolved local CLI inside its own process instead of spawning a second node (one process, about half the memory, a faster first answer: measured here with a stub CLI as 2 processes to 1, 89 MB to 46 MB, 30 ms to 19 ms spawn-to-initialize); `RUFLO_MCP_SPAWN=1` restores the child process, and the npx fallback still spawns (#3989, thanks @socrates6969). The real-CLI startup numbers in the PR (149 MB to 81 MB, 1.3 s to 0.7 s) were not re-measured. + ## 0.2.9 — 2026-10-14 - fix: the MCP launcher starts the server in the session project (`CLAUDE_PROJECT_DIR`, then `CLAUDE_FLOW_CWD`, then its own working directory; a drive root, the home folder or a missing folder is not a project) and looks for the local CLI from there, so on Windows the CLI and the MCP server keep one `.claude-flow` state, not two (#3987, thanks @socrates6969). It warns on stderr when there is no project. Tested with Windows-style environments only; no Windows machine was available. diff --git a/plugins/ruflo-core/scripts/smoke.sh b/plugins/ruflo-core/scripts/smoke.sh index aa3c9c8a19..abd0eb39da 100755 --- a/plugins/ruflo-core/scripts/smoke.sh +++ b/plugins/ruflo-core/scripts/smoke.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Structural smoke test for ruflo-core v0.2.9 (ADR-0001). +# Structural smoke test for ruflo-core v0.2.10 (ADR-0001). set -u ROOT="$(cd "$(dirname "$0")/.." && pwd)" PASS=0 @@ -8,10 +8,10 @@ step() { printf "→ %s ... " "$1"; } ok() { printf "PASS\n"; PASS=$((PASS+1)); } bad() { printf "FAIL: %s\n" "$1"; FAIL=$((FAIL+1)); } -step "1. plugin.json declares 0.2.9 with new keywords" +step "1. plugin.json declares 0.2.10 with new keywords" v=$(grep -E '"version"' "$ROOT/.claude-plugin/plugin.json" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) -if [[ "$v" != "0.2.9" ]]; then - bad "expected 0.2.9, got '$v'" +if [[ "$v" != "0.2.10" ]]; then + bad "expected 0.2.10, got '$v'" else miss="" for k in foundation mcp-server plugin-catalog discovery; do From b452756f2bd91469a6a8c2bf7772e9291325e93d Mon Sep 17 00:00:00 2001 From: r Date: Wed, 14 Oct 2026 07:04:22 -0400 Subject: [PATCH 5/5] chore(lock): root package-lock follows the published cli-core 3.7.3 Co-Authored-By: RuFlo --- package-lock.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index fbd3d8b298..74adb1a3f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "v3/@claude-flow/security" ], "dependencies": { - "@claude-flow/cli-core": "3.7.2", + "@claude-flow/cli-core": "3.7.3", "@claude-flow/codex": "3.0.5", "@claude-flow/mcp": "3.1.1", "@claude-flow/neural": "3.0.0", @@ -147,9 +147,9 @@ } }, "node_modules/@claude-flow/cli-core": { - "version": "3.7.2", - "resolved": "https://registry.npmjs.org/@claude-flow/cli-core/-/cli-core-3.7.2.tgz", - "integrity": "sha512-VzfMayqDMLpPprR7d3Z4jRZAF4kBraSmBUrAVmOY7aFpxnHFYHpfuj/vHowIDDxpgQitrQDAJJFu0Em8IYZf4g==", + "version": "3.7.3", + "resolved": "https://registry.npmjs.org/@claude-flow/cli-core/-/cli-core-3.7.3.tgz", + "integrity": "sha512-2rBTFde47ydpQQ9o84QrHnMUcJ8oCqi9XX8N8/VdUe1TFCSH3aYhGnYqREY32r47rNiH1XPqtaN4WwfQ390WeQ==", "license": "MIT", "bin": { "claude-flow-core": "dist/src/index.js"