import {z} from 'zod' export type MCPCheckOutputRequest = {client_id?: string | undefined, user_token?: string | undefined, tenant_id?: string | undefined, user_id?: string | undefined, connector_type: string, tool?: string | undefined, response_data: Array<{[key: string]: unknown}>, message?: string | undefined, metadata?: {[key: string]: unknown} | undefined, row_count?: number | undefined} | {client_id?: string | undefined, user_token?: string | undefined, tenant_id?: string | undefined, user_id?: string | undefined, connector_type: string, tool?: string | undefined, response_data?: Array<{[key: string]: unknown}> | undefined, message: string, metadata?: {[key: string]: unknown} | undefined, row_count?: number | undefined}; export type CreateApiApiV1McpCheckOutputArgs = {'X-Axonflow-Client'?: string | undefined, 'X-Axonflow-PEP-Handshake'?: string | undefined, body: MCPCheckOutputRequest}; export type CreateApiApiV1McpCheckOutputOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exfiltrationCheckInfo = z.object({rows_returned: z.number().int().optional(), row_limit: z.number().int().optional(), bytes_returned: z.number().int().optional(), byte_limit: z.number().int().optional(), within_limits: z.boolean().optional()}); export const policyMatchInfo = z.object({policy_id: z.string().optional(), policy_name: z.string().optional(), category: z.string().optional(), severity: z.string().optional(), action: z.string().optional()}); export const policyInfo = z.object({policies_evaluated: z.number().int().optional(), blocked: z.boolean().optional(), block_reason: z.string().optional(), redactions_applied: z.number().int().optional(), processing_time_ms: z.number().int().optional(), matched_policies: z.array(policyMatchInfo).optional(), exfiltration_check: exfiltrationCheckInfo.optional()}); export const mCPCheckOutputResponse = z.object({allowed: z.boolean().optional(), block_reason: z.string().optional(), redacted_data: z.unknown(), policies_evaluated: z.number().int().optional(), exfiltration_info: exfiltrationCheckInfo.optional(), policy_info: policyInfo.optional(), decision_id: z.string().optional(), redaction_evaluated: z.boolean().optional(), engine: z.literal("anchored").optional(), subject_type: z.string().optional(), policy_bundle: z.string().optional(), policy_packs: z.array(z.string()).optional(), legacy_validators: z.array(z.object({validator: z.enum(["indonesia_pii", "india_pii"]), action: z.enum(["blocked", "masked"])})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Validate MCP output against policies * * POST /api/v1/mcp/check-output */ export const createApiApiV1McpCheckOutput = async (args: CreateApiApiV1McpCheckOutputArgs, options?: CreateApiApiV1McpCheckOutputOptions) => { const {baseUrl = "https://agent.getaxonflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Axonflow-Client'] != null) headers.set("X-Axonflow-Client", String(args['X-Axonflow-Client'])) if (args['X-Axonflow-PEP-Handshake'] != null) headers.set("X-Axonflow-PEP-Handshake", String(args['X-Axonflow-PEP-Handshake'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/mcp/check-output${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(mCPCheckOutputResponse, await res.json()) };