refactor: declare auxiliary capabilities from source (#853)

This commit is contained in:
N0zoM1z0
2026-10-07 12:32:34 +08:00
committed by GitHub
parent c9f49db907
commit 4a6c17ce83
22 changed files with 499 additions and 240 deletions
@@ -0,0 +1,63 @@
import { execFile } from "node:child_process";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { z } from "zod";
import { GENERATED_MCP_TOOL_CATALOG } from "../../src/generatedMcpToolCatalog.js";
const execute = promisify(execFile);
const fixture = fileURLToPath(
new URL("../fixtures/contractPresentation.mjs", import.meta.url),
);
const resultSchema = z.object({
catalog: z.array(
z.object({
name: z.string(),
inputSchema: z.record(z.string(), z.unknown()),
outputSchema: z.record(z.string(), z.unknown()),
}),
),
responses: z.array(
z.object({ name: z.string(), response: z.record(z.string(), z.unknown()) }),
),
});
it.each(["original", "reversed"])(
"preserves advertised schemas and handler meaning with %s presentation",
async (presentation) => {
const { stdout } = await execute(
process.execPath,
[fixture, presentation],
{
timeout: 20_000,
maxBuffer: 16 * 1024 * 1024,
},
);
const result = resultSchema.parse(JSON.parse(stdout));
expect(result.catalog).toHaveLength(GENERATED_MCP_TOOL_CATALOG.length);
expect(new Set(result.catalog.map(({ name }) => name))).toEqual(
new Set(GENERATED_MCP_TOOL_CATALOG.map(({ name }) => name)),
);
for (const advertised of result.catalog) {
const canonical = GENERATED_MCP_TOOL_CATALOG.find(
({ name }) => name === advertised.name,
);
expect(advertised.inputSchema).toEqual(canonical?.inputSchema);
expect(advertised.outputSchema).toEqual(canonical?.outputSchema);
}
for (const { name, response } of result.responses) {
if (name === "reconcile_javascript_runtime") {
expect(response.isError).not.toBe(true);
expect(response.structuredContent).toMatchObject({
evidence: { operation: name },
});
} else {
expect(response.isError, name).toBe(true);
expect(response.structuredContent, name).toMatchObject({
error: { details: { operation: name } },
});
}
}
},
);
@@ -0,0 +1,89 @@
import { expect, it } from "vitest";
import { auxiliaryAnalysisProviderDeclarations } from "../../../src/composition/auxiliaryAnalysisProviders.js";
import { LazyAnalysisProvider } from "../../../src/application/LazyAnalysisProvider.js";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { GENERATED_AUXILIARY_PROVIDERS } from "../../../src/generatedMcpToolCatalog.js";
it.each(["darwin", "linux", "win32"] as const)(
"preserves canonical auxiliary metadata and host facts on %s",
async (platform) => {
const declarations = auxiliaryAnalysisProviderDeclarations(platform);
expect(declarations.map(({ identity }) => identity)).toEqual(
GENERATED_AUXILIARY_PROVIDERS.map(({ identity }) => identity),
);
const operations = new Set<string>();
for (const declaration of declarations) {
const canonical = GENERATED_AUXILIARY_PROVIDERS.find(
({ identity }) => identity.id === declaration.identity.id,
);
if (canonical === undefined)
throw new Error("Missing canonical provider");
const expected = canonical.capabilities.map((capability) => {
if (platform === "darwin") return capability;
if (capability.provider.id === "native-macos")
return {
...capability,
available: false,
availabilityCode: "unsupported_host",
reason: "Native macOS utilities require macOS.",
};
if (capability.operation === "inspect_asset_catalog")
return {
...capability,
available: false,
availabilityCode: "unsupported_host",
reason: "Apple asset catalogs require macOS assetutil.",
};
return capability;
});
expect(declaration.capabilities).toEqual(expected);
const first = await declaration.load();
const second = await declaration.load();
expect(first).not.toBe(second);
expect(first.identity()).toEqual(declaration.identity);
expect(first.capabilities()).toEqual(declaration.capabilities);
expect(second.capabilities()).toEqual(declaration.capabilities);
for (const capability of declaration.capabilities) {
expect(operations.has(capability.operation)).toBe(false);
operations.add(capability.operation);
expect(Object.isFrozen(capability)).toBe(true);
expect(Object.isFrozen(capability.effects)).toBe(true);
expect(Object.isFrozen(capability.limitations)).toBe(true);
}
}
expect(operations.has("inspect_native_dispatch_metadata")).toBe(true);
},
);
it("keeps declaration discovery and client creation lazy and independently owned", async () => {
const target = await parseBinaryTarget(process.execPath);
if (!target.ok) throw target.error;
for (const declaration of auxiliaryAnalysisProviderDeclarations()) {
let loads = 0;
const provider = new LazyAnalysisProvider({
...declaration,
load: async () => {
loads += 1;
return declaration.load();
},
});
expect(provider.identity()).toEqual(declaration.identity);
expect(provider.capabilities()).toEqual(declaration.capabilities);
const client = provider.createClient(target.value);
const unusedClient = provider.createClient(target.value);
await unusedClient.close();
expect(loads).toBe(0);
try {
expect(await client.execute("health", {})).toMatchObject({
ok: true,
value: { provider: declaration.identity },
});
expect(loads).toBe(1);
expect(await client.execute("health", {})).toMatchObject({ ok: true });
expect(loads).toBe(1);
} finally {
await client.close();
}
}
});
+58
View File
@@ -0,0 +1,58 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { BROWSER_TOOL_CONTRACTS } from "../../dist/contracts/browserToolContracts.js";
import { BROWSER_SCENARIO_TOOL_CONTRACTS } from "../../dist/contracts/browserScenarioToolContracts.js";
import { ELECTRON_TOOL_CONTRACTS } from "../../dist/contracts/electronToolContracts.js";
import { JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS } from "../../dist/contracts/javascriptRuntimeObservationToolContracts.js";
import { TOOL_CONTRACTS } from "../../dist/contracts/toolContracts.js";
import { parseConfig } from "../../dist/config.js";
import { createBinarySession } from "../../dist/application/runtime.js";
// Mutate only this child's presentation arrays, before importing registration.
if (process.argv[2] === "reversed") {
for (const contracts of [
BROWSER_TOOL_CONTRACTS,
BROWSER_SCENARIO_TOOL_CONTRACTS,
ELECTRON_TOOL_CONTRACTS,
JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS,
TOOL_CONTRACTS,
])
contracts.reverse();
}
const { createServer } = await import("../../dist/server/createServer.js");
const config = parseConfig({});
if (!config.ok) throw config.error;
const session = createBinarySession(config.value);
const server = createServer(session, session);
const client = new Client({ name: "contract-presentation", version: "1" });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const catalog = await client.listTools();
const responses = [];
for (const contract of [
...BROWSER_TOOL_CONTRACTS,
...BROWSER_SCENARIO_TOOL_CONTRACTS,
...ELECTRON_TOOL_CONTRACTS.filter(
({ name }) => name !== "analyze_javascript_application",
),
...JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS,
]) {
const example = contract.examples[0];
if (example === undefined)
throw new Error(`Missing example for ${contract.name}`);
responses.push({
name: contract.name,
response: await client.callTool({
name: contract.name,
arguments: example.input,
}),
});
}
process.stdout.write(JSON.stringify({ catalog: catalog.tools, responses }));
} finally {
await client.close();
await server.close();
await session.close();
}