@receiz/mcp-server
Version:
MCP server for agents building and verifying Receiz proof-native applications through the canonical Receiz SDK rails.
94 lines • 6.68 kB
JavaScript
import { createReceizArtifactAdmissionEngine, verifyReceizArtifact, receizBase64UrlDecode, createReceizExternalExecutionProfileV125, createReceizExternalExecutionRuntimeV125, planReceizExternalExecutionV125, verifyReceizExternalExecutionPlanV125, verifyReceizExternalExecutionOutcomeV125, } from "@receiz/sdk";
import { digestReceizV124McpMaterial } from "./v124Operations.js";
function fail(reason) { throw new TypeError(`V125_TRUST_MCP_EXTERNAL_${reason}`); }
/** Dispatch only named SDK execution operations. Sources are admitted before projecting their exact heads. */
export function createReceizV125TrustExternalOperations(input) {
const configuration = input.configuration;
const profiles = new Map(configuration.profiles.map((profile) => [profile.profileId, createReceizExternalExecutionProfileV125(profile)]));
if (profiles.size !== configuration.profiles.length)
fail("PROFILE_DUPLICATE");
const runtime = createReceizExternalExecutionRuntimeV125({ store: configuration.store });
const plans = new Map();
const results = new Map();
async function material(reference) {
const raw = structuredClone(await input.resolve(reference));
if (!raw || typeof raw !== "object" || Array.isArray(raw)
|| Object.keys(raw).sort().join() !== ["profileId", "operationId", "request", "idempotencyKey", "sources"].sort().join())
fail("MATERIAL_INVALID");
const profile = profiles.get(raw.profileId);
if (!profile)
fail("PROFILE_REQUIRED");
if (!raw.sources || Object.keys(raw.sources).sort().join() !== [...profile.requiredProofHeads].sort().join())
fail("SOURCES_REQUIRED");
const proofHeads = {};
for (const [name, source] of Object.entries(raw.sources)) {
const artifact = source.artifact;
const bytes = receizBase64UrlDecode(artifact.exactBytesB64u);
const verification = await verifyReceizArtifact(new File([new Uint8Array(bytes).buffer], artifact.filename, { type: artifact.mimeType }));
const admission = await createReceizArtifactAdmissionEngine()(verification, { profile: "portable-state" });
if (admission.verdict !== "bearer-recovery" || admission.profile !== "portable-state"
|| admission.artifactSha256 !== artifact.artifactSha256 || admission.payloadSha256 !== artifact.payloadSha256
|| !admission.proofHistory || admission.proofHistory.headDigests.length !== 1
|| admission.proofHistory.headDigests[0] !== source.expectedHead)
fail("SOURCE_REQUIRED");
proofHeads[name] = admission.proofHistory.headDigests[0];
}
return { material: raw, profile, proofHeads };
}
return async (request) => {
if (request.operation.endsWith("_plan")) {
const opened = await material(request.proofReference);
if (request.idempotencyKey !== opened.material.idempotencyKey || request.confirmationDigest !== null)
fail("BINDING_INVALID");
const plan = await planReceizExternalExecutionV125({ ...opened.material, profile: opened.profile, proofHeads: opened.proofHeads });
const held = Object.freeze({ sourceReference: request.proofReference,
sourceDigest: await digestReceizV124McpMaterial(opened.material), material: opened.material, plan });
const planReference = await input.persist(held);
plans.set(planReference, held);
return { status: "planned", planReference, writesPerformed: 0 };
}
if (request.operation.endsWith("_execute")) {
const held = plans.get(request.proofReference);
if (!held)
fail("PLAN_CUSTODY_REQUIRED");
if (request.idempotencyKey !== held.plan.idempotencyKey || request.selectedKai === null)
fail("BINDING_INVALID");
const profile = profiles.get(held.plan.profileId);
if (!await verifyReceizExternalExecutionPlanV125({ profile, plan: held.plan, request: held.material.request }))
fail("PLAN_INVALID");
if (request.confirmationDigest === null)
return { status: "confirmation-required", confirmationRequired: true,
...await input.attempts.prepare(request, held.plan), writesPerformed: 0 };
const reserved = await input.attempts.reserve(request, request.confirmationDigest);
if (reserved.plan !== held.plan)
fail("PLAN_CUSTODY_REQUIRED");
if (await digestReceizV124McpMaterial(await input.resolve(held.sourceReference)) !== held.sourceDigest)
fail("SOURCE_CHANGED");
const resolution = await runtime.execute({ profile, plan: held.plan, request: held.material.request,
confirmationDigest: held.plan.confirmationDigest, resolvedKai: request.selectedKai,
provider: (delivery) => configuration.provider(Object.freeze({ ...delivery, profileId: profile.profileId,
request: structuredClone(held.material.request), proofHeads: held.plan.proofHeads })) });
if (resolution.status !== "committed")
return resolution;
if (!await verifyReceizExternalExecutionOutcomeV125({ profile, plan: held.plan, outcome: resolution.outcome }))
fail("OUTCOME_INVALID");
const recovery = { held, durableHandle: resolution.outcome.durableHandle };
const retained = await input.retain(recovery);
results.set(retained.reference, recovery);
return { status: "committed", resultReference: retained.reference, writesPerformed: resolution.writesPerformed,
...(retained.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}) };
}
const recovery = results.get(request.proofReference);
if (!recovery)
fail("RESULT_CUSTODY_REQUIRED");
const resolution = await runtime.recover({ planIdentity: recovery.held.plan.planIdentity, durableHandle: recovery.durableHandle });
if (resolution.status !== "committed")
return resolution;
const valid = await verifyReceizExternalExecutionOutcomeV125({ profile: profiles.get(recovery.held.plan.profileId),
plan: recovery.held.plan, outcome: resolution.outcome });
if (!valid)
fail("OUTCOME_INVALID");
return { status: request.operation.endsWith("_verify") ? "verified" : "committed", resultReference: request.proofReference, writesPerformed: 0 };
};
}
//# sourceMappingURL=v125TrustExternal.js.map