@atxp/client 0.10.7 → 0.10.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/_virtual/index10.js +2 -2
  2. package/dist/_virtual/index11.js +2 -2
  3. package/dist/_virtual/index17.js +2 -2
  4. package/dist/_virtual/index19.js +2 -2
  5. package/dist/_virtual/index20.js +2 -2
  6. package/dist/_virtual/index3.js +2 -2
  7. package/dist/_virtual/index4.js +2 -2
  8. package/dist/_virtual/index8.js +2 -2
  9. package/dist/_virtual/index9.js +2 -2
  10. package/dist/atxpClient.d.ts +1 -1
  11. package/dist/atxpClient.d.ts.map +1 -1
  12. package/dist/atxpFetcher.d.ts +18 -1
  13. package/dist/atxpFetcher.d.ts.map +1 -1
  14. package/dist/atxpFetcher.js +65 -2
  15. package/dist/atxpFetcher.js.map +1 -1
  16. package/dist/atxpProtocolHandler.d.ts +17 -0
  17. package/dist/atxpProtocolHandler.d.ts.map +1 -0
  18. package/dist/atxpProtocolHandler.js +77 -0
  19. package/dist/atxpProtocolHandler.js.map +1 -0
  20. package/dist/destinationMakers/index.d.ts.map +1 -1
  21. package/dist/destinationMakers/index.js +6 -0
  22. package/dist/destinationMakers/index.js.map +1 -1
  23. package/dist/index.cjs +558 -2
  24. package/dist/index.cjs.map +1 -1
  25. package/dist/index.d.ts +183 -5
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +555 -4
  28. package/dist/index.js.map +1 -1
  29. package/dist/mppProtocolHandler.d.ts +46 -0
  30. package/dist/mppProtocolHandler.d.ts.map +1 -0
  31. package/dist/mppProtocolHandler.js +182 -0
  32. package/dist/mppProtocolHandler.js.map +1 -0
  33. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/compile/validate/index.js +1 -1
  34. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/vocabularies/applicator/index.js +1 -1
  35. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/vocabularies/core/index.js +1 -1
  36. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/vocabularies/discriminator/index.js +1 -1
  37. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/vocabularies/format/index.js +1 -1
  38. package/dist/node_modules/@modelcontextprotocol/sdk/node_modules/ajv/dist/vocabularies/validation/index.js +1 -1
  39. package/dist/node_modules/ajv-formats/node_modules/ajv/dist/vocabularies/applicator/index.js +1 -1
  40. package/dist/node_modules/ajv-formats/node_modules/ajv/dist/vocabularies/core/index.js +1 -1
  41. package/dist/node_modules/ajv-formats/node_modules/ajv/dist/vocabularies/format/index.js +1 -1
  42. package/dist/paymentClient.d.ts +55 -0
  43. package/dist/paymentClient.d.ts.map +1 -0
  44. package/dist/paymentClient.js +75 -0
  45. package/dist/paymentClient.js.map +1 -0
  46. package/dist/protocolHandler.d.ts +41 -0
  47. package/dist/protocolHandler.d.ts.map +1 -0
  48. package/dist/types.d.ts +6 -1
  49. package/dist/types.d.ts.map +1 -1
  50. package/dist/x402ProtocolHandler.d.ts +22 -0
  51. package/dist/x402ProtocolHandler.d.ts.map +1 -0
  52. package/dist/x402ProtocolHandler.js +166 -0
  53. package/dist/x402ProtocolHandler.js.map +1 -0
  54. package/package.json +5 -3
package/dist/index.d.ts CHANGED
@@ -1,11 +1,50 @@
1
- import { Currency, Account, Network, DestinationMaker, AuthorizationServerUrl, AccountId, OAuthDb, FetchLike, Logger, OAuthResourceClient, ClientCredentials, PKCEValues, AccessToken, PaymentRequestOption, Source, Destination } from '@atxp/common';
2
- export { ATXPAccount, Account, PaymentMaker } from '@atxp/common';
1
+ import { Account, Logger, FetchLike, Currency, Network, DestinationMaker, AuthorizationServerUrl, AccountId, OAuthDb, ProtocolFlag, OAuthResourceClient, ClientCredentials, PKCEValues, AccessToken, PaymentRequestOption, Source, Destination, PaymentProtocol, AuthorizeResult } from '@atxp/common';
2
+ export { ATXPAccount, Account, AuthorizeResult, PaymentMaker } from '@atxp/common';
3
3
  import { ClientOptions, Client } from '@modelcontextprotocol/sdk/client/index.js';
4
4
  import { Implementation } from '@modelcontextprotocol/sdk/types.js';
5
5
  import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
6
6
  import * as oauth from 'oauth4webapi';
7
7
  import { LocalAccount, Address, TypedData, Hex as Hex$1, SignableMessage, TransactionSerializable } from 'viem';
