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.
107 lines
3.8 KiB
TypeScript
107 lines
3.8 KiB
TypeScript
import type { ToolResultDelivery } from "./toolResult.js";
|
|
import type { EvidenceMcpServer } from "./EvidenceMcpServer.js";
|
|
import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js";
|
|
import {
|
|
optionalProviderUnavailable,
|
|
type OptionalProviderLoadFailure,
|
|
} from "../application/OptionalObservationProviders.js";
|
|
import { err } from "../domain/result.js";
|
|
import type { ServerContext } from "@modelcontextprotocol/server";
|
|
|
|
import type { JavaScriptRuntimeObservationPort } from "../application/javascript/JavaScriptRuntimeObservationPort.js";
|
|
import {
|
|
listJavaScriptRuntimeTargets,
|
|
observeJavaScriptRuntime,
|
|
} from "../application/javascript/JavaScriptRuntimeObservationService.js";
|
|
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 type { Result } from "../domain/result.js";
|
|
import type { Logger } from "pino";
|
|
import { logToolExecution } from "./toolLogging.js";
|
|
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
|
import type { WithAdmittedAnalysis } from "./analysisAdmission.js";
|
|
import { runAdmittedToolOperation } from "./admittedToolOperation.js";
|
|
|
|
interface RuntimeToolRegistration {
|
|
readonly logger: Logger;
|
|
readonly loadFailure?: OptionalProviderLoadFailure | undefined;
|
|
readonly runtime: JavaScriptRuntimeObservationPort | undefined;
|
|
readonly recordEvidence: EvidenceWriter["recordEvidence"] | undefined;
|
|
readonly withAdmittedAnalysis?: WithAdmittedAnalysis;
|
|
}
|
|
|
|
/** Register passive Inspector tools even when policy keeps them unavailable. */
|
|
export const registerJavaScriptRuntimeObservationTools = (
|
|
server: EvidenceMcpServer,
|
|
options: RuntimeToolRegistration,
|
|
): void => {
|
|
const registration = { ...options, delivery: server.delivery, server };
|
|
const listContract = toolContract("list_javascript_runtime_targets");
|
|
const observeContract = toolContract("observe_javascript_runtime");
|
|
server.registerTool(
|
|
listContract.name,
|
|
toolRegistrationOptions(listContract),
|
|
(input, context) =>
|
|
runRuntimeTool(
|
|
registration,
|
|
listContract,
|
|
{ input, context },
|
|
(parsed, signal) =>
|
|
listJavaScriptRuntimeTargets(options.runtime, parsed, { signal }),
|
|
),
|
|
);
|
|
server.registerTool(
|
|
observeContract.name,
|
|
toolRegistrationOptions(observeContract),
|
|
(input, context) =>
|
|
runRuntimeTool(
|
|
registration,
|
|
observeContract,
|
|
{ input, context },
|
|
(parsed, signal) =>
|
|
observeJavaScriptRuntime(options.runtime, parsed, { signal }),
|
|
),
|
|
);
|
|
};
|
|
|
|
const runRuntimeTool = async <Input>(
|
|
options: RuntimeToolRegistration & {
|
|
readonly delivery: ToolResultDelivery;
|
|
readonly server: EvidenceMcpServer;
|
|
},
|
|
contract: ToolContract,
|
|
request: { readonly input: Input; readonly context: ServerContext },
|
|
execute: (
|
|
input: Input,
|
|
signal: AbortSignal,
|
|
) => Promise<Result<Evidence, AnalysisError>>,
|
|
) => {
|
|
const { input, context } = request;
|
|
if (options.loadFailure !== undefined)
|
|
return options.delivery.toCallToolResult(
|
|
err(optionalProviderUnavailable(options.loadFailure, contract.name)),
|
|
contract,
|
|
);
|
|
return runAdmittedToolOperation(
|
|
options.server,
|
|
options.withAdmittedAnalysis,
|
|
contract.name,
|
|
context.mcpReq.signal,
|
|
async () => {
|
|
const result = await logToolExecution(options.logger, contract.name, () =>
|
|
execute(input, context.mcpReq.signal),
|
|
);
|
|
if (!result.ok)
|
|
return options.delivery.toCallToolResult(result, contract);
|
|
const recorded = options.recordEvidence?.(result.value);
|
|
return options.delivery.toEvidenceToolResult(
|
|
result.value,
|
|
contract,
|
|
recorded,
|
|
);
|
|
},
|
|
);
|
|
};
|