/** * Servicialo v0.8 — Delegated Agency Model * Mandate validation middleware for the MCP server. * * This module provides: * 1. Zod schemas for ServiceMandate and audit entries * 2. A validation function that enforces the 8 mandate checks (§10.3.2) * 3. A middleware wrapper for tool handlers * 4. Audit logging on every agent action */ import { z } from 'zod'; import type { ServicialoAdapter } from './adapter.js'; export declare const MandatePrincipalType: z.ZodEnum<["professional", "patient", "organization"]>; export declare const MandateActingFor: z.ZodEnum<["professional", "patient", "organization"]>; export declare const MandateStatus: z.ZodEnum<["active", "expired", "revoked", "suspended"]>; export declare const MandateConstraintsSchema: z.ZodOptional; allowed_hours: z.ZodOptional>; ip_allowlist: z.ZodOptional>; require_confirmation_above: z.ZodOptional>; service_types: z.ZodOptional>; }, "strip", z.ZodTypeAny, { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; }, { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; }>>; export declare const ServiceMandateSchema: z.ZodObject<{ mandate_id: z.ZodString; principal_id: z.ZodString; principal_type: z.ZodEnum<["professional", "patient", "organization"]>; agent_id: z.ZodString; agent_name: z.ZodOptional; acting_for: z.ZodEnum<["professional", "patient", "organization"]>; context: z.ZodString; scopes: z.ZodArray; constraints: z.ZodOptional; allowed_hours: z.ZodOptional>; ip_allowlist: z.ZodOptional>; require_confirmation_above: z.ZodOptional>; service_types: z.ZodOptional>; }, "strip", z.ZodTypeAny, { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; }, { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; }>>; issued_at: z.ZodString; expires_at: z.ZodString; revoked_at: z.ZodOptional; revocation_reason: z.ZodOptional; status: z.ZodEnum<["active", "expired", "revoked", "suspended"]>; metadata: z.ZodOptional>; }, "strip", z.ZodTypeAny, { status: "active" | "expired" | "revoked" | "suspended"; mandate_id: string; principal_id: string; principal_type: "organization" | "professional" | "patient"; agent_id: string; acting_for: "organization" | "professional" | "patient"; context: string; scopes: string[]; issued_at: string; expires_at: string; agent_name?: string | undefined; constraints?: { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; } | undefined; revoked_at?: string | undefined; revocation_reason?: string | undefined; metadata?: Record | undefined; }, { status: "active" | "expired" | "revoked" | "suspended"; mandate_id: string; principal_id: string; principal_type: "organization" | "professional" | "patient"; agent_id: string; acting_for: "organization" | "professional" | "patient"; context: string; scopes: string[]; issued_at: string; expires_at: string; agent_name?: string | undefined; constraints?: { max_actions_per_day?: number | undefined; allowed_hours?: { timezone: string; start: string; end: string; } | undefined; ip_allowlist?: string[] | undefined; require_confirmation_above?: { currency: string; amount: number; } | undefined; service_types?: string[] | undefined; } | undefined; revoked_at?: string | undefined; revocation_reason?: string | undefined; metadata?: Record | undefined; }>; export type ServiceMandate = z.infer; export declare const AuditResultType: z.ZodEnum<["success", "failure", "rejected", "halted"]>; export declare const AuditEntrySchema: z.ZodObject<{ audit_id: z.ZodOptional; mandate_id: z.ZodString; agent_id: z.ZodString; principal_id: z.ZodString; acting_for: z.ZodEnum<["professional", "patient", "organization"]>; action: z.ZodString; action_input: z.ZodOptional>; action_result: z.ZodEnum<["success", "failure", "rejected", "halted"]>; failure_reason: z.ZodOptional; resource_id: z.ZodOptional; context: z.ZodString; ip_address: z.ZodOptional; request_id: z.ZodOptional; timestamp: z.ZodOptional; }, "strip", z.ZodTypeAny, { action: string; mandate_id: string; principal_id: string; agent_id: string; acting_for: "organization" | "professional" | "patient"; context: string; action_result: "success" | "failure" | "rejected" | "halted"; resource_id?: string | undefined; timestamp?: string | undefined; audit_id?: string | undefined; action_input?: Record | undefined; failure_reason?: string | undefined; ip_address?: string | undefined; request_id?: string | undefined; }, { action: string; mandate_id: string; principal_id: string; agent_id: string; acting_for: "organization" | "professional" | "patient"; context: string; action_result: "success" | "failure" | "rejected" | "halted"; resource_id?: string | undefined; timestamp?: string | undefined; audit_id?: string | undefined; action_input?: Record | undefined; failure_reason?: string | undefined; ip_address?: string | undefined; request_id?: string | undefined; }>; export type AuditEntry = z.infer; export declare const ActorWithMandateSchema: z.ZodObject<{ type: z.ZodEnum<["client", "provider", "organization", "agent"]>; id: z.ZodString; mandate_id: z.ZodOptional; on_behalf_of: z.ZodOptional>; }, "strip", z.ZodTypeAny, { type: "agent" | "client" | "provider" | "organization"; id: string; on_behalf_of?: { type: string; id: string; } | undefined; mandate_id?: string | undefined; }, { type: "agent" | "client" | "provider" | "organization"; id: string; on_behalf_of?: { type: string; id: string; } | undefined; mandate_id?: string | undefined; }>; export type ActorWithMandate = z.infer; /** * Maps MCP tool names to the scope(s) required to execute them. * An agent must hold at least one of the listed scopes in its mandate. */ export declare const TOOL_SCOPE_MAP: Record; export declare class MandateValidationError extends Error { readonly code: string; readonly mandate_id?: string | undefined; constructor(code: string, message: string, mandate_id?: string | undefined); } export interface MandateValidationContext { mandate: ServiceMandate; agent_id: string; tool_name: string; context: string; /** For conflict-of-interest check: the acting_for of the counterparty agent, if any */ counterparty_acting_for?: string; /** Current time override for testing */ now?: Date; } /** * Validates a ServiceMandate against the 8 checks defined in §10.3.2. * Throws MandateValidationError on failure. */ export declare function validateMandate(ctx: MandateValidationContext): void; export declare function logAuditEntry(client: ServicialoAdapter, entry: Omit): Promise; export declare function checkAndIncrementDailyActions(client: ServicialoAdapter, mandate: ServiceMandate): Promise; export declare function resolveMandate(client: ServicialoAdapter, mandateId: string): Promise; export interface MandateMiddlewareOptions { /** Extract the organization context from tool args */ extractContext?: (args: Record) => string; /** Extract the primary resource ID for audit */ extractResourceId?: (args: Record) => string | undefined; } /** * Wraps a tool handler with mandate validation and audit logging. * * For tools where `actor.type === 'agent'`, validates the mandate before * executing and logs an audit entry after. For non-agent actors, passes * through without mandate checks. */ export declare function withMandateValidation>(toolName: string, handler: (client: ServicialoAdapter, args: TArgs) => Promise, options?: MandateMiddlewareOptions): (client: ServicialoAdapter, args: TArgs) => Promise; /** * Sanitizes an array of evidence objects. If any evidence item has * `data_sensitivity: "restricted"`, its `data` payload is redacted. */ export declare function sanitizeEvidence(evidence: unknown[]): unknown[]; //# sourceMappingURL=mandate.d.ts.map