Files
reverse-engineering-anything/tests/boundary/mcp/toolInputAdmission.test.ts
T
morluto 960a387731 refactor(contracts): own CLI command names, catalog identity and schema presentation in contracts (#11425)
* refactor(contracts): move the CLI command name owner into src/contracts

* refactor(contracts): own catalog identity in contracts and server identity in process (#11435)

* refactor(contracts): move catalog identity into contracts and server identity into process

* refactor(contracts): own the advertised MCP schema presentation in contracts

* test(mcp): import the advertised presentation from contracts in toolSchemaValidity

* test(release): expect the catalog identity at its contracts path in the canary

* test(mcp): import the catalog identity from contracts in syntheticContractSurfaces

* refactor(contracts): import CLI command names from contracts in the capability conformance test
2026-11-16 05:31:37 +08:00

99 lines
3.7 KiB
TypeScript

import type { CallToolResult } from "@modelcontextprotocol/server";
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
import { describe, expect } from "vitest";
import { mcpTest } from "../../support/mcp/mcpFixture.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { TOOL_CONTRACTS } from "../../../src/contracts/toolContracts.js";
import { EvidenceMcpServer } from "../../../src/server/EvidenceMcpServer.js";
import { createToolResultDelivery } from "../../../src/server/toolResult.js";
import { toolRegistrationOptions } from "../../../src/server/toolRegistrationOptions.js";
import { COMPACT_INPUT_SCHEMA_BUDGET_BYTES } from "../../../src/contracts/mcpSchemaPresentation.js";
// The server advertises its one (compact) input presentation.
const createServer = (): EvidenceMcpServer =>
new EvidenceMcpServer(
{ name: "tool-input-admission-test", version: "0" },
{ capabilities: {} },
undefined,
createToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE),
);
// These tests cover the registered SDK boundary with deterministic sentinel
// handlers. They establish canonical rejection before dispatch; they do not
// exercise providers, targets, or the shape of SDK-generated error text.
describe("catalog-wide MCP input admission", () => {
mcpTest(
"rejects unknown root keys before dispatch with the advertised presentation",
async ({ mcp }) => {
const server = createServer();
const dispatches = new Map<string, number>();
for (const contract of TOOL_CONTRACTS) {
server.registerTool(
contract.name,
toolRegistrationOptions(contract),
async (): Promise<CallToolResult> => {
dispatches.set(
contract.name,
(dispatches.get(contract.name) ?? 0) + 1,
);
return {
content: [{ type: "text", text: "sentinel handler reached" }],
isError: true,
};
},
);
}
const client = await mcp.connect(server, {
name: "tool-input-admission-test",
version: "0",
});
expect(TOOL_CONTRACTS.length).toBeGreaterThan(0);
for (const contract of TOOL_CONTRACTS) {
const input = {
...contract.examples[0]?.input,
__rea_unknown_admission_probe__: true,
};
expect(contract.inputSchema.safeParse(input).success).toBe(false);
const result = await client.callTool({
name: contract.name,
arguments: input,
});
expect(result.isError, contract.name).toBe(true);
// Missing required fields may also reject requests without examples.
// Require the unknown-key guard itself, independently of those errors.
expect(
parseMcpToolError(result).error.details,
contract.name,
).toMatchObject({
issues: expect.arrayContaining([
expect.objectContaining({
path: ["__rea_unknown_admission_probe__"],
reason: "unknown_argument",
}),
]),
});
expect(dispatches.has(contract.name), contract.name).toBe(false);
}
const firstWithExample = TOOL_CONTRACTS.find(
(contract) => contract.examples[0] !== undefined,
);
const liveExample = firstWithExample?.examples[0];
if (firstWithExample === undefined || liveExample === undefined)
throw new Error(
"No canonical example is available for the liveness check",
);
const live = await client.callTool({
name: firstWithExample.name,
arguments: liveExample.input,
});
expect(live.isError).toBe(true);
expect(dispatches.get(firstWithExample.name)).toBe(1);
},
);
});