8
8
 
9
+ /**
10
+ * Configuration passed to protocol handlers.
11
+ */
12
+ interface ProtocolConfig {
13
+ account: Account;
14
+ logger: Logger;
15
+ fetchFn: FetchLike;
16
+ approvePayment: (payment: ProspectivePayment) => Promise<boolean>;
17
+ onPayment: (args: {
18
+ payment: ProspectivePayment;
19
+ transactionHash: string;
20
+ network: string;
21
+ }) => Promise<void>;
22
+ onPaymentFailure: (context: PaymentFailureContext) => Promise<void>;
23
+ }
24
+ /**
25
+ * Strategy interface for handling different payment protocol challenges.
26
+ *
27
+ * Implementations detect whether a response contains a payment challenge
28
+ * they can handle, and execute the payment flow if so.
29
+ */
30
+ interface ProtocolHandler {
31
+ /** Unique protocol identifier */
32
+ readonly protocol: string;
33
+ /**
34
+ * Check if this handler can handle the given response.
35
+ * Must not consume the response body (use clone if needed).
36
+ */
37
+ canHandle(response: Response): Promise<boolean>;
38
+ /**
39
+ * Handle a payment challenge from the response.
40
+ * Returns the retry response after payment, or null if the challenge couldn't be handled.
41
+ */
42
+ handlePaymentChallenge(response: Response, originalRequest: {
43
+ url: string | URL;
44
+ init?: RequestInit;
45
+ }, config: ProtocolConfig): Promise<Response | null>;
46
+ }
47
+
9
48
  /**
10
49
  * Base class for all ATXP payment errors with structured error codes and actionable guidance
11
50
  */
@@ -185,6 +224,10 @@ type ClientConfig = {
185
224
  error: Error;
186
225
  remainingNetworks: string[];
187
226
  }) => Promise<void>;
227
+ /** Optional protocol handlers for multi-protocol support (strategy pattern) */
228
+ protocolHandlers?: ProtocolHandler[];
229
+ /** Optional protocol flag function to select protocol for omni-challenges */
230
+ protocolFlag?: ProtocolFlag;
188
231
  };
189
232
  type RequiredClientConfigFields$1 = 'mcpServer' | 'account';
190
233
  type RequiredClientConfig = Pick<ClientConfig, RequiredClientConfigFields$1>;
@@ -194,7 +237,7 @@ type FetchWrapper = (config: ClientArgs) => FetchLike;
194
237
 
195
238
  type RequiredClientConfigFields = 'mcpServer' | 'account';
196
239
  type OptionalClientConfig = Omit<ClientConfig, RequiredClientConfigFields>;
197
- type BuildableClientConfigFields = 'oAuthDb' | 'logger' | 'destinationMakers';
240
+ type BuildableClientConfigFields = 'oAuthDb' | 'logger' | 'destinationMakers' | 'protocolHandlers' | 'protocolFlag';
198
241
  declare const DEFAULT_CLIENT_CONFIG: Required<Omit<OptionalClientConfig, BuildableClientConfigFields>>;
199
242
  declare function buildClientConfig(args: ClientArgs): ClientConfig;
200
243
  declare function buildStreamableTransport(args: ClientArgs): StreamableHTTPClientTransport;
@@ -425,5 +468,140 @@ declare class PassthroughDestinationMaker implements DestinationMaker {
425
468
  makeDestinations(option: PaymentRequestOption, _logger: Logger, _paymentRequestId: string, _sources: Source[]): Promise<Destination[]>;
426
469
  }
427
470
 
