fix: address Copilot review round 3 — typed consts, null admission, preset coverage

Three findings (one inline, two in the suppressed block), all valid.

1. `toConstOptions` still stringified every non-null `const`, so
   `items: { anyOf: [{ const: 1 }, { const: 2 }] }` submitted `["1"]` where
   the schema says `[1]`. Same wrong-type-on-the-wire problem that keeps a
   numeric `enum` off the select path, so it gets the same answer: only a
   string `const` is selectable, anything else falls to the JSON editor
   where the value keeps its type.

2. `hasMissingRequiredFields` derived null admission from the *renderer's*
   collapse, which is narrower on purpose — it only flattens a two-member
   union. So `anyOf: [string, number, null]` renders through the JSON
   fallback (where `null` is typeable) yet was still marked missing,
   disabling submit on a value the schema accepts. Null admission is a
   validity question, not a rendering one, so it is now its own predicate:
   `admitsNull` in core, recognizing `nullable`, `type: "null"`,
   `type: [..., "null"]`, and a null branch anywhere in an `anyOf`/`oneOf`
   of any size.

3. Nothing exercised the `record_shipment` preset — the web and TUI tests
   build the `anyOf` shape by hand, which verifies the renderers but
   *assumes* the premise the whole fix rests on: that Zod's `.nullish()`
   emits `anyOf: [<branch>, { type: "null" }]` with the enum on the branch.
   A Zod output change or a registry typo would leave every unit test green
   while the documented reproduction server quietly stopped reproducing.
   Added an integration test that boots the preset over HTTP, asserts the
   wire shape, and runs it back through the collapse — following the
   `duplicate-tool-names.test.ts` precedent, which exists for the same
   reason (#1957).

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-15 18:15:38 -04:00
commit 34ba8256a4
2 changed files with 271 additions and 10 deletions

View file

@ -1,5 +1,8 @@
import { describe, it, expect } from "vitest";
import { normalizeNullableUnion } from "@inspector/core/json/nullableUnion.js";
import {
admitsNull,
normalizeNullableUnion,
} from "@inspector/core/json/nullableUnion.js";
// The two JSON Schema encodings of "this type, or null". Both form builders —
// the web `SchemaForm` and the TUI's `schemaToForm` — dispatch on a single
@ -281,3 +284,48 @@ describe("normalizeNullableUnion", () => {
expect(normalizeNullableUnion(schema)).toBe(schema);
});
});
// `admitsNull` answers a *validity* question and is deliberately decoupled from
// the collapse, which answers a narrower *rendering* one. A schema can admit
// null while being unrenderable as a single widget, and a form that conflated
// the two would reject a value its own schema accepts.
describe("admitsNull", () => {
it("recognizes every encoding the collapse handles", () => {
expect(admitsNull({ anyOf: [{ type: "string" }, { type: "null" }] })).toBe(
true,
);
expect(admitsNull({ type: ["string", "null"] })).toBe(true);
expect(admitsNull({ type: "null" })).toBe(true);
expect(admitsNull({ nullable: true })).toBe(true);
});
it("recognizes a null branch the collapse declines to flatten", () => {
// Three members, so `normalizeNullableUnion` leaves this alone and it
// renders through the JSON fallback — where a user can still enter `null`.
const wide = {
anyOf: [{ type: "string" }, { type: "number" }, { type: "null" }],
};
expect(normalizeNullableUnion(wide)).toBe(wide);
expect(admitsNull(wide)).toBe(true);
});
it("recognizes a null branch in a oneOf, and a nested type array", () => {
expect(admitsNull({ oneOf: [{ type: "string" }, { type: "null" }] })).toBe(
true,
);
expect(admitsNull({ anyOf: [{ type: ["string", "null"] }] })).toBe(true);
});
it("is false for a schema that does not permit null", () => {
expect(admitsNull({ type: "string" })).toBe(false);
expect(admitsNull({ type: ["string", "number"] })).toBe(false);
expect(
admitsNull({ anyOf: [{ type: "string" }, { type: "number" }] }),
).toBe(false);
expect(admitsNull({})).toBe(false);
});
it("ignores non-object anyOf members instead of throwing", () => {
expect(admitsNull({ anyOf: ["null", ["null"], 7] })).toBe(false);
});
});

