@mcpaid/sdk 2.0.0

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 (111) hide show
  1. package/LICENSE +176 -0
  2. package/NPM_RESTRUCTURE.md +86 -0
  3. package/OPERATOR-LICENSE.md +18 -0
  4. package/README.md +380 -0
  5. package/SELF-HOSTING.md +108 -0
  6. package/dist/circuit-breaker/circuit-breaker.d.ts +26 -0
  7. package/dist/circuit-breaker/circuit-breaker.d.ts.map +1 -0
  8. package/dist/circuit-breaker/circuit-breaker.js +128 -0
  9. package/dist/circuit-breaker/circuit-breaker.js.map +1 -0
  10. package/dist/cli.d.ts +21 -0
  11. package/dist/cli.d.ts.map +1 -0
  12. package/dist/cli.js +1560 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/contracts/ToolPayRouter.d.ts +288 -0
  15. package/dist/contracts/ToolPayRouter.d.ts.map +1 -0
  16. package/dist/contracts/ToolPayRouter.js +370 -0
  17. package/dist/contracts/ToolPayRouter.js.map +1 -0
  18. package/dist/contracts/router-registry.d.ts +19 -0
  19. package/dist/contracts/router-registry.d.ts.map +1 -0
  20. package/dist/contracts/router-registry.js +28 -0
  21. package/dist/contracts/router-registry.js.map +1 -0
  22. package/dist/fee/fee-calculator.d.ts +35 -0
  23. package/dist/fee/fee-calculator.d.ts.map +1 -0
  24. package/dist/fee/fee-calculator.js +57 -0
  25. package/dist/fee/fee-calculator.js.map +1 -0
  26. package/dist/fee/fee-ledger.d.ts +66 -0
  27. package/dist/fee/fee-ledger.d.ts.map +1 -0
  28. package/dist/fee/fee-ledger.js +181 -0
  29. package/dist/fee/fee-ledger.js.map +1 -0
  30. package/dist/gateway/config-loader.d.ts +38 -0
  31. package/dist/gateway/config-loader.d.ts.map +1 -0
  32. package/dist/gateway/config-loader.js +125 -0
  33. package/dist/gateway/config-loader.js.map +1 -0
  34. package/dist/gateway/server-registry.d.ts +25 -0
  35. package/dist/gateway/server-registry.d.ts.map +1 -0
  36. package/dist/gateway/server-registry.js +59 -0
  37. package/dist/gateway/server-registry.js.map +1 -0
  38. package/dist/gateway/toolpay-gateway.d.ts +57 -0
  39. package/dist/gateway/toolpay-gateway.d.ts.map +1 -0
  40. package/dist/gateway/toolpay-gateway.js +320 -0
  41. package/dist/gateway/toolpay-gateway.js.map +1 -0
  42. package/dist/index.d.ts +36 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +37 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/sdk/client-agent.d.ts +47 -0
  47. package/dist/sdk/client-agent.d.ts.map +1 -0
  48. package/dist/sdk/client-agent.js +158 -0
  49. package/dist/sdk/client-agent.js.map +1 -0
  50. package/dist/sdk/server-sdk.d.ts +51 -0
  51. package/dist/sdk/server-sdk.d.ts.map +1 -0
  52. package/dist/sdk/server-sdk.js +170 -0
  53. package/dist/sdk/server-sdk.js.map +1 -0
  54. package/dist/settlement/base-contract-client.d.ts +156 -0
  55. package/dist/settlement/base-contract-client.d.ts.map +1 -0
  56. package/dist/settlement/base-contract-client.js +156 -0
  57. package/dist/settlement/base-contract-client.js.map +1 -0
  58. package/dist/settlement/nonce-store.d.ts +54 -0
  59. package/dist/settlement/nonce-store.d.ts.map +1 -0
  60. package/dist/settlement/nonce-store.js +125 -0
  61. package/dist/settlement/nonce-store.js.map +1 -0
  62. package/dist/settlement/onchain-verifier.d.ts +41 -0
  63. package/dist/settlement/onchain-verifier.d.ts.map +1 -0
  64. package/dist/settlement/onchain-verifier.js +114 -0
  65. package/dist/settlement/onchain-verifier.js.map +1 -0
  66. package/dist/settlement/payment-orchestrator.d.ts +59 -0
  67. package/dist/settlement/payment-orchestrator.d.ts.map +1 -0
  68. package/dist/settlement/payment-orchestrator.js +282 -0
  69. package/dist/settlement/payment-orchestrator.js.map +1 -0
  70. package/dist/settlement/permit-verifier.d.ts +60 -0
  71. package/dist/settlement/permit-verifier.d.ts.map +1 -0
  72. package/dist/settlement/permit-verifier.js +80 -0
  73. package/dist/settlement/permit-verifier.js.map +1 -0
  74. package/dist/settlement/session-wallet.d.ts +60 -0
  75. package/dist/settlement/session-wallet.d.ts.map +1 -0
  76. package/dist/settlement/session-wallet.js +105 -0
  77. package/dist/settlement/session-wallet.js.map +1 -0
  78. package/dist/transports/sse-transport.d.ts +27 -0
  79. package/dist/transports/sse-transport.d.ts.map +1 -0
  80. package/dist/transports/sse-transport.js +65 -0
  81. package/dist/transports/sse-transport.js.map +1 -0
  82. package/dist/transports/stdio-bridge.d.ts +29 -0
  83. package/dist/transports/stdio-bridge.d.ts.map +1 -0
  84. package/dist/transports/stdio-bridge.js +133 -0
  85. package/dist/transports/stdio-bridge.js.map +1 -0
  86. package/dist/types/circuit-breaker.d.ts +22 -0
  87. package/dist/types/circuit-breaker.d.ts.map +1 -0
  88. package/dist/types/circuit-breaker.js +5 -0
  89. package/dist/types/circuit-breaker.js.map +1 -0
  90. package/dist/types/ledger.d.ts +57 -0
  91. package/dist/types/ledger.d.ts.map +1 -0
  92. package/dist/types/ledger.js +5 -0
  93. package/dist/types/ledger.js.map +1 -0
  94. package/dist/types/mcp.d.ts +67 -0
  95. package/dist/types/mcp.d.ts.map +1 -0
  96. package/dist/types/mcp.js +5 -0
  97. package/dist/types/mcp.js.map +1 -0
  98. package/dist/types/pricing.d.ts +68 -0
  99. package/dist/types/pricing.d.ts.map +1 -0
  100. package/dist/types/pricing.js +5 -0
  101. package/dist/types/pricing.js.map +1 -0
  102. package/dist/types/x402.d.ts +76 -0
  103. package/dist/types/x402.d.ts.map +1 -0
  104. package/dist/types/x402.js +5 -0
  105. package/dist/types/x402.js.map +1 -0
  106. package/dist/utils/units.d.ts +45 -0
  107. package/dist/utils/units.d.ts.map +1 -0
  108. package/dist/utils/units.js +141 -0
  109. package/dist/utils/units.js.map +1 -0
  110. package/package.json +72 -0
  111. package/src/contracts/ToolPayRouter.json +371 -0
