Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor(mcp): remove obsolete JSON Schema loading path (#1663)

Michael D'Angelo779f4bf37f4c
diff --git a/plugins/codex-security/mcp-app/src/artifact-schema-loader.ts b/plugins/codex-security/mcp-app/src/artifact-schema-loader.ts
index 1f7e06dc2..e3895733a 100644
--- a/plugins/codex-security/mcp-app/src/artifact-schema-loader.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-schema-loader.ts
@@ -2,117 +2,6 @@ import * as z from "zod/v4";
export type ArtifactSchemaObject = Record<string, unknown>;
-export type SchemaDocument = ArtifactSchemaObject & {
- $id: string;
- $defs: Record<string, unknown>;
-};
-
-/**
- * Bundle checked-in Draft 2020-12 definitions for the existing MCP registry.
- */
-export function bundleArtifactSchema(
- documents: readonly SchemaDocument[],
- documentId: string,
- definition: string,
-): ArtifactSchemaObject {
- const documentsById = new Map<string, SchemaDocument>();
- for (const document of documents) {
- if (documentsById.has(document.$id)) {
- throw new Error(
- "Duplicate Codex Security schema document: " + document.$id,
- );
- }
- documentsById.set(document.$id, document);
- }
-
- const document = documentsById.get(documentId);
- if (!document) {
- throw new Error("Unknown Codex Security schema document: " + documentId);
- }
- const source = document.$defs[definition];
- if (!source || typeof source !== "object" || Array.isArray(source)) {
- throw new Error("Unknown Codex Security schema definition: " + definition);
- }
-
- const activeReferences = new Set<string>();
-
- function dereferenceArtifactSchema(
- value: unknown,
- document: SchemaDocument,
- ): unknown {
- if (Array.isArray(value)) {
- return value.map((item) => dereferenceArtifactSchema(item, document));
- }
- if (!value || typeof value !== "object") return value;
-
- const object = value as ArtifactSchemaObject;
- if (typeof object.$ref !== "string") {
- return Object.fromEntries(
- Object.entries(object).map(([name, child]) => [
- name,
- dereferenceArtifactSchema(child, document),
- ]),
- );
- }
-
- const reference = object.$ref;
- const separator = reference.indexOf("#");
- const referencedDocumentId =
- separator < 0 ? reference : reference.slice(0, separator);
- const pointer = separator < 0 ? "" : reference.slice(separator + 1);
- const referencedDocument = referencedDocumentId
- ? documentsById.get(referencedDocumentId)
- : document;
- if (!referencedDocument) {
- throw new Error("Unknown Codex Security schema reference: " + reference);
- }
-
- const referenceKey = referencedDocument.$id + "#" + pointer;
- if (activeReferences.has(referenceKey)) {
- throw new Error("Cyclic Codex Security schema reference: " + reference);
- }
- activeReferences.add(referenceKey);
- const resolved = dereferenceArtifactSchema(
- readSchemaPointer(referencedDocument, pointer),
- referencedDocument,
- );
- // A recursive failure exits this bundle. Siblings use the parent path.
- activeReferences.delete(referenceKey);
- const siblings = Object.fromEntries(
- Object.entries(object).filter(([name]) => name !== "$ref"),
- );
- if (Object.keys(siblings).length === 0) return resolved;
- if (!resolved || typeof resolved !== "object" || Array.isArray(resolved)) {
- throw new Error(
- "Codex Security schema reference cannot have sibling fields: " +
- reference,
- );
- }
- return {
- ...resolved,
- ...(dereferenceArtifactSchema(
- siblings,
- document,
- ) as ArtifactSchemaObject),
- };
- }
-
- return dereferenceArtifactSchema(source, document) as ArtifactSchemaObject;
-}
-
-/**
- * Keep checked-in JSON Schema, tool validation, and tools/list in agreement.
- */
-export function loadArtifactZodSchema(
- documents: readonly SchemaDocument[],
- documentId: string,
- definition: string,
-): z.ZodType {
- const schema = bundleArtifactSchema(documents, documentId, definition);
- const validator = z.fromJSONSchema(schema as z.core.JSONSchema.JSONSchema);
- return validator.meta(modelFacingSchema(schema));
-}
-
/** Project a native contract for tools/list while preserving its validator and type. */
export function projectArtifactToolSchema<Schema extends z.ZodType>(
schema: Schema,
@@ -178,20 +67,3 @@ function forbiddenProperty(property: unknown): boolean {
Object.keys(not).length === 0
);
}
-
-function readSchemaPointer(document: SchemaDocument, pointer: string): unknown {
- if (!pointer) return document;
- if (!pointer.startsWith("/")) {
- throw new Error("Invalid Codex Security schema pointer: #" + pointer);
- }
-
- let value: unknown = document;
- for (const encoded of pointer.slice(1).split("/")) {
- const key = encoded.replaceAll("~1", "/").replaceAll("~0", "~");
- if (!value || typeof value !== "object" || !Object.hasOwn(value, key)) {
- throw new Error("Unknown Codex Security schema pointer: #" + pointer);
- }
- value = (value as ArtifactSchemaObject)[key];
- }
- return value;
-}
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_schema_loader.ts b/plugins/codex-security/mcp-app/tests/test_artifact_schema_loader.ts
index 1d87559a0..d574c8423 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_schema_loader.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_schema_loader.ts
@@ -1,9 +1,10 @@
import assert from "node:assert/strict";
import { join } from "node:path";
import { Ajv2020 } from "ajv/dist/2020.js";
+import * as z from "zod/v4";
import { importSource } from "./import-module.ts";
-const { bundleArtifactSchema, loadArtifactZodSchema } = await importSource(
+const { projectArtifactToolSchema } = await importSource(
join(import.meta.dirname, "../src/artifact-schema-loader.ts"),
);
@@ -47,10 +48,10 @@ const document = {
],
},
},
-};
+} satisfies z.core.JSONSchema.JSONSchema;
-const original = bundleArtifactSchema([document], document.$id, "input");
-const validator = loadArtifactZodSchema([document], document.$id, "input");
+const original = structuredClone(document.$defs.input);
+const validator = projectArtifactToolSchema(z.fromJSONSchema(original));
const advertised = validator.toJSONSchema({ io: "input" });
assert.equal(original.properties.scope.properties.includePaths, false);
assert.equal(
@@ -100,3 +101,23 @@ for (const [input, expected] of [
`runtime: ${label}`,
);
}
+
+// Keep examples in the native JSON-schema input that the projector visits.
+const nativeFixture = z
+ .object({
+ findings: z.array(z.looseObject({ title: z.string() })),
+ })
+ .meta({ examples: structuredClone(document.$defs.input.examples) });
+const nativeInputBefore = structuredClone(
+ z.toJSONSchema(nativeFixture, { io: "input" }),
+);
+assert.deepEqual(nativeInputBefore.examples, document.$defs.input.examples);
+const nativeProjected = projectArtifactToolSchema(nativeFixture);
+assert.deepEqual(
+ nativeProjected.toJSONSchema({ io: "input" }).examples,
+ document.$defs.input.examples,
+);
+assert.deepEqual(
+ z.toJSONSchema(nativeFixture, { io: "input" }),
+ nativeInputBefore,
+);