View file

@ -0,0 +1,122 @@
import { describe, it, expect, afterEach } from "vitest";
import { InspectorClient } from "@inspector/core/mcp/inspectorClient.js";
import { createTransportNode } from "@inspector/core/mcp/node/transport.js";
import { normalizeNullableUnion } from "@inspector/core/json/nullableUnion.js";
import { toFormSchema } from "../../../utils/jsonUtils";
import {
createTestServerHttp,
type TestServerHttp,
createTestServerInfo,
createNullableFieldsTool,
} from "@modelcontextprotocol/inspector-test-server";
/**
* Live coverage of the `record_shipment` preset behind
* `test-servers/configs/nullable-fields-http.json` — the documented manual
* reproduction for #1928.
*
* The unit tests on both form builders construct the `anyOf` shape by hand,
* which verifies the *renderers* but assumes the premise: that Zod's
* `.nullish()` actually emits `anyOf: [<branch>, { type: "null" }]` with the
* `enum` on the branch. That premise is the whole reason the fix exists, and
* nothing else pins it — a Zod change in how it compiles a nullish enum, or a
* typo in the preset registry, would leave every unit test green while the
* showcase server quietly stopped reproducing the bug.
*
* So this asserts the wire shape end to end, then runs it through the same
* collapse the forms use.
*/
describe("nullable argument schemas over the wire (#1928)", () => {
let client: InspectorClient | null = null;
let server: TestServerHttp | null = null;
afterEach(async () => {
if (client) {
try {
await client.disconnect();
} catch {
// ignore
}
client = null;
}
if (server) {
try {
await server.stop();
} catch {
// ignore
}
server = null;
}
});
async function connectToPreset(): Promise<InspectorClient> {
const started = createTestServerHttp({
serverInfo: createTestServerInfo("nullable-fields-test", "1.0.0"),
tools: [createNullableFieldsTool()],
});
await started.start();
server = started;
const connected = new InspectorClient(
{ type: "streamable-http", url: started.url },
{ environment: { transport: createTransportNode } },
);
await connected.connect();
client = connected;
return connected;
}
it("emits an anyOf-with-null for every nullish argument, enum on the branch", async () => {
const connected = await connectToPreset();
const { tools } = await connected.listAllTools();
const tool = tools.find((entry) => entry.name === "record_shipment");
expect(tool).toBeDefined();
const schema = toFormSchema(tool?.inputSchema);
const properties = schema?.properties ?? {};
expect(Object.keys(properties).sort()).toEqual([
"direction",
"express",
"quantity",
"reference",
]);
// The premise of the whole fix: no top-level `type`, and the `enum` sits on
// the surviving branch rather than beside it.
const direction = properties.direction;
expect(direction?.type).toBeUndefined();
expect(direction?.enum).toBeUndefined();
expect(direction?.anyOf).toHaveLength(2);
expect(direction?.anyOf).toEqual(
expect.arrayContaining([expect.objectContaining({ type: "null" })]),
);
});
it("collapses each argument to the type its widget dispatches on", async () => {
const connected = await connectToPreset();
const { tools } = await connected.listAllTools();
const schema = toFormSchema(
tools.find((entry) => entry.name === "record_shipment")?.inputSchema,
);
const properties = schema?.properties ?? {};
const collapsed = Object.fromEntries(
Object.entries(properties).map(([name, propertySchema]) => [
name,
normalizeNullableUnion(propertySchema),
]),
);
expect(collapsed.direction?.type).toBe("string");
expect(collapsed.direction?.enum).toEqual(["envio", "recebimento"]);
expect(collapsed.reference?.type).toBe("string");
expect(collapsed.quantity?.type).toBe("integer");
expect(collapsed.express?.type).toBe("boolean");
for (const propertySchema of Object.values(collapsed)) {
expect(propertySchema?.nullable).toBe(true);
expect(propertySchema?.anyOf).toBeUndefined();
}
});
});