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.
162 lines
5.8 KiB
TypeScript
162 lines
5.8 KiB
TypeScript
import {
|
|
Client,
|
|
StreamableHTTPClientTransport,
|
|
} from "@modelcontextprotocol/client";
|
|
import { describe, expect, onTestFinished } from "vitest";
|
|
|
|
import { advertisedMcpToolSchemas } from "../../fixtures/advertisedMcpTool.js";
|
|
import { expectForeignHostRefused } from "../../support/http/rawRequest.js";
|
|
import {
|
|
startMcpHttpProcess,
|
|
startListeningProcess,
|
|
} from "../../support/mcp/streamableHttpProcess.js";
|
|
import { processTest } from "../../support/process/processFixture.js";
|
|
import { assertAdvertisedMcpContracts } from "./mcpContractHarness.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const openSession = async (url: URL, name: string) => {
|
|
const transport = new StreamableHTTPClientTransport(url);
|
|
const client = new Client(
|
|
{ name, version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
onTestFinished(() => client.close());
|
|
await client.connect(transport);
|
|
expect(transport.sessionId).toEqual(expect.any(String));
|
|
const { toolDefinition } = await advertisedMcpToolSchemas(
|
|
client,
|
|
"binary_session",
|
|
);
|
|
assertAdvertisedMcpContracts(["binary_session"], [toolDefinition]);
|
|
return { transport, client, toolDefinition };
|
|
};
|
|
|
|
/** The shared target answers with its identity only while its providers are still open. */
|
|
const expectIdentity = async ({
|
|
client,
|
|
toolDefinition,
|
|
}: Awaited<ReturnType<typeof openSession>>): Promise<void> => {
|
|
const identity = await client.callTool(
|
|
{ name: "binary_session", arguments: {} },
|
|
{ toolDefinition },
|
|
);
|
|
expect(identity.isError).toBeFalsy();
|
|
expect(identity.structuredContent).toMatchObject({
|
|
result: {
|
|
server_identity: { package: { version: expect.any(String) } },
|
|
},
|
|
});
|
|
};
|
|
|
|
describe("rea mcp --http", () => {
|
|
processTest(
|
|
"keeps hot/cold visibility within its HTTP session and refreshes named contracts",
|
|
async ({ processes }) => {
|
|
const started = await startListeningProcess(
|
|
processes,
|
|
["mcp", "--http", "--port", "0"],
|
|
// Sessions validate advertised output schemas, which default to off.
|
|
{
|
|
REA_MCP_TOOL_CATALOG_PROFILE: "hot-cold",
|
|
},
|
|
);
|
|
const first = await openSession(started.url, "hot-cold-first");
|
|
const initialNames = (await first.client.listTools()).tools.map(
|
|
({ name }) => name,
|
|
);
|
|
expect(initialNames).not.toContain("inspect_analysis_view");
|
|
expect(initialNames).not.toContain("list_unknowns");
|
|
let changed = 0;
|
|
first.client.setNotificationHandler(
|
|
"notifications/tools/list_changed",
|
|
() => {
|
|
changed++;
|
|
},
|
|
);
|
|
const discovered = await first.client.callTool({
|
|
name: "discover_tools",
|
|
arguments: { names: ["list_unknowns"] },
|
|
});
|
|
expect(discovered.isError).not.toBe(true);
|
|
const { toolDefinition } = await advertisedMcpToolSchemas(
|
|
first.client,
|
|
"list_unknowns",
|
|
);
|
|
expect(changed).toBe(1);
|
|
expect(
|
|
(
|
|
await first.client.callTool(
|
|
{ name: "list_unknowns", arguments: {} },
|
|
{ toolDefinition },
|
|
)
|
|
).isError,
|
|
).not.toBe(true);
|
|
await first.transport.terminateSession();
|
|
await first.client.close();
|
|
const second = await openSession(started.url, "hot-cold-second");
|
|
expect(
|
|
(await second.client.listTools()).tools.map(({ name }) => name),
|
|
).toEqual(initialNames);
|
|
await expectIdentity(second);
|
|
await second.transport.terminateSession();
|
|
},
|
|
);
|
|
|
|
processTest(
|
|
"serves initialize, tools/list, a tool call and DELETE, then keeps serving later clients",
|
|
async ({ processes }) => {
|
|
const started = await startListeningProcess(
|
|
processes,
|
|
["mcp", "--http", "--port", "0"],
|
|
{},
|
|
);
|
|
expect(started.url.hostname).toBe("127.0.0.1");
|
|
expect(started.url.pathname).toBe("/mcp");
|
|
let stdout = "";
|
|
started.child.stdout.on("data", (chunk: Buffer) => {
|
|
stdout += chunk.toString("utf8");
|
|
});
|
|
const first = await openSession(started.url, "streamable-http-boundary");
|
|
expect(() =>
|
|
assertAdvertisedMcpContracts(
|
|
["binary_session"],
|
|
[first.toolDefinition, first.toolDefinition],
|
|
),
|
|
).toThrow();
|
|
const { name } = first.toolDefinition;
|
|
expect(() =>
|
|
assertAdvertisedMcpContracts(["binary_session"], [{ name }]),
|
|
).toThrow();
|
|
await expectIdentity(first);
|
|
await first.transport.terminateSession();
|
|
expect(first.transport.sessionId).toBeUndefined();
|
|
await first.client.close();
|
|
// DELETE ended the session, not the server: the next client's tools still work.
|
|
const second = await openSession(started.url, "next-session");
|
|
await expectIdentity(second);
|
|
// A client that leaves without DELETE does not block the listener: the next initialize replaces it.
|
|
const third = await openSession(started.url, "replacing-session");
|
|
expect(third.transport.sessionId).not.toBe(second.transport.sessionId);
|
|
await expectIdentity(third);
|
|
await expect(expectIdentity(second)).rejects.toThrow("Session not found");
|
|
await second.client.close();
|
|
await third.transport.terminateSession();
|
|
await third.client.close();
|
|
expect(stdout).toBe("");
|
|
expect(started.stderr()).toContain('"transport":"streamable-http"');
|
|
expect(started.stderr()).toContain(
|
|
"MCP HTTP session replaced by a new client",
|
|
);
|
|
},
|
|
);
|
|
|
|
processTest(
|
|
"refuses a non-loopback Host header on the loopback bind",
|
|
async ({ processes }) => {
|
|
const { url } = await startMcpHttpProcess(processes);
|
|
const refused = await expectForeignHostRefused(url);
|
|
expect(refused.body).not.toContain("127.0.0.1");
|
|
},
|
|
);
|
|
});
|