fix(mcp): express inspect_analysis_view selector constraints in the contract schema (#1705)

The item view advertised its four selector alternatives independently of the
selected collection, while collection-specific constraints lived only in a
superRefine that does not survive JSON Schema conversion. Clients validating
against tools/list could not discover that modules accept only path/node_id
and sections/symbols accept only index/name.

Replace the refined object with a discriminated union on collection so the
converted draft-2020-12 schema rejects the same combinations the MCP server
rejects. Runtime selection semantics are unchanged.

Fixes #1695

Co-authored-by: Saim Shafique <sx4imm@gmail.com>
This commit is contained in:
Saim Shafique
2026-10-11 19:55:16 +08:00
committed by GitHub
co-authored by Saim Shafique
parent cdd9fe7167
commit a8a9c4d817
2 changed files with 124 additions and 37 deletions
@@ -460,6 +460,95 @@ it(
advertiseAndEnforceProcessEnvironmentKeyConstraint,
);
const advertiseAndEnforceItemSelectorCollectionConstraints =
async (): Promise<void> => {
const contract = TOOL_CONTRACTS.find(
({ name }) => name === "inspect_analysis_view",
);
if (contract === undefined)
throw new Error("inspect_analysis_view contract was not registered");
const server = new McpServer({
name: "item-selector-schema",
version: "0",
});
server.registerTool(
contract.name,
toolRegistrationOptions(contract),
async () => ({
content: [],
}),
);
const client = new Client({ name: "item-selector-schema", version: "0" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools.find(
({ name }) => name === contract.name,
);
if (advertised === undefined)
throw new Error("inspect_analysis_view tool was not advertised");
const validate = new Ajv2020({
strict: false,
validateFormats: false,
}).compile(advertised.inputSchema);
const source = {
kind: "retained-evidence",
evidence_id: `ev_${"a".repeat(64)}`,
};
const itemInput = (collection: string, selector: unknown) => ({
source,
view: { kind: "item", collection, selector },
});
const nodeId = `jag_node_${"b".repeat(64)}`;
// Collection-specific selector constraints must survive JSON Schema
// conversion: the advertised schema rejects exactly what runtime
// input validation rejects.
const invalid: ReadonlyArray<readonly [string, unknown]> = [
["modules", { index: 0 }],
["modules", { name: "entry" }],
["sections", { path: "/src/entry.js" }],
["symbols", { node_id: nodeId }],
];
const valid: ReadonlyArray<readonly [string, unknown]> = [
["modules", { path: "/src/entry.js" }],
["modules", { node_id: nodeId }],
["sections", { index: 0 }],
["sections", { name: ".text" }],
["symbols", { index: 0 }],
["symbols", { name: "main" }],
];
for (const [collection, selector] of invalid) {
const input = itemInput(collection, selector);
const label = `${collection}/${Object.keys(selector as object)[0]}`;
expect(contract.inputSchema.safeParse(input).success, label).toBe(
false,
);
expect(validate(input), `advertised schema rejects ${label}`).toBe(
false,
);
}
for (const [collection, selector] of valid) {
const input = itemInput(collection, selector);
const label = `${collection}/${Object.keys(selector as object)[0]}`;
expect(contract.inputSchema.safeParse(input).success, label).toBe(true);
expect(validate(input), `advertised schema accepts ${label}`).toBe(
true,
);
}
} finally {
await Promise.allSettled([client.close(), server.close()]);
}
};
it(
"advertises and enforces the item selector collection constraints",
advertiseAndEnforceItemSelectorCollectionConstraints,
);
describe("MCP process input JSON Schema", () => {
it("advertises process strings without weakening terminal input", async () => {
const server = new McpServer({