import type { EvidenceMcpServer } from "../EvidenceMcpServer.js"; import { runAdmittedToolOperation } from "../admittedToolOperation.js"; import { recordEvidenceSources } from "../../application/investigation/recordEvidenceSources.js"; import { traceApplicationFeatureEvidence } from "../../application/javascript/JavaScriptApplicationWorkflowService.js"; import { toolContract } from "../../contracts/toolContracts.js"; import { logToolExecution } from "../toolLogging.js"; import { toolRegistrationOptions } from "../toolRegistrationOptions.js"; import { recordResult } from "./helpers.js"; import type { ApplicationToolRegistration } from "./types.js"; const traceContract = toolContract("trace_application_feature"); /** Register the provider-neutral JavaScript feature trace tool. */ export const registerTraceFeatureTool = ( server: EvidenceMcpServer, options: ApplicationToolRegistration, ): void => { server.registerTool( traceContract.name, toolRegistrationOptions(traceContract), async (input, context) => runAdmittedToolOperation( server, options.withAdmittedAnalysis, traceContract.name, context.mcpReq.signal, async () => { const result = await logToolExecution( options.logger, traceContract.name, () => Promise.resolve( traceApplicationFeatureEvidence(input, options.evidenceById, { signal: context.mcpReq.signal, }), ), ); if (!result.ok) return server.delivery.toCallToolResult(result, traceContract); const recorded = recordEvidenceSources( options.recordEvidence, result.value.sources, ); if (!recorded.ok) return server.delivery.toCallToolResult(recorded, traceContract); return recordResult( { ...options, delivery: server.delivery }, traceContract, result.value.evidence, ); }, ), ); };