/** * @name PaymentUsageLedger * @description Append-only JSONL audit ledger for remote MCP payment decisions and settlements. */ import type { SettleResponse } from '@x402/core/types'; import { Redis } from 'ioredis'; import type { PaymentDecision } from './pricing.js'; /** * @name PaymentLedgerEventType * @description Supported audit event types emitted by the monetization gate. */ export type PaymentLedgerEventType = 'payment_required' | 'payment_challenge_returned' | 'sponsor_bypass_granted' | 'payment_eligibility_blocked' | 'payment_verified' | 'payment_verification_failed' | 'payment_settled' | 'payment_failed' | 'payment_canceled'; /** * @name PaymentLedgerEvent * @description Durable audit record for a monetized MCP request without storing private key material or payment payloads. */ export interface PaymentLedgerEvent { event: PaymentLedgerEventType; timestamp: string; requestHash: string; method: string; path: string; toolNames: string[]; priceUsd?: number; paymentHeaderPresent: boolean; remoteAddress?: string; userAgent?: string; transaction?: string; network?: string; amount?: string; payer?: string; challengeTransport?: 'mcp-json-rpc' | 'http-402'; sponsorOrigin?: string; errorReason?: string; errorMessage?: string; } /** * @name PaymentRequestMetadata * @description Public request metadata that is safe to persist for payment auditability. */ export interface PaymentRequestMetadata { requestHash: string; method: string; path: string; remoteAddress?: string; userAgent?: string; paymentHeaderPresent: boolean; } /** * @name UsageLedger * @description Writes MCP payment events to an append-only local JSONL ledger. */ export declare class UsageLedger { private readonly ledgerPath; private readonly redis?; private readonly redisStreamKey; private readonly buffer; private flushTimer?; private readonly flushIntervalMs; private readonly maxBufferSize; private flushing; constructor(ledgerPath?: string, redis?: InstanceType, redisStreamKey?: string); /** * @name createFromEnv * @description Creates the payment usage ledger and optionally attaches a Redis Stream sink for clustered hosted deployments. */ static createFromEnv(): Promise; /** * @name append * @description Buffers a payment audit event. Flushes to disk when the buffer * is full or the periodic timer fires (5s). Redis stream is written immediately * for real-time consumers. */ append(event: PaymentLedgerEvent): Promise; /** * @name flush * @description Flushes buffered events to disk in a single write. */ flush(): Promise; /** * @name close * @description Stops periodic flushing and forces any buffered events to disk. */ close(): Promise; private startFlushTimer; /** * @name recordDecision * @description Records that a request requires payment. */ recordDecision(metadata: PaymentRequestMetadata, decision: PaymentDecision): Promise; /** * @name recordChallengeReturned * @description Records that SAP MCP returned a standards-compatible x402 challenge before any payment was signed. */ recordChallengeReturned(metadata: PaymentRequestMetadata, decision: PaymentDecision, challengeTransport: 'mcp-json-rpc' | 'http-402'): Promise; /** * @name recordSponsorBypassGranted * @description Records a trusted server-to-server sponsor bypass that grants a paid call without x402. */ recordSponsorBypassGranted(metadata: PaymentRequestMetadata, decision: PaymentDecision, sponsorOrigin: string): Promise; /** * @name recordEligibilityBlocked * @description Records a hosted accountless write rejected before monetization, proving that no x402 fee was charged. */ recordEligibilityBlocked(metadata: PaymentRequestMetadata, toolNames: string[], reason: string, errorMessage?: string): Promise; /** * @name recordVerified * @description Records that an x402 payment was accepted before handler execution. */ recordVerified(metadata: PaymentRequestMetadata, decision: PaymentDecision): Promise; /** * @name recordVerificationFailed * @description Records that a supplied x402 payment header failed before tool execution or settlement. */ recordVerificationFailed(metadata: PaymentRequestMetadata, decision: PaymentDecision, reason: string, errorMessage?: string): Promise; /** * @name recordCanceled * @description Records that a verified payment was canceled because MCP execution did not complete successfully. */ recordCanceled(metadata: PaymentRequestMetadata, decision: PaymentDecision, reason: string, errorMessage?: string): Promise; /** * @name recordSettlement * @description Records successful or failed x402 settlement without storing payment payload bytes. */ recordSettlement(metadata: PaymentRequestMetadata, decision: PaymentDecision, settlement: SettleResponse): Promise; } /** * @name hashRequestBody * @description Computes a deterministic SHA-256 hash for request correlation without persisting raw arguments. */ export declare function hashRequestBody(body: Buffer): string; /** * @name hashPaymentRequest * @description Computes the x402 payment binding hash for an MCP request. * * The hash intentionally ignores JSON-RPC transport metadata such as `id` and * `jsonrpc`, while preserving `method` and `params`. This binds a receipt to * the paid operation and arguments without breaking legitimate agent retries * that replay the same tool call with a different request id. */ export declare function hashPaymentRequest(rawBody: Buffer, parsedBody: unknown): string; //# sourceMappingURL=usage-ledger.d.ts.map