/** * @name McpMonetizationGate * @description x402/pay.sh payment gate for hosted SAP MCP Streamable HTTP requests. */ import * as http from 'http'; import type { SapMcpConfig } from '../../config-runtime/src/env.js'; import type { PaymentDecision } from './pricing.js'; import { PrepaidCreditStore } from './prepaid-credit-store.js'; /** * @name McpRequestHandler * @description Handler invoked after the payment gate has authorized or bypassed a request. */ export type McpRequestHandler = (request: http.IncomingMessage, response: http.ServerResponse, parsedBody?: unknown) => Promise; /** * @name McpPaidRequestValidationContext * @description Context provided to hosted transports before a paid request reaches the x402 facilitator. */ export interface McpPaidRequestValidationContext { request: http.IncomingMessage; parsedBody: unknown; decision: Extract; } /** * @name McpPaidRequestValidationFailure * @description JSON-RPC error returned when a paid request cannot safely enter the payment flow. */ export interface McpPaidRequestValidationFailure { code: number; message: string; data?: unknown; } /** * @name McpMonetizationGateOptions * @description Optional hooks used by HTTP transports to fail closed before facilitator verification. */ export interface McpMonetizationGateOptions { validatePaidRequest?: (context: McpPaidRequestValidationContext) => McpPaidRequestValidationFailure | undefined; } /** * @name McpMonetizationGate * @description Applies x402 payment verification and settlement before forwarding requests to the MCP transport. */ export declare class McpMonetizationGate { private readonly appConfig; private readonly monetization; private readonly httpResourceServer; private readonly usageLedger; /** Prepaid credit store for session-based payments (x402 Lifecycle Hooks grantAccess). */ private readonly prepaidStore; /** * @description x402 idempotency cache — prevents double-charge on retry. * * Keyed by `requestHash`. When a settlement succeeds, the receipt is cached * for 5 minutes. If the same requestHash is retried (e.g. network failure * after the tool executed but before the response reached the client), the * cached settlement is returned without re-charging the payer's USDC. * * The cache is bounded to 10,000 entries (evicts oldest on overflow). Each * entry has a TTL of 5 minutes — long enough for client retry logic, short * enough to avoid stale receipts blocking legitimate re-requests. * * @internal */ private readonly idempotencyCache; private static readonly IDEMPOTENCY_TTL_MS; private static readonly IDEMPOTENCY_MAX_ENTRIES; private constructor(); /** * @name create * @description Builds and initializes an x402 HTTP resource server for SAP MCP requests. */ static create(appConfig: SapMcpConfig): Promise; /** * @name close * @description Flushes hosted payment audit state before the remote server exits. */ close(): Promise; /** * @name isJsonRpcError * @description Checks if a buffered HTTP response body contains a JSON-RPC error. * * JSON-RPC errors have the shape `{ "error": { "code": , "message": } }` * or MCP tool results with `isError: true`. When detected, settlement is skipped * so the user is not charged for failed tool calls. * * @internal */ private isJsonRpcError; /** * @name pruneExpiredIdempotencyEntries * @description Opportunistically remove expired entries from the idempotency cache. * * Called after each successful settlement to keep the cache bounded without * a separate timer. Scans at most 100 entries per call to avoid O(n) overhead * on every settle. * * @internal */ private pruneExpiredIdempotencyEntries; /** Prepaid credit store for session-based payments. */ get prepaidCreditStore(): PrepaidCreditStore; /** * @name handle * @description Authorizes a request via x402 when required, then forwards it to MCP and settles on success. */ handle(request: http.IncomingMessage, response: http.ServerResponse, next: McpRequestHandler, options?: McpMonetizationGateOptions): Promise; /** * @name handleSignedPayment * @description Verifies a client-supplied x402 payment payload against the original accepted requirements. */ private handleSignedPayment; private callFacilitator; private buildRequestContext; /** * @name returnX402Challenge * @description Returns a standard HTTP 402 payment challenge for non-MCP clients * (x402scan discovery, curl, health checks). Decodes the PaymentRequired object * from the PAYMENT-REQUIRED header and returns it as the response body so x402 * scanners can validate the challenge format. */ private returnX402Challenge; private forwardAndSettle; private settleAndReplay; } /** * @name resolvePaymentNetwork * @description Resolves the x402 CAIP-2 Solana network from explicit config or the SAP RPC URL. */ export declare function resolvePaymentNetwork(config: SapMcpConfig): string; /** * @name normalizePaymentNetwork * @description Converts user-friendly Solana network aliases to the CAIP-2 identifiers expected by x402 SVM. */ export declare function normalizePaymentNetwork(network: string): string; /** * @name buildPaidVirtualPath * @description Builds the paid x402 virtual resource path, bound to the canonical MCP method-and-params hash. */ export declare function buildPaidVirtualPath(decision: Extract, requestHash: string): string; //# sourceMappingURL=monetization-gate.d.ts.map