refactor(mcp): trust validated inputs and tighten contract fixtures

Remove repeated MCP input parsing after the SDK has validated named contracts. Reject unmodeled fixture operations and locate prompt contracts by name so unrelated catalog ordering cannot select a contract.

Validated focused browser, Electron, runtime-observation and MCP workflow tests, advertised schemas, packaged MCP and static checks. Application-boundary validation remains in place.
This commit is contained in:
morluto
2026-10-12 05:57:35 +08:00
parent 74d7da6ced
commit 264615059c
6 changed files with 53 additions and 53 deletions
+5 -15
View File
@@ -24,8 +24,6 @@ import { toolContract } from "../contracts/toolContracts.js";
import type { ToolContract } from "../contracts/toolContractTypes.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Evidence } from "../domain/evidence.js";
import { analyzeWebBundleInputSchema } from "../domain/webBundleAnalysis.js";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
import type { Result } from "../domain/result.js";
import type { Logger } from "pino";
import { mcpProgressReporter } from "./mcpProgress.js";
@@ -86,11 +84,7 @@ export const registerBrowserTools = (
inspectContract,
{ input, context },
(parsed, { signal, progress }) =>
inspectWebPage(
options.browser,
inspectWebPageInputSchema.parse(parsed),
{ signal, progress },
),
inspectWebPage(options.browser, parsed, { signal, progress }),
),
);
server.registerTool(
@@ -102,14 +96,10 @@ export const registerBrowserTools = (
analyzeContract,
{ input, context },
(parsed, { signal, progress }) =>
analyzeWebBundle(
options.browser,
analyzeWebBundleInputSchema.parse(parsed),
{
signal,
progress,
},
),
analyzeWebBundle(options.browser, parsed, {
signal,
progress,
}),
),
);
server.registerTool(
+3 -6
View File
@@ -26,7 +26,6 @@ import type { ToolContract } from "../contracts/toolContractTypes.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Evidence } from "../domain/evidence.js";
import type { Result } from "../domain/result.js";
import { inspectElectronPageInputSchema } from "../domain/javascript/electronObservation.js";
import type { Logger } from "pino";
import { mcpProgressReporter } from "./mcpProgress.js";
import { logToolExecution } from "./toolLogging.js";
@@ -86,13 +85,11 @@ export const registerElectronTools = (
registration,
inspectContract,
{ input, context },
async (parsed, { signal, progress }) => {
const request = inspectElectronPageInputSchema.parse(parsed);
return inspectElectronPage(options.electron, request, {
(parsed, { signal, progress }) =>
inspectElectronPage(options.electron, parsed, {
signal,
progress,
});
},
}),
),
);
server.registerTool(
@@ -18,7 +18,6 @@ import type { ToolContract } from "../contracts/toolContractTypes.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Evidence } from "../domain/evidence.js";
import type { Result } from "../domain/result.js";
import { observeJavaScriptRuntimeInputSchema } from "../domain/javascript/javascriptRuntimeObservation.js";
import type { Logger } from "pino";
import { logToolExecution } from "./toolLogging.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
@@ -61,10 +60,8 @@ export const registerJavaScriptRuntimeObservationTools = (
registration,
observeContract,
{ input, context },
async (parsed, signal) => {
const request = observeJavaScriptRuntimeInputSchema.parse(parsed);
return observeJavaScriptRuntime(options.runtime, request, { signal });
},
(parsed, signal) =>
observeJavaScriptRuntime(options.runtime, parsed, { signal }),
),
);
};
@@ -130,7 +130,7 @@ describe("enhanced MCP tools", () => {
);
}
default:
return Promise.resolve(ok(null));
throw new Error(`Unexpected analysis operation: ${name}`);
}
},
});
@@ -174,26 +174,29 @@ describe("enhanced MCP tools", () => {
it("rejects a segment with omitted coordinates instead of reporting zero length", async () => {
const client = await connect({
execute: (name) =>
Promise.resolve(
name === "list_segments"
? ok([
{
name: "__TEXT",
end: "0x2000",
readable: null,
writable: null,
executable: null,
},
])
: ok(name === "current_document" ? "fixture" : []),
),
execute: (name) => {
if (name === "list_segments")
return Promise.resolve(
ok([
{
name: "__TEXT",
end: "0x2000",
readable: null,
writable: null,
executable: null,
},
]),
);
if (name === "current_document") return Promise.resolve(ok("fixture"));
if (name === "list_procedures" || name === "list_strings")
return Promise.resolve(ok([]));
throw new Error(`Unexpected analysis operation: ${name}`);
},
});
const result = await client.callTool({
name: "binary_overview",
arguments: {},
});
expect(result.isError).toBe(true);
expect(result.content).toContainEqual({
type: "text",
@@ -225,8 +228,11 @@ const overviewPort = (
},
]),
);
default:
case "list_procedures":
case "list_strings":
return Promise.resolve(ok([]));
default:
throw new Error(`Unexpected analysis operation: ${name}`);
}
},
});
+19 -9
View File
@@ -54,9 +54,11 @@ describe("guided prompts over MCP", () => {
completions: {},
prompts: { listChanged: true },
});
for (const [index, contract] of PROMPT_CONTRACTS.entries()) {
const advertised = prompts.prompts[index];
expect(advertised?.arguments).toEqual(
const advertisedByName = new Map(
prompts.prompts.map((prompt) => [prompt.name, prompt]),
);
for (const contract of PROMPT_CONTRACTS) {
expect(advertisedByName.get(contract.name)?.arguments).toEqual(
Object.entries(contract.arguments).map(
([name, { description, required }]) => ({
name,
@@ -271,15 +273,21 @@ describe("guided prompt completion registry", () => {
});
await connectPair(server, client);
resources.push(session);
expect((await client.listPrompts()).prompts[0]?.title).toBe(
"Investigate a feature",
const contract = PROMPT_CONTRACTS.find(
({ name }) => name === "investigate_feature",
);
if (contract === undefined)
throw new Error("Missing investigate_feature prompt contract");
const initialPrompt = (await client.listPrompts()).prompts.find(
({ name }) => name === contract.name,
);
expect(initialPrompt?.title).toBe(contract.title);
registry.update({
...PROMPT_CONTRACTS[0],
title: "Investigate a feature (updated)",
...contract,
title: `${contract.title} (updated)`,
arguments: {
...PROMPT_CONTRACTS[0].arguments,
...contract.arguments,
document: {
description: "Updated provider-backed selector",
required: false,
@@ -289,7 +297,9 @@ describe("guided prompt completion registry", () => {
});
await changed;
const prompts = await client.listPrompts();
expect(prompts.prompts[0]?.title).toBe("Investigate a feature (updated)");
expect(
prompts.prompts.find(({ name }) => name === contract.name)?.title,
).toBe(`${contract.title} (updated)`);
expect(await complete(client, "document", "fixture")).toEqual(["fixture"]);
});
});
+1 -1
View File
@@ -65,7 +65,7 @@ it("executes a realistic workflow: list methods, decompile selected, get xrefs",
case "xrefs":
return Promise.resolve(ok(["0x1000"]));
default:
return Promise.resolve(ok(null));
throw new Error(`Unexpected analysis operation: ${name}`);
}
},
});