Files
ruflo/v3/@claude-flow/cli/bin/cli.js
T
ReuvenandRuFlo 1884ed1010 fix(3.7.0-alpha.12): batch resolve issue-fix loop #1839-#1847
Closes #1839, #1840, #1841, #1842, #1843, #1844, #1845, #1846, #1847.

#1839 — bin/cli.js, bin/mcp-server.js, config-adapter.ts: rename
  serverInfo.name 'claude-flow' → 'ruflo' so MCP wire identity matches the
  package name. Live probe of `ruflo mcp start` now returns
  `serverInfo.name="ruflo"` (verified post-build).

#1840 — init/executor.ts: detectExistingRufloMCP() now also walks
  parsed.projects[*].mcpServers.ruflo with normalized path matching
  (lowercase + forward-slash) so `claude mcp add ruflo` registrations
  stored project-scoped in ~/.claude.json are detected and init skips
  writing a duplicate .mcp.json.

#1841 — init/mcp-generator.ts: generated .mcp.json now keys
  `mcpServers.ruflo` instead of legacy `mcpServers['claude-flow']`. The
  printed `claude mcp add` instructions match. Prevents the
  duplicate-tool-prefix problem when init runs over an existing ruflo
  registration.

#1842 — commands/doctor.ts: checkMcpServers() now traverses
  ~/.claude.json.projects[*].mcpServers in addition to top-level
  mcpServers, so doctor stops warning "No MCP config found" when a
  working project-scoped ruflo registration exists.

#1843 — mcp-tools/system-tools.ts: exclude `unknown` from health
  denominator (was dragging the score down even when actionable checks
  were green) and surface as `advisory` count. Add YAML config and
  sql.js/HNSW/.swarm/ruvector memory paths to detection. Local probe:
  4 healthy / 5 actionable = 80% (was 4/8 = 50%) with 3 advisory.

#1844 — services/worker-daemon.ts: WorkerDaemon now reads
  .claude-flow/config.yaml (and .yml) when config.json is absent, with
  lazy `yaml` import so missing dep falls back to a clear warning.
  Adds yaml@^2.8.0 to runtime deps. Logs the chosen source.

#1845 — services/worker-daemon.ts + mcp-tools/hooks-tools.ts: durable
  queue boundary between MCP dispatch and daemon. mcp__hooks_worker-
  dispatch now writes .claude-flow/daemon-queue/<id>.json (the
  process-local Map was invisible to the daemon). The daemon polls this
  directory every 5s, executes triggerWorker(), and moves processed
  entries to .processed/. Adds `mcp-only` status for queue-write
  failures so we never claim `queued` without proof on disk.

#1846 — mcp-tools/memory-tools.ts + commands/memory.ts: feature-detect
  smartSearch on the resolved @claude-flow/memory build. When absent
  (older published memory packages predate the export), fall through to
  plain semantic search with an explicit warning + `smartFallback`
  field on the response, instead of throwing
  `smartSearch is not a function`.

#1847 — mcp-tools/hooks-tools.ts: hooks_pretrain now emits an explicit
  note when a non-empty corpus produces zero patterns, naming the
  supported extraction extensions (.ts/.js/.py/.go/.rs/.java) and
  clarifying that pretrain is a separate axis from live trajectories
  and statusline state. Local probe on a Markdown vault returns the
  new note correctly.

