fix(mcp): integrate managed contracts after rebase
This commit is contained in:
@@ -29,15 +29,23 @@ export const compareManagedMembersEvidence = (
|
||||
const operation = "compare_managed_members";
|
||||
const parsed = compareManagedMembersInputSchema.safeParse(rawInput);
|
||||
if (!parsed.success) return err(new AnalysisInputError(operation));
|
||||
return compareManagedMembersEvidenceValidated(parsed.data);
|
||||
};
|
||||
|
||||
/** Compare managed members from input parsed by a trusted adapter. */
|
||||
export const compareManagedMembersEvidenceValidated = (
|
||||
input: CompareManagedMembersInput,
|
||||
): Result<Evidence, AnalysisError> => {
|
||||
const operation = "compare_managed_members";
|
||||
try {
|
||||
const left = parseManagedMemberEvidence(parsed.data.left);
|
||||
const right = parseManagedMemberEvidence(parsed.data.right);
|
||||
const left = parseManagedMemberEvidence(input.left);
|
||||
const right = parseManagedMemberEvidence(input.right);
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidenceId, result: left.result },
|
||||
{ evidenceId: right.evidenceId, result: right.result },
|
||||
parsed.data.limits,
|
||||
input.limits,
|
||||
);
|
||||
return ok(createManagedMemberComparisonEvidence(parsed.data, result));
|
||||
return ok(createManagedMemberComparisonEvidence(input, result));
|
||||
} catch (cause: unknown) {
|
||||
return workflowFailure(operation, cause);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,17 @@ export const planManagedRuntimeCorrelationEvidence = async (
|
||||
const parsed = managedRuntimeCorrelationInputSchema.safeParse(rawInput);
|
||||
if (!parsed.success)
|
||||
return err(new AnalysisInputError(OPERATION, { cause: parsed.error }));
|
||||
return planManagedRuntimeCorrelationEvidenceValidated(
|
||||
dependencies,
|
||||
parsed.data,
|
||||
);
|
||||
};
|
||||
|
||||
/** Plan runtime correlation from input parsed by a trusted adapter. */
|
||||
export const planManagedRuntimeCorrelationEvidenceValidated = async (
|
||||
dependencies: ManagedRuntimeCorrelationDependencies,
|
||||
input: ManagedRuntimeCorrelationInput,
|
||||
): Promise<Result<Evidence, AnalysisError>> => {
|
||||
if (!dependencies.policy.enabled)
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
@@ -61,7 +72,7 @@ export const planManagedRuntimeCorrelationEvidence = async (
|
||||
let staticEvidence: Evidence;
|
||||
let artifactPath: string;
|
||||
try {
|
||||
staticEvidence = parseEvidence(parsed.data.static_members);
|
||||
staticEvidence = parseEvidence(input.static_members);
|
||||
artifactPath = artifactPathFor(staticEvidence);
|
||||
} catch (cause: unknown) {
|
||||
return err(new AnalysisInputError(OPERATION, { cause }));
|
||||
@@ -74,7 +85,7 @@ export const planManagedRuntimeCorrelationEvidence = async (
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: operationIdentity(parsed.data, staticEvidence),
|
||||
operation_identity: operationIdentity(input, staticEvidence),
|
||||
},
|
||||
"read",
|
||||
{ restartRequired: true },
|
||||
@@ -89,7 +100,7 @@ export const planManagedRuntimeCorrelationEvidence = async (
|
||||
);
|
||||
try {
|
||||
const result = planManagedRuntimeCorrelation(
|
||||
parsed.data,
|
||||
input,
|
||||
dependencies.policy.executablePath,
|
||||
authorized.value.grant_id,
|
||||
);
|
||||
@@ -106,10 +117,10 @@ export const planManagedRuntimeCorrelationEvidence = async (
|
||||
operation: OPERATION,
|
||||
parameters: {
|
||||
static_members_evidence_id: staticEvidence.evidence_id,
|
||||
method: jsonValueSchema.parse(parsed.data.method),
|
||||
requested_effect: parsed.data.requested_effect,
|
||||
host: jsonValueSchema.parse(parsed.data.host),
|
||||
bounds: jsonValueSchema.parse(parsed.data.bounds),
|
||||
method: jsonValueSchema.parse(input.method),
|
||||
requested_effect: input.requested_effect,
|
||||
host: jsonValueSchema.parse(input.host),
|
||||
bounds: jsonValueSchema.parse(input.bounds),
|
||||
},
|
||||
result: jsonValueSchema.parse(result),
|
||||
rawResult: null,
|
||||
|
||||
@@ -135,17 +135,12 @@ export const MANAGED_TOOL_CONTRACTS = [
|
||||
},
|
||||
{
|
||||
name: "inspect_managed_members",
|
||||
...toolContractMetadata("inspect_managed_members"),
|
||||
description:
|
||||
"Inspect bounded PE/CLI metadata members, signatures, method body IL hashes, exception regions, call edges, and field-access anchors without loading or executing target code. Metadata tokens are reported as build-local coordinates bound to the artifact SHA-256 and MVID.",
|
||||
kind: "managed-provider",
|
||||
inputSchema: managedMemberInputSchema,
|
||||
outputSchema: memberOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Example inspect managed members request",
|
||||
@@ -155,17 +150,12 @@ export const MANAGED_TOOL_CONTRACTS = [
|
||||
},
|
||||
{
|
||||
name: "inspect_managed_native_boundaries",
|
||||
...toolContractMetadata("inspect_managed_native_boundaries"),
|
||||
description:
|
||||
"Inspect PE/CLI ModuleRef, ImplMap/PInvoke declarations, CLI native-header indicators, and non-IL method implementation flags without loading or executing target code. Results are managed declarations and degraded native-boundary observations, not verified native exports or addresses.",
|
||||
kind: "managed-provider",
|
||||
inputSchema: managedNativeBoundaryInputSchema,
|
||||
outputSchema: nativeBoundaryOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Example inspect managed/native boundary request",
|
||||
|
||||
@@ -8,6 +8,36 @@ import {
|
||||
MANAGED_RECONSTRUCTION_IMPORT_EXAMPLE,
|
||||
MANAGED_RUNTIME_CORRELATION_EXAMPLE,
|
||||
} from "./managedWorkflowExamples.js";
|
||||
import { toolContractMetadata } from "./toolEffects.js";
|
||||
|
||||
const managedEvidenceIdSchema = z
|
||||
.string()
|
||||
.regex(/^ev_[a-f0-9]{64}$/u)
|
||||
.describe("Session-owned inspect_managed_members Evidence ID");
|
||||
|
||||
/** MCP references for comparing two session-owned managed observations. */
|
||||
export const compareManagedMembersReferenceInputSchema = z
|
||||
.strictObject({
|
||||
left_evidence_id: managedEvidenceIdSchema,
|
||||
right_evidence_id: managedEvidenceIdSchema,
|
||||
limits: compareManagedMembersInputSchema.shape.limits,
|
||||
unknown_registry_approved:
|
||||
compareManagedMembersInputSchema.shape.unknown_registry_approved,
|
||||
})
|
||||
.superRefine((input, context) => {
|
||||
if (input.left_evidence_id === input.right_evidence_id)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["right_evidence_id"],
|
||||
message: "Managed member Evidence must be distinct",
|
||||
});
|
||||
});
|
||||
|
||||
/** MCP reference for planning from one session-owned managed observation. */
|
||||
export const managedRuntimeCorrelationReferenceInputSchema =
|
||||
managedRuntimeCorrelationInputSchema
|
||||
.omit({ static_members: true })
|
||||
.extend({ static_members_evidence_id: managedEvidenceIdSchema });
|
||||
|
||||
const comparisonOutputSchema =
|
||||
managedWorkflowOutputSchemas.compare_managed_members;
|
||||
@@ -32,21 +62,21 @@ if (reconstructionOutputSchema === undefined)
|
||||
export const MANAGED_WORKFLOW_TOOL_CONTRACTS = [
|
||||
{
|
||||
name: "compare_managed_members",
|
||||
...toolContractMetadata("compare_managed_members"),
|
||||
description:
|
||||
"Compare two authenticated inspect_managed_members Evidence records using unique-only CIL/signature and structural method-shape tiers. Names are reported as observations but are not used as a matching basis; metadata tokens are remapped as build-local coordinates bound to each artifact SHA-256 and MVID.",
|
||||
kind: "application",
|
||||
inputSchema: compareManagedMembersInputSchema,
|
||||
inputSchema: compareManagedMembersReferenceInputSchema,
|
||||
outputSchema: comparisonOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Compare two managed member observations",
|
||||
input: MANAGED_MEMBER_COMPARISON_EXAMPLE,
|
||||
input: {
|
||||
left_evidence_id: MANAGED_MEMBER_COMPARISON_EXAMPLE.left.evidence_id,
|
||||
right_evidence_id:
|
||||
MANAGED_MEMBER_COMPARISON_EXAMPLE.right.evidence_id,
|
||||
limits: MANAGED_MEMBER_COMPARISON_EXAMPLE.limits,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -72,22 +102,26 @@ export const MANAGED_WORKFLOW_TOOL_CONTRACTS = [
|
||||
},
|
||||
{
|
||||
name: "plan_managed_runtime_correlation",
|
||||
...toolContractMetadata("plan_managed_runtime_correlation"),
|
||||
description:
|
||||
"Prepare a separately authorized managed runtime-correlation admission plan from authenticated inspect_managed_members Evidence. The operation is default-disabled, requires the managed_runtime permission ceiling and grant, locks the exact artifact SHA-256, MVID, method signature, and normalized IL shape, distinguishes attach/load/debugger/reflection/instrumentation effects, and records that no target code was executed.",
|
||||
kind: "application",
|
||||
inputSchema: managedRuntimeCorrelationInputSchema,
|
||||
inputSchema: managedRuntimeCorrelationReferenceInputSchema,
|
||||
outputSchema: runtimeOutputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
title: "Plan an exact-build managed runtime correlation",
|
||||
input: MANAGED_RUNTIME_CORRELATION_EXAMPLE,
|
||||
input: {
|
||||
static_members_evidence_id:
|
||||
MANAGED_RUNTIME_CORRELATION_EXAMPLE.static_members.evidence_id,
|
||||
method: MANAGED_RUNTIME_CORRELATION_EXAMPLE.method,
|
||||
requested_effect:
|
||||
MANAGED_RUNTIME_CORRELATION_EXAMPLE.requested_effect,
|
||||
host: MANAGED_RUNTIME_CORRELATION_EXAMPLE.host,
|
||||
bounds: MANAGED_RUNTIME_CORRELATION_EXAMPLE.bounds,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
] as const satisfies readonly ToolContract[];
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -95,6 +95,10 @@ export const TOOL_EFFECTS: Readonly<Record<string, ToolEffects>> = {
|
||||
inventory_artifact: evidence,
|
||||
extract_artifact: effects({ mutatesSession: true, writesFilesystem: true }),
|
||||
inspect_managed_artifact: evidence,
|
||||
inspect_managed_members: evidence,
|
||||
inspect_managed_native_boundaries: evidence,
|
||||
compare_managed_members: sessionEvidence,
|
||||
plan_managed_runtime_correlation: sessionEvidence,
|
||||
list_browser_targets: browserEvidence,
|
||||
inspect_web_page: browserEvidence,
|
||||
analyze_web_bundle: browserEvidence,
|
||||
|
||||
@@ -183,6 +183,7 @@ export const createServer = (
|
||||
logger: toolLogger,
|
||||
recordEvidence,
|
||||
recordEvidenceWithUnknown,
|
||||
session,
|
||||
runtime: {
|
||||
policy: options.managedRuntimePolicy ?? {
|
||||
enabled: false,
|
||||
|
||||
@@ -1,24 +1,31 @@
|
||||
import type { McpServer } from "@modelcontextprotocol/server";
|
||||
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import { compareManagedMembersEvidence } from "../application/ManagedMemberComparisonService.js";
|
||||
import { compareManagedMembersEvidenceValidated } from "../application/ManagedMemberComparisonService.js";
|
||||
import { importManagedReconstructionEvidence } from "../application/ManagedReconstructionService.js";
|
||||
import {
|
||||
planManagedRuntimeCorrelationEvidence,
|
||||
planManagedRuntimeCorrelationEvidenceValidated,
|
||||
type ManagedRuntimeCorrelationDependencies,
|
||||
} from "../application/ManagedRuntimeCorrelationService.js";
|
||||
import { MANAGED_WORKFLOW_TOOL_CONTRACTS } from "../contracts/managedWorkflowToolContracts.js";
|
||||
import {
|
||||
compareManagedMembersInputSchema,
|
||||
managedMemberComparisonResultSchema,
|
||||
} from "../domain/managedMemberComparison.js";
|
||||
compareManagedMembersReferenceInputSchema,
|
||||
managedRuntimeCorrelationReferenceInputSchema,
|
||||
MANAGED_WORKFLOW_TOOL_CONTRACTS,
|
||||
} from "../contracts/managedWorkflowToolContracts.js";
|
||||
import { managedMemberComparisonResultSchema } from "../domain/managedMemberComparison.js";
|
||||
import { managedReconstructionImportInputSchema } from "../domain/managedReconstruction.js";
|
||||
import { managedRuntimeCorrelationInputSchema } from "../domain/managedRuntimeCorrelation.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import {
|
||||
EvidenceIntegrityError,
|
||||
EvidenceReferenceError,
|
||||
} from "../domain/errors.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
import { safeParseToolInput } from "./toolInputValidation.js";
|
||||
import { resolveSessionEvidenceIds } from "./sessionEvidence.js";
|
||||
|
||||
interface ManagedWorkflowToolRegistration {
|
||||
readonly logger: Logger;
|
||||
@@ -27,6 +34,7 @@ interface ManagedWorkflowToolRegistration {
|
||||
| BinarySessionPort["recordEvidenceWithUnknown"]
|
||||
| undefined;
|
||||
readonly runtime: ManagedRuntimeCorrelationDependencies;
|
||||
readonly session: BinarySessionPort | undefined;
|
||||
}
|
||||
|
||||
/** Register provider-neutral managed-code workflows. */
|
||||
@@ -41,11 +49,31 @@ export const registerManagedWorkflowTools = (
|
||||
compareContract.name,
|
||||
toolRegistrationOptions(compareContract),
|
||||
async (input) => {
|
||||
const parsed = compareManagedMembersInputSchema.parse(input);
|
||||
const parsedInput = safeParseToolInput(
|
||||
compareManagedMembersReferenceInputSchema,
|
||||
input,
|
||||
compareContract.name,
|
||||
);
|
||||
if (!parsedInput.ok)
|
||||
return toCallToolResult(parsedInput, compareContract);
|
||||
const resolved = resolveManagedEvidence(options.session, [
|
||||
parsedInput.value.left_evidence_id,
|
||||
parsedInput.value.right_evidence_id,
|
||||
]);
|
||||
if (!resolved.ok) return toCallToolResult(resolved, compareContract);
|
||||
const [left, right] = resolved.value;
|
||||
if (left === undefined || right === undefined)
|
||||
throw new TypeError("Managed comparison Evidence resolution failed");
|
||||
const {
|
||||
left_evidence_id: _leftEvidenceId,
|
||||
right_evidence_id: _rightEvidenceId,
|
||||
...referencedInput
|
||||
} = parsedInput.value;
|
||||
const parsed = { ...referencedInput, left, right };
|
||||
const result = await logToolExecution(
|
||||
options.logger,
|
||||
compareContract.name,
|
||||
() => Promise.resolve(compareManagedMembersEvidence(parsed)),
|
||||
() => Promise.resolve(compareManagedMembersEvidenceValidated(parsed)),
|
||||
);
|
||||
if (!result.ok) return toCallToolResult(result, compareContract);
|
||||
const recorded = recordSources(options.recordEvidence, [
|
||||
@@ -117,11 +145,33 @@ export const registerManagedWorkflowTools = (
|
||||
runtimeContract.name,
|
||||
toolRegistrationOptions(runtimeContract),
|
||||
async (input) => {
|
||||
const parsed = managedRuntimeCorrelationInputSchema.parse(input);
|
||||
const parsedInput = safeParseToolInput(
|
||||
managedRuntimeCorrelationReferenceInputSchema,
|
||||
input,
|
||||
runtimeContract.name,
|
||||
);
|
||||
if (!parsedInput.ok)
|
||||
return toCallToolResult(parsedInput, runtimeContract);
|
||||
const resolved = resolveManagedEvidence(options.session, [
|
||||
parsedInput.value.static_members_evidence_id,
|
||||
]);
|
||||
if (!resolved.ok) return toCallToolResult(resolved, runtimeContract);
|
||||
const staticMembers = resolved.value[0];
|
||||
if (staticMembers === undefined)
|
||||
throw new TypeError("Managed runtime Evidence resolution failed");
|
||||
const {
|
||||
static_members_evidence_id: _staticMembersEvidenceId,
|
||||
...referencedInput
|
||||
} = parsedInput.value;
|
||||
const parsed = { ...referencedInput, static_members: staticMembers };
|
||||
const result = await logToolExecution(
|
||||
options.logger,
|
||||
runtimeContract.name,
|
||||
() => planManagedRuntimeCorrelationEvidence(options.runtime, parsed),
|
||||
() =>
|
||||
planManagedRuntimeCorrelationEvidenceValidated(
|
||||
options.runtime,
|
||||
parsed,
|
||||
),
|
||||
);
|
||||
if (!result.ok) return toCallToolResult(result, runtimeContract);
|
||||
const recordedSource = options.recordEvidence?.(parsed.static_members);
|
||||
@@ -147,6 +197,26 @@ const contract = (
|
||||
return found;
|
||||
};
|
||||
|
||||
const resolveManagedEvidence = (
|
||||
session: BinarySessionPort | undefined,
|
||||
evidenceIds: readonly string[],
|
||||
): Result<Evidence[], EvidenceIntegrityError> => {
|
||||
const firstId = evidenceIds[0] ?? "ev_missing";
|
||||
return session === undefined
|
||||
? err(
|
||||
new EvidenceReferenceError(
|
||||
firstId,
|
||||
"missing",
|
||||
"inspect_managed_members",
|
||||
null,
|
||||
),
|
||||
)
|
||||
: resolveSessionEvidenceIds(session, evidenceIds, {
|
||||
operation: "inspect_managed_members",
|
||||
predicate: "rea.analysis/v2",
|
||||
});
|
||||
};
|
||||
|
||||
const recordSources = (
|
||||
recordEvidence: ManagedWorkflowToolRegistration["recordEvidence"],
|
||||
sources: readonly Evidence[],
|
||||
|
||||
@@ -127,13 +127,35 @@ const compactJsonSchema = (
|
||||
const counts = new Map<string, number>();
|
||||
collectSchemas(schemaBody, counts, true);
|
||||
const candidates = [...counts.entries()]
|
||||
.filter(([encoded, count]) => count > 1 && encoded.length >= 128)
|
||||
.filter(([, count]) => count > 1)
|
||||
.sort(
|
||||
([left], [right]) =>
|
||||
right.length - left.length || left.localeCompare(right),
|
||||
)
|
||||
.map(([encoded], index) => [encoded, `rea_${String(index)}`] as const);
|
||||
.map(([encoded]) => encoded);
|
||||
if (candidates.length === 0) return schemaBody;
|
||||
let compacted: Record<string, unknown> = schemaBody;
|
||||
let compactedBytes = JSON.stringify(compacted).length;
|
||||
const selected: Array<readonly [string, string]> = [];
|
||||
for (const encoded of candidates) {
|
||||
const proposed = [
|
||||
...selected,
|
||||
[encoded, `rea_${String(selected.length)}`] as const,
|
||||
];
|
||||
const candidate = compactWithDefinitions(schemaBody, proposed);
|
||||
const candidateBytes = JSON.stringify(candidate).length;
|
||||
if (candidateBytes >= compactedBytes) continue;
|
||||
selected.push([encoded, `rea_${String(selected.length)}`]);
|
||||
compacted = candidate;
|
||||
compactedBytes = candidateBytes;
|
||||
}
|
||||
return compacted;
|
||||
};
|
||||
|
||||
const compactWithDefinitions = (
|
||||
schemaBody: Readonly<Record<string, unknown>>,
|
||||
candidates: readonly (readonly [string, string])[],
|
||||
): Record<string, unknown> => {
|
||||
const names = new Map(candidates);
|
||||
const definitions = Object.fromEntries(
|
||||
candidates.map(([encoded, name]) => [
|
||||
|
||||
Reference in New Issue
Block a user