← Files Daybreak Defense FactoryARCHIVED FILE

desktop/local-mcp.mjs

1.91 KB · Nov 28, 2026 · 18:13 UTC

↓ Download file

See the change to this file →

import { spawn, spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { once } from "node:events";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";

import { relayDaybreakMcp } from "./scan-context-relay.mjs";

const digest =
  "5215668aefba782405e325b51451af819f621ab85321c39e65a9b6f437c8f20d";
const bytes = await readFile(new URL("./codex-security.tgz", import.meta.url));
if (createHash("sha256").update(bytes).digest("hex") !== digest) {
  throw new Error("The installed Codex Security runtime failed verification.");
}

const bundle = "node_modules/@openai/codex-security/_bundled_plugin";
const scratch = await mkdtemp(path.join(tmpdir(), "daybreak-local-mcp-"));
try {
  const extracted = spawnSync("tar", ["-xzf", "-", "-C", scratch, bundle], {
    cwd: scratch,
    env: { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot },
    input: bytes,
    windowsHide: true,
  });
  if (extracted.error || extracted.status !== 0) {
    throw new Error("Could not unpack the installed Codex Security runtime.");
  }
  const root = path.join(scratch, bundle);
  const server = spawn(
    process.execPath,
    [path.join(root, "mcp/server.mjs"), "--stdio"],
    {
      cwd: root,
      stdio: ["pipe", "pipe", "inherit"],
      windowsHide: true,
    },
  );
  const exit = once(server, "exit");
  const stop = () => server.kill();
  process.once("SIGINT", stop);
  process.once("SIGTERM", stop);
  try {
    const [[code]] = await Promise.all([
      exit,
      relayDaybreakMcp(process.stdin, process.stdout, server, root),
    ]);
    process.exitCode = code ?? 1;
  } catch (error) {
    server.kill();
    await exit.catch(() => undefined);
    throw error;
  } finally {
    process.removeListener("SIGINT", stop);
    process.removeListener("SIGTERM", stop);
  }
} finally {
  await rm(scratch, { recursive: true, force: true });
}

SHA-256: 73367d127cca340a189c079f2dd3d08e1ff18f34cbd07befd98bfe5fc0693dbd