feat(mcp): expose progress resources and availability

This commit is contained in:
morluto
2026-07-14 02:11:56 +00:00
parent 190efcaf17
commit da59299a8a
25 changed files with 1999 additions and 217 deletions
+78
View File
@@ -13,11 +13,19 @@ import type { Logger } from "../logger.js";
import { logToolExecution } from "./toolLogging.js";
import { toCallToolResult } from "./toolResult.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
import { mcpProgressReporter } from "./mcpProgress.js";
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
import {
AnalysisProtocolError,
PermissionRequiredError,
} from "../domain/errors.js";
import { err } from "../domain/result.js";
interface EvidenceToolRegistration {
readonly logger: Logger;
readonly activeTarget: (() => BinaryTarget | undefined) | undefined;
readonly recordEvidence: BinarySessionPort["recordEvidence"] | undefined;
readonly permissionAuthority?: PermissionAuthority;
}
/** Register provider-backed contracts that return atomic Evidence v2 observations. */
@@ -32,17 +40,52 @@ export const registerEvidenceTools = (
contract.name,
toolRegistrationOptions(contract),
async (input, context) => {
const progress = mcpProgressReporter(context);
await progress.report({
phase: contract.name,
completed: 0,
total: 1,
message: "started",
});
const parameters = jsonObjectSchema.parse(
contract.inputSchema.parse(input),
);
if (options.permissionAuthority !== undefined) {
const request = permissionRequest(contract.name, parameters);
if (request !== undefined) {
const authorized = await options.permissionAuthority.authorize(
request,
contract.name === "extract_artifact" ? "write" : "read",
);
if (!authorized.ok)
return toCallToolResult(
err(
authorized.error instanceof PermissionRequiredError
? authorized.error
: new AnalysisProtocolError(authorized.error.message, {
cause: authorized.error,
}),
),
contract,
);
}
}
const execution = await logToolExecution(
options.logger,
contract.name,
() =>
analysis.execute(contract.name, parameters, {
signal: context.mcpReq.signal,
progress,
}),
);
await progress.report({
phase: contract.name,
completed: 1,
total: 1,
message: execution.ok ? "completed" : "failed",
terminal: true,
});
if (!execution.ok) return toCallToolResult(execution, contract);
const evidence = createEvidence(
execution.value.subject ?? options.activeTarget?.(),
@@ -64,3 +107,38 @@ export const registerEvidenceTools = (
);
}
};
const permissionRequest = (
operation: string,
parameters: Readonly<
Record<string, import("../domain/jsonValue.js").JsonValue>
>,
) => {
if (
operation === "extract_artifact" &&
typeof parameters.output_root === "string"
)
return {
capability: "artifact_extract" as const,
roots: [parameters.output_root],
executables: [],
environment_names: [],
network: "none" as const,
mount: false,
operation_identity: `extract_artifact:${parameters.output_root}`,
};
if (
operation === "inventory_artifact" &&
parameters.native_mount_approved === true
)
return {
capability: "native_mount" as const,
roots: [],
executables: [],
environment_names: [],
network: "none" as const,
mount: true,
operation_identity: "inventory_artifact:native_mount",
};
return undefined;
};