Files
rea/tests/boundary/mcp/advertisedPatternsVMode.test.ts
T
felipeatpandGitHub f0da05f67f fix: emit v-mode-compatible patterns for tool schemas (#1220)
* fix: emit v-mode-compatible patterns for tool schemas

Character classes in emitted patterns carried unescaped '-', '/' and '['. Annex-B and u-mode accept them, so REA's own Zod validation (u) and every u-based validator never saw a defect, but v-mode rejects ClassSetSyntaxCharacters and Ruby rejects [^s[]]. With 6.1.0 tools/list, 117 pattern fields across 11 tools were rejected under v.

The characters are escaped as \x2d, \x2f, \x5b and \x5d rather than \-/\//\[, because oxlint --fix removes an escaped '-' inside a character class as a useless escape, so an escape-only change does not survive the pre-commit hook. \x2d-style escapes are valid under Annex B, u and v, are matched identically, and are not useless-escape candidates.

22 pattern literals in 18 files are touched; no other token changes.

* fix: replace the Zod offset pattern and guard the advertised catalog

The offset datetime site projected Zod's offset class as [ +- ], which no escape in this repository's sources can reach, so evaluate_reconstruction_coverage stayed unreachable for exactly the clients this change targets. It now uses offsetDateTimeSchema in src/domain/offsetDateTime.ts: the same accepted set with the offset class escaped as [ +\x2d ].

Adds tests/boundary/mcp/advertisedPatternsVMode.test.ts, which performs the real tools/list projection, traverses every advertised input and output schema, compiles each pattern under v, and asserts the inventory it walked so a silent traversal collapse fails instead of passing vacuously.
2026-10-09 16:20:57 +08:00

78 lines
2.7 KiB
TypeScript

import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { expect, it, onTestFinished } from "vitest";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
interface AdvertisedPattern {
readonly tool: string;
readonly where: string;
readonly pattern: string;
}
const collectPatterns = (
value: unknown,
where: string,
tool: string,
found: AdvertisedPattern[],
): void => {
if (value === null || typeof value !== "object") return;
if (Array.isArray(value)) {
value.forEach((entry, index) =>
collectPatterns(entry, `${where}[${String(index)}]`, tool, found),
);
return;
}
for (const [key, entry] of Object.entries(value)) {
if (key === "pattern" && typeof entry === "string")
found.push({ tool, where: `${where}/pattern`, pattern: entry });
collectPatterns(entry, `${where}/${key}`, tool, found);
}
};
it("advertises patterns that compile under every engine a client may use", async () => {
const session = createTestBinarySession(() => {
throw new Error("No deep provider may start for a schema projection");
});
const server = createServer(session, session);
const client = new Client({ name: "advertised-patterns", version: "1" });
onTestFinished(async () => {
await client.close();
await server.close();
await session.close();
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools;
const patterns: AdvertisedPattern[] = [];
for (const tool of advertised) {
collectPatterns(tool.inputSchema, "inputSchema", tool.name, patterns);
if (tool.outputSchema !== undefined)
collectPatterns(tool.outputSchema, "outputSchema", tool.name, patterns);
}
// A traversal that silently collapses would make the compilation check below
// vacuous, so the inventory itself is asserted.
expect(advertised.length).toBeGreaterThan(100);
expect(patterns.length).toBeGreaterThan(500);
// Annex B and `u` mode accept an unescaped `-`, `/` or `[` inside a character
// class, so REA's own schema validation cannot observe the defect. `v` mode
// rejects them, and a client that compiles advertised patterns rejects the
// complete tools request rather than the affected call.
const uncompilable = patterns.flatMap((entry) => {
try {
new RegExp(entry.pattern, "v");
return [];
} catch (cause: unknown) {
return [
`${entry.tool} ${entry.where} ${entry.pattern}: ${cause instanceof Error ? cause.message : String(cause)}`,
];
}
});
expect(uncompilable).toEqual([]);
});