428
- export { ATXPDestinationMaker, ATXPLocalAccount, ATXPPaymentError, DEFAULT_CLIENT_CONFIG, GasEstimationError, InsufficientFundsError, OAuthAuthenticationRequiredError, OAuthClient, POLYGON_AMOY, POLYGON_MAINNET, PassthroughDestinationMaker, PaymentExpiredError, PaymentNetworkError, PaymentServerError, RpcError, TransactionRevertedError, USDC_CONTRACT_ADDRESS_POLYGON_AMOY, USDC_CONTRACT_ADDRESS_POLYGON_MAINNET, USDC_CONTRACT_ADDRESS_WORLD_MAINNET, USDC_CONTRACT_ADDRESS_WORLD_SEPOLIA, UnsupportedCurrencyError, UserRejectedError, WORLD_CHAIN_MAINNET, WORLD_CHAIN_SEPOLIA, atxpClient, atxpFetch, buildClientConfig, buildStreamableTransport, getPolygonAmoyWithRPC, getPolygonByChainId, getPolygonMainnetWithRPC, getPolygonUSDCAddress, getWorldChainByChainId, getWorldChainMainnetWithRPC, getWorldChainSepoliaWithRPC, getWorldChainUSDCAddress };
429
- export type { AccountIdString, ClientArgs, ClientConfig, FetchWrapper, Hex, OAuthClientConfig, PaymentFailureContext, PolygonChain, ProspectivePayment, WorldChain };
471
+ interface X402ProtocolHandlerConfig {
472
+ accountsServer?: string;
473
+ }
474
+ /**
475
+ * Protocol handler for X402 payment challenges.
476
+ *
477
+ * Detects HTTP 402 responses with x402Version in the JSON body.
478
+ * Creates payment headers using the x402 library and retries the request.
479
+ */
480
+ declare class X402ProtocolHandler implements ProtocolHandler {
481
+ readonly protocol = "x402";
482
+ private accountsServer;
483
+ constructor(config?: X402ProtocolHandlerConfig);
484
+ canHandle(response: Response): Promise<boolean>;
485
+ handlePaymentChallenge(response: Response, originalRequest: {
486
+ url: string | URL;
487
+ init?: RequestInit;
488
+ }, config: ProtocolConfig): Promise<Response | null>;
489
+ private reconstructResponse;
490
+ }
491
+
492
+ /**
493
+ * Protocol handler for ATXP-MCP payment challenges.
494
+ *
495
+ * Detects JSON-RPC errors with code -30402 (PAYMENT_REQUIRED_ERROR_CODE) in
496
+ * MCP responses (both SSE and JSON formats).
497
+ */
498
+ declare class ATXPProtocolHandler implements ProtocolHandler {
499
+ readonly protocol = "atxp";
500
+ canHandle(response: Response): Promise<boolean>;
501
+ handlePaymentChallenge(response: Response, _originalRequest: {
502
+ url: string | URL;
503
+ init?: RequestInit;
504
+ }, config: ProtocolConfig): Promise<Response | null>;
505
+ private extractPaymentRequests;
506
+ }
507
+
508
+ /**
509
+ * Configuration for MPP protocol handler.
510
+ */
511
+ interface MPPProtocolHandlerConfig {
512
+ accountsServer?: string;
513
+ }
514
+ /**
515
+ * Protocol handler for MPP (Machine Payments Protocol) payment challenges.
516
+ *
517
+ * Detects MPP challenges in two forms:
518
+ * 1. HTTP level: HTTP 402 with WWW-Authenticate: Payment header
519
+ * 2. MCP level: JSON-RPC error with code -32042 containing MPP data
520
+ *
521
+ * Handles the challenge by calling /authorize/mpp on the accounts service
522
+ * and retrying with an Authorization: Payment header.
523
+ */
524
+ declare class MPPProtocolHandler implements ProtocolHandler {
525
+ readonly protocol = "mpp";
526
+ private accountsServer;
527
+ constructor(config?: MPPProtocolHandlerConfig);
528
+ canHandle(response: Response): Promise<boolean>;
529
+ handlePaymentChallenge(response: Response, originalRequest: {
530
+ url: string | URL;
531
+ init?: RequestInit;
532
+ }, config: ProtocolConfig): Promise<Response | null>;
533
+ /**
534
+ * Extract MPP challenge from response - tries HTTP header first, then MCP error body.
535
+ * Returns both the challenge and the body text to avoid double-consumption.
536
+ */
537
+ private extractChallenge;
538
+ /**
539
+ * Build a ProspectivePayment from an MPP challenge.
540
+ */
541
+ private buildProspectivePayment;
542
+ /**
543
+ * Report a payment failure via the onPaymentFailure callback.
544
+ */
545
+ private reportFailure;
546
+ /**
547
+ * Call /authorize/mpp on accounts service and retry the original request with the credential.
548
+ */
549
+ private authorizeAndRetry;
550
+ private reconstructResponse;
551
+ }
552
+
553
+ /**
554
+ * Build protocol-specific payment headers for retrying a request after authorization.
555
+ *
556
+ * @param result - The authorization result containing protocol and credential
557
+ * @param originalHeaders - Optional original request headers to preserve
558
+ * @returns New Headers object with protocol-specific payment headers added
559
+ */
560
+ declare function buildPaymentHeaders(result: AuthorizeResult, originalHeaders?: HeadersInit): Headers;
561
+ /**
562
+ * Client for authorizing payments.
563
+ *
564
+ * Resolves the payment protocol via protocolFlag, then delegates to
565
+ * account.authorize() for the actual authorization logic.
566
+ */
567
+ declare class PaymentClient {
568
+ private protocolFlag?;
569
+ private logger;
570
+ constructor(config: {
571
+ protocolFlag?: ProtocolFlag;
572
+ logger: Logger;
573
+ /** @deprecated No longer used — authorization delegates to account.authorize() */
574
+ accountsServer?: string;
575
+ /** @deprecated No longer used — authorization delegates to account.authorize() */
576
+ fetchFn?: FetchLike;
577
+ });
578
+ /**
579
+ * Authorize a payment by delegating to the account's authorize method.
580
+ *
581
+ * PaymentClient resolves the protocol (via explicit param or protocolFlag),
582
+ * then delegates all protocol-specific logic to account.authorize().
583
+ *
584
+ * @param params.account - The account to authorize the payment through
585
+ * @param params.userId - Passed to protocolFlag for protocol selection
586
+ * @param params.destination - Payment destination address
587
+ * @param params.protocol - Explicit protocol override (skips protocolFlag)
588
+ * @param params.amount - Payment amount
589
+ * @param params.memo - Payment memo
590
+ * @param params.paymentRequirements - X402 payment requirements
591
+ * @param params.challenge - MPP challenge object
592
+ * @returns AuthorizeResult with protocol and opaque credential
593
+ */
594
+ authorize(params: {
595
+ account: Account;
596
+ userId: string;
597
+ destination: string;
598
+ protocol?: PaymentProtocol;
599
+ amount?: BigNumber;
600
+ memo?: string;
601
+ paymentRequirements?: unknown;
602
+ challenge?: unknown;
603
+ }): Promise<AuthorizeResult>;
604
+ }
605
+
606
+ export { ATXPDestinationMaker, ATXPLocalAccount, ATXPPaymentError, ATXPProtocolHandler, DEFAULT_CLIENT_CONFIG, GasEstimationError, InsufficientFundsError, MPPProtocolHandler, OAuthAuthenticationRequiredError, OAuthClient, POLYGON_AMOY, POLYGON_MAINNET, PassthroughDestinationMaker, PaymentClient, PaymentExpiredError, PaymentNetworkError, PaymentServerError, RpcError, TransactionRevertedError, USDC_CONTRACT_ADDRESS_POLYGON_AMOY, USDC_CONTRACT_ADDRESS_POLYGON_MAINNET, USDC_CONTRACT_ADDRESS_WORLD_MAINNET, USDC_CONTRACT_ADDRESS_WORLD_SEPOLIA, UnsupportedCurrencyError, UserRejectedError, WORLD_CHAIN_MAINNET, WORLD_CHAIN_SEPOLIA, X402ProtocolHandler, atxpClient, atxpFetch, buildClientConfig, buildPaymentHeaders, buildStreamableTransport, getPolygonAmoyWithRPC, getPolygonByChainId, getPolygonMainnetWithRPC, getPolygonUSDCAddress, getWorldChainByChainId, getWorldChainMainnetWithRPC, getWorldChainSepoliaWithRPC, getWorldChainUSDCAddress };
607
+ export type { AccountIdString, ClientArgs, ClientConfig, FetchWrapper, Hex, OAuthClientConfig, PaymentFailureContext, PolygonChain, ProspectivePayment, ProtocolConfig, ProtocolHandler, WorldChain };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,wBAAwB,EACxB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,gCAAgC,EAChC,KAAK,iBAAiB,EACtB,WAAW,EACZ,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,SAAS,EACV,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,WAAW,EACZ,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,mCAAmC,EACnC,mCAAmC,EACnC,mBAAmB,EACnB,mBAAmB,EACnB,2BAA2B,EAC3B,2BAA2B,EAC3B,sBAAsB,EACtB,wBAAwB,EACxB,KAAK,UAAU,EAChB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,qCAAqC,EACrC,kCAAkC,EAClC,eAAe,EACf,YAAY,EACZ,wBAAwB,EACxB,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,KAAK,GAAG,EACR,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,kBAAkB,EAClB,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACpB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,oBAAoB,EACpB,2BAA2B,GAC5B,MAAM,8BAA8B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,wBAAwB,EACxB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,gCAAgC,EAChC,KAAK,iBAAiB,EACtB,WAAW,EACZ,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,SAAS,EACV,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACL,WAAW,EACZ,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,mCAAmC,EACnC,mCAAmC,EACnC,mBAAmB,EACnB,mBAAmB,EACnB,2BAA2B,EAC3B,2BAA2B,EAC3B,sBAAsB,EACtB,wBAAwB,EACxB,KAAK,UAAU,EAChB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,qCAAqC,EACrC,kCAAkC,EAClC,eAAe,EACf,YAAY,EACZ,wBAAwB,EACxB,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,YAAY,EAClB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,KAAK,GAAG,EACR,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,wBAAwB,EACxB,wBAAwB,EACxB,kBAAkB,EAClB,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACpB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,gBAAgB,EACjB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,oBAAoB,EACpB,2BAA2B,GAC5B,MAAM,8BAA8B,CAAC;AAGtC,YAAY,EACV,eAAe,EACf,cAAc,EACf,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACL,mBAAmB,EACpB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,mBAAmB,EACpB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,kBAAkB,EACnB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,KAAK,eAAe,EACrB,MAAM,oBAAoB,CAAC"}