Files
reverse-engineering-anything/tests/boundary/mcp/absoluteLocalPathTools.test.ts
T
morluto 858ff73880 perf(tests): give MCP test clients the shared lazy schema validation (#10960)
* perf(tests): give MCP test clients the shared lazy schema validation

The MCP SDK client compiles every listed tool's output schema on its
first tool call. With the SDK's default Ajv options, REA's catalog costs
gigabytes of allocation per client (inspect_analysis_view alone: 1.7 s and
~500 MiB retained), so native MCP files peaked at 1-2 GiB per worker.

tests/fixtures/mcpSchemaValidation already owns lazy, reference-preserving
validation. Route the native MCP, process-boundary and shared harness
clients through it, share compiled validators across clients in a worker
(bounded LRU keyed by schema text), and add a counted architecture guard so
default-validation test clients cannot spread; the remaining copies are the
baseline.

* fix(guards): read call arguments past parentheses inside string literals

* perf(tests): give MCP boundary test clients the shared schema validation (#11022)

* perf(tests): give MCP boundary test clients the shared schema validation

* perf(tests): give the remaining MCP test clients the shared schema validation (#11025)

* fix(guards): read an MCP client's validator option from parsed code

The default-validation counter searched each new Client(...) call's text for jsonSchemaValidator, so an inline comment, a string literal or a client name naming the option hid a default client. Parse the source with @babel/parser and count NewExpressions of Client whose object-literal arguments have no jsonSchemaValidator property; a source that does not parse counts every textual call. The guard reads raw test sources, and the generated-child-script allowlist entry, which no longer counts as a call, is removed.

* fix(guards): read an MCP client's options only from a whole object-literal argument

The default-validation counter skipped files without the text
'new Client(', so a comment between the tokens hid a client, and it took
any depth-two key as an option, so 'enabled ? { jsonSchemaValidator } : {}'
passed while its false branch uses the SDK default. The skip now only
requires the name Client, and the validator counts only when it is a key
of an object literal that is a whole argument; anything else counts as the
default.
2026-11-15 21:53:53 -05:00

149 lines
5.4 KiB
TypeScript

import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import type { AnySchema } from "ajv";
import { expect, it } from "vitest";
import { JAVASCRIPT_RECOVERY_TOOL_CONTRACTS } from "../../../src/contracts/javascript/javascriptRecoveryToolContracts.js";
import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../../src/contracts/native/binaryDiagnosticsToolContracts.js";
import { V8_CODE_CACHE_TOOL_CONTRACTS } from "../../../src/contracts/v8/v8CodeCacheToolContracts.js";
import { WEB_SCRIPT_TOOL_CONTRACTS } from "../../../src/contracts/webScriptToolContracts.js";
import { API_CONTRACT_TOOL_CONTRACTS } from "../../../src/contracts/apiContractToolContracts.js";
import { PLAYWRIGHT_TRACE_TOOL_CONTRACTS } from "../../../src/contracts/playwrightTraceToolContracts.js";
import { PYTHON_PYC_TOOL_CONTRACTS } from "../../../src/contracts/python/pythonPycToolContracts.js";
import { RequestTrackingMcpServer } from "../../../src/server/RequestTrackingMcpServer.js";
import { toolRegistrationOptions } from "../../../src/server/toolRegistrationOptions.js";
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
const CONTRACTS = [
...BINARY_DIAGNOSTICS_TOOL_CONTRACTS,
...WEB_SCRIPT_TOOL_CONTRACTS,
...JAVASCRIPT_RECOVERY_TOOL_CONTRACTS,
...API_CONTRACT_TOOL_CONTRACTS,
...PLAYWRIGHT_TRACE_TOOL_CONTRACTS,
...PYTHON_PYC_TOOL_CONTRACTS,
...V8_CODE_CACHE_TOOL_CONTRACTS,
];
const PATH_FIELDS = {
inspect_apple_ips: ["path"],
inspect_elf_notes: ["path"],
inspect_elf_unwind_ranges: ["path"],
inspect_kernel_module: ["path"],
inspect_byte_entropy: ["path"],
inspect_api_contract: ["path", "operations_path"],
inspect_devicetree: ["path"],
inspect_playwright_trace: ["path"],
inspect_python_pyc: ["path"],
inspect_v8_code_cache: ["path"],
export_web_scripts: ["capture_path", "output_directory"],
recover_javascript_sources: ["path", "output_directory", "additional_paths"],
} as const;
const absoluteFile =
process.platform === "win32" ? "C:\\sample.bin" : "/tmp/sample.bin";
const absoluteDirectory = process.platform === "win32" ? "C:\\out" : "/tmp/out";
const contractFor = (name: keyof typeof PATH_FIELDS) => {
const contract = CONTRACTS.find((candidate) => candidate.name === name);
if (contract === undefined) throw new Error(`Missing contract ${name}`);
return contract;
};
const validInput = (
name: keyof typeof PATH_FIELDS,
): Record<string, unknown> => {
if (name === "inspect_api_contract")
return {
format: "graphql-sdl",
path: absoluteFile,
operations_path: absoluteFile,
};
if (name === "export_web_scripts")
return {
capture_path: absoluteFile,
output_directory: absoluteDirectory,
};
if (name === "recover_javascript_sources")
return {
path: absoluteFile,
output_directory: absoluteDirectory,
additional_paths: [absoluteFile],
};
return { path: absoluteFile };
};
const withRelativeField = (
name: keyof typeof PATH_FIELDS,
field: string,
): Record<string, unknown> => ({
...validInput(name),
[field]: field === "additional_paths" ? ["sample.bin"] : "sample.bin",
});
it("rejects one relative path field at a time on tools/list", async () => {
const server = new RequestTrackingMcpServer({
name: "absolute-path-schema",
version: "1",
});
for (const name of Object.keys(PATH_FIELDS) as (keyof typeof PATH_FIELDS)[]) {
const contract = contractFor(name);
server.registerTool(
contract.name,
toolRegistrationOptions(contract),
() => ({ content: [] }),
);
}
const client = new Client(
{ name: "absolute-path-client", version: "1" },
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const checked: string[] = [];
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const listed = await client.listTools();
for (const name of Object.keys(
PATH_FIELDS,
) as (keyof typeof PATH_FIELDS)[]) {
const schema = listed.tools.find(
(tool) => tool.name === name,
)?.inputSchema;
expect(schema, name).toBeDefined();
if (schema === undefined) throw new Error(`Missing schema ${name}`);
const validate = ajv.compile(schema as AnySchema);
expect(validate(validInput(name)), `${name} valid`).toBe(true);
for (const field of PATH_FIELDS[name]) {
expect(
validate(withRelativeField(name, field)),
`${name}.${field}`,
).toBe(false);
checked.push(`${name}.${field}`);
}
}
} finally {
await client.close();
await server.close();
}
expect(checked).toEqual([
"inspect_apple_ips.path",
"inspect_elf_notes.path",
"inspect_elf_unwind_ranges.path",
"inspect_kernel_module.path",
"inspect_byte_entropy.path",
"inspect_api_contract.path",
"inspect_api_contract.operations_path",
"inspect_devicetree.path",
"inspect_playwright_trace.path",
"inspect_python_pyc.path",
"inspect_v8_code_cache.path",
"export_web_scripts.capture_path",
"export_web_scripts.output_directory",
"recover_javascript_sources.path",
"recover_javascript_sources.output_directory",
"recover_javascript_sources.additional_paths",
]);
});