@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.
- package/LICENSE +176 -0
- package/NPM_RESTRUCTURE.md +86 -0
- package/OPERATOR-LICENSE.md +18 -0
- package/README.md +380 -0
- package/SELF-HOSTING.md +108 -0
- package/dist/circuit-breaker/circuit-breaker.d.ts +26 -0
- package/dist/circuit-breaker/circuit-breaker.d.ts.map +1 -0
- package/dist/circuit-breaker/circuit-breaker.js +128 -0
- package/dist/circuit-breaker/circuit-breaker.js.map +1 -0
- package/dist/cli.d.ts +21 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1560 -0
- package/dist/cli.js.map +1 -0
- package/dist/contracts/ToolPayRouter.d.ts +288 -0
- package/dist/contracts/ToolPayRouter.d.ts.map +1 -0
- package/dist/contracts/ToolPayRouter.js +370 -0
- package/dist/contracts/ToolPayRouter.js.map +1 -0
- package/dist/contracts/router-registry.d.ts +19 -0
- package/dist/contracts/router-registry.d.ts.map +1 -0
- package/dist/contracts/router-registry.js +28 -0
- package/dist/contracts/router-registry.js.map +1 -0
- package/dist/fee/fee-calculator.d.ts +35 -0
- package/dist/fee/fee-calculator.d.ts.map +1 -0
- package/dist/fee/fee-calculator.js +57 -0
- package/dist/fee/fee-calculator.js.map +1 -0
- package/dist/fee/fee-ledger.d.ts +66 -0
- package/dist/fee/fee-ledger.d.ts.map +1 -0
- package/dist/fee/fee-ledger.js +181 -0
- package/dist/fee/fee-ledger.js.map +1 -0
- package/dist/gateway/config-loader.d.ts +38 -0
- package/dist/gateway/config-loader.d.ts.map +1 -0
- package/dist/gateway/config-loader.js +125 -0
- package/dist/gateway/config-loader.js.map +1 -0
- package/dist/gateway/server-registry.d.ts +25 -0
- package/dist/gateway/server-registry.d.ts.map +1 -0
- package/dist/gateway/server-registry.js +59 -0
- package/dist/gateway/server-registry.js.map +1 -0
- package/dist/gateway/toolpay-gateway.d.ts +57 -0
- package/dist/gateway/toolpay-gateway.d.ts.map +1 -0
- package/dist/gateway/toolpay-gateway.js +320 -0
- package/dist/gateway/toolpay-gateway.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/sdk/client-agent.d.ts +47 -0
- package/dist/sdk/client-agent.d.ts.map +1 -0
- package/dist/sdk/client-agent.js +158 -0
- package/dist/sdk/client-agent.js.map +1 -0
- package/dist/sdk/server-sdk.d.ts +51 -0
- package/dist/sdk/server-sdk.d.ts.map +1 -0
- package/dist/sdk/server-sdk.js +170 -0
- package/dist/sdk/server-sdk.js.map +1 -0
- package/dist/settlement/base-contract-client.d.ts +156 -0
- package/dist/settlement/base-contract-client.d.ts.map +1 -0
- package/dist/settlement/base-contract-client.js +156 -0
- package/dist/settlement/base-contract-client.js.map +1 -0
- package/dist/settlement/nonce-store.d.ts +54 -0
- package/dist/settlement/nonce-store.d.ts.map +1 -0
- package/dist/settlement/nonce-store.js +125 -0
- package/dist/settlement/nonce-store.js.map +1 -0
- package/dist/settlement/onchain-verifier.d.ts +41 -0
- package/dist/settlement/onchain-verifier.d.ts.map +1 -0
- package/dist/settlement/onchain-verifier.js +114 -0
- package/dist/settlement/onchain-verifier.js.map +1 -0
- package/dist/settlement/payment-orchestrator.d.ts +59 -0
- package/dist/settlement/payment-orchestrator.d.ts.map +1 -0
- package/dist/settlement/payment-orchestrator.js +282 -0
- package/dist/settlement/payment-orchestrator.js.map +1 -0
- package/dist/settlement/permit-verifier.d.ts +60 -0
- package/dist/settlement/permit-verifier.d.ts.map +1 -0
- package/dist/settlement/permit-verifier.js +80 -0
- package/dist/settlement/permit-verifier.js.map +1 -0
- package/dist/settlement/session-wallet.d.ts +60 -0
- package/dist/settlement/session-wallet.d.ts.map +1 -0
- package/dist/settlement/session-wallet.js +105 -0
- package/dist/settlement/session-wallet.js.map +1 -0
- package/dist/transports/sse-transport.d.ts +27 -0
- package/dist/transports/sse-transport.d.ts.map +1 -0
- package/dist/transports/sse-transport.js +65 -0
- package/dist/transports/sse-transport.js.map +1 -0
- package/dist/transports/stdio-bridge.d.ts +29 -0
- package/dist/transports/stdio-bridge.d.ts.map +1 -0
- package/dist/transports/stdio-bridge.js +133 -0
- package/dist/transports/stdio-bridge.js.map +1 -0
- package/dist/types/circuit-breaker.d.ts +22 -0
- package/dist/types/circuit-breaker.d.ts.map +1 -0
- package/dist/types/circuit-breaker.js +5 -0
- package/dist/types/circuit-breaker.js.map +1 -0
- package/dist/types/ledger.d.ts +57 -0
- package/dist/types/ledger.d.ts.map +1 -0
- package/dist/types/ledger.js +5 -0
- package/dist/types/ledger.js.map +1 -0
- package/dist/types/mcp.d.ts +67 -0
- package/dist/types/mcp.d.ts.map +1 -0
- package/dist/types/mcp.js +5 -0
- package/dist/types/mcp.js.map +1 -0
- package/dist/types/pricing.d.ts +68 -0
- package/dist/types/pricing.d.ts.map +1 -0
- package/dist/types/pricing.js +5 -0
- package/dist/types/pricing.js.map +1 -0
- package/dist/types/x402.d.ts +76 -0
- package/dist/types/x402.d.ts.map +1 -0
- package/dist/types/x402.js +5 -0
- package/dist/types/x402.js.map +1 -0
- package/dist/utils/units.d.ts +45 -0
- package/dist/utils/units.d.ts.map +1 -0
- package/dist/utils/units.js +141 -0
- package/dist/utils/units.js.map +1 -0
- package/package.json +72 -0
- package/src/contracts/ToolPayRouter.json +371 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"circuit-breaker.js","sourceRoot":"","sources":["../../src/types/circuit-breaker.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolPay Financial Ledger and Platform Fee Tracking Types
|
|
3
|
+
*/
|
|
4
|
+
import { PaymentScheme } from './x402.js';
|
|
5
|
+
export type TransactionStatus = 'confirmed' | 'pending' | 'failed' | 'refunded';
|
|
6
|
+
export interface TransactionRecord {
|
|
7
|
+
transactionId: string;
|
|
8
|
+
serverId: string;
|
|
9
|
+
toolName: string;
|
|
10
|
+
agentWallet: string;
|
|
11
|
+
developerWallet: string;
|
|
12
|
+
platformTreasuryWallet: string;
|
|
13
|
+
scheme: PaymentScheme;
|
|
14
|
+
grossAmountMicro: bigint;
|
|
15
|
+
platformFeeMicro: bigint;
|
|
16
|
+
developerPayoutMicro: bigint;
|
|
17
|
+
platformFeeBasisPoints: number;
|
|
18
|
+
settlementRef?: string;
|
|
19
|
+
idempotencyKey?: string;
|
|
20
|
+
status: TransactionStatus;
|
|
21
|
+
createdAt: number;
|
|
22
|
+
confirmedAt?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface DeveloperAccountBalance {
|
|
25
|
+
developerWallet: string;
|
|
26
|
+
totalGrossEarnedMicro: bigint;
|
|
27
|
+
totalPlatformFeesPaidMicro: bigint;
|
|
28
|
+
netPayoutEarnedMicro: bigint;
|
|
29
|
+
availableBalanceMicro: bigint;
|
|
30
|
+
totalPaidCallsCount: number;
|
|
31
|
+
}
|
|
32
|
+
export interface PlatformTreasurySummary {
|
|
33
|
+
treasuryWallet: string;
|
|
34
|
+
totalGrossVolumeMicro: bigint;
|
|
35
|
+
totalPlatformFeesCollectedMicro: bigint;
|
|
36
|
+
totalDeveloperPayoutsMicro: bigint;
|
|
37
|
+
totalTransactionCount: number;
|
|
38
|
+
uniqueDevelopersCount: number;
|
|
39
|
+
uniqueAgentsCount: number;
|
|
40
|
+
}
|
|
41
|
+
export type WithdrawalStatus = 'pending' | 'processing' | 'settling' | 'completed' | 'failed';
|
|
42
|
+
export interface WithdrawalRecord {
|
|
43
|
+
id: string;
|
|
44
|
+
userId: string;
|
|
45
|
+
developerWallet: string;
|
|
46
|
+
amountMicro: number;
|
|
47
|
+
currency: string;
|
|
48
|
+
network: string;
|
|
49
|
+
chainId: number;
|
|
50
|
+
status: WithdrawalStatus;
|
|
51
|
+
txHash?: string;
|
|
52
|
+
errorMessage?: string;
|
|
53
|
+
createdAt: string;
|
|
54
|
+
updatedAt: string;
|
|
55
|
+
completedAt?: string;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=ledger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../src/types/ledger.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,MAAM,MAAM,iBAAiB,GAAG,WAAW,GAAG,SAAS,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEhF,MAAM,WAAW,iBAAiB;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,sBAAsB,EAAE,MAAM,CAAC;IAC/B,MAAM,EAAE,aAAa,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,uBAAuB;IACtC,eAAe,EAAE,MAAM,CAAC;IACxB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,0BAA0B,EAAE,MAAM,CAAC;IACnC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,uBAAuB;IACtC,cAAc,EAAE,MAAM,CAAC;IACvB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,+BAA+B,EAAE,MAAM,CAAC;IACxC,0BAA0B,EAAE,MAAM,CAAC;IACnC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,QAAQ,CAAC;AAE9F,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,gBAAgB,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../src/types/ledger.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model Context Protocol (MCP) JSON-RPC 2.0 Types
|
|
3
|
+
*/
|
|
4
|
+
export interface JsonRpcRequest<T = any> {
|
|
5
|
+
jsonrpc: '2.0';
|
|
6
|
+
id?: string | number | null;
|
|
7
|
+
method: string;
|
|
8
|
+
params?: T;
|
|
9
|
+
}
|
|
10
|
+
export interface JsonRpcResponse<T = any> {
|
|
11
|
+
jsonrpc: '2.0';
|
|
12
|
+
id: string | number | null;
|
|
13
|
+
result?: T;
|
|
14
|
+
error?: JsonRpcError;
|
|
15
|
+
}
|
|
16
|
+
export interface JsonRpcError {
|
|
17
|
+
code: number;
|
|
18
|
+
message: string;
|
|
19
|
+
data?: any;
|
|
20
|
+
}
|
|
21
|
+
export interface McpToolAnnotation {
|
|
22
|
+
readOnlyHint?: boolean;
|
|
23
|
+
destructiveHint?: boolean;
|
|
24
|
+
idempotentHint?: boolean;
|
|
25
|
+
payment?: {
|
|
26
|
+
required: boolean;
|
|
27
|
+
costUsd?: string;
|
|
28
|
+
currency?: string;
|
|
29
|
+
networks?: string[];
|
|
30
|
+
description?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface McpToolDefinition {
|
|
34
|
+
name: string;
|
|
35
|
+
description?: string;
|
|
36
|
+
inputSchema: {
|
|
37
|
+
type: string;
|
|
38
|
+
properties?: Record<string, any>;
|
|
39
|
+
required?: string[];
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
};
|
|
42
|
+
annotations?: McpToolAnnotation;
|
|
43
|
+
}
|
|
44
|
+
export interface McpToolsListResult {
|
|
45
|
+
tools: McpToolDefinition[];
|
|
46
|
+
nextCursor?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface McpToolCallParams {
|
|
49
|
+
name: string;
|
|
50
|
+
arguments?: Record<string, any>;
|
|
51
|
+
_meta?: {
|
|
52
|
+
progressToken?: string | number;
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export interface McpToolCallContentItem {
|
|
57
|
+
type: 'text' | 'image' | 'resource';
|
|
58
|
+
text?: string;
|
|
59
|
+
data?: string;
|
|
60
|
+
mimeType?: string;
|
|
61
|
+
resource?: any;
|
|
62
|
+
}
|
|
63
|
+
export interface McpToolCallResult {
|
|
64
|
+
content: McpToolCallContentItem[];
|
|
65
|
+
isError?: boolean;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=mcp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/types/mcp.ts"],"names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ACF,WAAW,CAAC,EAAE,iBAAiB,CAAC;CACjC;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,iBAAiB,EAAE,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE;QACN,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QAChC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB,CAAC;CACH;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,GAAG,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,sBAAsB,EAAE,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../../src/types/mcp.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolPay Pricing Configurations and Rules
|
|
3
|
+
*/
|
|
4
|
+
export type PricingType = 'free' | 'fixed' | 'dynamic';
|
|
5
|
+
export type SupportedNetwork = 'base' | 'base-sepolia' | 'solana' | 'ethereum' | 'arbitrum';
|
|
6
|
+
export type DynamicPricingFn = (args: Record<string, any>) => string;
|
|
7
|
+
export interface ToolCircuitBreakerConfig {
|
|
8
|
+
/** Maximum calls permitted in a 60-second sliding window */
|
|
9
|
+
maxCallsPerMinute?: number;
|
|
10
|
+
/** Maximum aggregate spend in USD permitted per session/agent */
|
|
11
|
+
maxSessionSpendUsd?: string;
|
|
12
|
+
/** Disallow rapid duplicate calls with identical arguments */
|
|
13
|
+
preventDuplicateLoops?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface FreemiumAllowanceConfig {
|
|
16
|
+
/** Number of calls per calendar day or window that are free */
|
|
17
|
+
freeCallsPerPeriod: number;
|
|
18
|
+
/** Period duration in seconds (defaults to 86400 = 24 hours) */
|
|
19
|
+
periodSeconds?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface ToolPricingRule {
|
|
22
|
+
/** Tool name matching the MCP schema */
|
|
23
|
+
toolName: string;
|
|
24
|
+
/** 'free', 'fixed', or 'dynamic' */
|
|
25
|
+
type: PricingType;
|
|
26
|
+
/** Fixed USD price (e.g. "0.005"), required if type === 'fixed' */
|
|
27
|
+
priceUsd?: string;
|
|
28
|
+
/** Dynamic pricing function if type === 'dynamic' */
|
|
29
|
+
dynamicPricing?: DynamicPricingFn;
|
|
30
|
+
/** Freemium allowance config */
|
|
31
|
+
freemium?: FreemiumAllowanceConfig;
|
|
32
|
+
/** Tool-specific circuit breaker overrides */
|
|
33
|
+
circuitBreaker?: ToolCircuitBreakerConfig;
|
|
34
|
+
/** Tool-specific description for payment prompt */
|
|
35
|
+
description?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface PlatformFeeConfig {
|
|
38
|
+
/** Platform cut in basis points (e.g., 300 = 3.00%) */
|
|
39
|
+
platformFeeBasisPoints: number;
|
|
40
|
+
/** ToolPay platform treasury wallet address */
|
|
41
|
+
platformTreasuryWallet: string;
|
|
42
|
+
/** Minimum platform cut in micro-units ($0.000001) */
|
|
43
|
+
minPlatformFeeMicro?: bigint;
|
|
44
|
+
}
|
|
45
|
+
export interface ServerPricingConfig {
|
|
46
|
+
/** Identifier for this MCP server instance */
|
|
47
|
+
serverId: string;
|
|
48
|
+
/** Human-readable server name */
|
|
49
|
+
serverName: string;
|
|
50
|
+
/** Developer's destination payout wallet address (e.g. 0x...) */
|
|
51
|
+
payoutWallet: string;
|
|
52
|
+
/** Default settlement network */
|
|
53
|
+
network: SupportedNetwork;
|
|
54
|
+
/** Default settlement currency */
|
|
55
|
+
currency: string;
|
|
56
|
+
/** Platform fee configuration for this server */
|
|
57
|
+
platformFee?: Partial<PlatformFeeConfig>;
|
|
58
|
+
/** Global circuit breaker rules for the server */
|
|
59
|
+
globalCircuitBreaker?: ToolCircuitBreakerConfig;
|
|
60
|
+
/** Default tool pricing if not explicitly specified ('free' or 'fixed') */
|
|
61
|
+
defaultPricing?: {
|
|
62
|
+
type: PricingType;
|
|
63
|
+
priceUsd?: string;
|
|
64
|
+
};
|
|
65
|
+
/** Per-tool pricing rules */
|
|
66
|
+
tools: ToolPricingRule[];
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=pricing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../../src/types/pricing.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAEvD,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,cAAc,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,CAAC;AAE5F,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAAC;AAErE,MAAM,WAAW,wBAAwB;IACvC,4DAA4D;IAC5D,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iEAAiE;IACjE,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,8DAA8D;IAC9D,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED,MAAM,WAAW,uBAAuB;IACtC,+DAA+D;IAC/D,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gEAAgE;IAChE,aAAa,CAAC,EAAE,MAAM,C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"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 Protocol & Payment Challenge Types for ToolPay
|
|
3
|
+
*/
|
|
4
|
+
import { SupportedNetwork } from './pricing.js';
|
|
5
|
+
export type PaymentScheme = 'onchain-tx' | 'eip712-permit' | 'session-wallet';
|
|
6
|
+
export interface X402ChallengeData {
|
|
7
|
+
serverId: string;
|
|
8
|
+
toolName: string;
|
|
9
|
+
priceUsd: string;
|
|
10
|
+
amountMicro: bigint;
|
|
11
|
+
currency: string;
|
|
12
|
+
recipientWallet: string;
|
|
13
|
+
verifyingContract?: string;
|
|
14
|
+
developerWallet?: string;
|
|
15
|
+
platformTreasuryWallet: string;
|
|
16
|
+
platformFeeBasisPoints: number;
|
|
17
|
+
platformCutMicro: bigint;
|
|
18
|
+
developerCutMicro: bigint;
|
|
19
|
+
network: SupportedNetwork;
|
|
20
|
+
chainId: number;
|
|
21
|
+
tokenAddress?: string;
|
|
22
|
+
challengeNonce: string;
|
|
23
|
+
expiresAt: number;
|
|
24
|
+
}
|
|
25
|
+
export interface X402ChallengeHeaders {
|
|
26
|
+
'X-Payment-Required': 'true';
|
|
27
|
+
'X-Payment-Amount': string;
|
|
28
|
+
'X-Payment-Currency': string;
|
|
29
|
+
'X-Payment-Recipient': string;
|
|
30
|
+
'X-Payment-Network': string;
|
|
31
|
+
'X-Payment-Chain-Id': string;
|
|
32
|
+
'X-Payment-Nonce': string;
|
|
33
|
+
'X-Payment-Expires': string;
|
|
34
|
+
'X-Payment-Platform-Fee-Bps'?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface PaymentProof {
|
|
37
|
+
scheme: PaymentScheme;
|
|
38
|
+
challengeNonce: string;
|
|
39
|
+
signerWallet: string;
|
|
40
|
+
txHash?: string;
|
|
41
|
+
network?: SupportedNetwork;
|
|
42
|
+
signature?: string;
|
|
43
|
+
deadline?: number;
|
|
44
|
+
maxAmountMicro?: bigint;
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
idempotencyKey?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface X402ErrorResponse {
|
|
49
|
+
status: 402;
|
|
50
|
+
headers: Record<string, string>;
|
|
51
|
+
body: {
|
|
52
|
+
error: {
|
|
53
|
+
code: 402;
|
|
54
|
+
message: string;
|
|
55
|
+
data: Omit<X402ChallengeData, 'amountMicro' | 'platformCutMicro' | 'developerCutMicro'> & {
|
|
56
|
+
amountMicro: string;
|
|
57
|
+
platformCutMicro: string;
|
|
58
|
+
developerCutMicro: string;
|
|
59
|
+
instructions?: {
|
|
60
|
+
summary: string;
|
|
61
|
+
steps: string[];
|
|
62
|
+
network: string;
|
|
63
|
+
chainId?: number;
|
|
64
|
+
currency: string;
|
|
65
|
+
cost: string;
|
|
66
|
+
fundingGuide?: {
|
|
67
|
+
network: string;
|
|
68
|
+
usdcTokenAddress: string;
|
|
69
|
+
methods: string[];
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=x402.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402.d.ts","sourceRoot":"","sources":["../../src/types/x402.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG,eAAe,GAAG,gBAAgB,CAAC;AAE9E,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAC;IAAE,sBAAsB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E,IAAI,CAAC,iBAAiB,EAAE,aAAa,GAAG,kBAAkB,GAAG,mBAAmB,CAAC,GAAG;gBACxF,WAAW,EAAE,MAAM,CAAC;gBACpB,gBAAgB,EAAE,MAAM,CAAC;gBACzB,iBAAiB,EAAE,MAAM,CAAC;gBAC1B,YAAY,CAAC,EAAE;oBACb,OAAO,EAAE,MAAM,CAAC;oBAChB,KAAK,EAAE,MAAM,EAAE,CAAC;oBAChB,OAAO,EAAE,MAAM,CAAC;oBAChB,OAAO,CAAC,EAAE,MAAM,CAAC;oBACjB,QAAQ,EAAE,MAAM,CAAC;oBACjB,IAAI,EAAE,MAAM,CAAC;oBACb,YAAY,CAAC,EAAE;wBACb,OAAO,EAAE,MAAM,CAAC;wBAChB,gBAAgB,EAAE,MAAM,CAAC;wBACzB,OAAO,EAAE,MAAM,EAAE,CAAC;qBACnB,CAAC;iBACH,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402.js","sourceRoot":"","sources":["../../src/types/x402.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe currency and micro-unit arithmetic for ToolPay micropayments.
|
|
3
|
+
* Standardizes on 6 decimal places (micro-USD / atomic USDC units).
|
|
4
|
+
* 1.000000 USD = 1,000,000 micro-units.
|
|
5
|
+
*/
|
|
6
|
+
import { SupportedNetwork } from '../types/pricing.js';
|
|
7
|
+
export declare const DECIMALS = 6;
|
|
8
|
+
export declare const SCALE = 1000000n;
|
|
9
|
+
export declare const BASIS_POINTS_DIVISOR = 10000n;
|
|
10
|
+
export declare const MAX_PLATFORM_FEE_BPS = 2000;
|
|
11
|
+
export declare const NETWORK_CHAIN_IDS: Record<string, number>;
|
|
12
|
+
export declare function getChainIdForNetwork(network: SupportedNetwork): number;
|
|
13
|
+
export declare function uuidToBytes32(uuid: string): `0x${string}`;
|
|
14
|
+
export declare function normalizeNonceToBytes32(nonce: string): `0x${string}`;
|
|
15
|
+
/**
|
|
16
|
+
* Deterministically serializes values to canonical JSON with sorted object keys at all nesting depths.
|
|
17
|
+
* Prevents key-ordering divergence and nested object stripping.
|
|
18
|
+
*/
|
|
19
|
+
export declare function canonicalJsonStringify(val: any): string;
|
|
20
|
+
/**
|
|
21
|
+
* Safely stringifies objects containing BigInt values without throwing.
|
|
22
|
+
*/
|
|
23
|
+
export declare function safeJsonStringify(val: any, space?: number): string;
|
|
24
|
+
/**
|
|
25
|
+
* Converts a USD string or number (e.g. "0.005" or 0.01) to atomic micro-units (BigInt).
|
|
26
|
+
* Throws if the input is negative, NaN, or has more than 6 decimal places.
|
|
27
|
+
*/
|
|
28
|
+
export declare function usdToMicro(amount: string | number): bigint;
|
|
29
|
+
/**
|
|
30
|
+
* Converts atomic micro-units (BigInt) to a formatted USD string (e.g. "0.005000" or "1.250000").
|
|
31
|
+
*/
|
|
32
|
+
export declare function microToUsd(micro: bigint, trimTrailingZeros?: boolean): string;
|
|
33
|
+
/**
|
|
34
|
+
* Calculates platform fee percentage cut and developer payout with precision.
|
|
35
|
+
*
|
|
36
|
+
* @param grossMicro The total gross cost in atomic micro-units
|
|
37
|
+
* @param feeBasisPoints The platform fee in basis points (e.g., 300 bps = 3.00%)
|
|
38
|
+
* @param minPlatformFeeMicro Optional minimum platform fee (e.g. at least 1 micro-cent)
|
|
39
|
+
* @returns { platformFeeMicro, developerPayoutMicro }
|
|
40
|
+
*/
|
|
41
|
+
export declare function calculatePlatformCut(grossMicro: bigint, feeBasisPoints: number, minPlatformFeeMicro?: bigint): {
|
|
42
|
+
platformFeeMicro: bigint;
|
|
43
|
+
developerPayoutMicro: bigint;
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=units.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../src/utils/units.ts"],"names":[],"mappings":"AAAA;;;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
|
+
* 1.000000 USD = 1,000,000 micro-units.
|
|
5
|
+
*/
|
|
6
|
+
import { keccak256, stringToBytes } from 'viem';
|
|
7
|
+
export const DECIMALS = 6;
|
|
8
|
+
export const SCALE = 1000000n;
|
|
9
|
+
export const BASIS_POINTS_DIVISOR = 10000n; // 10,000 bps = 100.00%
|
|
10
|
+
export const MAX_PLATFORM_FEE_BPS = 2000; // 20.00% platform fee cap matching smart contract
|
|
11
|
+
export const NETWORK_CHAIN_IDS = {
|
|
12
|
+
'base': 8453,
|
|
13
|
+
'base-sepolia': 84532,
|
|
14
|
+
'ethereum': 1,
|
|
15
|
+
'arbitrum': 42161,
|
|
16
|
+
};
|
|
17
|
+
export function getChainIdForNetwork(network) {
|
|
18
|
+
const chainId = NETWORK_CHAIN_IDS[network];
|
|
19
|
+
if (!chainId) {
|
|
20
|
+
throw new Error(`Unsupported network for EVM settlement: "${network}"`);
|
|
21
|
+
}
|
|
22
|
+
return chainId;
|
|
23
|
+
}
|
|
24
|
+
export function uuidToBytes32(uuid) {
|
|
25
|
+
return keccak256(stringToBytes(uuid));
|
|
26
|
+
}
|
|
27
|
+
export function normalizeNonceToBytes32(nonce) {
|
|
28
|
+
if (nonce.startsWith('0x') && nonce.length === 66) {
|
|
29
|
+
return nonce.toLowerCase();
|
|
30
|
+
}
|
|
31
|
+
return uuidToBytes32(nonce);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Deterministically serializes values to canonical JSON with sorted object keys at all nesting depths.
|
|
35
|
+
* Prevents key-ordering divergence and nested object stripping.
|
|
36
|
+
*/
|
|
37
|
+
export function canonicalJsonStringify(val) {
|
|
38
|
+
if (typeof val === 'bigint') {
|
|
39
|
+
return JSON.stringify(val.toString());
|
|
40
|
+
}
|
|
41
|
+
if (val === null || typeof val !== 'object') {
|
|
42
|
+
return JSON.stringify(val);
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(val)) {
|
|
45
|
+
return `[${val.map(canonicalJsonStringify).join(',')}]`;
|
|
46
|
+
}
|
|
47
|
+
const keys = Object.keys(val).sort();
|
|
48
|
+
const pairs = keys.map((k) => `${JSON.stringify(k)}:${canonicalJsonStringify(val[k])}`);
|
|
49
|
+
return `{${pairs.join(',')}}`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Safely stringifies objects containing BigInt values without throwing.
|
|
53
|
+
*/
|
|
54
|
+
export function safeJsonStringify(val, space) {
|
|
55
|
+
return JSON.stringify(val, (_, v) => (typeof v === 'bigint' ? v.toString() : v), space);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts a USD string or number (e.g. "0.005" or 0.01) to atomic micro-units (BigInt).
|
|
59
|
+
* Throws if the input is negative, NaN, or has more than 6 decimal places.
|
|
60
|
+
*/
|
|
61
|
+
export function usdToMicro(amount) {
|
|
62
|
+
if (typeof amount === 'number') {
|
|
63
|
+
if (!Number.isFinite(amount) || amount < 0) {
|
|
64
|
+
throw new Error(`Invalid USD amount: ${amount}`);
|
|
65
|
+
}
|
|
66
|
+
// Convert to fixed 6 decimal string to avoid float precision issues
|
|
67
|
+
amount = amount.toFixed(6);
|
|
68
|
+
}
|
|
69
|
+
const trimmed = amount.trim();
|
|
70
|
+
if (!trimmed || !/^\d+(\.\d+)?$/.test(trimmed)) {
|
|
71
|
+
throw new Error(`Invalid USD string format: "${amount}"`);
|
|
72
|
+
}
|
|
73
|
+
const parts = trimmed.split('.');
|
|
74
|
+
const wholePart = BigInt(parts[0]);
|
|
75
|
+
let fractionPart = parts[1] || '';
|
|
76
|
+
if (fractionPart.length > DECIMALS) {
|
|
77
|
+
throw new Error(`USD amount "${amount}" exceeds maximum 6 decimal precision`);
|
|
78
|
+
}
|
|
79
|
+
// Pad fraction to 6 digits
|
|
80
|
+
fractionPart = fractionPart.padEnd(DECIMALS, '0');
|
|
81
|
+
const fractionBigInt = BigInt(fractionPart);
|
|
82
|
+
return wholePart * SCALE + fractionBigInt;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Converts atomic micro-units (BigInt) to a formatted USD string (e.g. "0.005000" or "1.250000").
|
|
86
|
+
*/
|
|
87
|
+
export function microToUsd(micro, trimTrailingZeros = false) {
|
|
88
|
+
if (micro < 0n) {
|
|
89
|
+
throw new Error(`Negative micro-units not allowed: ${micro}`);
|
|
90
|
+
}
|
|
91
|
+
const whole = micro / SCALE;
|
|
92
|
+
const fraction = (micro % SCALE).toString().padStart(DECIMALS, '0');
|
|
93
|
+
let result = `${whole}.${fraction}`;
|
|
94
|
+
if (trimTrailingZeros) {
|
|
95
|
+
// Keep at least 2 decimal places (e.g., "1.20" instead of "1.2")
|
|
96
|
+
result = result.replace(/(\.\d{2,}?)0+$/, '$1');
|
|
97
|
+
if (result.endsWith('.')) {
|
|
98
|
+
result += '00';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Calculates platform fee percentage cut and developer payout with precision.
|
|
105
|
+
*
|
|
106
|
+
* @param grossMicro The total gross cost in atomic micro-units
|
|
107
|
+
* @param feeBasisPoints The platform fee in basis points (e.g., 300 bps = 3.00%)
|
|
108
|
+
* @param minPlatformFeeMicro Optional minimum platform fee (e.g. at least 1 micro-cent)
|
|
109
|
+
* @returns { platformFeeMicro, developerPayoutMicro }
|
|
110
|
+
*/
|
|
111
|
+
export function calculatePlatformCut(grossMicro, feeBasisPoints, minPlatformFeeMicro = 0n) {
|
|
112
|
+
if (grossMicro < 0n) {
|
|
113
|
+
throw new Error(`Gross amount cannot be negative: ${grossMicro}`);
|
|
114
|
+
}
|
|
115
|
+
if (!Number.isInteger(feeBasisPoints) || feeBasisPoints < 0 || feeBasisPoints > MAX_PLATFORM_FEE_BPS) {
|
|
116
|
+
throw new Error(`Fee basis points must be between 0 and ${MAX_PLATFORM_FEE_BPS} (0% to 20%), got ${feeBasisPoints}`);
|
|
117
|
+
}
|
|
118
|
+
if (grossMicro === 0n || feeBasisPoints === 0) {
|
|
119
|
+
return {
|
|
120
|
+
platformFeeMicro: 0n,
|
|
121
|
+
developerPayoutMicro: grossMicro,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const bpsBigInt = BigInt(feeBasisPoints);
|
|
125
|
+
// Use integer floor division matching EVM smart contracts: (gross * bps) / 10000
|
|
126
|
+
let platformFeeMicro = (grossMicro * bpsBigInt) / BASIS_POINTS_DIVISOR;
|
|
127
|
+
// Apply minimum fee floor if gross is greater than 0
|
|
128
|
+
if (platformFeeMicro < minPlatformFeeMicro && grossMicro > 0n) {
|
|
129
|
+
platformFeeMicro = minPlatformFeeMicro;
|
|
130
|
+
}
|
|
131
|
+
// Safety: platform fee cannot exceed gross
|
|
132
|
+
if (platformFeeMicro > grossMicro) {
|
|
133
|
+
platformFeeMicro = grossMicro;
|
|
134
|
+
}
|
|
135
|
+
const developerPayoutMicro = grossMicro - platformFeeMicro;
|
|
136
|
+
return {
|
|
137
|
+
platformFeeMicro,
|
|
138
|
+
developerPayoutMicro,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.js","sourceRoot":"","sources":["../../src/utils/units.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAGhD,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAC;AAC1B,MAAM,CAAC,MAAM,KAAK,GAAG,QAAU,CAAC;AAChC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAO,CAAC,CAAC,uBAAuB;AACpE,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC,CAAC,kDAAkD;AAE5F,MAAM,CAAC,MAAM,iBAAiB,GAA2B;IACvD,MAAM,EAAE,IAAI;IACZ,cAAc,EAAE,KAAK;IACrB,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,KAAK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,IAAI,KAAK,CAAC,oCAAoC,UAAU,EAAE,CAAC,CAAC;IACpE,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,cAAc,GAAG,CAAC,IAAI,cAAc,GAAG,oBAAoB,EAAE,CAAC;QACrG,MAAM,IAAI,KAAK,CAAC,0CAA0C,oBAAoB,qBAAqB,cAAc,EAAE,CAAC,CAAC;IACvH,CAAC;IAED,IAAI,UAAU,KAAK,EAAE,IAAI,cAAc,KAAK,CAAC,EAAE,CAAC;QAC9C,OAAO;YACL,gBAAgB,EAAE,EAAE;YACpB,oBAAoB,EAAE,UAAU;SACjC,CAAC;IACJ,CAAC;IAED,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;IAEzC,iFAAiF;IACjF,IAAI,gBAAgB,GAAG,CAAC,UAAU,GAAG,SAAS,CAAC,GAAG,oBAAoB,CAAC;IAEvE,qDAAqD;IACrD,IAAI,gBAAgB,GAAG,mBAAmB,IAAI,UAAU,GAAG,EAAE,EAAE,CAAC;QAC9D,gBAAgB,GAAG,mBAAmB,CAAC;IACzC,CAAC;IAED,2CAA2C;IAC3C,IAAI,gBAAgB,GAAG,UAAU,EAAE,CAAC;QAClC,gBAAgB,GAAG,UAAU,CAAC;IAChC,CAAC;IAED,MAAM,oBAAoB,GAAG,UAAU,GAAG,gBAAgB,CAAC;IAE3D,OAAO;QACL,gBAAgB;QAChB,oBAAoB;KACrB,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mcpaid/sdk",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "MCPaid: Zero-friction MCP monetization and micropayment infrastructure with x402 and blockchain settlement",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./cli": "./dist/cli.js",
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"mcpaid": "dist/cli.js",
|
|
18
|
+
"toolpay": "dist/cli.js"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"!dist/worker.*",
|
|
23
|
+
"!dist/gateway/d1-store.*",
|
|
24
|
+
"!dist/gateway/dashboard-html.*",
|
|
25
|
+
"!dist/gateway/landing-html.*",
|
|
26
|
+
"!dist/gateway/docs-html.*",
|
|
27
|
+
"!dist/gateway/docs-markdown.*",
|
|
28
|
+
"!dist/settlement/withdrawal-relayer.*",
|
|
29
|
+
"!dist/services/**",
|
|
30
|
+
"!dist/oracle/**",
|
|
31
|
+
"src/contracts/ToolPayRouter.json",
|
|
32
|
+
"README.md",
|
|
33
|
+
"SELF-HOSTING.md",
|
|
34
|
+
"OPERATOR-LICENSE.md",
|
|
35
|
+
"NPM_RESTRUCTURE.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"homepage": "https://mcpaid.dev",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"compile:contracts": "tsx scripts/compile-contract.ts",
|
|
41
|
+
"bridge:sepolia": "tsx scripts/bridge-to-base.ts",
|
|
42
|
+
"settle:withdrawals": "tsx scripts/settle-withdrawals.ts",
|
|
43
|
+
"build": "tsc",
|
|
44
|
+
"prepublishOnly": "npm run build && npm test",
|
|
45
|
+
"test": "tsx --test test/*.test.ts",
|
|
46
|
+
"test:watch": "tsx --test --watch test/*.test.ts"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"mcp",
|
|
50
|
+
"model-context-protocol",
|
|
51
|
+
"x402",
|
|
52
|
+
"micropayments",
|
|
53
|
+
"ai-agents",
|
|
54
|
+
"monetization",
|
|
55
|
+
"blockchain",
|
|
56
|
+
"base",
|
|
57
|
+
"usdc"
|
|
58
|
+
],
|
|
59
|
+
"author": "MCPaid",
|
|
60
|
+
"license": "Apache-2.0",
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@cloudflare/workers-types": "^5.20260911.1",
|
|
63
|
+
"@types/node": "^22.13.9",
|
|
64
|
+
"solc": "^0.8.37",
|
|
65
|
+
"tsx": "^4.19.3",
|
|
66
|
+
"typescript": "^5.8.2"
|
|
67
|
+
},
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"mimetext": "^3.0.28",
|
|
70
|
+
"viem": "^2.23.12"
|
|
71
|
+
}
|
|
72
|
+
}
|