drain-mcp 0.1.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 (46) hide show
  1. package/README.md +177 -0
  2. package/dist/config.d.ts +17530 -0
  3. package/dist/config.d.ts.map +1 -0
  4. package/dist/config.js +74 -0
  5. package/dist/config.js.map +1 -0
  6. package/dist/constants.d.ts +174 -0
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/constants.js +119 -0
  9. package/dist/constants.js.map +1 -0
  10. package/dist/index.d.ts +9 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +170 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/services/channel.d.ts +79 -0
  15. package/dist/services/channel.d.ts.map +1 -0
  16. package/dist/services/channel.js +190 -0
  17. package/dist/services/channel.js.map +1 -0
  18. package/dist/services/inference.d.ts +54 -0
  19. package/dist/services/inference.d.ts.map +1 -0
  20. package/dist/services/inference.js +81 -0
  21. package/dist/services/inference.js.map +1 -0
  22. package/dist/services/provider.d.ts +70 -0
  23. package/dist/services/provider.d.ts.map +1 -0
  24. package/dist/services/provider.js +84 -0
  25. package/dist/services/provider.js.map +1 -0
  26. package/dist/services/wallet.d.ts +48 -0
  27. package/dist/services/wallet.d.ts.map +1 -0
  28. package/dist/services/wallet.js +102 -0
  29. package/dist/services/wallet.js.map +1 -0
  30. package/dist/tools/balance.d.ts +40 -0
  31. package/dist/tools/balance.d.ts.map +1 -0
  32. package/dist/tools/balance.js +86 -0
  33. package/dist/tools/balance.js.map +1 -0
  34. package/dist/tools/channel.d.ts +67 -0
  35. package/dist/tools/channel.d.ts.map +1 -0
  36. package/dist/tools/channel.js +177 -0
  37. package/dist/tools/channel.js.map +1 -0
  38. package/dist/tools/chat.d.ts +64 -0
  39. package/dist/tools/chat.d.ts.map +1 -0
  40. package/dist/tools/chat.js +118 -0
  41. package/dist/tools/chat.js.map +1 -0
  42. package/dist/tools/providers.d.ts +54 -0
  43. package/dist/tools/providers.d.ts.map +1 -0
  44. package/dist/tools/providers.js +102 -0
  45. package/dist/tools/providers.js.map +1 -0
  46. package/package.json +43 -0
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Wallet Service
3
+ *
4
+ * Handles wallet operations: balance, approvals, signing.
5
+ */
6
+ import { formatUnits, parseUnits } from 'viem';
7
+ import { USDC_DECIMALS, ERC20_ABI } from '../constants.js';
8
+ export class WalletService {
9
+ publicClient;
10
+ walletClient;
11
+ account;
12
+ config;
13
+ constructor(publicClient, walletClient, account, config) {
14
+ this.publicClient = publicClient;
15
+ this.walletClient = walletClient;
16
+ this.account = account;
17
+ this.config = config;
18
+ }
19
+ /**
20
+ * Get the agent's wallet address
21
+ */
22
+ getAddress() {
23
+ return this.account.address;
24
+ }
25
+ /**
26
+ * Get USDC balance
27
+ */
28
+ async getUsdcBalance() {
29
+ const balance = await this.publicClient.readContract({
30
+ address: this.config.usdcAddress,
31
+ abi: ERC20_ABI,
32
+ functionName: 'balanceOf',
33
+ args: [this.account.address],
34
+ });
35
+ return {
36
+ raw: balance,
37
+ formatted: formatUnits(balance, USDC_DECIMALS),
38
+ };
39
+ }
40
+ /**
41
+ * Get current USDC allowance for DRAIN contract
42
+ */
43
+ async getAllowance() {
44
+ const allowance = await this.publicClient.readContract({
45
+ address: this.config.usdcAddress,
46
+ abi: ERC20_ABI,
47
+ functionName: 'allowance',
48
+ args: [this.account.address, this.config.drainAddress],
49
+ });
50
+ return {
51
+ raw: allowance,
52
+ formatted: formatUnits(allowance, USDC_DECIMALS),
53
+ };
54
+ }
55
+ /**
56
+ * Approve USDC spending for DRAIN contract
57
+ */
58
+ async approveUsdc(amount) {
59
+ const amountWei = parseUnits(amount, USDC_DECIMALS);
60
+ const hash = await this.walletClient.writeContract({
61
+ account: this.account,
62
+ address: this.config.usdcAddress,
63
+ abi: ERC20_ABI,
64
+ functionName: 'approve',
65
+ args: [this.config.drainAddress, amountWei],
66
+ chain: this.config.chain,
67
+ });
68
+ // Wait for confirmation
69
+ await this.publicClient.waitForTransactionReceipt({ hash });
70
+ return hash;
71
+ }
72
+ /**
73
+ * Approve maximum USDC spending (for convenience)
74
+ */
75
+ async approveMax() {
76
+ const maxAmount = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
77
+ const hash = await this.walletClient.writeContract({
78
+ account: this.account,
79
+ address: this.config.usdcAddress,
80
+ abi: ERC20_ABI,
81
+ functionName: 'approve',
82
+ args: [this.config.drainAddress, maxAmount],
83
+ chain: this.config.chain,
84
+ });
85
+ // Wait for confirmation
86
+ await this.publicClient.waitForTransactionReceipt({ hash });
87
+ return hash;
88
+ }
89
+ /**
90
+ * Get native token (POL/MATIC) balance for gas
91
+ */
92
+ async getNativeBalance() {
93
+ const balance = await this.publicClient.getBalance({
94
+ address: this.account.address,
95
+ });
96
+ return {
97
+ raw: balance,
98
+ formatted: formatUnits(balance, 18),
99
+ };
100
+ }
101
+ }
102
+ //# sourceMappingURL=wallet.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet.js","sourceRoot":"","sources":["../../src/services/wallet.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAA+E,MAAM,MAAM,CAAC;AAC5H,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG3D,MAAM,OAAO,aAAa;IAEd;IACA;IACA;IACA;IAJV,YACU,YAA0B,EAC1B,YAA0B,EAC1B,OAAgB,EAChB,MAAmB;QAHnB,iBAAY,GAAZ,YAAY,CAAc;QAC1B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAa;IAC1B,CAAC;IAEJ;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,cAAc;QAClB,MAAM,OAAO,GAAG,MAAM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,CAAC,YAAY,CAAC,yBAAyB,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;QAE5D,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU;QACd,MAAM,SAAS,GAAG,MAAM,CAAC,oEAAoE,CAAC,CAAC;QAE/F,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC;YACjD,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW;YAChC,GAAG,EAAE,SAAS;YACd,YAAY,EAAE,SAAS;YACvB,IAAI,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC;YAC3C,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK;SACzB,CAAC,CAAC;QAEH,wBAAwB;QACxB,MAAM,IAAI,CAAC,YAAY,CAAC,yBAAyB,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;QAE5D,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,gBAAgB;QACpB,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;YACjD,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;SAC9B,CAAC,CAAC;QAEH,OAAO;YACL,GAAG,EAAE,OAAO;YACZ,SAAS,EAAE,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC;SACpC,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Balance Tools
3
+ *
4
+ * Check wallet balance and status.
5
+ */
6
+ import type { WalletService } from '../services/wallet.js';
7
+ import type { DrainConfig } from '../config.js';
8
+ /**
9
+ * Get wallet balance and status
10
+ */
11
+ export declare function getBalance(walletService: WalletService, config: DrainConfig): Promise<string>;
12
+ /**
13
+ * Approve USDC spending
14
+ */
15
+ export declare function approveUsdc(walletService: WalletService, args: {
16
+ amount?: string;
17
+ }): Promise<string>;
18
+ export declare const balanceTools: ({
19
+ name: string;
20
+ description: string;
21
+ inputSchema: {
22
+ type: string;
23
+ properties: {
24
+ amount?: undefined;
25
+ };
26
+ };
27
+ } | {
28
+ name: string;
29
+ description: string;
30
+ inputSchema: {
31
+ type: string;
32
+ properties: {
33
+ amount: {
34
+ type: string;
35
+ description: string;
36
+ };
37
+ };
38
+ };
39
+ })[];
40
+ //# sourceMappingURL=balance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"balance.d.ts","sourceRoot":"","sources":["../../src/tools/balance.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD;;GAEG;AACH,wBAAsB,UAAU,CAC9B,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,MAAM,CAAC,CA4BjB;AAED;;GAEG;AACH,wBAAsB,WAAW,CAC/B,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACxB,OAAO,CAAC,MAAM,CAAC,CAUjB;AAGD,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;IAkCxB,CAAC"}
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Balance Tools
3
+ *
4
+ * Check wallet balance and status.
5
+ */
6
+ /**
7
+ * Get wallet balance and status
8
+ */
9
+ export async function getBalance(walletService, config) {
10
+ const address = walletService.getAddress();
11
+ const usdcBalance = await walletService.getUsdcBalance();
12
+ const nativeBalance = await walletService.getNativeBalance();
13
+ const allowance = await walletService.getAllowance();
14
+ const network = config.chainId === 137 ? 'Polygon Mainnet' : 'Polygon Amoy Testnet';
15
+ const hasGas = parseFloat(nativeBalance.formatted) > 0.01;
16
+ const hasAllowance = parseFloat(allowance.formatted) > 0;
17
+ return `# Wallet Status
18
+
19
+ **Address:** \`${address}\`
20
+ **Network:** ${network}
21
+
22
+ ## Balances
23
+ - **USDC:** $${usdcBalance.formatted}
24
+ - **POL (for gas):** ${nativeBalance.formatted} POL
25
+
26
+ ## DRAIN Contract
27
+ - **Allowance:** $${allowance.formatted} USDC
28
+ - **Contract:** \`${config.drainAddress}\`
29
+
30
+ ## Status
31
+ ${parseFloat(usdcBalance.formatted) > 0 ? '✅' : '⚠️'} USDC Balance: ${parseFloat(usdcBalance.formatted) > 0 ? 'OK' : 'LOW - need USDC to open channels'}
32
+ ${hasGas ? '✅' : '⚠️'} Gas: ${hasGas ? 'OK' : 'LOW - need POL for transactions'}
33
+ ${hasAllowance ? '✅' : 'ℹ️'} Allowance: ${hasAllowance ? 'Approved' : 'Not yet approved - will need to approve before opening channel'}
34
+ `;
35
+ }
36
+ /**
37
+ * Approve USDC spending
38
+ */
39
+ export async function approveUsdc(walletService, args) {
40
+ let txHash;
41
+ if (args.amount) {
42
+ txHash = await walletService.approveUsdc(args.amount);
43
+ return `✅ Approved ${args.amount} USDC for DRAIN contract.\n\nTransaction: \`${txHash}\``;
44
+ }
45
+ else {
46
+ txHash = await walletService.approveMax();
47
+ return `✅ Approved unlimited USDC for DRAIN contract.\n\nTransaction: \`${txHash}\``;
48
+ }
49
+ }
50
+ // Tool definitions for MCP
51
+ export const balanceTools = [
52
+ {
53
+ name: 'drain_balance',
54
+ description: `Check wallet balance, USDC allowance, and readiness for DRAIN payments.
55
+
56
+ Use this to verify:
57
+ - You have enough USDC to open channels
58
+ - You have enough POL for gas fees
59
+ - USDC is approved for the DRAIN contract
60
+
61
+ Returns: Wallet address, balances, and status indicators.`,
62
+ inputSchema: {
63
+ type: 'object',
64
+ properties: {},
65
+ },
66
+ },
67
+ {
68
+ name: 'drain_approve',
69
+ description: `Approve USDC spending for the DRAIN contract.
70
+
71
+ This is required before opening payment channels.
72
+ If no amount is specified, approves unlimited spending (recommended for convenience).
73
+
74
+ Returns: Transaction hash of the approval.`,
75
+ inputSchema: {
76
+ type: 'object',
77
+ properties: {
78
+ amount: {
79
+ type: 'string',
80
+ description: 'Amount of USDC to approve (e.g., "100"). If omitted, approves unlimited.',
81
+ },
82
+ },
83
+ },
84
+ },
85
+ ];
86
+ //# sourceMappingURL=balance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"balance.js","sourceRoot":"","sources":["../../src/tools/balance.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,aAA4B,EAC5B,MAAmB;IAEnB,MAAM,OAAO,GAAG,aAAa,CAAC,UAAU,EAAE,CAAC;IAC3C,MAAM,WAAW,GAAG,MAAM,aAAa,CAAC,cAAc,EAAE,CAAC;IACzD,MAAM,aAAa,GAAG,MAAM,aAAa,CAAC,gBAAgB,EAAE,CAAC;IAC7D,MAAM,SAAS,GAAG,MAAM,aAAa,CAAC,YAAY,EAAE,CAAC;IAErD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,sBAAsB,CAAC;IACpF,MAAM,MAAM,GAAG,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;IAC1D,MAAM,YAAY,GAAG,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAEzD,OAAO;;iBAEQ,OAAO;eACT,OAAO;;;eAGP,WAAW,CAAC,SAAS;uBACb,aAAa,CAAC,SAAS;;;oBAG1B,SAAS,CAAC,SAAS;oBACnB,MAAM,CAAC,YAAY;;;EAGrC,UAAU,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,kBAAkB,UAAU,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,kCAAkC;EACrJ,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,SAAS,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,iCAAiC;EAC7E,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,eAAe,YAAY,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,gEAAgE;CACrI,CAAC;AACF,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,aAA4B,EAC5B,IAAyB;IAEzB,IAAI,MAAc,CAAC;IAEnB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAChB,MAAM,GAAG,MAAM,aAAa,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtD,OAAO,cAAc,IAAI,CAAC,MAAM,+CAA+C,MAAM,IAAI,CAAC;IAC5F,CAAC;SAAM,CAAC;QACN,MAAM,GAAG,MAAM,aAAa,CAAC,UAAU,EAAE,CAAC;QAC1C,OAAO,mEAAmE,MAAM,IAAI,CAAC;IACvF,CAAC;AACH,CAAC;AAED,2BAA2B;AAC3B,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B;QACE,IAAI,EAAE,eAAe;QACrB,WAAW,EAAE;;;;;;;0DAOyC;QACtD,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,EAAE;SACf;KACF;IACD;QACE,IAAI,EAAE,eAAe;QACrB,WAAW,EAAE;;;;;2CAK0B;QACvC,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,MAAM,EAAE;oBACN,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,0EAA0E;iBACxF;aACF;SACF;KACF;CACF,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Channel Tools
3
+ *
4
+ * Manage payment channels: open, close, status.
5
+ */
6
+ import type { ChannelService } from '../services/channel.js';
7
+ import type { ProviderService } from '../services/provider.js';
8
+ /**
9
+ * Open a payment channel
10
+ */
11
+ export declare function openChannel(channelService: ChannelService, providerService: ProviderService, args: {
12
+ provider: string;
13
+ amount: string;
14
+ duration: string;
15
+ }): Promise<string>;
16
+ /**
17
+ * Close an expired channel
18
+ */
19
+ export declare function closeChannel(channelService: ChannelService, args: {
20
+ channelId: string;
21
+ }): Promise<string>;
22
+ /**
23
+ * Get channel status
24
+ */
25
+ export declare function getChannelStatus(channelService: ChannelService, args: {
26
+ channelId: string;
27
+ }): Promise<string>;
28
+ export declare const channelTools: ({
29
+ name: string;
30
+ description: string;
31
+ inputSchema: {
32
+ type: string;
33
+ properties: {
34
+ provider: {
35
+ type: string;
36
+ description: string;
37
+ };
38
+ amount: {
39
+ type: string;
40
+ description: string;
41
+ };
42
+ duration: {
43
+ type: string;
44
+ description: string;
45
+ };
46
+ channelId?: undefined;
47
+ };
48
+ required: string[];
49
+ };
50
+ } | {
51
+ name: string;
52
+ description: string;
53
+ inputSchema: {
54
+ type: string;
55
+ properties: {
56
+ channelId: {
57
+ type: string;
58
+ description: string;
59
+ };
60
+ provider?: undefined;
61
+ amount?: undefined;
62
+ duration?: undefined;
63
+ };
64
+ required: string[];
65
+ };
66
+ })[];
67
+ //# sourceMappingURL=channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../src/tools/channel.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AA0B/D;;GAEG;AACH,wBAAsB,WAAW,CAC/B,cAAc,EAAE,cAAc,EAC9B,eAAe,EAAE,eAAe,EAChC,IAAI,EAAE;IACJ,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,GACA,OAAO,CAAC,MAAM,CAAC,CAwCjB;AAED;;GAEG;AACH,wBAAsB,YAAY,CAChC,cAAc,EAAE,cAAc,EAC9B,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAC1B,OAAO,CAAC,MAAM,CAAC,CAYjB;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,cAAc,EAAE,cAAc,EAC9B,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAC1B,OAAO,CAAC,MAAM,CAAC,CA0BjB;AAGD,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAkExB,CAAC"}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Channel Tools
3
+ *
4
+ * Manage payment channels: open, close, status.
5
+ */
6
+ /**
7
+ * Parse duration string to seconds
8
+ */
9
+ function parseDuration(duration) {
10
+ const match = duration.match(/^(\d+)(s|m|h|d)$/);
11
+ if (!match) {
12
+ // Try parsing as raw seconds
13
+ const seconds = parseInt(duration);
14
+ if (!isNaN(seconds))
15
+ return seconds;
16
+ throw new Error(`Invalid duration format: ${duration}. Use formats like "1h", "24h", "7d", or seconds.`);
17
+ }
18
+ const value = parseInt(match[1]);
19
+ const unit = match[2];
20
+ switch (unit) {
21
+ case 's': return value;
22
+ case 'm': return value * 60;
23
+ case 'h': return value * 3600;
24
+ case 'd': return value * 86400;
25
+ default: throw new Error(`Unknown duration unit: ${unit}`);
26
+ }
27
+ }
28
+ /**
29
+ * Open a payment channel
30
+ */
31
+ export async function openChannel(channelService, providerService, args) {
32
+ // Resolve provider address
33
+ let providerAddress;
34
+ let providerName;
35
+ if (args.provider.startsWith('0x')) {
36
+ providerAddress = args.provider;
37
+ providerName = args.provider;
38
+ }
39
+ else {
40
+ const provider = await providerService.getProvider(args.provider);
41
+ if (!provider) {
42
+ throw new Error(`Provider "${args.provider}" not found. Use drain_providers to list available providers.`);
43
+ }
44
+ providerAddress = provider.providerAddress;
45
+ providerName = provider.name;
46
+ }
47
+ const durationSeconds = parseDuration(args.duration);
48
+ const result = await channelService.openChannel(providerAddress, args.amount, durationSeconds);
49
+ const expiryDate = result.channel.expiry.toISOString();
50
+ const hours = Math.floor(durationSeconds / 3600);
51
+ return `# ✅ Channel Opened
52
+
53
+ **Channel ID:** \`${result.channelId}\`
54
+ **Transaction:** \`${result.txHash}\`
55
+
56
+ ## Details
57
+ - **Provider:** ${providerName} (\`${providerAddress}\`)
58
+ - **Deposit:** $${args.amount} USDC
59
+ - **Duration:** ${hours} hours
60
+ - **Expires:** ${expiryDate}
61
+
62
+ ## Next Steps
63
+ Use \`drain_chat\` to make AI requests through this channel.
64
+ The channel ID will be used automatically, or you can specify it explicitly.
65
+
66
+ When finished, wait for expiry and use \`drain_close_channel\` to reclaim unused funds.`;
67
+ }
68
+ /**
69
+ * Close an expired channel
70
+ */
71
+ export async function closeChannel(channelService, args) {
72
+ const channelId = args.channelId;
73
+ const result = await channelService.closeChannel(channelId);
74
+ return `# ✅ Channel Closed
75
+
76
+ **Channel ID:** \`${channelId}\`
77
+ **Transaction:** \`${result.txHash}\`
78
+ **Refunded:** $${result.refundAmount} USDC
79
+
80
+ The remaining balance has been returned to your wallet.`;
81
+ }
82
+ /**
83
+ * Get channel status
84
+ */
85
+ export async function getChannelStatus(channelService, args) {
86
+ const channelId = args.channelId;
87
+ const channel = await channelService.getChannel(channelId);
88
+ const localSpending = channelService.getSpending(channelId);
89
+ const hoursRemaining = Math.floor(channel.secondsRemaining / 3600);
90
+ const minutesRemaining = Math.floor((channel.secondsRemaining % 3600) / 60);
91
+ const statusEmoji = channel.isExpired ? '⏰ EXPIRED' : '🟢 ACTIVE';
92
+ return `# Channel Status
93
+
94
+ **Channel ID:** \`${channel.id}\`
95
+ **Status:** ${statusEmoji}
96
+
97
+ ## Balances
98
+ - **Deposit:** $${channel.deposit} USDC
99
+ - **Claimed by Provider:** $${channel.claimed} USDC
100
+ - **Remaining:** $${channel.remaining} USDC
101
+ - **Local Spending (this session):** $${localSpending} USDC
102
+
103
+ ## Timing
104
+ - **Expires:** ${channel.expiry.toISOString()}
105
+ - **Time Remaining:** ${channel.isExpired ? 'EXPIRED' : `${hoursRemaining}h ${minutesRemaining}m`}
106
+
107
+ ${channel.isExpired ? '⚠️ Channel has expired. You can now close it to reclaim remaining funds using `drain_close_channel`.' : ''}`;
108
+ }
109
+ // Tool definitions for MCP
110
+ export const channelTools = [
111
+ {
112
+ name: 'drain_open_channel',
113
+ description: `Open a DRAIN payment channel with an AI provider.
114
+
115
+ This deposits USDC into a smart contract, creating a payment channel.
116
+ You can then use the channel to pay for AI inference requests.
117
+
118
+ IMPORTANT: You must have:
119
+ 1. Sufficient USDC balance
120
+ 2. USDC approved for the DRAIN contract (use drain_approve if needed)
121
+ 3. POL for gas fees
122
+
123
+ The channel will expire after the specified duration. After expiry, you can close it to reclaim unused funds.`,
124
+ inputSchema: {
125
+ type: 'object',
126
+ properties: {
127
+ provider: {
128
+ type: 'string',
129
+ description: 'Provider ID (from drain_providers) or provider wallet address (0x...)',
130
+ },
131
+ amount: {
132
+ type: 'string',
133
+ description: 'Amount of USDC to deposit (e.g., "5.00" for $5)',
134
+ },
135
+ duration: {
136
+ type: 'string',
137
+ description: 'Channel duration. Examples: "1h", "24h", "7d", or seconds like "3600"',
138
+ },
139
+ },
140
+ required: ['provider', 'amount', 'duration'],
141
+ },
142
+ },
143
+ {
144
+ name: 'drain_close_channel',
145
+ description: `Close an expired DRAIN payment channel and reclaim remaining funds.
146
+
147
+ Can only be called after the channel has expired.
148
+ Any unused deposit will be returned to your wallet.`,
149
+ inputSchema: {
150
+ type: 'object',
151
+ properties: {
152
+ channelId: {
153
+ type: 'string',
154
+ description: 'The channel ID to close (0x...)',
155
+ },
156
+ },
157
+ required: ['channelId'],
158
+ },
159
+ },
160
+ {
161
+ name: 'drain_channel_status',
162
+ description: `Get the current status of a DRAIN payment channel.
163
+
164
+ Shows deposit, spending, remaining balance, and expiry time.`,
165
+ inputSchema: {
166
+ type: 'object',
167
+ properties: {
168
+ channelId: {
169
+ type: 'string',
170
+ description: 'The channel ID to check (0x...)',
171
+ },
172
+ },
173
+ required: ['channelId'],
174
+ },
175
+ },
176
+ ];
177
+ //# sourceMappingURL=channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.js","sourceRoot":"","sources":["../../src/tools/channel.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;GAEG;AACH,SAAS,aAAa,CAAC,QAAgB;IACrC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACjD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,6BAA6B;QAC7B,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACnC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;YAAE,OAAO,OAAO,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,4BAA4B,QAAQ,mDAAmD,CAAC,CAAC;IAC3G,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IAEtB,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,GAAG,CAAC,CAAC,OAAO,KAAK,CAAC;QACvB,KAAK,GAAG,CAAC,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;QAC5B,KAAK,GAAG,CAAC,CAAC,OAAO,KAAK,GAAG,IAAI,CAAC;QAC9B,KAAK,GAAG,CAAC,CAAC,OAAO,KAAK,GAAG,KAAK,CAAC;QAC/B,OAAO,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,cAA8B,EAC9B,eAAgC,EAChC,IAIC;IAED,2BAA2B;IAC3B,IAAI,e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AAE,QAAQ;YACd,UAAU,EAAE;gBACV,QAAQ,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,uEAAuE;iBACrF;gBACD,MAAM,EAAE;oBACN,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,iDAAiD;iBAC/D;gBACD,QAAQ,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,uEAAuE;iBACrF;aACF;YACD,QAAQ,EAAE,CAAC,UAAU,EAAE,QAAQ,EAAE,UAAU,CAAC;SAC7C;KACF;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,WAAW,EAAE;;;oDAGmC;QAChD,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,iCAAiC;iBAC/C;aACF;YACD,QAAQ,EAAE,CAAC,WAAW,CAAC;SACxB;KACF;IACD;QACE,IAAI,EAAE,sBAAsB;QAC5B,WAAW,EAAE;;6DAE4C;QACzD,WAAW,EAAE;YACX,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,SAAS,EAAE;oBACT,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,iCAAiC;iBAC/C;aACF;YACD,QAAQ,EAAE,CAAC,WAAW,CAAC;SACxB;KACF;CACF,CAAC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Chat Tool
3
+ *
4
+ * The main tool: Make AI chat requests with DRAIN payment.
5
+ */
6
+ import type { ChannelService } from '../services/channel.js';
7
+ import type { ProviderService } from '../services/provider.js';
8
+ import type { InferenceService, ChatMessage } from '../services/inference.js';
9
+ /**
10
+ * Make a chat completion request
11
+ */
12
+ export declare function chat(channelService: ChannelService, providerService: ProviderService, inferenceService: InferenceService, args: {
13
+ channelId: string;
14
+ model: string;
15
+ messages: ChatMessage[];
16
+ maxTokens?: number;
17
+ temperature?: number;
18
+ }): Promise<string>;
19
+ export declare const chatTools: {
20
+ name: string;
21
+ description: string;
22
+ inputSchema: {
23
+ type: string;
24
+ properties: {
25
+ channelId: {
26
+ type: string;
27
+ description: string;
28
+ };
29
+ model: {
30
+ type: string;
31
+ description: string;
32
+ };
33
+ messages: {
34
+ type: string;
35
+ description: string;
36
+ items: {
37
+ type: string;
38
+ properties: {
39
+ role: {
40
+ type: string;
41
+ enum: string[];
42
+ description: string;
43
+ };
44
+ content: {
45
+ type: string;
46
+ description: string;
47
+ };
48
+ };
49
+ required: string[];
50
+ };
51
+ };
52
+ maxTokens: {
53
+ type: string;
54
+ description: string;
55
+ };
56
+ temperature: {
57
+ type: string;
58
+ description: string;
59
+ };
60
+ };
61
+ required: string[];
62
+ };
63
+ }[];
64
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/tools/chat.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE9E;;GAEG;AACH,wBAAsB,IAAI,CACxB,cAAc,EAAE,cAAc,EAC9B,eAAe,EAAE,eAAe,EAChC,gBAAgB,EAAE,gBAAgB,EAClC,IAAI,EAAE;IACJ,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GACA,OAAO,CAAC,MAAM,CAAC,CA4CjB;AAcD,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DrB,CAAC"}