OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(mcp): reuse SDK validation and protocol test helpers (#1498)
mldangelo-oai2d51ca6a4c1b
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.tsindex f9c42a2d3..367d29602 100644--- a/plugins/codex-security/mcp-app/server.ts+++ b/plugins/codex-security/mcp-app/server.ts@@ -838,19 +838,10 @@ export function createCodexSecurityServer(): McpServer {"Accepted user input did not contain structured answers.",);}- const answers: Record<string, string> = {};- for (const question of questions) {- const answer = result.content[question.id];- if (- typeof answer !== "string" ||- !question.options.some((option) => option.label === answer)- ) {- throw new Error(- `User input did not contain a valid answer for ${question.id}.`,- );- }- answers[question.id] = answer;- }+ // elicitInput validates accepted content against requestedSchema.+ const answers = Object.fromEntries(+ questions.map(({ id }) => [id, result.content![id] as string]),+ );return userInputToolResult("accepted", answers);} catch (error) {if (signal?.aborted) throw error;diff --git a/plugins/codex-security/mcp-app/tests/assertions.ts b/plugins/codex-security/mcp-app/tests/assertions.tsindex abb186e4f..6a027a643 100644--- a/plugins/codex-security/mcp-app/tests/assertions.ts+++ b/plugins/codex-security/mcp-app/tests/assertions.ts@@ -7,9 +7,10 @@ interface ToolResponse {export function assertNoError(response: ToolResponse) {assert.equal(response.error, undefined, response.error?.message!);- assert.equal(- response.result?.isError,- undefined,+ assert.ok(response.result, "Expected a JSON-RPC result");+ assert.notEqual(+ response.result.isError,+ true,response.result?.content?.map((item) => item.text).join(" ")!,);}diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts b/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.tsindex 96f3eb8a4..879e8de59 100644--- a/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts+++ b/plugins/codex-security/mcp-app/tests/deep_scan_coordinator_fixture.ts@@ -36,7 +36,7 @@ export const {export * from "./registry.ts";export { classifyCodexWorkerError } from "./errors.ts";`,- resolveDir: new URL("../src/deep-scan/", import.meta.url).pathname,+ resolveDir: path.join(import.meta.dirname, "../src/deep-scan/"),},loader: { ".md": "text" },});diff --git a/plugins/codex-security/mcp-app/tests/support/mcp-client.ts b/plugins/codex-security/mcp-app/tests/support/mcp-client.tsnew file mode 100644index 000000000..e1f38e989--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/support/mcp-client.ts@@ -0,0 +1,25 @@+import { Client } from "@modelcontextprotocol/sdk/client/index.js";+import {+ StdioClientTransport,+ type StdioServerParameters,+} from "@modelcontextprotocol/sdk/client/stdio.js";+import { ResultSchema } from "@modelcontextprotocol/sdk/types.js";++export async function startMcpClient(+ parameters: StdioServerParameters,+ name: string,+) {+ const client = new Client({ name, version: "1.0.0" });+ try {+ await client.connect(new StdioClientTransport(parameters));+ } catch (error) {+ await client.close();+ throw error;+ }+ return {+ request(method: string, params: Record<string, unknown>) {+ return client.request({ method, params }, ResultSchema);+ },+ close: () => client.close(),+ };+}diff --git a/plugins/codex-security/mcp-app/tests/support/rpc-server.ts b/plugins/codex-security/mcp-app/tests/support/rpc-server.tsnew file mode 100644index 000000000..dbb910827--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/support/rpc-server.ts@@ -0,0 +1,127 @@+import { spawn } from "node:child_process";+import { EventEmitter, once } from "node:events";+import { setTimeout as delay } from "node:timers/promises";+import { consumeStreamLines, writeMessage } from "./streams.ts";++export function startRpcServer(+ parameters: {+ command: string;+ args?: string[];+ cwd?: string;+ env?: NodeJS.ProcessEnv;+ stderr?: "pipe" | "inherit";+ },+ options: {+ component?: string;+ timeoutMs?: number;+ terminateOnStop?: boolean;+ } = {},+) {+ const child = spawn(parameters.command, parameters.args ?? [], {+ cwd: parameters.cwd,+ env: parameters.env,+ stdio: ["pipe", "pipe", parameters.stderr ?? "pipe"],+ });+ const events = new EventEmitter();+ const messages: ReturnType<typeof JSON.parse>[] = [];+ const stderrLines: string[] = [];+ const stderrEvents: ReturnType<typeof JSON.parse>[] = [];+ let failure: Error | undefined;+ const fail = (error: Error) => {+ failure ??= error;+ events.emit("message");+ };+ child.on("error", fail);+ child.stdin!.on("error", fail);+ child.on("close", (code, signal) =>+ fail(+ new Error(+ `MCP server exited with ${signal ?? code} while waiting for a response.`,+ ),+ ),+ );+ consumeStreamLines(child.stdout!, (line) => {+ messages.push(JSON.parse(line));+ events.emit("message");+ });+ if (child.stderr)+ consumeStreamLines(child.stderr, (line) => {+ stderrLines.push(line);+ try {+ const event = JSON.parse(line);+ if (event.component === options.component) stderrEvents.push(event);+ } catch {}+ });+ const send = (message: object) =>+ writeMessage({ stdin: child.stdin! }, { jsonrpc: "2.0", ...message });+ const response = (id: number) =>+ messages.find(+ (message) => message.id === id && message.method === undefined,+ );+ const waitForMessage = async (+ predicate: (message: ReturnType<typeof JSON.parse>) => boolean,+ description = "matching JSON-RPC message",+ timeoutMs = options.timeoutMs ?? 30_000,+ ) => {+ const signal = AbortSignal.timeout(timeoutMs);+ while (true) {+ const message = messages.find(predicate);+ if (message) return message;+ if (failure) throw failure;+ try {+ await once(events, "message", { signal });+ } catch (error) {+ throw new Error(`Timed out waiting for ${description}`, {+ cause: error,+ });+ }+ }+ };+ const waitForResponse = (id: number, timeoutMs?: number) =>+ waitForMessage(+ (message) => message.id === id && message.method === undefined,+ `JSON-RPC response ${id}`,+ timeoutMs,+ );+ const request = (id: number, method: string, params = {}) => {+ send({ id, method, params });+ return waitForResponse(id);+ };+ return {+ pid: child.pid,+ notify: (method: string, params = {}) => send({ method, params }),+ sendRequest: (id: number, method: string, params = {}) =>+ send({ id, method, params }),+ sendResponse: (id: string, result: unknown) => send({ id, result }),+ sendError: (id: string, code: number, message: string) =>+ send({ id, error: { code, message } }),+ request,+ waitForMessage,+ waitForResponse,+ response,+ initialize(name: string, capabilities: Record<string, unknown> = {}) {+ return request(1, "initialize", {+ protocolVersion: "2025-11-25",+ capabilities,+ clientInfo: { name, version: "0.1.0" },+ });+ },+ callTool: (id: number, params: Record<string, unknown>) =>+ request(id, "tools/call", params),+ stderrEvents: () => [...stderrEvents],+ stderrText: () => stderrLines.join("\n"),+ async stop() {+ if (child.exitCode !== null || child.signalCode !== null) return;+ const exited = new Promise((resolve) =>+ child.once(options.terminateOnStop ? "close" : "exit", resolve),+ );+ child.stdin!.end();+ if (options.terminateOnStop) child.kill();+ else {+ await Promise.race([exited, delay(2_000)]);+ child.kill("SIGKILL");+ }+ await exited;+ },+ };+}diff --git a/plugins/codex-security/mcp-app/tests/support/streams.ts b/plugins/codex-security/mcp-app/tests/support/streams.tsindex e56f991f4..91745ddfc 100644--- a/plugins/codex-security/mcp-app/tests/support/streams.ts+++ b/plugins/codex-security/mcp-app/tests/support/streams.ts@@ -1,7 +1,5 @@import type { ChildProcessWithoutNullStreams } from "node:child_process";import type { Readable } from "node:stream";-import { spawn } from "node:child_process";-import { setTimeout as delay } from "node:timers/promises";export function consumeStreamLines(stream: Readable,@@ -20,93 +18,10 @@ export function consumeStreamLines(});}-export function startServer(- serverPath: string,- env: NodeJS.ProcessEnv,- {- cwd,- component,- timeoutMessage,- }: {- cwd: string;- component: string;- timeoutMessage: (id: number) => string;- },-) {- const child = spawn(process.execPath, [serverPath, "--stdio"], {- cwd,- env,- stdio: ["pipe", "pipe", "pipe"],- });- const responses = new Map<number, ReturnType<typeof JSON.parse>>();- const waiters = new Map<- number,- (response: ReturnType<typeof JSON.parse>) => void- >();- const stderrEvents: ReturnType<typeof JSON.parse>[] = [];- const stderrLines: string[] = [];- consumeStreamLines(child.stdout, (line) => {- const response = JSON.parse(line);- responses.set(response.id, response);- waiters.get(response.id)?.(response);- waiters.delete(response.id);- });- consumeStreamLines(child.stderr, (line) => {- stderrLines.push(line);- try {- const event = JSON.parse(line);- if (event.component === component) stderrEvents.push(event);- } catch {- // Non-structured diagnostics remain available through stderrText.- }- });- return {- pid: child.pid,- notify(method: string, params = {}) {- writeMessage(child, { jsonrpc: "2.0", method, params });- },- sendRequest(id: number, method: string, params = {}) {- writeMessage(child, { jsonrpc: "2.0", id, method, params });- },- request(id: number, method: string, params = {}) {- this.sendRequest(id, method, params);- return this.waitForResponse(id);- },- async waitForResponse(id: number, timeoutMs = 15_000) {- const existing = responses.get(id);- if (existing) return existing;- const { promise, resolve, reject } =- Promise.withResolvers<ReturnType<typeof JSON.parse>>();- waiters.set(id, resolve);- const timer = setTimeout(- () => reject(new Error(timeoutMessage(id))),- timeoutMs,- );- return promise.finally(() => clearTimeout(timer));- },- stderrEvents() {- return [...stderrEvents];- },- stderrText() {- return stderrLines.join("\n");- },- response(id: number) {- return responses.get(id);- },- async stop() {- if (child.exitCode !== null || child.signalCode !== null) return;- const exited = new Promise((resolve) => child.once("exit", resolve));- child.stdin.end();- await Promise.race([exited, delay(2_000)]);- child.kill("SIGKILL");- await exited;- },- };-}-export function writeMessage(child: Pick<ChildProcessWithoutNullStreams, "stdin">,message: unknown,) {- child.stdin.write(`${JSON.stringify(message)}\n`);+ child.stdin.write(JSON.stringify(message));+ child.stdin.write("\n");}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.ts b/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.tsindex d12ac1d8c..c34e10812 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_attack_path.ts@@ -11,7 +11,7 @@ const {candidateAttackPathsInputSchema,recordCodexSecurityCandidateAttackPaths,} = await importSource(- new URL("../src/artifact-attack-path.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-attack-path.ts"),);const scanId = "11111111-1111-4111-8111-111111111111";diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.ts b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.tsindex 9d6cf90ec..96cb757c4 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer.ts@@ -15,7 +15,7 @@ const {getCodexSecurityDeepReducerInputs,recordCodexSecurityDeepReduction,} = await importSource(- new URL("../src/artifact-deep-reducer.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-deep-reducer.ts"),);const validReduction = reduction([]);diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.ts b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.tsindex ce5649058..9c4658bee 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_deep_reducer_pages.ts@@ -25,7 +25,7 @@ const {export * from "./artifact-deep-reducer-pages.ts";export * from "./artifact-deep-reducer.ts";`,- resolveDir: new URL("../src/", import.meta.url).pathname,+ resolveDir: path.join(import.meta.dirname, "../src/"),},});diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.tsindex 0927d4eb4..8720c5673 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts@@ -17,13 +17,13 @@ import path from "node:path";import { importSource } from "./import-module.ts";const io = await importSource(- new URL("../src/artifact-io.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-io.ts"),);const contextApi = await importSource(- new URL("../src/artifact-context.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-context.ts"),);const writerApi = await importSource(- new URL("../artifact-writer-main.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../artifact-writer-main.ts"),{plugins: [{@@ -40,7 +40,7 @@ const writerApi = await importSource(},);const schemas = await importSource(- new URL("../src/artifact-schema-loader.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-schema-loader.ts"),);const fixture = await realpath(await mkdtemp(path.join(tmpdir(), "codex-security-artifact-foundation-")),diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.tsindex 1ba22ef33..b2b98b350 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts@@ -10,7 +10,7 @@ const execFile = promisify(nodeExecFile);const temporaryDirectories = createTemporaryDirectories(true);const inventory = await importSource(- new URL("../src/artifact-inventory.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-inventory.ts"),);try {@@ -434,7 +434,7 @@ async function createFixture(label: string) {const repoRoot = path.join(fixtureRoot, "repository");const scanRoot = path.join(fixtureRoot, "scan");const workerRoot = path.join(fixtureRoot, "worker");- const pluginRoot = new URL("../../", import.meta.url).pathname;+ const pluginRoot = path.join(import.meta.dirname, "../../");await mkdir(repoRoot, { recursive: true });await mkdir(scanRoot, { recursive: true });return {diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.ts b/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.tsindex a12d9bc38..a4b0964d7 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_validation_phase.ts@@ -11,7 +11,7 @@ const {candidateValidationsInputSchema,recordCodexSecurityCandidateValidations,} = await importSource(- new URL("../src/artifact-validation-phase.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-validation-phase.ts"),);const toolSchema = await readJson(diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.tsindex 9a7ac50c0..110f7bbf8 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts@@ -8,8 +8,7 @@ import { importSource } from "./import-module.ts";const { validateDiscoveryArtifacts, validateReducerArtifacts } =await importSource(- new URL("../src/deep-scan/artifact-validation.ts", import.meta.url)- .pathname,+ path.join(import.meta.dirname, "../src/deep-scan/artifact-validation.ts"),);const otherScanId = "12c17317-9594-49e0-b06a-d72fd7e14bba";diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.tsindex 1a2a27397..4ce6e52e5 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts@@ -22,7 +22,7 @@ import { promisify } from "node:util";import { pathToFileURL } from "node:url";import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";-import * as streams from "./support/streams.ts";+import { startRpcServer } from "./support/rpc-server.ts";const execFileAsync = promisify(execFile);@@ -997,11 +997,16 @@ model_reasoning_summary = "none"}function startServer(serverPath: string, env: NodeJS.ProcessEnv) {- return streams.startServer(serverPath, env, {- cwd: pluginRoot,- component: "codex_security_deep_scan",- timeoutMessage: (id) => `Timed out waiting for JSON-RPC response ${id}.`,- });+ return startRpcServer(+ {+ command: process.execPath,+ args: [serverPath, "--stdio"],+ cwd: pluginRoot,+ env: env,+ stderr: "pipe",+ },+ { component: "codex_security_deep_scan", timeoutMs: 15_000 },+ );}function toolCall(diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.tsindex a88321441..188522809 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts@@ -9,7 +9,7 @@ import { importSource } from "./import-module.ts";import type { WorkbenchDeepScanStore as Store } from "../src/deep-scan/store.js";const { WorkbenchDeepScanStore, parseDeepScan } = await importSource(- new URL("../src/deep-scan/store.ts", import.meta.url).pathname,+ join(import.meta.dirname, "../src/deep-scan/store.ts"),);await testBeginProtocolAndParsing();diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.tsindex c3f54252e..0fd148fee 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_templates.ts@@ -1,9 +1,10 @@+import { join } from "node:path";import { readFile } from "node:fs/promises";import assert from "node:assert/strict";import { importSource } from "./import-module.ts";const { renderDedupPrompt, renderDiscoveryPrompt } = await importSource(- new URL("../src/deep-scan/templates.ts", import.meta.url).pathname,+ join(import.meta.dirname, "../src/deep-scan/templates.ts"),{ loader: { ".md": "text" } },);diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.tsindex d98947b24..1218c713c 100644--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts@@ -2,10 +2,10 @@ import { temporaryDirectory } from "./support/temporary-directories.ts";import { readJson, writeJson } from "./support/json.ts";import { gitText } from "../scripts/git.mjs";import { assertNoError } from "./assertions.ts";-import { consumeStreamLines, writeMessage } from "./support/streams.ts";+import { startRpcServer } from "./support/rpc-server.ts";import { readOnlyParentSandboxState } from "./sandbox-state.ts";import assert from "node:assert/strict";-import { execFileSync, spawn } from "node:child_process";+import { execFileSync } from "node:child_process";import { hash, randomUUID } from "node:crypto";import {chmod,@@ -187,88 +187,16 @@ function startTestServer({cwd: string;env?: NodeJS.ProcessEnv;}) {- const childEnvironment = { ...process.env, ...env };- for (const [name, value] of Object.entries(env)) {- if (value === undefined) {- delete childEnvironment[name];- }- }- const childProcess = spawn(command, args, {- cwd,- env: childEnvironment,- stdio: ["pipe", "pipe", "inherit"],- });- const responses: ReturnType<typeof JSON.parse>[] = [];- consumeStreamLines(childProcess.stdout, (line) =>- responses.push(JSON.parse(line)),- );-- return {- notify(method: string, params = {}) {- writeMessage(childProcess, { jsonrpc: "2.0", method, params });- },- sendRequest(id: number, method: string, params = {}) {- writeMessage(childProcess, { jsonrpc: "2.0", id, method, params });- },- sendResponse(id: string, result: unknown) {- writeMessage(childProcess, { jsonrpc: "2.0", id, result });- },- sendError(id: string, code: number, message: string) {- writeMessage(childProcess, {- jsonrpc: "2.0",- id,- error: { code, message },- });- },- async waitForMessage(- predicate: (message: ReturnType<typeof JSON.parse>) => boolean,- description = "matching JSON-RPC message",- ) {- const started = Date.now();- while (Date.now() - started < 30000) {- const message = responses.find(predicate);- if (message) return message;- await new Promise((resolve) => setTimeout(resolve, 10));- }- throw new Error(`Timed out waiting for ${description}`);- },- initialize(name: string, capabilities: Record<string, unknown> = {}) {- return this.requestAndWait(1, "initialize", {- protocolVersion: "2025-11-25",- capabilities,- clientInfo: { name, version: "0.1.0" },- });- },- callTool(id: number, params: Record<string, unknown>) {- return this.requestAndWait(id, "tools/call", params);- },- async requestAndWait(id: number, method: string, params = {}) {- writeMessage(childProcess, { jsonrpc: "2.0", id, method, params });- const started = Date.now();- while (Date.now() - started < 30000) {- const response = responses.find((candidate) => candidate.id === id);- if (response) return response;- if (childProcess.exitCode !== null) {- throw new Error(- `MCP server exited with code ${childProcess.exitCode} while waiting for JSON-RPC response ${id}`,- );- }- await new Promise((resolve) => setTimeout(resolve, 10));- }- throw new Error(`Timed out waiting for JSON-RPC response ${id}`);- },- async stop() {- if (childProcess.exitCode != null || childProcess.signalCode != null) {- return;- }- const closed = new Promise((resolve) => {- childProcess.once("close", resolve);- });- childProcess.stdin.end();- childProcess.kill();- await closed;+ return startRpcServer(+ {+ command,+ args,+ cwd,+ env: { ...process.env, ...env },+ stderr: "inherit",},- };+ { terminateOnStop: true },+ );}const testServer = startTestServer({@@ -281,7 +209,7 @@ const testServer = startTestServer({TMPDIR: scanRoot,},});-const requestAndWait = testServer.requestAndWait;+const request = testServer.request;async function assertBundledNodeLauncher() {const emptyPath = await temporaryDirectory("codex-security-empty-path-");@@ -330,7 +258,7 @@ async function assertBundledNodeLauncher() {assertNoError(await bundledNodeServer.initialize("codex-security-bundled-node-smoke"),);- assertNoError(await bundledNodeServer.requestAndWait(2, "tools/list"));+ assertNoError(await bundledNodeServer.request(2, "tools/list"));if (!windows) {assert.equal(await readFile(path.join(emptyPath, "bundled-node-used"), "utf8"),@@ -403,7 +331,7 @@ async function assertWorkbenchStdinFailureDoesNotCrashServer() {_meta: { "openai/threadId": "fixture-thread" },});assert.equal(response.result.isError, true);- assertNoError(await server.requestAndWait(3, "tools/list"));+ assertNoError(await server.request(3, "tools/list"));} finally {server.stop();await rm(helperRoot, { recursive: true, force: true });@@ -993,7 +921,7 @@ try {);assert.deepEqual(initialized.result.capabilities.logging, {});- const trustedAccessToolList = await requestAndWait(9600, "tools/list");+ const trustedAccessToolList = await request(9600, "tools/list");assertNoError(trustedAccessToolList);const trustedAccessTool = trustedAccessToolList.result.tools.find((tool: { name: string }) =>@@ -1376,7 +1304,7 @@ try {if (process.platform !== "win32") {await rm(launchCwd, { recursive: true, force: true });}- const toolList = await requestAndWait(2, "tools/list");+ const toolList = await request(2, "tools/list");assertNoError(toolList);for (const tool of toolList.result.tools) {assert.equal(tool._meta?.["openai/outputTemplate"], undefined);@@ -1553,11 +1481,16 @@ try {.description,"How should Codex Security handle the blocked preflight?",);+ assert.deepEqual(elicitationRequest.params.requestedSchema.required, [+ "concurrent_deep_scan",+ "preflight_action",+ ]);testServer.sendResponse(elicitationRequest.id, {action: "accept",content: {concurrent_deep_scan: "Cancel (Recommended)",preflight_action: "Leave paused",+ extra_answer: "Ignored",},});const userInputResponse = await testServer.waitForMessage(@@ -1596,75 +1529,57 @@ try {});assert.equal(invalidUserInput.result.isError, true);- testServer.sendRequest(9002, "tools/call", {- name: "request_codex_security_user_input",- arguments: {- questions: [- {- header: "Decline?",- id: "decline_request",- question: "Decline this Codex Security input request?",- options: continuationOptions,- },- ],- },- });- const declinedElicitation = await testServer.waitForMessage(- (message) =>- message.method === "elicitation/create" &&- message.params?.message.startsWith(- "Decline this Codex Security input request?",- ),- "declined Codex Security elicitation request",- );- assert.equal(- declinedElicitation.params.message,+ for (const [index, [reply, status]] of [+ [{ action: "decline" }, "declined"],+ [{ action: "cancel" }, "cancelled"],+ [{ action: "accept" }, "unavailable"],+ [{ action: "accept", content: null }, "unavailable"],+ [{ action: "accept", content: {} }, "unavailable"],+ [{ action: "accept", content: { choice: false } }, "unavailable"],+ [{ action: "accept", content: { choice: 0 } }, "unavailable"],+ [{ action: "accept", content: { choice: "" } }, "unavailable"],[- "Decline this Codex Security input request?",- "- Continue: Continue the current workflow.",- "- Cancel: Leave the current workflow paused.",- ].join("\n"),- );- testServer.sendResponse(declinedElicitation.id, { action: "decline" });- const declinedUserInput = await testServer.waitForMessage(- (message) => message.id === 9002,- "declined Codex Security user-input response",- );- assertNoError(declinedUserInput);- assert.deepEqual(declinedUserInput.result.structuredContent, {- status: "declined",- });-- testServer.sendRequest(9003, "tools/call", {- name: "request_codex_security_user_input",- arguments: {- questions: [- {- header: "Cancel?",- id: "cancel_request",- question: "Cancel this Codex Security input request?",- options: continuationOptions,- },- ],- },- });- const cancelledElicitation = await testServer.waitForMessage(- (message) =>- message.method === "elicitation/create" &&- message.params?.message.startsWith(- "Cancel this Codex Security input request?",- ),- "cancelled Codex Security elicitation request",- );- testServer.sendResponse(cancelledElicitation.id, { action: "cancel" });- const cancelledUserInput = await testServer.waitForMessage(- (message) => message.id === 9003,- "cancelled Codex Security user-input response",- );- assertNoError(cancelledUserInput);- assert.deepEqual(cancelledUserInput.result.structuredContent, {- status: "cancelled",- });+ { action: "accept", content: { choice: "Unknown option" } },+ "unavailable",+ ],+ ].entries()) {+ const requestId = 9002 + index;+ const question = `Choose an option for request ${requestId}?`;+ testServer.sendRequest(requestId, "tools/call", {+ name: "request_codex_security_user_input",+ arguments: {+ questions: [+ {+ header: "Choice",+ id: "choice",+ question,+ options: continuationOptions,+ },+ ],+ },+ });+ const elicitation = await testServer.waitForMessage(+ (message) =>+ message.method === "elicitation/create" &&+ message.params?.message.startsWith(question),+ `Codex Security elicitation request ${requestId}`,+ );+ assert.equal(+ elicitation.params.message,+ [+ question,+ "- Continue: Continue the current workflow.",+ "- Cancel: Leave the current workflow paused.",+ ].join("\n"),+ );+ testServer.sendResponse(elicitation.id, reply);+ const response = await testServer.waitForMessage(+ (message) => message.id === requestId,+ `Codex Security user-input response ${requestId}`,+ );+ assertNoError(response);+ assert.deepEqual(response.result.structuredContent, { status });+ }const start = toolList.result.tools.find((tool: { name: string }) => tool.name === "start_codex_security_scan",);diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.tsindex 99d675b97..ef780c2fe 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts@@ -96,11 +96,7 @@ test("opens a private WAL database at the configured state path", async () => {database.prepare("PRAGMA journal_mode").get()?.journal_mode,"wal",);- assert.equal(- database.prepare("SELECT COUNT(*) AS count FROM schema_migrations").get()- ?.count,- migrations.length,- );+ assertMigrationNames(database, ...migrations.map(({ version }) => version));assert.deepEqual(database.prepare("PRAGMA foreign_key_check").all(), []);if (process.platform !== "win32") {assert.equal((await stat(databasePath)).mode & 0o777, 0o600);@@ -239,11 +235,9 @@ test(database.prepare("SELECT value FROM retained").get()?.value,"original",);- assert.equal(- database- .prepare("SELECT MAX(version) AS version FROM schema_migrations")- .get()?.version,- migrations.at(-1)!.version,+ assertMigrationNames(+ database,+ ...migrations.map(({ version }) => version),);} finally {database.close();@@ -730,11 +724,9 @@ test("retries an upgrade when another process holds the write lock beyond the buawait once(writer, "message");const database = await openWorkbenchDatabase(databasePath);try {- assert.equal(- database- .prepare("SELECT COUNT(*) AS count FROM schema_migrations")- .get()?.count,- migrations.length,+ assertMigrationNames(+ database,+ ...migrations.map(({ version }) => version),);assert.equal(database.prepare("SELECT COUNT(*) AS count FROM security_targets").get()diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.tsindex 01bdf1792..3258212f0 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_state_fallback.ts@@ -1,4 +1,5 @@import { readJsonLines } from "./support/json.ts";+import { assertNoError } from "./assertions.ts";import assert from "node:assert/strict";import { execFileSync } from "node:child_process";import { randomUUID } from "node:crypto";@@ -16,7 +17,7 @@ import { tmpdir } from "node:os";import path from "node:path";import { applicationRoot as mcpAppRoot, buildServer } from "./build-server.ts";-import * as streams from "./support/streams.ts";+import { startRpcServer } from "./support/rpc-server.ts";if (process.platform !== "win32") {await testWorkbenchStateFallback();@@ -490,14 +491,15 @@ async function writeFakePython(executablePath: string) {}function startServer(serverPath: string, env: NodeJS.ProcessEnv) {- return streams.startServer(- serverPath,- { ...process.env, ...env },+ return startRpcServer({+ command: process.execPath,+ args: [serverPath, "--stdio"],cwd: path.dirname(path.dirname(serverPath)),- component: "codex_security_workbench",- timeoutMessage: (id) => `Timed out waiting for response ${id}`,+ env: { ...process.env, ...env },+ stderr: "pipe",},+ { component: "codex_security_workbench", timeoutMs: 15_000 },);}@@ -582,18 +584,6 @@ function reopenWorkspace(});}-function assertNoError(response: {- error?: { message?: string };- result?: { isError?: boolean; content?: { text?: string }[] };-}) {- assert.equal(response.error, undefined, response.error?.message!);- assert.equal(- response.result?.isError,- undefined,- response.result?.content?.[0]?.text!,- );-}-function assertToolError(response: {error?: unknown;diff --git a/sdk/typescript/tests-ts/compact-diff-scan.test.ts b/sdk/typescript/tests-ts/compact-diff-scan.test.tsindex 183407515..1b4cb0f14 100644--- a/sdk/typescript/tests-ts/compact-diff-scan.test.ts+++ b/sdk/typescript/tests-ts/compact-diff-scan.test.ts@@ -1,8 +1,8 @@import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js";import { git } from "./git-fixture.js";-import { initializeMcpClient } from "./support/mcp-client.js";+import { startMcpClient } from "../../../plugins/codex-security/mcp-app/tests/support/mcp-client.js";import { writeSource } from "./support/shell.js";-import { spawn, spawnSync } from "node:child_process";+import { spawnSync } from "node:child_process";import { createHash, randomUUID } from "node:crypto";import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { dirname, join } from "node:path";@@ -45,23 +45,18 @@ function candidate(path: string): JsonObject {}async function startMcp(root: string) {- const child = spawn(- process.execPath,- [join(PLUGIN_ROOT, "mcp", "server.mjs"), "--stdio"],+ const { request, close } = await startMcpClient({+ command: process.execPath,+ args: [join(PLUGIN_ROOT, "mcp", "server.mjs"), "--stdio"],env: {...process.env,CODEX_SECURITY_SCAN_ROOT: join(root, "scans"),CODEX_SECURITY_STATE_DIR: join(root, "state"),PYTHONDONTWRITEBYTECODE: "1",- },- stdio: ["pipe", "pipe", "pipe"],+ } as Record<string, string>,},- );- const { request, close } = await initializeMcpClient(- child,"compact-diff-test",- true,);return {diff --git a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.tsindex 3ccb6bc8b..75966a531 100644--- a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts+++ b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts@@ -1,11 +1,10 @@-import { spawn } from "node:child_process";import { join } from "node:path";import { describe, expect, test } from "bun:test";import Ajv, { type ValidateFunction } from "ajv";import Ajv2020 from "ajv/dist/2020.js";import { PLUGIN_ROOT } from "./plugin-root.js";import { readJson as readJsonFile } from "./support/json.js";-import { initializeMcpClient } from "./support/mcp-client.js";+import { startMcpClient } from "../../../plugins/codex-security/mcp-app/tests/support/mcp-client.js";type JsonObject = Record<string, unknown>;@@ -96,12 +95,14 @@ const stringAssessmentInput = {const readJson = readJsonFile<JsonObject>;async function startMcp() {- const child = spawn(- process.execPath,- [join(PLUGIN_ROOT, "mcp", "server.mjs"), "--stdio"],- { stdio: ["pipe", "pipe", "pipe"] },+ return startMcpClient(+ {+ command: process.execPath,+ args: [join(PLUGIN_ROOT, "mcp", "server.mjs"), "--stdio"],+ env: process.env as Record<string, string>,+ },+ "finding-detail-contract-test",);- return initializeMcpClient(child, "finding-detail-contract-test", false);}function expectScanDraftDetails(validate: ValidateFunction) {@@ -145,7 +146,11 @@ describe("bundled plugin finding detail contracts", () => {test("publishes the strict scan-draft contract through MCP", async () => {const client = await startMcp();try {- const result = await client.request("tools/list", {});+ const [result, concurrentResult] = await Promise.all([+ client.request("tools/list", {}),+ client.request("tools/list", {}),+ ]);+ expect(concurrentResult).toEqual(result);const tools = result["tools"] as Array<JsonObject>;const tool = tools.find((candidate) => candidate["name"] === "record_codex_security_scan_draft",diff --git a/sdk/typescript/tests-ts/scan-recipe.test.ts b/sdk/typescript/tests-ts/scan-recipe.test.tsindex a250afcc8..4caf3191a 100644--- a/sdk/typescript/tests-ts/scan-recipe.test.ts+++ b/sdk/typescript/tests-ts/scan-recipe.test.ts@@ -1,5 +1,5 @@import { createCliTest } from "./support/cli-run.js";-import { PLUGIN_ROOT } from "./plugin-root.js";+import { workbenchCommand } from "./support/workbench-command.js";import { mkdir, rm, writeFile } from "node:fs/promises";import { join } from "node:path";import { afterEach, expect, test } from "bun:test";@@ -39,12 +39,7 @@ test.each(["standard", "deep"])(CODEX_SECURITY_STATE_DIR: join(root, "state"),OPENAI_API_KEY: "synthetic-launch-key",};- const command = (args: readonly string[], input?: string) =>- runWorkbench(- { python, pluginRoot: PLUGIN_ROOT, environment },- args,- input,- );+ const command = workbenchCommand(python, () => environment);const { stderr, runCli } = createCliTest(main);const code = await runCli(diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.tsindex 1d4d2b578..cefabbb37 100644--- a/sdk/typescript/tests-ts/scan-recovery.test.ts+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts@@ -2,7 +2,7 @@ import { spawnSync } from "node:child_process";import { mkdir, readFile, writeFile } from "node:fs/promises";import { join } from "node:path";import { afterEach, describe, expect, test } from "bun:test";-import { runWorkbench } from "../src/runtime.js";+import { workbenchCommand } from "./support/workbench-command.js";import { runNodePython } from "./support/python-probe.js";import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";import { createApiTestFixtures } from "./support/temporary-directories.js";@@ -93,18 +93,7 @@ async function workbench(args: readonly string[],input?: string,) {- return runWorkbench(- {- python: fixture.python,- pluginRoot: PLUGIN_ROOT,- environment: {- PATH: process.env["PATH"],- CODEX_SECURITY_STATE_DIR: fixture.stateDir,- },- },- args,- input,- );+ return workbenchCommand(fixture.python, fixture.stateDir)(args, input);}async function startDraftScan(diff --git a/sdk/typescript/tests-ts/scan-resume.test.ts b/sdk/typescript/tests-ts/scan-resume.test.tsindex 7e5b2a232..85e6dea44 100644--- a/sdk/typescript/tests-ts/scan-resume.test.ts+++ b/sdk/typescript/tests-ts/scan-resume.test.ts@@ -1,4 +1,5 @@import { createCliTest } from "./support/cli-run.js";+import { workbenchCommand } from "./support/workbench-command.js";import { gitText } from "./support/shell.js";import { readJsonLines } from "./support/json.js";import { randomUUID } from "node:crypto";@@ -136,8 +137,7 @@ async function interruptedScan(? {}: { OPENAI_API_KEY: "synthetic-resume-key" }),};- const command = (args: readonly string[], input?: string) =>- runWorkbench({ python, pluginRoot: PLUGIN_ROOT, environment }, args, input);+ const command = workbenchCommand(python, () => environment);const recipe = {repository,target: { kind: "repository", paths: [] },@@ -843,12 +843,7 @@ test.each(["chatgpt", "api-key"] as const)(CODEX_SECURITY_STATE_DIR: join(root, "state"),OPENAI_API_KEY: "synthetic-launch-key",};- const command = (args: readonly string[], input?: string) =>- runWorkbench(- { python, pluginRoot: PLUGIN_ROOT, environment },- args,- input,- );+ const command = workbenchCommand(python, () => environment);const { stderr, runCli } = createCliTest(main);const code = await runCli(diff --git a/sdk/typescript/tests-ts/support/mcp-client.ts b/sdk/typescript/tests-ts/support/mcp-client.tsdeleted file mode 100644index 0a0ef0832..000000000--- a/sdk/typescript/tests-ts/support/mcp-client.ts+++ /dev/null@@ -1,69 +0,0 @@-import type { ChildProcessWithoutNullStreams } from "node:child_process";-import { createInterface } from "node:readline";-import { capture } from "../cli-fixtures.js";--type JsonObject = Record<string, unknown>;--export async function initializeMcpClient(- child: ChildProcessWithoutNullStreams,- clientName: string,- splitRequestWrites: boolean,-) {- const messages = createInterface({ input: child.stdout })[- Symbol.asyncIterator- ]();- const stderr = capture();- child.stderr.setEncoding("utf8");- child.stderr.on("data", stderr.stream.write);- let nextId = 0;-- async function request(- method: string,- params: JsonObject,- ): Promise<JsonObject> {- const id = ++nextId;- if (splitRequestWrites) {- child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }));- child.stdin.write("\n");- } else {- child.stdin.write(- `${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`,- );- }- while (true) {- const message = await messages.next();- if (message.done) {- throw new Error(`MCP server exited before replying: ${stderr.text()}`);- }- const response = JSON.parse(message.value) as JsonObject;- if (response["id"] !== id) continue;- if (response["error"] !== undefined) {- throw new Error(JSON.stringify(response["error"]));- }- return response["result"] as JsonObject;- }- }-- await request("initialize", {- protocolVersion: "2025-11-25",- capabilities: {},- clientInfo: { name: clientName, version: "1.0.0" },- });- child.stdin.write(- `${JSON.stringify({- jsonrpc: "2.0",- method: "notifications/initialized",- params: {},- })}\n`,- );-- return {- request,- async close(): Promise<void> {- child.stdin.end();- await new Promise<void>((resolve) => {- child.once("close", () => resolve());- });- },- };-}