src/index.ts: pre-existing TS strict-null error in showCommandHelp()
  that was preventing `npm run build` (introduced by #1791.4 / 93b97548e)
  fixed by extracting a non-null `current: Command` local instead of
  reassigning the optional `command`. Unblocks publish + restores the
  integration-docker `npm run build succeeds` test.

Verification:
- 1963 tests pass / 0 fail (was 1962 / 1 due to the index.ts TS error)
- Live MCP probe: serverInfo.name="ruflo" ✅
- Live mcp-generator: emits mcpServers.ruflo ✅
- Live system_health: 80% (4/5 healthy, 3 advisory) ✅
- Live hooks_pretrain on Markdown vault: emits explicit note ✅

Published @claude-flow/cli@3.7.0-alpha.12, claude-flow@3.7.0-alpha.12,
ruflo@3.7.0-alpha.12 with all dist-tags (alpha, latest, v3alpha for
cli/claude-flow; alpha, latest for ruflo).

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-08 09:36:58 -04:00

221 lines
7.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* @claude-flow/cli - CLI Entry Point
*
* Claude Flow V3 Command Line Interface
*
* Auto-detects MCP mode when stdin is piped and no args provided.
* This allows: echo '{"jsonrpc":"2.0",...}' | npx @claude-flow/cli
*/
import { randomUUID } from 'crypto';
// Suppress the SPECIFIC cosmetic "[AgentDB Patch] Controller index not found"
// warning from agentic-flow's runtime patch — these are emitted because the
// patch was written for agentdb v1.x and we use v3, where the controllers
// dist directory is laid out differently. The warning surfaces on every
// command and the audit (audit_1776483149979) flagged a too-broad suppression
// as a security risk because it could hide legitimate [AgentDB Patch] warnings.
//
// Tight match: must include both the prefix AND the specific "Controller
// index not found" text. Anything else (including future [AgentDB Patch]
// warnings about real issues) flows through unchanged. Also patch
// console.log because the underlying code uses it (the previous filter
// only caught console.warn and was therefore a no-op).
const _origWarn = console.warn;
const _origLog = console.log;
const _isCosmeticAgentdbPatchNoise = (msg) =>
msg.includes('[AgentDB Patch]') && msg.includes('Controller index not found');
console.warn = (...args) => {
if (_isCosmeticAgentdbPatchNoise(String(args[0] ?? ''))) return;
_origWarn.apply(console, args);
};
console.log = (...args) => {
if (_isCosmeticAgentdbPatchNoise(String(args[0] ?? ''))) return;
_origLog.apply(console, args);
};
// Check if we should run in MCP server mode
// Conditions:
// 1. stdin is being piped AND no CLI arguments provided (auto-detect)
// 2. stdin is being piped AND args are "mcp start" (explicit, e.g. npx claude-flow@alpha mcp start)
const cliArgs = process.argv.slice(2);
const isExplicitMCP = cliArgs.length >= 1 && cliArgs[0] === 'mcp' && (cliArgs.length === 1 || cliArgs[1] === 'start');
const isMCPMode = !process.stdin.isTTY && (process.argv.length === 2 || isExplicitMCP);
if (isMCPMode) {
// Run MCP server mode
const { listMCPTools, callMCPTool, hasTool } = await import('../dist/src/mcp-client.js');
const VERSION = '3.0.0';
const sessionId = `mcp-${Date.now()}-${randomUUID().slice(0, 8)}`;
console.error(
`[${new Date().toISOString()}] INFO [claude-flow-mcp] (${sessionId}) Starting in stdio mode`
);
// Audit-flagged DoS protection (audit_1776483149979): cap the
// newline-buffered stdin parser so a malicious client cannot pipe
// gigabytes of un-newlined data and exhaust memory before
// JSON.parse runs. 10MB is far above any legitimate MCP message
// (the protocol's largest realistic payloads — tool descriptions,
// batch search results — top out at ~1MB).
const MCP_MAX_BUFFER_BYTES = 10 * 1024 * 1024;
let buffer = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', async (chunk) => {
buffer += chunk;
if (buffer.length > MCP_MAX_BUFFER_BYTES) {
// Drop the buffer + emit a protocol-level error so the client
// sees the rejection rather than a silent OOM.
console.log(JSON.stringify({
jsonrpc: '2.0',
id: null,
error: {
code: -32700,
message: `Buffered stdin exceeds ${MCP_MAX_BUFFER_BYTES} bytes without newline; resetting`,
},
}));
buffer = '';
return;
}
let lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.trim()) {
let message;
try {
message = JSON.parse(line);
} catch {
console.log(JSON.stringify({
jsonrpc: '2.0',
id: null,
error: { code: -32700, message: 'Parse error' },
}));
continue;
}
try {
const response = await handleMessage(message);
if (response) {
console.log(JSON.stringify(response));
}
} catch (error) {
// #1606: Return proper internal error instead of parse error
console.log(JSON.stringify({
jsonrpc: '2.0',
id: message.id ?? null,
error: { code: -32603, message: error instanceof Error ? error.message : 'Internal error' },
}));
}
}
}
});
process.stdin.on('end', () => {
process.exit(0);
});
async function handleMessage(message) {
if (!message.method) {
return {
jsonrpc: '2.0',
id: message.id,
error: { code: -32600, message: 'Invalid Request: missing method' },
};
}
const params = message.params || {};
switch (message.method) {
case 'initialize':
return {
jsonrpc: '2.0',
id: message.id,
result: {
protocolVersion: '2024-11-05',
serverInfo: { name: 'ruflo', version: VERSION },
capabilities: {
tools: { listChanged: true },
resources: { subscribe: true, listChanged: true },
},
},
};
case 'tools/list': {
const tools = listMCPTools();
return {
jsonrpc: '2.0',
id: message.id,
result: {
tools: tools.map(tool => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema,
})),
},
};
}
case 'tools/call': {
const toolName = params.name;
const toolParams = params.arguments || {};
if (!hasTool(toolName)) {
return {
jsonrpc: '2.0',
id: message.id,
error: { code: -32601, message: `Tool not found: ${toolName}` },
};
}
try {
const result = await callMCPTool(toolName, toolParams, { sessionId });
return {
jsonrpc: '2.0',
id: message.id,
result: { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] },
};
} catch (error) {
return {
jsonrpc: '2.0',
id: message.id,
error: {
code: -32603,
message: error instanceof Error ? error.message : 'Tool execution failed',
},
};
}
}
case 'notifications/initialized':
return null;
case 'ping':
return { jsonrpc: '2.0', id: message.id, result: {} };
default:
return {
jsonrpc: '2.0',
id: message.id,
error: { code: -32601, message: `Method not found: ${message.method}` },
};
}
}
} else {
// Run normal CLI mode
const { CLI } = await import('../dist/src/index.js');
const cli = new CLI();
cli.run()
.then(() => {
// #1552: Exit cleanly after one-shot commands.
// Long-running commands (daemon foreground, mcp, status --watch) never resolve,
// so this only fires for normal CLI commands.
process.exit(0);
})
.catch((error) => {
console.error('Fatal error:', error.message);
process.exit(1);
});
}