From 2cb4142d4e212fa3d83c4f6f28c1827a72713f9e Mon Sep 17 00:00:00 2001 From: ruv Date: Tue, 16 Jun 2026 14:57:41 -0400 Subject: [PATCH] =?UTF-8?q?feat(metaharness):=20MCP=20tool=20registry=20?= =?UTF-8?q?=E2=80=94=205=20first-class=20mcp=5F=5Fclaude-flow=5F=5Fmetahar?= =?UTF-8?q?ness=5F*=20tools=20(iter=2020)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The deepest possible integration. ADR-150's static-analysis surfaces are now first-class MCP tools that Claude Code agents can call programmatically without shelling out themselves: mcp__claude-flow__metaharness_score 5-dim readiness scorecard mcp__claude-flow__metaharness_genome 7-section categorical report mcp__claude-flow__metaharness_mcp_scan static MCP security findings mcp__claude-flow__metaharness_threat_model enterprise-grade threat model mcp__claude-flow__metaharness_oia_audit composite audit → memory DESIGN - v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts (~180 LOC). - Each tool's handler subprocess-invokes the corresponding plugin script (score.mjs / genome.mjs / mcp-scan.mjs / threat-model.mjs / oia-audit.mjs) via the same locator the CLI dispatcher uses. - Input schemas are minimal: `path` (default `.`), per-tool alert thresholds (alertOnFitBelow / alertOnRiskAbove / failOn). - Output: { success, data: , degraded, exitCode } — the full plugin script's --format json output passes through as `data`. ADR-150 ARCHITECTURAL CONSTRAINTS (still satisfied at this layer) - ZERO static `@metaharness/*` imports in this file or in mcp-client.ts. The MCP tools delegate to plugin scripts which delegate to npx subprocesses. Smoke step 17m explicitly grep-checks that no `from '@metaharness/...'` appears in metaharness-tools.ts. - Tool handlers detect degraded payloads (`json.degraded === true`) and surface them via { success: false, degraded: true } so MCP callers can branch on `result.degraded` cleanly. - Subprocess hard timeout 120s per call. CALL PATHS — all four now reach the same skill family Claude Code skill: invokes SKILL.md → bash → script.mjs CLI dispatch: npx ruflo metaharness → script.mjs MCP tool (NEW): mcp__claude-flow__metaharness_* → script.mjs Direct script: node plugins/ruflo-metaharness/scripts/.mjs Smoke step 17m - All 5 tool names present in source - No static @metaharness/* import (anti-regression) - locatePluginScripts + child_process present - Both import + spread in mcp-client.ts TOOL_REGISTRY Smoke: 28 → 29. Fleet: 33/33. tsc clean. Co-Authored-By: RuFlo --- plugins/ruflo-metaharness/scripts/smoke.sh | 19 ++ v3/@claude-flow/cli/src/mcp-client.ts | 4 + v3/@claude-flow/cli/src/mcp-tools/index.ts | 2 + .../cli/src/mcp-tools/metaharness-tools.ts | 191 ++++++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts diff --git a/plugins/ruflo-metaharness/scripts/smoke.sh b/plugins/ruflo-metaharness/scripts/smoke.sh index 4f6726ae82..fcf04dd65c 100755 --- a/plugins/ruflo-metaharness/scripts/smoke.sh +++ b/plugins/ruflo-metaharness/scripts/smoke.sh @@ -170,6 +170,25 @@ if (!od.metaharness) { console.error('missing metaharness in optionalDependencie if (j.dependencies && j.dependencies.metaharness) { console.error('metaharness leaked into dependencies'); process.exit(1); } " 2>/dev/null && ok || bad "ruflo wrapper missing metaharness optionalDep" +step "17m. metaharness MCP tools registered (ADR-150 deepest integration — iter 20)" +F="$ROOT/../../v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts" +miss="" +[[ -f "$F" ]] || miss="$miss tools-file-missing" +# All 5 tools declared +for tool in metaharness_score metaharness_genome metaharness_mcp_scan metaharness_threat_model metaharness_oia_audit; do + grep -q "name: '${tool}'" "$F" || miss="$miss missing-${tool}" +done +# ADR-150 architectural-constraint anchor: zero static @metaharness/* import +grep -q "from '@metaharness/" "$F" && miss="$miss static-metaharness-import-LEAK" +# Subprocess isolation + locator +grep -q "locatePluginScripts" "$F" || miss="$miss no-locator" +grep -q "child_process" "$F" || miss="$miss no-subprocess" +# Registered in mcp-client.ts +CLIENT="$ROOT/../../v3/@claude-flow/cli/src/mcp-client.ts" +grep -q "import { metaharnessTools }" "$CLIENT" || miss="$miss not-imported-in-client" +grep -q "\.\.\.metaharnessTools" "$CLIENT" || miss="$miss not-spread-in-registry" +[[ -z "$miss" ]] && ok || bad "$miss" + step "17l. test-graceful-degradation drill (ADR-150 rule #3 — iter 19)" F="$ROOT/scripts/test-graceful-degradation.mjs" miss="" diff --git a/v3/@claude-flow/cli/src/mcp-client.ts b/v3/@claude-flow/cli/src/mcp-client.ts index 876f751cf3..fdfb0a93e8 100644 --- a/v3/@claude-flow/cli/src/mcp-client.ts +++ b/v3/@claude-flow/cli/src/mcp-client.ts @@ -48,6 +48,8 @@ import { wasmAgentTools } from './mcp-tools/wasm-agent-tools.js'; import { managedAgentTools } from './mcp-tools/managed-agent-tools.js'; import { guidanceTools } from './mcp-tools/guidance-tools.js'; import { autopilotTools } from './mcp-tools/autopilot-tools.js'; +// ADR-150 — MetaHarness MCP tools (score / genome / mcp-scan / threat-model / oia-audit) +import { metaharnessTools } from './mcp-tools/metaharness-tools.js'; // #1916: coverage-aware routing tools — defined in ruvector/coverage-tools.ts // but were never registered, so the `ruflo hooks coverage-*` CLI subcommands // failed with `Tool not found: hooks_coverage-route`. @@ -130,6 +132,8 @@ registerTools([ ...autopilotTools, // #1916: coverage-aware routing (hooks_coverage-route / -suggest / -gaps) ...coverageRouterTools, + // ADR-150 — MetaHarness static-analysis tools (5) + ...metaharnessTools, ]); /** diff --git a/v3/@claude-flow/cli/src/mcp-tools/index.ts b/v3/@claude-flow/cli/src/mcp-tools/index.ts index 8491b7759f..6b67b6fc84 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/index.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/index.ts @@ -25,3 +25,5 @@ export { wasmAgentTools } from './wasm-agent-tools.js'; export { ruvllmWasmTools } from './ruvllm-tools.js'; export { guidanceTools } from './guidance-tools.js'; export { autopilotTools } from './autopilot-tools.js'; +// ADR-150 — MetaHarness MCP tools (score / genome / mcp-scan / threat-model / oia-audit) +export { metaharnessTools } from './metaharness-tools.js'; diff --git a/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts new file mode 100644 index 0000000000..8a32b0ee1d --- /dev/null +++ b/v3/@claude-flow/cli/src/mcp-tools/metaharness-tools.ts @@ -0,0 +1,191 @@ +/** + * MetaHarness MCP Tools — ADR-150 Phase-2 deep-integration surface. + * + * Exposes the static-analysis MetaHarness CLIs as first-class MCP tools + * so Claude Code agents can call them programmatically without shelling + * out themselves. Five tools, all read-only / subprocess-isolated: + * + * - metaharness_score 5-dim readiness scorecard + * - metaharness_genome 7-section categorical report + * - metaharness_mcp_scan static MCP security findings + * - metaharness_threat_model enterprise-grade threat model + * - metaharness_oia_audit composite audit (score + threat + mcp) → memory + * + * Every tool resolves the corresponding plugin script + * (`plugins/ruflo-metaharness/scripts/.mjs`) via the same locator + * the commands/metaharness.ts dispatcher uses, then spawns it with + * `--format json` and parses the response. + * + * ADR-150 ARCHITECTURAL CONSTRAINT + * -------------------------------- + * This file has ZERO static `@metaharness/*` imports. All metaharness + * invocation stays in the plugin scripts behind the `_harness.mjs` + * subprocess bridge. When the plugin scripts aren't reachable at + * runtime, each tool returns a structured `{ degraded: true }` payload + * — never throws. + * + * @module @claude-flow/cli/mcp-tools/metaharness + */ + +import type { MCPTool, getProjectCwd as _ } from './types.js'; +import { getProjectCwd } from './types.js'; +import { spawn } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { join, dirname, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +/** + * Walk up from this module to find plugins/ruflo-metaharness/scripts/. + * Handles three install layouts (mirrors commands/metaharness.ts). + */ +function locatePluginScripts(): string | null { + const candidates: string[] = []; + let p = resolve(__dirname); + for (let i = 0; i < 8; i++) { + candidates.push(join(p, 'plugins', 'ruflo-metaharness', 'scripts')); + candidates.push(join(p, '..', 'plugins', 'ruflo-metaharness', 'scripts')); + p = dirname(p); + } + const cwd = getProjectCwd(); + candidates.push(join(cwd, 'plugins', 'ruflo-metaharness', 'scripts')); + candidates.push(join(cwd, 'node_modules', '@claude-flow', 'cli', 'plugins', 'ruflo-metaharness', 'scripts')); + for (const c of candidates) { + if (existsSync(join(c, '_harness.mjs'))) return c; + } + return null; +} + +function runScript(scriptName: string, args: string[]): Promise<{ exitCode: number; stdout: string; json: unknown; degraded: boolean }> { + return new Promise((resolve) => { + const dir = locatePluginScripts(); + if (!dir) { + resolve({ + exitCode: 0, stdout: '', json: { degraded: true, reason: 'plugin-not-found' }, degraded: true, + }); + return; + } + const scriptPath = join(dir, scriptName); + const argv = [...args]; + if (!argv.includes('--format')) argv.push('--format', 'json'); + const p = spawn('node', [scriptPath, ...argv], { + stdio: ['ignore', 'pipe', 'pipe'], + env: process.env, + }); + let stdout = ''; + p.stdout?.on('data', (d) => { stdout += d.toString(); }); + p.stderr?.on('data', () => { /* swallow — graceful */ }); + const timer = setTimeout(() => { try { p.kill('SIGTERM'); } catch { /* ignore */ } }, 120_000); + p.on('close', (code) => { + clearTimeout(timer); + let json: unknown = null; + const m = /\{[\s\S]*\}/.exec(stdout); + if (m) { try { json = JSON.parse(m[0]); } catch { /* leave null */ } } + const looksDegraded = !!(json && typeof json === 'object' && (json as { degraded?: unknown }).degraded === true); + resolve({ exitCode: code ?? 0, stdout, json, degraded: looksDegraded }); + }); + p.on('error', () => { + clearTimeout(timer); + resolve({ exitCode: 127, stdout, json: { degraded: true, reason: 'spawn-failed' }, degraded: true }); + }); + }); +} + +export const metaharnessTools: MCPTool[] = [ + { + name: 'metaharness_score', + description: 'ADR-150 — 5-dimension harness readiness scorecard from `metaharness score ` (harnessFit / compileConfidence / taskCoverage / toolSafety / memoryUsefulness + estCostPerRunUsd). Pure-read subprocess; graceful degradation when metaharness optional dep absent. Use BEFORE recommending the user run `ruflo metaharness mint` so you have an evidence-based readiness signal.', + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + path: { type: 'string', description: 'Repo path to score (default: cwd)', default: '.' }, + alertOnFitBelow: { type: 'number', description: 'Set to make the tool flag harnessFit < N (informational only; tool result has alert.triggered field)' }, + }, + }, + handler: async (input) => { + const path = (input.path as string) || '.'; + const args = ['--path', path]; + if (input.alertOnFitBelow !== undefined) args.push('--alert-on-fit-below', String(input.alertOnFitBelow)); + const r = await runScript('score.mjs', args); + return { success: !r.degraded, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_genome', + description: 'ADR-150 — 7-section categorical readiness report from `metaharness genome ` (repo_type / agent_topology / risk_score / mcp_surface / test_confidence / publish_readiness). Pairs with metaharness_score for the full readiness view — score is numeric, genome is categorical.', + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + path: { type: 'string', description: 'Repo path to analyze (default: cwd)', default: '.' }, + alertOnRiskAbove: { type: 'number', description: 'Set to flag risk_score > N' }, + }, + }, + handler: async (input) => { + const path = (input.path as string) || '.'; + const args = ['--path', path]; + if (input.alertOnRiskAbove !== undefined) args.push('--alert-on-risk-above', String(input.alertOnRiskAbove)); + const r = await runScript('genome.mjs', args); + return { success: !r.degraded, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_mcp_scan', + description: 'ADR-150 — static security scan of `.mcp/servers.json` + `.harness/claims.json` via `harness mcp-scan `. Reads only; no dispatch. Use before exposing a new MCP server config to humans/agents.', + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + path: { type: 'string', description: 'Repo path with .mcp/servers.json (default: cwd)', default: '.' }, + failOn: { type: 'string', enum: ['low', 'medium', 'high'], description: 'Severity floor for tool.alert.triggered (default: high)', default: 'high' }, + }, + }, + handler: async (input) => { + const path = (input.path as string) || '.'; + const failOn = (input.failOn as string) || 'high'; + const r = await runScript('mcp-scan.mjs', ['--path', path, '--fail-on', failOn]); + return { success: !r.degraded, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_threat_model', + description: 'ADR-150 — enterprise-grade threat model from `harness threat-model `. Returns worst-severity verdict (clean/low/medium/high) + categorized findings suitable for sharing with infosec.', + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + path: { type: 'string', description: 'Repo path (default: cwd)', default: '.' }, + failOn: { type: 'string', enum: ['clean', 'low', 'medium', 'high'], description: 'Severity floor for tool.alert.triggered (default: high)', default: 'high' }, + }, + }, + handler: async (input) => { + const path = (input.path as string) || '.'; + const failOn = (input.failOn as string) || 'high'; + const r = await runScript('threat-model.mjs', ['--path', path, '--fail-on', failOn]); + return { success: !r.degraded, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, + { + name: 'metaharness_oia_audit', + description: 'ADR-150 — composite weekly audit. Bundles oia-manifest + threat-model + mcp-scan into one timestamped record persisted to `metaharness-audit` memory namespace (or --dry-run to skip persistence). Designed for periodic drift detection.', + category: 'metaharness', + inputSchema: { + type: 'object', + properties: { + path: { type: 'string', description: 'Repo path (default: cwd)', default: '.' }, + dryRun: { type: 'boolean', description: 'Skip memory persistence — local-only run', default: false }, + alertOnWorst: { type: 'string', enum: ['clean', 'low', 'medium', 'high'], description: 'Composite worst-severity floor for tool.alert.triggered' }, + }, + }, + handler: async (input) => { + const path = (input.path as string) || '.'; + const args = ['--path', path]; + if (input.dryRun === true) args.push('--dry-run'); + if (input.alertOnWorst !== undefined) args.push('--alert-on-worst', String(input.alertOnWorst)); + const r = await runScript('oia-audit.mjs', args); + return { success: !r.degraded, data: r.json, degraded: r.degraded, exitCode: r.exitCode }; + }, + }, +];