mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* 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.
120 lines
4.4 KiB
TypeScript
120 lines
4.4 KiB
TypeScript
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import { AjvJsonSchemaValidator } from "@modelcontextprotocol/client/validators/ajv";
|
|
import { expect, it, onTestFinished } from "vitest";
|
|
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { advertisedSchemaPatterns } from "../../../src/contracts/advertisedDialect.js";
|
|
import { projectPortablePattern } from "../../../src/contracts/portablePattern.js";
|
|
import {
|
|
BASE64_QUARTET_LENGTH_PATTERN,
|
|
INVALID_BASE64_CHARACTER_PATTERN,
|
|
INVALID_BASE64_PADDING_PATTERN,
|
|
INVALID_CANONICAL_BASE64_PAD_BITS_PATTERN,
|
|
} from "../../../src/domain/stringPatterns.js";
|
|
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
|
import { canonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
// Pattern portability across every profile is owned by
|
|
// advertisedDialect.test.ts. This file keeps the composed canonical base64
|
|
// constraint's safe advertisement after portable re-emission.
|
|
|
|
const advertised = (pattern: RegExp): string => {
|
|
const projection = projectPortablePattern(pattern.source);
|
|
if (projection.kind === "unexpressible")
|
|
throw new Error(`No portable form for ${pattern.source}`);
|
|
return projection.pattern;
|
|
};
|
|
|
|
const collectCanonicalBase64Leaves = (
|
|
value: unknown,
|
|
leaves: Record<string, unknown>[],
|
|
): void => {
|
|
if (value === null || typeof value !== "object") return;
|
|
if (Array.isArray(value)) {
|
|
value.forEach((entry) => collectCanonicalBase64Leaves(entry, leaves));
|
|
return;
|
|
}
|
|
const record = value as Record<string, unknown>;
|
|
const allOf = record.allOf;
|
|
if (
|
|
Array.isArray(allOf) &&
|
|
allOf.some(
|
|
(entry) =>
|
|
entry !== null &&
|
|
typeof entry === "object" &&
|
|
"pattern" in entry &&
|
|
entry.pattern === advertised(BASE64_QUARTET_LENGTH_PATTERN),
|
|
)
|
|
)
|
|
leaves.push(record);
|
|
Object.values(record).forEach((entry) =>
|
|
collectCanonicalBase64Leaves(entry, leaves),
|
|
);
|
|
};
|
|
|
|
const listAdvertisedTools = async () => {
|
|
const session = createTestBinarySession(() => {
|
|
throw new Error("No deep provider may start for a schema projection");
|
|
});
|
|
const server = createServer({ kind: "session", session }, {});
|
|
const client = new Client(
|
|
{ name: "advertised-patterns", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(async () => {
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
});
|
|
const [clientTransport, serverTransport] =
|
|
InMemoryTransport.createLinkedPair();
|
|
await server.connect(serverTransport);
|
|
await client.connect(clientTransport);
|
|
return (await client.listTools()).tools;
|
|
};
|
|
|
|
it("advertises base64 without rejecting valid inputs or overflowing V8", async () => {
|
|
const tools = await listAdvertisedTools();
|
|
const patterns: string[] = [];
|
|
const canonicalBase64Leaves: Record<string, unknown>[] = [];
|
|
// tools/list omits output schemas by default; a validating client checks
|
|
// replies against the canonical output contract, which carries the pattern.
|
|
for (const tool of tools)
|
|
for (const schema of [tool.inputSchema, canonicalOutputSchema(tool.name)]) {
|
|
patterns.push(
|
|
...advertisedSchemaPatterns(schema).map(({ pattern }) => pattern),
|
|
);
|
|
collectCanonicalBase64Leaves(schema, canonicalBase64Leaves);
|
|
}
|
|
|
|
expect(patterns).toContain(advertised(BASE64_QUARTET_LENGTH_PATTERN));
|
|
for (const pattern of [
|
|
INVALID_BASE64_CHARACTER_PATTERN,
|
|
INVALID_BASE64_PADDING_PATTERN,
|
|
INVALID_CANONICAL_BASE64_PAD_BITS_PATTERN,
|
|
])
|
|
expect(projectPortablePattern(pattern.source)).toEqual({
|
|
kind: "unexpressible",
|
|
});
|
|
|
|
expect(canonicalBase64Leaves.length).toBeGreaterThan(0);
|
|
const sdkValidator = new AjvJsonSchemaValidator();
|
|
const validators = canonicalBase64Leaves.map((schema) =>
|
|
sdkValidator.getValidator(schema),
|
|
);
|
|
for (const value of ["", "AA==", "AQID"])
|
|
expect(
|
|
validators.map((validate) => validate(value).valid),
|
|
JSON.stringify(value),
|
|
).toEqual(canonicalBase64Leaves.map(() => true));
|
|
const large = "A".repeat(8 * 1024 * 1024);
|
|
const base64Patterns = new Set(
|
|
canonicalBase64Leaves.flatMap((schema) =>
|
|
advertisedSchemaPatterns(schema).map(({ pattern }) => pattern),
|
|
),
|
|
);
|
|
for (const pattern of base64Patterns)
|
|
expect(new RegExp(pattern, "v").test(large)).toBe(true);
|
|
});
|