UNPKG

@receiz/mcp-server

Version:

MCP server for agents building and verifying Receiz proof-native applications through the canonical Receiz SDK rails.

597 lines • 46.6 kB
import { planReceizSubjectMessageEventV125, applyReceizSubjectMessageEventV125, openReceizSubjectThreadSourceFamilyV125 } from "@receiz/sdk"; import { digestReceizV124McpMaterial } from "./v124Operations.js"; import { createReceizV125TrustExternalOperations } from "./v125TrustExternal.js"; import { planReceizSubjectRelationshipEventV125, applyReceizSubjectRelationshipEventV125, discoverReceizSubjectsV125, planReceizSubjectPresenceEventV125, applyReceizSubjectPresenceEventV125, createReceizAttestationAuthorityV125, planReceizInitialAttestationSourceV125, planReceizInitialCredentialSourceV125, applyReceizInitialSourceV125, verifyAndOpenReceizCredentialArtifactV125, resolveReceizCredentialCurrentnessV125, createReceizCredentialPresentationV125, createReceizSourceBoundCredentialPresentationVerifierV125, verifyReceizCredentialPresentationV125, exportReceizCredentialPresentationFileV125, createReceizCredentialPresentationContentUrlV125, createReceizCredentialPresentationQrHandoffV125, createReceizCredentialPresentationNfcHandoffV125, createReceizPbiCeremonyChallengeV125, approveReceizPbiCeremonyV125, issueReceizPbiCeremonyReceiptV125, verifyReceizPbiCeremonyReceiptV125, planReceizCredentialLifecycleEventV125, applyReceizCredentialLifecycleEventV125, verifyAndOpenReceizAttestationArtifactV125, planReceizAttestationEventV125, applyReceizAttestationEventV125 } from "@receiz/sdk"; import { admitReceizV124McpMaterial, persistReceizV124McpMaterial, resolveReceizV124McpMaterial, } from "./v124Operations.js"; import { createReceizV125TrustAttemptCustody } from "./v125TrustCustody.js"; const canonicalHosts = new WeakMap(); export const isReceizV125TrustMcpHost = (host) => canonicalHosts.has(host); export function assertReceizV125TrustMcpHostScope(host, scope) { const held = canonicalHosts.get(host); if (!held || held.client !== scope.client || held.applicationAudience !== scope.applicationAudience) { throw new TypeError("V125_TRUST_MCP_HOST_SCOPE_MISMATCH"); } } const initialKinds = Object.freeze({ receiz_v125_attestation_application: "attestation", receiz_v125_credential_issue: "credential", }); const attestationKinds = Object.freeze({ receiz_v125_attestation_workflow: ["submitted", "evidence_requested", "evidence_submitted", "evaluation_complete", "review_complete", "approval_recorded", "issued", "denied", "withdrawn", "expired"], receiz_v125_attestation_evidence: ["evidence_requested", "evidence_submitted"], receiz_v125_attestation_evaluation: ["evaluation_complete"], receiz_v125_attestation_review: ["review_complete"], receiz_v125_attestation_approval: ["approval_recorded"], }); const relationPlans = new Set(["receiz_v125_subject_relationship_plan", "receiz_v125_subject_introduction_plan"]); const relationApplies = new Set(["receiz_v125_subject_relationship_apply", "receiz_v125_subject_introduction_apply"]); const unavailable = (missingBoundary) => Object.freeze({ status: "unavailable", code: "V125_TRUST_HOST_CAPABILITY_UNAVAILABLE", missingBoundary, writesPerformed: 0 }); /** Named SDK bindings; private references and confirmation never grant proof authority. */ export function createReceizV125TrustMcpOperations(input) { const client = input.client; const options = { ...input.materialOptions, applicationId: input.applicationAudience }; const attempts = createReceizV125TrustAttemptCustody({ client, applicationAudience: input.applicationAudience, materialOptions: input.materialOptions }); const plans = new Map(); const presencePlans = new Map(); const initialPlans = new Map(); const credentialPlans = new Map(); const attestationPlans = new Map(); const domainAuthorities = new Map(); const resolvePresentationPbi = input.credentialPresentation?.resolvePbiAuthorship; const handoffConfiguration = input.credentialPresentation?.handoff ? Object.freeze({ ...input.credentialPresentation.handoff }) : null; const pbiMechanics = input.pbiCeremony ? Object.freeze({ ...input.pbiCeremony }) : null; const pbiSelection = (value) => Object.freeze({ proofObjectId: value.proofObjectId, acceptedHead: value.acceptedHead, holderReceizId: value.holderReceizId }); const persist = (kind, value) => persistReceizV124McpMaterial(client, options, kind, value); const resolve = (reference) => resolveReceizV124McpMaterial(client, options, reference, "operation-plan"); const retainCommitted = async (kind, value) => { const localReference = admitReceizV124McpMaterial(client, { kind, applicationId: input.applicationAudience, value }); if (!options.persistMaterial) return { reference: localReference, pending: false }; try { return { reference: await persist(kind, value), pending: false }; } catch { return { reference: localReference, pending: true }; } }; const external = input.externalExecution ? createReceizV125TrustExternalOperations({ configuration: input.externalExecution, resolve, persist: (value) => persist("operation-plan", value), retain: (value) => retainCommitted("operation-plan", value), attempts }) : null; function bindRequest(request, material) { if (!material?.event || !material.sealedSource || request.idempotencyKey !== material.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== material.event.eventKai)) { throw new TypeError("V125_TRUST_MCP_SUBJECT_BINDING_INVALID"); } const introduction = request.operation.includes("_introduction_"); if (introduction !== material.event.kind.startsWith("introduction_")) { throw new TypeError("V125_TRUST_MCP_SUBJECT_OPERATION_MISMATCH"); } } const messagePlans = new Map(); const host = async (request) => { if (request.operation.startsWith("receiz_v125_subject_message_")) { if (!input.subjectMessage) return unavailable("thread-source-and-atomic-commit-mechanics"); if (request.operation.endsWith("_plan")) { const material = structuredClone(await resolve(request.proofReference)); if (!material || Object.keys(material).sort().join() !== "carried,commitDomain,kksCoordinate,thread") throw new TypeError("V125_TRUST_MCP_THREAD_MATERIAL_INVALID"); const thread = await openReceizSubjectThreadSourceFamilyV125(material.thread); const plan = await planReceizSubjectMessageEventV125({ ...material, thread }); if (request.idempotencyKey !== material.carried.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== String(plan.event.kaiUPulse)) || request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_THREAD_BINDING_INVALID"); const held = Object.freeze({ material, plan, sourceReference: request.proofReference, sourceDigest: await digestReceizV124McpMaterial(material) }); const planReference = await persist("operation-plan", held); messagePlans.set(planReference, held); return { status: "planned", planReference, writesPerformed: 0 }; } const held = messagePlans.get(request.proofReference); if (!held) return unavailable("same-runtime-canonical-plan-custody"); if (request.idempotencyKey !== held.material.carried.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== String(held.plan.event.kaiUPulse))) throw new TypeError("V125_TRUST_MCP_THREAD_BINDING_INVALID"); if (request.confirmationDigest === null) return { status: "confirmation-required", confirmationRequired: true, ...await attempts.prepare(request, held.plan), writesPerformed: 0 }; const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_THREAD_PLAN_CUSTODY_REQUIRED"); if (await digestReceizV124McpMaterial(await resolve(held.sourceReference)) !== held.sourceDigest) throw new TypeError("V125_TRUST_MCP_THREAD_SOURCE_CHANGED"); const mechanics = await input.subjectMessage.resolveCommitAuthority(held.plan); const result = await applyReceizSubjectMessageEventV125({ signedCapability: mechanics.signedCapability, sealer: mechanics.sealer, store: mechanics.store, plan: held.plan, confirmationDigest: held.plan.confirmationDigest }); const retained = await retainCommitted("operation-plan", result.sourceFamily); return { status: "committed", sourceFamilyReference: retained.reference, acceptedHead: result.acceptedHead, writesPerformed: result.writesPerformed, ...(retained.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}) }; } if (request.operation.startsWith("receiz_v125_external_execution_")) { return external ? external(request) : unavailable("external-execution-custody-and-provider"); } if (request.operation.startsWith("receiz_v125_pbi_ceremony_")) { if (request.selectedKai === null) throw new TypeError("V125_TRUST_MCP_PBI_KAI_REQUIRED"); const create = request.operation.endsWith("_challenge_create"); const approve = request.operation.endsWith("_approve"); const issue = request.operation.endsWith("_receipt_issue"); const redeem = request.operation.endsWith("_challenge_redeem"); const required = [...(create ? ["challenge"] : approve ? ["challenge", "pbiAuthorship"] : issue ? ["challenge", "approval"] : ["receipt"]), ...(request.idempotencyKey === null ? [] : ["idempotencyKey"])]; const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== required.sort().join()) throw new TypeError("V125_TRUST_MCP_PBI_INPUT_INVALID"); const material = structuredClone(raw); if (request.idempotencyKey !== null && request.idempotencyKey !== material.idempotencyKey) throw new TypeError("V125_TRUST_MCP_PBI_BINDING_INVALID"); if (!approve && !redeem && request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PBI_CONFIRMATION_UNEXPECTED"); if (create || approve || issue) { const challenge = await createReceizPbiCeremonyChallengeV125(material.challenge); if (create) { if (request.selectedKai !== challenge.issuedKai) throw new TypeError("V125_TRUST_MCP_PBI_BINDING_INVALID"); return Object.freeze({ status: "created", resultReference: await persist("operation-plan", challenge), writesPerformed: 0 }); } if (material.challenge.schema !== challenge.schema || material.challenge.challengeDigest !== challenge.challengeDigest || BigInt(request.selectedKai) < BigInt(challenge.issuedKai) || BigInt(request.selectedKai) >= BigInt(challenge.expiresAtKai)) { throw new TypeError("V125_TRUST_MCP_PBI_BINDING_INVALID"); } if (!pbiMechanics || (approve && !pbiMechanics.resolveHolderSigner)) return unavailable("pbi-source-and-holder-signing-mechanics"); if (approve) { if (request.confirmationDigest === null) { const plan = Object.freeze({ challenge, pbiAuthorship: material.pbiAuthorship }); return Object.freeze({ status: "confirmation-required", confirmationRequired: true, ...await attempts.prepare(request, plan), writesPerformed: 0 }); } const reserved = await attempts.reserve(request, request.confirmationDigest); const plan = reserved.plan; const verifyPbiAuthorship = await pbiMechanics.resolveAuthorshipVerifier(pbiSelection(plan.challenge)); const signer = await pbiMechanics.resolveHolderSigner(plan.challenge); const approval = await approveReceizPbiCeremonyV125({ challenge: plan.challenge, pbiAuthorship: plan.pbiAuthorship, keyFile: signer.keyFile, passphrase: signer.passphrase, verifyPbiAuthorship }); return Object.freeze({ status: "approved", resultReference: await persist("operation-plan", approval), writesPerformed: 0 }); } const verifyPbiAuthorship = await pbiMechanics.resolveAuthorshipVerifier(pbiSelection(challenge)); const receipt = await issueReceizPbiCeremonyReceiptV125({ challenge, approval: material.approval, verifyPbiAuthorship }); return Object.freeze({ status: "issued", resultReference: await persist("operation-plan", receipt), writesPerformed: 0 }); } if (!pbiMechanics || (redeem && !pbiMechanics.replayGuard)) return unavailable("pbi-source-and-replay-mechanics"); const verifyPbiAuthorship = await pbiMechanics.resolveAuthorshipVerifier(pbiSelection(material.receipt)); const readOnlyResult = await verifyReceizPbiCeremonyReceiptV125({ receipt: material.receipt, selectedKai: request.selectedKai, verifyPbiAuthorship }); if (!redeem || !readOnlyResult.valid) return Object.freeze({ status: readOnlyResult.code, resultReference: await persist("operation-plan", readOnlyResult), writesPerformed: 0 }); if (request.confirmationDigest === null) { const receipt = Object.freeze(material.receipt); return Object.freeze({ status: "confirmation-required", confirmationRequired: true, ...await attempts.prepare(request, receipt), writesPerformed: 0 }); } const reserved = await attempts.reserve(request, request.confirmationDigest); const receipt = reserved.plan; const confirmedVerifier = await pbiMechanics.resolveAuthorshipVerifier(pbiSelection(receipt)); const result = await verifyReceizPbiCeremonyReceiptV125({ receipt, selectedKai: request.selectedKai, verifyPbiAuthorship: confirmedVerifier, replayGuard: pbiMechanics.replayGuard }); // Redemption already happened; optional reference persistence cannot erase it. const local = admitReceizV124McpMaterial(client, { kind: "operation-plan", applicationId: input.applicationAudience, value: result }); let resultReference = local; let pending = false; if (options.persistMaterial) { try { resultReference = await persist("operation-plan", result); } catch { pending = true; } } return Object.freeze({ status: result.code, resultReference, writesPerformed: result.writesPerformed, ...(pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}) }); } if (request.operation.startsWith("receiz_v125_credential_presentation_")) { if (request.selectedKai === null) throw new TypeError("V125_TRUST_MCP_PRESENTATION_KAI_REQUIRED"); if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PRESENTATION_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); const create = request.operation.endsWith("_create"); const required = create ? ["domainReference", "sourceArtifact", "disclose", "idempotencyKey"] : ["domainReference", "sourceArtifact", "presentation"]; const allowed = new Set([...required, ...(create ? [] : ["holderPresence"])]); if (!raw || typeof raw !== "object" || Array.isArray(raw) || required.some((key) => !Object.hasOwn(raw, key)) || Object.keys(raw).some((key) => !allowed.has(key))) { throw new TypeError("V125_TRUST_MCP_PRESENTATION_INPUT_INVALID"); } const material = structuredClone(raw); const authority = domainAuthorities.get(material.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); const opened = await verifyAndOpenReceizCredentialArtifactV125({ artifact: material.sourceArtifact, verifier: {} }, authority); if (create) { if (request.idempotencyKey !== material.idempotencyKey) throw new TypeError("V125_TRUST_MCP_PRESENTATION_BINDING_INVALID"); const currentness = await resolveReceizCredentialCurrentnessV125({ issuance: opened.issuance, credential: opened.history.credential, verifiedHistory: opened.history, presentedHead: opened.history.credential.acceptedHead, selectedKai: request.selectedKai }, authority); if (!currentness.credentialValid) throw new TypeError("V125_TRUST_MCP_PRESENTATION_VERIFICATION_REQUIRED"); const presentation = await createReceizCredentialPresentationV125({ domain: opened.history.domain, claimSet: opened.issuance.attestation.genesis.claimSet, credential: { ...opened.history.credential, acceptedHead: currentness.acceptedHead }, disclose: material.disclose, selectedKai: request.selectedKai }); return Object.freeze({ status: "created", resultReference: await persist("operation-plan", presentation), writesPerformed: 0 }); } const verifyCredential = await createReceizSourceBoundCredentialPresentationVerifierV125({ artifact: material.sourceArtifact, verifier: {} }, authority); const verifyPbiAuthorship = material.holderPresence && resolvePresentationPbi ? await resolvePresentationPbi({ sourceArtifact: structuredClone(material.sourceArtifact), authority }) : undefined; // Verification and transport never redeem a one-use holder ceremony. const verification = await verifyReceizCredentialPresentationV125({ domain: opened.history.domain, presentation: material.presentation, selectedKai: request.selectedKai, verifyCredential, ...(material.holderPresence ? { holderPresence: material.holderPresence } : {}), ...(verifyPbiAuthorship ? { verifyPbiAuthorship } : {}) }); const valid = verification.credentialValid && verification.disclosuresValid && verification.failureCodes.length === 0; if (request.operation.endsWith("_verify")) return Object.freeze({ status: valid ? "verified" : "invalid", resultReference: await persist("operation-plan", verification), writesPerformed: 0 }); if (!valid) throw new TypeError("V125_TRUST_MCP_PRESENTATION_VERIFICATION_REQUIRED"); const file = await exportReceizCredentialPresentationFileV125(material.presentation); const contentUrl = handoffConfiguration ? await createReceizCredentialPresentationContentUrlV125({ origin: handoffConfiguration.origin, presentation: material.presentation }) : null; const handoff = { file, ...(contentUrl && handoffConfiguration ? { contentUrl, qr: await createReceizCredentialPresentationQrHandoffV125({ contentUrl, presentationDigest: material.presentation.presentationDigest }), nfc: await createReceizCredentialPresentationNfcHandoffV125({ contentUrl, presentationDigest: material.presentation.presentationDigest, nfcAvailable: handoffConfiguration.nfcAvailable, maximumPayloadBytes: handoffConfiguration.maximumNfcPayloadBytes }) } : {}) }; return Object.freeze({ status: "resolved", resultReference: await persist("operation-plan", handoff), writesPerformed: 0 }); } const initialKind = initialKinds[request.operation.replace(/_(plan|apply)$/u, "")]; if (initialKind && request.operation.endsWith("_plan")) { if (!input.attestation?.resolveInitialActorEvidence || !input.attestation.resolveInitialCommitAuthority) { return unavailable("initial-source-canonical-commit-mechanics"); } if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PLAN_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); const keys = ["domainReference", "commitDomain", "expectedDomainArtifactDigest", "expectedDomainHead", "kksCoordinate", "idempotencyKey", ...(initialKind === "attestation" ? ["application"] : ["issuanceSource", "expectedIssuanceArtifactDigest", "expectedIssuanceHead"])]; if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== keys.sort().join()) { throw new TypeError("V125_TRUST_MCP_INITIAL_INPUT_INVALID"); } const material = structuredClone(raw); if (request.idempotencyKey !== material.idempotencyKey) throw new TypeError("V125_TRUST_MCP_INITIAL_BINDING_INVALID"); const authority = domainAuthorities.get(material.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); const verifiedActorEvidence = await input.attestation.resolveInitialActorEvidence({ kind: initialKind, material: structuredClone(material) }); const { domainReference: _domainReference, ...parameters } = material; const plan = initialKind === "attestation" ? await planReceizInitialAttestationSourceV125({ ...parameters, authority, verifiedActorEvidence }) : await planReceizInitialCredentialSourceV125({ ...parameters, authority, verifiedActorEvidence, verifier: {} }); if (request.selectedKai !== null && request.selectedKai !== plan.kaiUpulse) throw new TypeError("V125_TRUST_MCP_INITIAL_BINDING_INVALID"); const held = Object.freeze({ schema: "receiz.mcp.initial-source-plan.v125", material, plan }); const planReference = await persist("operation-plan", held); initialPlans.set(planReference, held); return Object.freeze({ status: "planned", planReference, writesPerformed: 0 }); } if (initialKind && request.operation.endsWith("_apply")) { if (!input.attestation?.resolveInitialCommitAuthority) return unavailable("initial-source-canonical-commit-mechanics"); const held = initialPlans.get(request.proofReference); if (!held) return unavailable("same-runtime-canonical-plan-custody"); if (held.plan.kind !== initialKind) throw new TypeError("V125_TRUST_MCP_INITIAL_OPERATION_MISMATCH"); if (request.idempotencyKey !== held.material.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== held.plan.kaiUpulse)) throw new TypeError("V125_TRUST_MCP_INITIAL_BINDING_INVALID"); if (request.confirmationDigest === null) return Object.freeze({ status: "confirmation-required", confirmationRequired: true, planReference: request.proofReference, ...await attempts.prepare(request, held.plan), writesPerformed: 0 }); const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const mechanics = await input.attestation.resolveInitialCommitAuthority(held.plan); const result = await applyReceizInitialSourceV125({ plan: held.plan, confirmationDigest: held.plan.confirmationDigest, capability: mechanics.capability, sealer: mechanics.sealer, store: mechanics.store, verifier: {} }); const proof = await retainCommitted("sealed-source-artifact", result.sourceArtifact); return Object.freeze({ status: "committed", proofReference: proof.reference, ...(proof.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}), acceptedHead: held.plan.proposedGenesisHead, writesPerformed: result.writesPerformed }); } const permittedAttestationKinds = attestationKinds[request.operation.replace(/_(plan|apply)$/u, "")]; if (permittedAttestationKinds && request.operation.endsWith("_plan")) { if (!input.attestation?.resolveAttestationCommitAuthority) return unavailable("attestation-canonical-commit-mechanics"); if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PLAN_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== "domainReference,event,sealedSource") throw new TypeError("V125_TRUST_MCP_ATTESTATION_INPUT_INVALID"); const material = structuredClone(raw); const context = material.sealedSource; if (!context || typeof context !== "object" || Array.isArray(context) || Object.keys(context).sort().join() !== "commitDomain,expectedArtifactDigest,expectedHead,identityArtifact,kksCoordinate,sourceArtifact") { throw new TypeError("V125_TRUST_MCP_ATTESTATION_SOURCE_INVALID"); } const authority = domainAuthorities.get(material.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); if (!material.event || !permittedAttestationKinds.includes(material.event.kind)) throw new TypeError("V125_TRUST_MCP_ATTESTATION_OPERATION_MISMATCH"); if (request.idempotencyKey !== material.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== material.event.eventKai)) throw new TypeError("V125_TRUST_MCP_ATTESTATION_BINDING_INVALID"); const opened = await verifyAndOpenReceizAttestationArtifactV125({ artifact: context.sourceArtifact, verifier: {} }, authority); const planInput = { domain: opened.domain, genesis: opened.genesis, events: opened.state.history, event: material.event, sealedSource: { ...context, verifier: {} } }; const plan = await planReceizAttestationEventV125(planInput, authority); if (!plan.transitionPlan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const held = Object.freeze({ schema: "receiz.mcp.attestation-plan.v125", domainReference: material.domainReference, input: planInput, plan }); const planReference = await persist("operation-plan", held); attestationPlans.set(planReference, held); return Object.freeze({ status: "planned", planReference, writesPerformed: 0 }); } if (permittedAttestationKinds && request.operation.endsWith("_apply")) { if (!input.attestation?.resolveAttestationCommitAuthority) return unavailable("attestation-canonical-commit-mechanics"); const held = attestationPlans.get(request.proofReference); if (!held) return unavailable("same-runtime-canonical-plan-custody"); if (!permittedAttestationKinds.includes(held.plan.event.kind)) throw new TypeError("V125_TRUST_MCP_ATTESTATION_OPERATION_MISMATCH"); if (request.idempotencyKey !== held.input.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== held.input.event.eventKai)) throw new TypeError("V125_TRUST_MCP_ATTESTATION_BINDING_INVALID"); const authority = domainAuthorities.get(held.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); if (request.confirmationDigest === null) return Object.freeze({ status: "confirmation-required", confirmationRequired: true, planReference: request.proofReference, ...await attempts.prepare(request, held.plan), writesPerformed: 0 }); const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const mechanics = await input.attestation.resolveAttestationCommitAuthority(held.plan); const result = await applyReceizAttestationEventV125({ domain: held.input.domain, genesis: held.input.genesis, events: held.input.events, plan: held.plan, confirmationDigest: held.plan.confirmationDigest, capability: mechanics.capability, sealer: mechanics.sealer, store: mechanics.store }, authority); const proof = await retainCommitted("sealed-source-artifact", result.sourceArtifact); const transition = await retainCommitted("committed-artifact-transitions", [result.committedTransition]); return Object.freeze({ status: "committed", proofReference: proof.reference, resultReference: transition.reference, ...(proof.pending || transition.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}), acceptedHead: result.committedTransition.receipt.acceptedHead.value, writesPerformed: result.writesPerformed }); } if (request.operation === "receiz_v125_credential_lifecycle_plan") { if (!input.attestation?.resolveCredentialCommitAuthority) return unavailable("credential-canonical-commit-mechanics"); if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PLAN_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== "domainReference,event,sealedSource") throw new TypeError("V125_TRUST_MCP_CREDENTIAL_INPUT_INVALID"); const material = structuredClone(raw); const context = material.sealedSource; if (!context || typeof context !== "object" || Array.isArray(context) || Object.keys(context).sort().join() !== "commitDomain,expectedArtifactDigest,expectedHead,identityArtifact,kksCoordinate,sourceArtifact") { throw new TypeError("V125_TRUST_MCP_CREDENTIAL_SOURCE_INVALID"); } const authority = domainAuthorities.get(material.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); if (!material.event || request.idempotencyKey !== material.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== material.event.eventKai)) throw new TypeError("V125_TRUST_MCP_CREDENTIAL_BINDING_INVALID"); const opened = await verifyAndOpenReceizCredentialArtifactV125({ artifact: context.sourceArtifact, verifier: {} }, authority); const planInput = { issuance: opened.issuance, credential: opened.history.credential, events: opened.history.events, event: material.event, sealedSource: { ...context, verifier: {} } }; const plan = await planReceizCredentialLifecycleEventV125(planInput, authority); if (!plan.transitionPlan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const held = Object.freeze({ schema: "receiz.mcp.credential-plan.v125", domainReference: material.domainReference, input: planInput, plan }); const planReference = await persist("operation-plan", held); credentialPlans.set(planReference, held); return Object.freeze({ status: "planned", planReference, writesPerformed: 0 }); } if (request.operation === "receiz_v125_credential_lifecycle_apply") { if (!input.attestation?.resolveCredentialCommitAuthority) return unavailable("credential-canonical-commit-mechanics"); const held = credentialPlans.get(request.proofReference); if (!held) return unavailable("same-runtime-canonical-plan-custody"); if (request.idempotencyKey !== held.input.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== held.input.event.eventKai)) throw new TypeError("V125_TRUST_MCP_CREDENTIAL_BINDING_INVALID"); const authority = domainAuthorities.get(held.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); if (request.confirmationDigest === null) return Object.freeze({ status: "confirmation-required", confirmationRequired: true, planReference: request.proofReference, ...await attempts.prepare(request, held.plan), writesPerformed: 0 }); const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const mechanics = await input.attestation.resolveCredentialCommitAuthority(held.plan); const result = await applyReceizCredentialLifecycleEventV125({ issuance: held.input.issuance, credential: held.input.credential, events: held.input.events, plan: held.plan, confirmationDigest: held.plan.confirmationDigest, capability: mechanics.capability, sealer: mechanics.sealer, store: mechanics.store }, authority); const proof = await retainCommitted("sealed-source-artifact", result.sourceArtifact); const transition = await retainCommitted("committed-artifact-transitions", [result.committedTransition]); return Object.freeze({ status: "committed", proofReference: proof.reference, resultReference: transition.reference, ...(proof.pending || transition.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}), acceptedHead: result.committedTransition.receipt.acceptedHead.value, writesPerformed: result.writesPerformed }); } if (request.operation === "receiz_v125_attestation_domain_resolve") { if (!input.attestation) return unavailable("attestation-domain-source-verification"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).join() !== "sourceArtifact") throw new TypeError("V125_TRUST_MCP_DOMAIN_INPUT_INVALID"); const material = structuredClone(raw); const authority = await createReceizAttestationAuthorityV125({ domainAuthorityArtifact: material.sourceArtifact, resolveAdmittedGrantor: input.attestation.resolveAdmittedGrantor, resolveEvidenceArtifact: input.attestation.resolveEvidenceArtifact, }); const resultReference = await persist("operation-plan", authority); domainAuthorities.set(resultReference, authority); return Object.freeze({ status: "resolved", resultReference, acceptedHead: authority.acceptedDomainHead, writesPerformed: 0 }); } if (request.operation === "receiz_v125_credential_currentness_resolve") { if (request.selectedKai === null) throw new TypeError("V125_TRUST_MCP_CURRENTNESS_KAI_REQUIRED"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== "domainReference,presentedHead,sourceArtifact") { throw new TypeError("V125_TRUST_MCP_CURRENTNESS_INPUT_INVALID"); } const material = structuredClone(raw); const authority = domainAuthorities.get(material.domainReference); if (!authority) return unavailable("same-runtime-domain-authority-custody"); const opened = await verifyAndOpenReceizCredentialArtifactV125({ artifact: material.sourceArtifact, verifier: {} }, authority); const result = await resolveReceizCredentialCurrentnessV125({ issuance: opened.issuance, credential: opened.history.credential, verifiedHistory: opened.history, presentedHead: material.presentedHead, selectedKai: request.selectedKai }, authority); return Object.freeze({ status: "resolved", resultReference: await persist("operation-plan", result), writesPerformed: 0 }); } const subject = input.subjectCoordination; if (request.operation === "receiz_v125_subject_presence_plan") { if (!subject?.resolvePresenceCommitAuthority) return unavailable("presence-canonical-commit-mechanics"); if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PLAN_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== "event,sealedSource") throw new TypeError("V125_TRUST_MCP_PRESENCE_INPUT_INVALID"); const material = structuredClone(raw); if (request.idempotencyKey !== material.event?.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== material.event?.eventKai)) { throw new TypeError("V125_TRUST_MCP_PRESENCE_BINDING_INVALID"); } const plan = await planReceizSubjectPresenceEventV125(material); const held = Object.freeze({ input: material, plan }); const planReference = await persist("operation-plan", held); presencePlans.set(planReference, held); return Object.freeze({ status: "planned", planReference, writesPerformed: 0 }); } if (request.operation === "receiz_v125_subject_presence_apply") { if (!subject?.resolvePresenceCommitAuthority) return unavailable("presence-canonical-commit-mechanics"); const held = presencePlans.get(request.proofReference); if (!held) return unavailable("same-runtime-canonical-plan-custody"); if (request.idempotencyKey !== held.plan.event.idempotencyKey || (request.selectedKai !== null && request.selectedKai !== held.plan.event.eventKai)) { throw new TypeError("V125_TRUST_MCP_PRESENCE_BINDING_INVALID"); } if (request.confirmationDigest === null) return Object.freeze({ status: "confirmation-required", confirmationRequired: true, planReference: request.proofReference, ...await attempts.prepare(request, held.plan), writesPerformed: 0 }); const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const mechanics = await subject.resolvePresenceCommitAuthority(held.plan); const result = await applyReceizSubjectPresenceEventV125({ plan: held.plan, confirmationDigest: held.plan.confirmationDigest, replayGuard: subject.replayGuard, capability: mechanics.capability, sealer: mechanics.sealer, store: mechanics.store }); const proof = await retainCommitted("sealed-source-artifact", result.sourceArtifact); const family = await retainCommitted("operation-plan", result.sourceFamily); const transition = await retainCommitted("committed-artifact-transitions", [result.committedTransition]); return Object.freeze({ status: "committed", proofReference: proof.reference, sourceFamilyReference: family.reference, resultReference: transition.reference, ...(proof.pending || family.pending || transition.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}), acceptedHead: result.committedTransition.receipt.acceptedHead.value, writesPerformed: result.writesPerformed }); } if (relationPlans.has(request.operation)) { if (!subject) return unavailable("subject-canonical-commit-mechanics"); if (request.confirmationDigest !== null) throw new TypeError("V125_TRUST_MCP_PLAN_CONFIRMATION_UNEXPECTED"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw) || Object.keys(raw).sort().join() !== "event,sealedSource") throw new TypeError("V125_TRUST_MCP_SUBJECT_INPUT_INVALID"); const material = structuredClone(raw); bindRequest(request, material); const plan = await planReceizSubjectRelationshipEventV125({ ...material, verifyPbiAuthorship: subject.verifyPbiAuthorship }); if (!plan.transitionPlan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const held = Object.freeze({ schema: "receiz.mcp.subject-plan.v125", input: material, plan }); const planReference = await persist("operation-plan", held); plans.set(planReference, held); return Object.freeze({ status: "planned", planReference, writesPerformed: 0 }); } if (relationApplies.has(request.operation)) { if (!subject) return unavailable("subject-canonical-commit-mechanics"); const held = plans.get(request.proofReference); // Serialized plans must be replanned through the SDK after restart; an // external material resolver cannot restore WeakMap admission custody. if (!held) return unavailable("same-runtime-canonical-plan-custody"); bindRequest(request, held.input); if (request.confirmationDigest === null) { const preview = await attempts.prepare(request, held.plan); return Object.freeze({ status: "confirmation-required", confirmationRequired: true, planReference: request.proofReference, ...preview, writesPerformed: 0 }); } const reserved = await attempts.reserve(request, request.confirmationDigest); if (reserved.plan !== held.plan) throw new TypeError("V125_TRUST_MCP_CANONICAL_PLAN_REQUIRED"); const mechanics = await subject.resolveCommitAuthority(held.plan); const result = await applyReceizSubjectRelationshipEventV125({ plan: held.plan, confirmationDigest: held.plan.confirmationDigest, verifyPbiAuthorship: subject.verifyPbiAuthorship, replayGuard: subject.replayGuard, capability: mechanics.capability, sealer: mechanics.sealer, store: mechanics.store }); if (result.committedTransition.status !== "committed-artifact-transition") { throw new TypeError("V125_TRUST_MCP_COMMITTED_SOURCE_REQUIRED"); } // The actual canonical store already retained the successor. Optional // reference persistence cannot turn that committed write into a failure. const proof = await retainCommitted("sealed-source-artifact", result.sourceArtifact); const transition = await retainCommitted("committed-artifact-transitions", [result.committedTransition]); const family = result.sourceFamily ? await retainCommitted("operation-plan", result.sourceFamily) : null; return Object.freeze({ status: "committed", proofReference: proof.reference, resultReference: transition.reference, ...(family ? { sourceFamilyReference: family.reference } : {}), ...(proof.pending || transition.pending || family?.pending ? { code: "V125_TRUST_MCP_REFERENCE_PERSISTENCE_PENDING" } : {}), acceptedHead: result.committedTransition.receipt.acceptedHead.value, writesPerformed: result.writesPerformed }); } if (request.operation === "receiz_v125_subject_discovery_resolve") { if (!subject) return unavailable("subject-source-verification"); const raw = await resolve(request.proofReference); if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new TypeError("V125_TRUST_MCP_SUBJECT_INPUT_INVALID"); const args = structuredClone(raw); if (request.selectedKai !== null && args.selectedKai !== request.selectedKai) throw new TypeError("V125_TRUST_MCP_SUBJECT_BINDING_INVALID"); const result = await discoverReceizSubjectsV125({ ...args, verifyPbiAuthorship: subject.verifyPbiAuthorship }); return Object.freeze({ status: "resolved", resultReference: await persist("operation-plan", result), writesPerformed: 0 }); } return unavailable("canonical-operation-binding"); }; canonicalHosts.set(host, Object.freeze({ client: input.client, applicationAudience: input.applicationAudience })); return host; } //# sourceMappingURL=v125TrustHost.js.map