@@ -0,0 +1,181 @@
1
+ /**
2
+ * ToolPay In-Memory Financial Ledger & Fee Tracking Engine
3
+ * Records all transaction events, splits, developer balances, and platform cuts.
4
+ */
5
+ export class FeeLedger {
6
+ transactions = new Map();
7
+ idempotencyMap = new Map(); // idempotencyKey -> txId
8
+ developerBalances = new Map();
9
+ platformTreasuryWallet;
10
+ constructor(platformTreasuryWallet) {
11
+ this.platformTreasuryWallet = platformTreasuryWallet;
12
+ }
13
+ /**
14
+ * Records a confirmed transaction and updates developer & platform balances.
15
+ * Ensures idempotency: duplicate records with the same idempotency key are safely ignored.
16
+ */
17
+ recordTransaction(params) {
18
+ // Check idempotency
19
+ if (params.idempotencyKey && this.idempotencyMap.has(params.idempotencyKey)) {
20
+ const existingTxId = this.idempotencyMap.get(params.idempotencyKey);
21
+ return this.transactions.get(existingTxId);
22
+ }
23
+ if (this.transactions.has(params.transactionId)) {
24
+ return this.transactions.get(params.transactionId);
25
+ }
26
+ const treasuryWallet = params.platformTreasuryWallet || this.platformTreasuryWallet;
27
+ const now = params.timestamp || Date.now();
28
+ const record = {
29
+ transactionId: params.transactionId,
30
+ serverId: params.serverId,
31
+ toolName: params.toolName,
32
+ agentWallet: params.agentWallet.toLowerCase(),
33
+ developerWallet: params.developerWallet.toLowerCase(),
34
+ platformTreasuryWallet: treasuryWallet.toLowerCase(),
35
+ scheme: params.scheme,
36
+ grossAmountMicro: params.grossAmountMicro,
37
+ platformFeeMicro: params.platformFeeMicro,
38
+ developerPayoutMicro: params.developerPayoutMicro,
39
+ platformFeeBasisPoints: params.platformFeeBasisPoints,
40
+ settlementRef: params.settlementRef,
41
+ idempotencyKey: params.idempotencyKey,
42
+ status: 'confirmed',
43
+ createdAt: now,
44
+ confirmedAt: now,
45
+ };
46
+ this.transactions.set(record.transactionId, record);
47
+ if (params.idempotencyKey) {
48
+ this.idempotencyMap.set(params.idempotencyKey, record.transactionId);
49
+ }
50
+ // Update Developer Account Balance
51
+ const devKey = record.developerWallet;
52
+ const currentDevBalance = this.developerBalances.get(devKey) || {
53
+ developerWallet: devKey,
54
+ totalGrossEarnedMicro: 0n,
55
+ totalPlatformFeesPaidMicro: 0n,
56
+ netPayoutEarnedMicro: 0n,
57
+ availableBalanceMicro: 0n,
58
+ totalPaidCallsCount: 0,
59
+ };
60
+ currentDevBalance.totalGrossEarnedMicro += record.grossAmountMicro;
61
+ currentDevBalance.totalPlatformFeesPaidMicro += record.platformFeeMicro;
62
+ currentDevBalance.netPayoutEarnedMicro += record.developerPayoutMicro;
63
+ currentDevBalance.availableBalanceMicro += record.developerPayoutMicro;
64
+ currentDevBalance.totalPaidCallsCount += 1;
65
+ this.developerBalances.set(devKey, currentDevBalance);
66
+ return record;
67
+ }
68
+ /**
69
+ * Retrieves a developer's financial balance.
70
+ */
71
+ getDeveloperBalance(developerWallet) {
72
+ const key = developerWallet.toLowerCase();
73
+ return (this.developerBalances.get(key) || {
74
+ developerWallet: key,
75
+ totalGrossEarnedMicro: 0n,
76
+ totalPlatformFeesPaidMicro: 0n,
77
+ netPayoutEarnedMicro: 0n,
78
+ availableBalanceMicro: 0n,
79
+ totalPaidCallsCount: 0,
80
+ });
81
+ }
82
+ /**
83
+ * Retrieves an aggregated summary of the ToolPay Platform Treasury.
84
+ */
85
+ getPlatformTreasurySummary() {
86
+ let totalGross = 0n;
87
+ let totalPlatformFees = 0n;
88
+ let totalDevPayouts = 0n;
89
+ const uniqueDevelopers = new Set();
90
+ const uniqueAgents = new Set();
91
+ for (const tx of this.transactions.values()) {
92
+ if (tx.status === 'confirmed') {
93
+ totalGross += tx.grossAmountMicro;
94
+ totalPlatformFees += tx.platformFeeMicro;
95
+ totalDevPayouts += tx.developerPayoutMicro;
96
+ uniqueDevelopers.add(tx.developerWallet);
97
+ uniqueAgents.add(tx.agentWallet);
98
+ }
99
+ }
100
+ return {
101
+ treasuryWallet: this.platformTreasuryWallet,
102
+ totalGrossVolumeMicro: totalGross,
103
+ totalPlatformFeesCollectedMicro: totalPlatformFees,
104
+ totalDeveloperPayoutsMicro: totalDevPayouts,
105
+ totalTransactionCount: this.transactions.size,
106
+ uniqueDevelopersCount: uniqueDevelopers.size,
107
+ uniqueAgentsCount: uniqueAgents.size,
108
+ };
109
+ }
110
+ /**
111
+ * Processes a developer payout withdrawal.
112
+ */
113
+ withdrawDeveloperBalance(developerWallet, amountMicro) {
114
+ const key = developerWallet.toLowerCase();
115
+ const balance = this.developerBalances.get(key);
116
+ if (!balance || balance.availableBalanceMicro <= 0n) {
117
+ throw new Error(`Insufficient available balance for developer ${developerWallet}`);
118
+ }
119
+ const toWithdraw = amountMicro !== undefined ? amountMicro : balance.availableBalanceMicro;
120
+ if (toWithdraw <= 0n) {
121
+ throw new Error(`Withdrawal amount must be positive, got ${toWithdraw}`);
122
+ }
123
+ if (toWithdraw > balance.availableBalanceMicro) {
124
+ throw new Error(`Requested withdrawal ${toWithdraw} exceeds available balance ${balance.availableBalanceMicro}`);
125
+ }
126
+ balance.availableBalanceMicro -= toWithdraw;
127
+ return {
128
+ withdrawnMicro: toWithdraw,
129
+ remainingBalanceMicro: balance.availableBalanceMicro,
130
+ };
131
+ }
132
+ /**
133
+ * Queries transaction history with filters.
134
+ */
135
+ queryTransactions(filters) {
136
+ const results = [];
137
+ for (const tx of this.transactions.values()) {
138
+ if (filters?.serverId && tx.serverId !== filters.serverId)
139
+ continue;
140
+ if (filters?.toolName && tx.toolName !== filters.toolName)
141
+ continue;
142
+ if (filters?.developerWallet && tx.developerWallet !== filters.developerWallet.toLowerCase())
143
+ continue;
144
+ if (filters?.agentWallet && tx.agentWallet !== filters.agentWallet.toLowerCase())
145
+ continue;
146
+ results.push(tx);
147
+ }
148
+ return results;
149
+ }
150
+ /**
151
+ * Retrieves a transaction record by its unique transaction ID.
152
+ */
153
+ getTransaction(transactionId) {
154
+ return this.transactions.get(transactionId);
155
+ }
156
+ /**
157
+ * Reverses a transaction and updates developer account balance.
158
+ */
159
+ refundTransaction(transactionId) {
160
+ const tx = this.transactions.get(transactionId);
161
+ if (!tx || tx.status !== 'confirmed')
162
+ return false;
163
+ tx.status = 'refunded';
164
+ const devKey = tx.developerWallet;
165
+ const balance = this.developerBalances.get(devKey);
166
+ if (balance) {
167
+ balance.totalGrossEarnedMicro -= tx.grossAmountMicro;
168
+ balance.totalPlatformFeesPaidMicro -= tx.platformFeeMicro;
169
+ balance.netPayoutEarnedMicro -= tx.developerPayoutMicro;
170
+ if (balance.availableBalanceMicro >= tx.developerPayoutMicro) {
171
+ balance.availableBalanceMicro -= tx.developerPayoutMicro;
172
+ }
173
+ else {
174
+ balance.availableBalanceMicro = 0n;
175
+ }
176
+ balance.totalPaidCallsCount = Math.max(0, balance.totalPaidCallsCount - 1);
177
+ }
178
+ return true;
179
+ }
180
+ }
181
+ //# sourceMappingURL=fee-ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fee-ledger.js","sourceRoot":"","sources":["../../src/fee/fee-ledger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,MAAM,OAAO,SAAS;IACZ,YAAY,GAAmC,IAAI,GAAG,EAAE,CAAC;IACzD,cAAc,GAAwB,IAAI,GAAG,EAAE,CAAC,CAAC,yBAAyB;IAC1E,iBAAiB,GAAyC,IAAI,GAAG,EAAE,CAAC;IACpE,sBAAsB,CAAS;IAEvC,YAAY,sBAA8B;QACxC,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,iBAAiB,CAAC,MAexB;QACC,oBAAoB;QACpB,IAAI,MAAM,CAAC,cAAc,IAAI,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5E,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,cAAc,CAAE,CAAC;YACrE,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,YAAY,CAAE,CAAC;QAC9C,CAAC;QAED,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;YAChD,OAAO,IA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AE,IAAI,EAAE,CAAC,MAAM,KAAK,WAAW;YAAE,OAAO,KAAK,CAAC;QAEnD,EAAE,CAAC,MAAM,GAAG,UAAU,CAAC;QACvB,MAAM,MAAM,GAAG,EAAE,CAAC,eAAe,CAAC;QAClC,MAAM,OAAO,GAAG,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACnD,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,CAAC,qBAAqB,IAAI,EAAE,CAAC,gBAAgB,CAAC;YACrD,OAAO,CAAC,0BAA0B,IAAI,EAAE,CAAC,gBAAgB,CAAC;YAC1D,OAAO,CAAC,oBAAoB,IAAI,EAAE,CAAC,oBAAoB,CAAC;YACxD,IAAI,OAAO,CAAC,qBAAqB,IAAI,EAAE,CAAC,oBAAoB,EAAE,CAAC;gBAC7D,OAAO,CAAC,qBAAqB,IAAI,EAAE,CAAC,oBAAoB,CAAC;YAC3D,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,qBAAqB,GAAG,EAAE,CAAC;YACrC,CAAC;YACD,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,mBAAmB,GAAG,CAAC,CAAC,CAAC;QAC7E,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * ToolPay Configuration Loader
3
+ * Reads and validates declarative developer configuration files (toolpay.config.json).
4
+ */
5
+ import { ServerPricingConfig } from '../types/pricing.js';
6
+ export interface ToolPayConfigFile {
7
+ version?: string;
8
+ server: {
9
+ id: string;
10
+ name: string;
11
+ upstreamUrl: string;
12
+ payoutWallet: string;
13
+ network?: 'base' | 'solana' | 'ethereum' | 'arbitrum';
14
+ currency?: string;
15
+ platformFeeBps?: number;
16
+ platformTreasuryWallet?: string;
17
+ };
18
+ tools: Array<{
19
+ name: string;
20
+ type: 'free' | 'fixed';
21
+ priceUsd?: string;
22
+ description?: string;
23
+ freemium?: {
24
+ freeCallsPerDay: number;
25
+ };
26
+ circuitBreaker?: {
27
+ maxCallsPerMinute?: number;
28
+ maxSessionSpendUsd?: string;
29
+ preventDuplicateLoops?: boolean;
30
+ };
31
+ }>;
32
+ }
33
+ export declare function loadConfigFile(filePath: string): {
34
+ config: ServerPricingConfig;
35
+ upstreamUrl: string;
36
+ };
37
+ export declare function generateSampleConfigFile(): string;
38
+ //# sourceMappingURL=config-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.d.ts","sourceRoot":"","sources":["../../src/gateway/config-loader.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,mBAAmB,EAAmB,MAAM,qBAAqB,CAAC;AAG3E,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE;QACN,EAAE,EAAE,MAAM,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,OAAO,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,CAAC;QACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,sBAAsB,CAAC,EAAE,MAAM,CAAC;KACjC,CAAC;IACF,KAAK,EAAE,KAAK,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE;YACT,eAAe,EAAE,MAAM,CAAC;SACzB,CAAC;QACF,cAAc,CAAC,EAAE;YACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;YAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;YAC5B,qBAAqB,CAAC,EAAE,OAAO,CAAC;SACjC,CAAC;KACH,CAAC,CAAC;CACJ;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,mBAAmB,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAiFrG;AAED,wBAAgB,wBAAwB,IAAI,MAAM,CAyCjD"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * ToolPay Configuration Loader
3
+ * Reads and validates declarative developer configuration files (toolpay.config.json).
4
+ */
5
+ import { readFileSync, existsSync } from 'node:fs';
6
+ import { usdToMicro } from '../utils/units.js';
7
+ export function loadConfigFile(filePath) {
8
+ if (!existsSync(filePath)) {
9
+ throw new Error(`Configuration file not found: ${filePath}`);
10
+ }
11
+ const raw = readFileSync(filePath, 'utf-8');
12
+ let parsed;
13
+ try {
14
+ parsed = JSON.parse(raw);
15
+ }
16
+ catch (err) {
17
+ throw new Error(`Failed to parse JSON config at ${filePath}: ${err.message}`);
18
+ }
19
+ if (!parsed.server?.id || typeof parsed.server.id !== 'string') {
20
+ throw new Error('Missing or invalid server.id in configuration');
21
+ }
22
+ if (!parsed.server?.upstreamUrl || typeof parsed.server.upstreamUrl !== 'string') {
23
+ throw new Error('Missing or invalid server.upstreamUrl in configuration');
24
+ }
25
+ try {
26
+ const parsedUrl = new URL(parsed.server.upstreamUrl);
27
+ if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
28
+ throw new Error(`Invalid upstreamUrl protocol: ${parsedUrl.protocol}`);
29
+ }
30
+ }
31
+ catch (e) {
32
+ throw new Error(`Invalid server.upstreamUrl: ${e.message}`);
33
+ }
34
+ if (!parsed.server?.payoutWallet || typeof parsed.server.payoutWallet !== 'string') {
35
+ throw new Error('Missing or invalid server.payoutWallet in configuration');
36
+ }
37
+ if (!parsed.server.payoutWallet.startsWith('0x') || parsed.server.payoutWallet.length < 10) {
38
+ throw new Error(`Invalid server.payoutWallet format: ${parsed.server.payoutWallet}`);
39
+ }
40
+ if (parsed.server.platformFeeBps !== undefined) {
41
+ if (!Number.isInteger(parsed.server.platformFeeBps) || parsed.server.platformFeeBps < 0 || parsed.server.platformFeeBps > 2000) {
42
+ throw new Error(`Invalid platformFeeBps: ${parsed.server.platformFeeBps}. Must be an integer between 0 and 2000`);
43
+ }
44
+ }
45
+ const tools = (parsed.tools || []).map((t, idx) => {
46
+ if (!t.name || typeof t.name !== 'string') {
47
+ throw new Error(`Tool at index ${idx} is missing a valid name`);
48
+ }
49
+ if (t.type !== 'free' && t.type !== 'fixed') {
50
+ throw new Error(`Tool "${t.name}" has invalid type "${t.type}". Must be 'free' or 'fixed'`);
51
+ }
52
+ if (t.type === 'fixed') {
53
+ if (!t.priceUsd) {
54
+ throw new Error(`Fixed price tool "${t.name}" must specify priceUsd`);
55
+ }
56
+ usdToMicro(t.priceUsd); // validates format and max 6 decimals
57
+ }
58
+ return {
59
+ toolName: t.name,
60
+ type: t.type,
61
+ priceUsd: t.priceUsd,
62
+ description: t.description,
63
+ freemium: t.freemium ? { freeCallsPerPeriod: t.freemium.freeCallsPerDay, periodSeconds: 86400 } : undefined,
64
+ circuitBreaker: t.circuitBreaker,
65
+ };
66
+ });
67
+ const serverConfig = {
68
+ serverId: parsed.server.id,
69
+ serverName: parsed.server.name || parsed.server.id,
70
+ payoutWallet: parsed.server.payoutWallet,
71
+ network: parsed.server.network || 'base',
72
+ currency: parsed.server.currency || 'USDC',
73
+ platformFee: {
74
+ platformFeeBasisPoints: parsed.server.platformFeeBps ?? 300,
75
+ platformTreasuryWallet: parsed.server.platformTreasuryWallet,
76
+ },
77
+ tools,
78
+ };
79
+ return {
80
+ config: serverConfig,
81
+ upstreamUrl: parsed.server.upstreamUrl,
82
+ };
83
+ }
84
+ export function generateSampleConfigFile() {
85
+ const sample = {
86
+ version: '1.0',
87
+ server: {
88
+ id: 'market-intel-mcp',
89
+ name: 'Market Intelligence MCP Server',
90
+ upstreamUrl: 'http://localhost:3000/mcp',
91
+ payoutWallet: '0xYourEthereumOrBaseWalletAddressHere',
92
+ network: 'base',
93
+ currency: 'USDC',
94
+ platformFeeBps: 300, // 3% platform fee
95
+ },
96
+ tools: [
97
+ {
98
+ name: 'ping',
99
+ type: 'free',
100
+ description: 'Server health check (free)',
101
+ },
102
+ {
103
+ name: 'get_basic_quote',
104
+ type: 'free',
105
+ description: 'Free public stock quote',
106
+ },
107
+ {
108
+ name: 'deep_equity_analysis',
109
+ type: 'fixed',
110
+ priceUsd: '0.02',
111
+ description: 'Comprehensive financial breakdown ($0.02 per call)',
112
+ freemium: {
113
+ freeCallsPerDay: 3, // First 3 calls free per day per agent
114
+ },
115
+ circuitBreaker: {
116
+ maxCallsPerMinute: 20,
117
+ maxSessionSpendUsd: '1.00',
118
+ preventDuplicateLoops: true,
119
+ },
120
+ },
121
+ ],
122
+ };
123
+ return JSON.stringify(sample, null, 2);
124
+ }
125
+ //# sourceMappingURL=config-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.js","sourceRoot":"","sources":["../../src/gateway/config-loader.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AA8B/C,MAAM,UAAU,cAAc,CAAC,QAAgB;IAC7C,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,iCAAiC,QAAQ,EAAE,CAAC,CAAC;IAC/D,CAAC;IAED,MAAM,GAAG,GAAG,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC5C,IAAI,MAAyB,CAAC;IAC9B,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,kCAAkC,QAAQ,KAAK,GAAG,CAAC,OAAO,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AAE,MAAM;gBACZ,WAAW,EAAE,4BAA4B;aAC1C;YACD;gBACE,IAAI,EAAE,iBAAiB;gBACvB,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,yBAAyB;aACvC;YACD;gBACE,IAAI,EAAE,sBAAsB;gBAC5B,IAAI,EAAE,OAAO;gBACb,QAAQ,EAAE,MAAM;gBAChB,WAAW,EAAE,oDAAoD;gBACjE,QAAQ,EAAE;oBACR,eAAe,EAAE,CAAC,EAAE,uCAAuC;iBAC5D;gBACD,cAAc,EAAE;oBACd,iBAAiB,EAAE,EAAE;oBACrB,kBAAkB,EAAE,MAAM;oBAC1B,qBAAqB,EAAE,IAAI;iBAC5B;aACF;SACF;KACF,CAAC;IAEF,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * ToolPay Server Registry
3
+ * Manages configuration and pricing rules for monetized MCP servers.
4
+ */
5
+ import { ServerPricingConfig, ToolPricingRule } from '../types/pricing.js';
6
+ export declare class ServerRegistry {
7
+ private servers;
8
+ /**
9
+ * Registers or updates an MCP server configuration.
10
+ */
11
+ registerServer(config: ServerPricingConfig): void;
12
+ /**
13
+ * Gets a server configuration by ID.
14
+ */
15
+ getServer(serverId: string): ServerPricingConfig | undefined;
16
+ /**
17
+ * Retrieves the pricing rule for a specific tool on a server.
18
+ */
19
+ getToolRule(serverId: string, toolName: string): ToolPricingRule | undefined;
20
+ /**
21
+ * Lists all registered servers.
22
+ */
23
+ listServers(): ServerPricingConfig[];
24
+ }
25
+ //# sourceMappingURL=server-registry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-registry.d.ts","sourceRoot":"","sources":["../../src/gateway/server-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3E,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAA+C;IAE9D;;OAEG;IACI,cAAc,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI;IAUxD;;OAEG;IACI,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS;IAInE;;OAEG;IACI,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IA0BnF;;OAEG;IACI,WAAW,IAAI,mBAAmB,EAAE;CAG5C"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * ToolPay Server Registry
3
+ * Manages configuration and pricing rules for monetized MCP servers.
4
+ */
5
+ export class ServerRegistry {
6
+ servers = new Map();
7
+ /**
8
+ * Registers or updates an MCP server configuration.
9
+ */
10
+ registerServer(config) {
11
+ if (!config.serverId) {
12
+ throw new Error('serverId is required');
13
+ }
14
+ if (!config.payoutWallet) {
15
+ throw new Error('payoutWallet is required');
16
+ }
17
+ this.servers.set(config.serverId, config);
18
+ }
19
+ /**
20
+ * Gets a server configuration by ID.
21
+ */
22
+ getServer(serverId) {
23
+ return this.servers.get(serverId);
24
+ }
25
+ /**
26
+ * Retrieves the pricing rule for a specific tool on a server.
27
+ */
28
+ getToolRule(serverId, toolName) {
29
+ const server = this.servers.get(serverId);
30
+ if (!server)
31
+ return undefined;
32
+ const specificRule = server.tools.find((t) => t.toolName === toolName);
33
+ if (specificRule)
34
+ return specificRule;
35
+ // Fall back to server default pricing if configured
36
+ if (server.defaultPricing) {
37
+ if (server.defaultPricing.type === 'dynamic') {
38
+ throw new Error(`Server "${serverId}" specifies default pricing of type 'dynamic' without a dynamic pricing function`);
39
+ }
40
+ return {
41
+ toolName,
42
+ type: server.defaultPricing.type,
43
+ priceUsd: server.defaultPricing.priceUsd,
44
+ };
45
+ }
46
+ // Default to free if unconfigured
47
+ return {
48
+ toolName,
49
+ type: 'free',
50
+ };
51
+ }
52
+ /**
53
+ * Lists all registered servers.
54
+ */
55
+ listServers() {
56
+ return Array.from(this.servers.values());
57
+ }
58
+ }
59
+ //# sourceMappingURL=server-registry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-registry.js","sourceRoot":"","sources":["../../src/gateway/server-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,MAAM,OAAO,cAAc;IACjB,OAAO,GAAqC,IAAI,GAAG,EAAE,CAAC;IAE9D;;OAEG;IACI,cAAc,CAAC,MAA2B;QAC/C,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAC9C,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACI,SAAS,CAAC,QAAgB;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACpC,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,QAAgB,EAAE,QAAgB;QACnD,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC1C,IAAI,CAAC,MAAM;YAAE,OAAO,SAAS,CAAC;QAE9B,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC;QACvE,IAAI,YAAY;YAAE,OAAO,YAAY,CAAC;QAEtC,oDAAoD;QACpD,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;YAC1B,IAAI,MAAM,CAAC,cAAc,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC7C,MAAM,IAAI,KAAK,CAAC,WAAW,QAAQ,kFAAkF,CAAC,CAAC;YACzH,CAAC;YACD,OAAO;gBACL,QAAQ;gBACR,IAAI,EAAE,MAAM,CAAC,cAAc,CAAC,IAAI;gBAChC,QAAQ,EAAE,MAAM,CAAC,cAAc,CAAC,QAAQ;aACzC,CAAC;QACJ,CAAC;QAED,kCAAkC;QAClC,OAAO;YACL,QAAQ;YACR,IAAI,EAAE,MAAM;SACb,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,WAAW;QAChB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * ToolPay Gateway Engine
3
+ * Reverse proxy interceptor for Model Context Protocol (MCP) JSON-RPC requests.
4
+ */
5
+ import { JsonRpcRequest, JsonRpcResponse } from '../types/mcp.js';
6
+ import { ServerRegistry } from './server-registry.js';
7
+ import { PaymentOrchestrator } from '../settlement/payment-orchestrator.js';
8
+ import { CircuitBreaker } from '../circuit-breaker/circuit-breaker.js';
9
+ import { PaymentProof, X402ErrorResponse } from '../types/x402.js';
10
+ export interface GatewayRequestContext {
11
+ serverId: string;
12
+ agentWallet?: string;
13
+ paymentProof?: PaymentProof;
14
+ idempotencyKey?: string;
15
+ nowSec?: number;
16
+ }
17
+ export interface GatewayResponse {
18
+ statusCode: number;
19
+ headers: Record<string, string>;
20
+ body: JsonRpcResponse | X402ErrorResponse['body'];
21
+ }
22
+ export type UpstreamMcpHandler = (req: JsonRpcRequest) => Promise<JsonRpcResponse>;
23
+ export interface ToolPayGatewayOptions {
24
+ registry: ServerRegistry;
25
+ orchestrator: PaymentOrchestrator;
26
+ circuitBreaker: CircuitBreaker;
27
+ upstreamHandler: UpstreamMcpHandler;
28
+ }
29
+ export declare class ToolPayGateway {
30
+ private registry;
31
+ private orchestrator;
32
+ private circuitBreaker;
33
+ private upstreamHandler;
34
+ private freemiumUsage;
35
+ private idempotencyCache;
36
+ constructor(params: {
37
+ registry: ServerRegistry;
38
+ orchestrator: PaymentOrchestrator;
39
+ circuitBreaker: CircuitBreaker;
40
+ upstreamHandler: UpstreamMcpHandler;
41
+ idempotencyCache?: Map<string, GatewayResponse>;
42
+ freemiumUsage?: Map<string, number>;
43
+ });
44
+ /**
45
+ * Main entry point to handle an incoming MCP JSON-RPC request.
46
+ */
47
+ handleRequest(request: JsonRpcRequest, context: GatewayRequestContext): Promise<GatewayResponse>;
48
+ /**
49
+ * Handles `tools/list`, enriching tools with payment metadata annotations.
50
+ */
51
+ private handleToolsList;
52
+ /**
53
+ * Handles `tools/call`, checking pricing, circuit breakers, and payment verification.
54
+ */
55
+ private handleToolsCall;
56
+ }
57
+ //# sourceMappingURL=toolpay-gateway.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolpay-gateway.d.ts","sourceRoot":"","sources":["../../src/gateway/toolpay-gateway.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,cAAc,EACd,eAAe,EAIhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,MAAM,uCAAuC,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAInE,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,eAAe,G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