drain-mcp 0.2.1 → 0.2.3

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/README.md CHANGED
@@ -1,262 +1,382 @@
1
- # DRAIN MCP Server
2
-
3
- MCP server for the DRAIN payment protocol. Agents discover service providers, open USDC payment channels on Polygon, and call any service LLM inference, web scraping, image generation, VPN, and more. No API keys, no subscriptions.
4
-
5
- ## When to Use DRAIN
6
-
7
- Use DRAIN when you need to:
8
- - Call any service (LLM, scraping, image generation, etc.) without API keys
9
- - Pay per-request with USDC on Polygon
10
- - Access multiple service types through one protocol
11
-
12
- Do NOT use DRAIN when:
13
- - You already have API keys for the service you need
14
- - The task doesn't require external services
15
- - Cost is not a concern (DRAIN adds ~$0.04 blockchain overhead per session)
16
-
17
- ---
18
-
19
- ## TL;DR - Minimum Viable Workflow
20
-
21
- ```
22
- 1. drain_providers() → Find providers (filter by model or category)
23
- 2. drain_provider_info(id) → Get provider details + usage docs
24
- 3. drain_balance() → Check USDC + POL
25
- 4. drain_approve() → Approve USDC spending (once)
26
- 5. drain_open_channel(...) → Deposit USDC, get channelId
27
- 6. drain_chat(...) Send requests (repeat as needed)
28
- 7. drain_channels() Check all channels
29
- 8. drain_close_channel(...) Reclaim funds (after expiry)
30
- ```
31
-
32
- ---
33
-
34
- ## Tools Reference
35
-
36
- ### drain_providers
37
- Find service providers. Filter by model name, category, or online status.
38
-
39
- ```json
40
- {
41
- "model": "gpt-4o",
42
- "category": "scraping",
43
- "onlineOnly": true
44
- }
45
- ```
46
-
47
- Categories: `llm`, `image`, `audio`, `code`, `scraping`, `vpn`, `multi-modal`, `other`
48
-
49
- ### drain_provider_info
50
- Get details about a provider including usage instructions (docs). The docs explain how to format requests for that provider.
51
-
52
- ```json
53
- { "providerId": "hs58-openai" }
54
- ```
55
-
56
- ### drain_balance
57
- Check wallet USDC balance, POL for gas, and DRAIN contract allowance.
58
-
59
- ### drain_approve
60
- Approve USDC spending for the DRAIN contract. Required once before opening channels.
61
-
62
- ```json
63
- { "amount": "100" }
64
- ```
65
-
66
- ### drain_open_channel
67
- Open a payment channel. Locks USDC for the specified duration.
68
-
69
- ```json
70
- {
71
- "provider": "hs58-openai",
72
- "amount": "5.00",
73
- "duration": "24h"
74
- }
75
- ```
76
-
77
- Returns channelId, expiry time, and provider usage docs. **Set a cron/timer for the expiry time to call drain_close_channel and recover funds.**
78
-
79
- ### drain_chat
80
- Send a paid request through a channel. Works for ALL provider types:
81
-
82
- - **LLM providers:** Standard chat messages
83
- - **Non-LLM providers:** JSON payload in the user message content (check provider docs)
84
-
85
- ```json
86
- {
87
- "channelId": "0x...",
88
- "model": "gpt-4o",
89
- "messages": [{"role": "user", "content": "Hello"}]
90
- }
91
- ```
92
-
93
- ### drain_channel_status
94
- Check a channel's deposit, spending, remaining balance, and expiry.
95
-
96
- ### drain_channels
97
- List all known channels with status (active/expired/closed). Find expired channels that need closing.
98
-
99
- ### drain_close_channel
100
- Close an expired channel and reclaim unspent USDC.
101
-
102
- ---
103
-
104
- ## Provider Categories
105
-
106
- Providers are not limited to LLM chat. Each provider has a `category` field and a docs endpoint explaining how to format requests.
107
-
108
- | Category | Description | Message Format |
109
- |----------|-------------|----------------|
110
- | llm | Language models | Standard chat messages |
111
- | image | Image generation | JSON in user content (see docs) |
112
- | audio | Audio/TTS/STT | JSON in user content (see docs) |
113
- | code | Code generation | Standard chat or JSON (see docs) |
114
- | scraping | Web scraping | JSON in user content (see docs) |
115
- | vpn | VPN services | JSON in user content (see docs) |
116
- | multi-modal | Multi-modal models | Standard chat messages |
117
- | other | Everything else | Always check docs |
118
-
119
- **Rule: For any category other than `llm`, call `drain_provider_info` first to read the docs.**
120
-
121
- ---
122
-
123
- ## Decision Trees
124
-
125
- ### Starting a Session
126
-
127
- ```
128
- Do I have an active channel?
129
- ├── YES → Use drain_chat() with existing channelId
130
- └── NO →
131
- ├── drain_balance() Do I have USDC?
132
- │ ├── NO → Cannot proceed. Need USDC on Polygon.
133
- │ └── YES →
134
- │ ├── drain_providers() Find provider
135
- │ │ ├── NO providers → Cannot proceed.
136
- │ │ └── Found provider →
137
- │ │ ├── drain_approve() → First time only
138
- │ │ └── drain_open_channel() → Get channelId
139
- │ │ └── drain_chat() Send requests
140
- ```
141
-
142
- ### Choosing Amount and Duration
143
-
144
- | Use Case | Amount | Duration |
145
- |----------|--------|----------|
146
- | Quick test / single query | $0.50 - $1 | 1h |
147
- | Short task (few queries) | $2 - $5 | 4h |
148
- | Extended session | $5 - $20 | 24h |
149
- | Long-running agent | $20 - $100 | 7d |
150
-
151
- ### Handling Errors
152
-
153
- ```
154
- "Insufficient balance"
155
- Need more USDC. Check drain_balance().
156
-
157
- "Insufficient allowance"
158
- Run drain_approve().
159
-
160
- "Channel not found"
161
- channelId is wrong or channel was closed. Open new channel.
162
-
163
- "Channel expired"
164
- → For drain_chat: Open a new channel.
165
- For drain_close_channel: Expected. Proceed with close.
166
-
167
- "Insufficient channel balance"
168
- → Channel deposit used up. Open new channel with more funds.
169
-
170
- "Provider offline"
171
- → Use drain_providers() to find alternative provider.
172
- ```
173
-
174
- ---
175
-
176
- ## Setup
177
-
178
- ### Agent Can Do
179
-
180
- 1. **Install** `npm install -g drain-mcp`
181
- 2. **Create wallet** `require('viem').generatePrivateKey()` or ask user for existing key
182
- 3. **Configure** — Add to MCP client config (see below)
183
-
184
- ### Requires Human
185
-
186
- 4. **Fund wallet** — Send $1-5 USDC + $0.10 POL on Polygon to the wallet address
187
-
188
- ### MCP Config
189
-
190
- ```json
191
- {
192
- "mcpServers": {
193
- "drain": {
194
- "command": "drain-mcp",
195
- "env": { "DRAIN_PRIVATE_KEY": "0x..." }
196
- }
197
- }
198
- }
199
- ```
200
-
201
- ### Environment Variables
202
-
203
- | Variable | Required | Default |
204
- |----------|----------|---------|
205
- | `DRAIN_PRIVATE_KEY` | Yes | — |
206
- | `DRAIN_CHAIN_ID` | No | 137 (Polygon) |
207
- | `DRAIN_RPC_URL` | No | polygon-rpc.com |
208
-
209
- ---
210
-
211
- ## Security & Privacy
212
-
213
- ### Key Handling
214
- `DRAIN_PRIVATE_KEY` is loaded into memory by the local MCP process. It is used for:
215
- 1. EIP-712 voucher signing (off-chain, no network call)
216
- 2. On-chain transaction signing (signed locally, only the signature is broadcast)
217
-
218
- The key is never transmitted to any server. Providers verify signatures against on-chain state.
219
-
220
- ### Spending Limits
221
- Exposure is capped by the smart contract:
222
- - Maximum spend = channel deposit (you choose the amount)
223
- - Channel has a fixed duration (you choose)
224
- - After expiry, unspent funds are reclaimable via drain_close_channel
225
- - No recurring charges, no stored payment methods
226
-
227
- ### What Leaves Your Machine
228
- - Public API queries to handshake58.com (provider list, config, channel status)
229
- - Request messages to providers (sent to the provider's apiUrl, NOT to Handshake58)
230
- - Signed payment vouchers (contain a cryptographic signature, not the key)
231
- - Signed on-chain transactions (broadcast to Polygon RPC)
232
-
233
- ### What Stays Local
234
- - Private key (never transmitted)
235
- - All cryptographic operations (signing happens in-process)
236
-
237
- ### External Endpoints
238
-
239
- Every network request the MCP server makes:
240
-
241
- | Endpoint | Method | Data Sent | Key Transmitted? |
242
- |---|---|---|---|
243
- | handshake58.com/api/mcp/providers | GET | Nothing (public catalog) | No |
244
- | handshake58.com/api/directory/config | GET | Nothing (reads fee wallet) | No |
245
- | handshake58.com/api/channels/status | GET | channelId (public on-chain data) | No |
246
- | Provider apiUrl /v1/docs | GET | Nothing (fetches usage docs) | No |
247
- | Provider apiUrl /v1/chat/completions | POST | Request messages + signed voucher | No |
248
- | Polygon RPC (on-chain tx) | POST | Signed transactions | No |
249
-
250
- ### Safeguards
251
- - Use a **dedicated wallet** with $1-5 USDC. Never reuse your main wallet.
252
- - **Audit the source**: https://github.com/kimbo128/DRAIN
253
- - Run in an **isolated environment** if handling sensitive data
254
-
255
- ---
256
-
257
- ## Links
258
-
259
- - NPM: https://www.npmjs.com/package/drain-mcp
260
- - GitHub: https://github.com/kimbo128/DRAIN
261
- - Marketplace: https://handshake58.com
262
- - Contract: `0x1C1918C99b6DcE977392E4131C91654d8aB71e64` (Polygon)
1
+ # DRAIN MCP Server
2
+
3
+ **Payments for AI Agents**No API keys, no credit cards, no subscriptions.
4
+
5
+ MCP server for the DRAIN payment protocol. Agents discover service providers, open USDC payment channels on Polygon, and call any service — LLM inference, web scraping, image generation, VPN, and more.
6
+
7
+ - **One-time gas cost:** ~$0.02 to open a channel
8
+ - **Per request:** $0.0001–$0.01 depending on service
9
+ - **Setup time:** 30 seconds
10
+
11
+ ## When to Use DRAIN
12
+
13
+ Use DRAIN when you need to:
14
+ - Call any service (LLM, scraping, image generation, etc.) without API keys
15
+ - Pay per-request with USDC on Polygon
16
+ - Access multiple service types through one protocol
17
+
18
+ Do NOT use DRAIN when:
19
+ - You already have API keys for the service you need
20
+ - The task doesn't require external services
21
+
22
+ ---
23
+
24
+ ## TL;DR - Minimum Viable Workflow
25
+
26
+ ```
27
+ 1. drain_providers() Find providers (filter by model or category)
28
+ 2. drain_provider_info(id) Get provider details + usage docs
29
+ 3. drain_balance() Check USDC + POL
30
+ 4. drain_approve() → Approve USDC spending (once)
31
+ 5. drain_open_channel(...) → Deposit USDC, get channelId
32
+ 6. drain_chat(...) → Send requests (repeat as needed)
33
+ 7. drain_cooperative_close(...) → Close early, instant refund (when task is done)
34
+ OR after expiry —
35
+ 8. drain_close_channel(...) → Reclaim funds (after expiry)
36
+ ```
37
+
38
+ ---
39
+
40
+ ## When to Close a Channel
41
+
42
+ - **Done with your task?** → Use `drain_cooperative_close(channelId)`.
43
+ Instant refund of unspent deposit. Always do this when finished.
44
+
45
+ - **Planning more requests soon?** → Keep the channel open.
46
+ No need to close and reopen — saves $0.04 in gas fees.
47
+
48
+ - **Channel expired?** → Use `drain_close_channel(channelId)`.
49
+ Reclaims unspent USDC after the expiry timestamp.
50
+ Funds do NOT return automatically.
51
+
52
+ **Rule of thumb:** Close when the task is done. Keep open while working.
53
+
54
+ ---
55
+
56
+ ## Already Hit a 402?
57
+
58
+ DRAIN providers return `402 Payment Required` with headers when no payment voucher is sent:
59
+
60
+ | Header | Value |
61
+ |---|---|
62
+ | `X-Payment-Protocol` | `drain-v2` |
63
+ | `X-Payment-Provider` | Provider wallet address (open channel to this) |
64
+ | `X-Payment-Contract` | DRAIN contract address |
65
+ | `X-Payment-Chain` | `137` (Polygon Mainnet) |
66
+ | `X-Payment-Signing` | URL for EIP-712 signing parameters |
67
+ | `X-Payment-Docs` | Provider docs endpoint |
68
+
69
+ **Flow:**
70
+ 1. Call a provider endpoint (no voucher) → get `402` with headers
71
+ 2. Install `drain-mcp` or use the signing API at the `X-Payment-Signing` URL
72
+ 3. Open a channel to the `X-Payment-Provider` address
73
+ 4. Retry the request with an `X-DRAIN-Voucher` header
74
+
75
+ Everything you need is in the 402 response. No prior registration required.
76
+
77
+ ---
78
+
79
+ ## Tools Reference
80
+
81
+ ### Discovery
82
+
83
+ #### drain_providers
84
+ Find service providers. Filter by model name, category, or online status.
85
+
86
+ ```json
87
+ {
88
+ "model": "gpt-4o",
89
+ "category": "scraping",
90
+ "onlineOnly": true
91
+ }
92
+ ```
93
+
94
+ Categories: `llm`, `image`, `audio`, `code`, `scraping`, `vpn`, `multi-modal`, `other`
95
+
96
+ #### drain_provider_info
97
+ Get details about a provider including usage instructions (docs). The docs explain how to format requests for that provider.
98
+
99
+ ```json
100
+ { "providerId": "hs58-openai" }
101
+ ```
102
+
103
+ ### Wallet
104
+
105
+ #### drain_balance
106
+ Check wallet USDC balance, POL for gas, and DRAIN contract allowance.
107
+
108
+ #### drain_approve
109
+ Approve USDC spending for the DRAIN contract. Required once before opening channels.
110
+
111
+ ```json
112
+ { "amount": "100" }
113
+ ```
114
+
115
+ ### Channels
116
+
117
+ #### drain_open_channel
118
+ Open a payment channel. Locks USDC for the specified duration.
119
+
120
+ ```json
121
+ {
122
+ "provider": "hs58-openai",
123
+ "amount": "5.00",
124
+ "duration": "24h"
125
+ }
126
+ ```
127
+
128
+ Returns channelId, expiry time, and provider usage docs.
129
+
130
+ #### drain_channel_status
131
+ Check a channel's deposit, spending, remaining balance, and expiry.
132
+
133
+ #### drain_channels
134
+ List all known channels with status (active/expired/closed). Find expired channels that need closing.
135
+
136
+ ### Usage
137
+
138
+ #### drain_chat
139
+ Send a paid request through a channel. Works for ALL provider types:
140
+
141
+ - **LLM providers:** Standard chat messages
142
+ - **Non-LLM providers:** JSON payload in the user message content (check provider docs)
143
+
144
+ ```json
145
+ {
146
+ "channelId": "0x...",
147
+ "model": "gpt-4o",
148
+ "messages": [{"role": "user", "content": "Hello"}]
149
+ }
150
+ ```
151
+
152
+ ### Settlement
153
+
154
+ #### drain_cooperative_close
155
+ Close a channel early with provider consent. **Use this when your task is done** — instant refund of unspent deposit. No need to wait for expiry.
156
+
157
+ ```json
158
+ { "channelId": "0x..." }
159
+ ```
160
+
161
+ #### drain_close_channel
162
+ Close an expired channel and reclaim unspent USDC. Use when the channel has passed its expiry timestamp.
163
+
164
+ ```json
165
+ { "channelId": "0x..." }
166
+ ```
167
+
168
+ ---
169
+
170
+ ## Economics Example
171
+
172
+ Opening a GPT-4o channel:
173
+
174
+ ```
175
+ Gas to open channel: $0.02 (one-time)
176
+ Deposit: $0.50 (refundable remainder)
177
+ Per request: ~$0.001755
178
+ Requests possible: ~285
179
+
180
+ Cost for 10 requests: $0.02 gas + $0.01755 usage = $0.04
181
+ Refund after close: $0.50 - $0.01755 = $0.48
182
+ Gas to close: $0.02
183
+ ```
184
+
185
+ - Protocol fee: 2% on provider claims (on-chain)
186
+ - Session fee: none
187
+ - Live pricing: `GET https://handshake58.com/api/mcp/providers`
188
+
189
+ ---
190
+
191
+ ## Provider Categories
192
+
193
+ Providers are not limited to LLM chat. Each provider has a `category` field and a docs endpoint explaining how to format requests.
194
+
195
+ | Category | Description | Message Format |
196
+ |----------|-------------|----------------|
197
+ | llm | Language models (GPT-4o, Claude, Grok, Gemini) | Standard chat messages |
198
+ | image | Image generation | JSON in user content (see docs) |
199
+ | audio | Audio/TTS/STT | JSON in user content (see docs) |
200
+ | code | Code generation | Standard chat or JSON (see docs) |
201
+ | scraping | Web scraping, data extraction | JSON in user content (see docs) |
202
+ | vpn | VPN services | JSON in user content (see docs) |
203
+ | multi-modal | Multi-modal models | Standard chat messages |
204
+ | other | Everything else | Always check docs |
205
+
206
+ **Rule: For any category other than `llm`, call `drain_provider_info` first to read the docs.**
207
+
208
+ ---
209
+
210
+ ## Decision Trees
211
+
212
+ ### Starting a Session
213
+
214
+ ```
215
+ Do I have an active channel?
216
+ ├── YES Use drain_chat() with existing channelId
217
+ └── NO →
218
+ ├── drain_balance() Do I have USDC?
219
+ │ ├── NO → Cannot proceed. Need USDC on Polygon.
220
+ │ └── YES
221
+ │ ├── drain_providers() Find provider
222
+ │ │ ├── NO providers Cannot proceed.
223
+ │ │ └── Found provider
224
+ │ │ ├── drain_approve() First time only
225
+ │ │ └── drain_open_channel() Get channelId
226
+ │ │ └── drain_chat() → Send requests
227
+ ```
228
+
229
+ ### Ending a Session
230
+
231
+ ```
232
+ Am I done with this task?
233
+ ├── YES drain_cooperative_close(channelId) → Instant refund
234
+ └── NO
235
+ ├── More requests soon? Keep channel open
236
+ └── Channel expired? → drain_close_channel(channelId) → Reclaim funds
237
+ ```
238
+
239
+ ### Choosing Amount and Duration
240
+
241
+ | Use Case | Amount | Duration |
242
+ |----------|--------|----------|
243
+ | Quick test / single query | $0.50 - $1 | 1h |
244
+ | Short task (few queries) | $2 - $5 | 4h |
245
+ | Extended session | $5 - $20 | 24h |
246
+ | Long-running agent | $20 - $100 | 7d |
247
+
248
+ ### Handling Errors
249
+
250
+ ```
251
+ "Insufficient balance"
252
+ Need more USDC. Check drain_balance().
253
+
254
+ "Insufficient allowance"
255
+ → Run drain_approve().
256
+
257
+ "Channel not found"
258
+ → channelId is wrong or channel was closed. Open new channel.
259
+
260
+ "Channel expired"
261
+ For drain_chat: Open a new channel.
262
+ For drain_close_channel: Expected. Proceed with close.
263
+
264
+ "Insufficient channel balance"
265
+ → Channel deposit used up. Open new channel with more funds.
266
+
267
+ "Provider offline"
268
+ → Use drain_providers() to find alternative provider.
269
+ ```
270
+
271
+ ---
272
+
273
+ ## Setup
274
+
275
+ ### 1. Install
276
+
277
+ ```bash
278
+ npm install -g drain-mcp
279
+ ```
280
+
281
+ ### 2. Create a Wallet (locally)
282
+
283
+ Generate a key on your own machine — nothing is sent over the network:
284
+
285
+ ```bash
286
+ node -e "const w=require('ethers').Wallet.createRandom();console.log('Address:', w.address, '\nKey:', w.privateKey)"
287
+ ```
288
+
289
+ ### 3. Fund the Wallet
290
+
291
+ Send **$1–5 USDC** on **Polygon Mainnet** to your wallet address.
292
+ Use a dedicated low-value wallet — never your main wallet.
293
+
294
+ **No POL needed** — if your wallet holds $5+ USDC, free gas is provided:
295
+
296
+ ```bash
297
+ curl -X POST https://handshake58.com/api/gas-station \
298
+ -H "Content-Type: application/json" \
299
+ -d '{"address": "0x_your_wallet_address"}'
300
+ ```
301
+
302
+ Returns 0.1 POL (~10K transactions). Sends only your public address.
303
+
304
+ ### 4. Configure MCP Client
305
+
306
+ ```json
307
+ {
308
+ "mcpServers": {
309
+ "drain": {
310
+ "command": "drain-mcp",
311
+ "env": { "DRAIN_PRIVATE_KEY": "0x..." }
312
+ }
313
+ }
314
+ }
315
+ ```
316
+
317
+ ### Environment Variables
318
+
319
+ | Variable | Required | Default |
320
+ |----------|----------|---------|
321
+ | `DRAIN_PRIVATE_KEY` | Yes | — |
322
+ | `DRAIN_CHAIN_ID` | No | 137 (Polygon) |
323
+ | `DRAIN_RPC_URL` | No | Public RPC |
324
+
325
+ ---
326
+
327
+ ## Security & Privacy
328
+
329
+ ### Key Handling
330
+ `DRAIN_PRIVATE_KEY` is loaded into memory by the local MCP process. It is used for:
331
+ 1. EIP-712 voucher signing (off-chain, no network call)
332
+ 2. On-chain transaction signing (signed locally, only the signature is broadcast)
333
+
334
+ The key is never transmitted to any server. Providers verify signatures against on-chain state.
335
+
336
+ ### Spending Limits
337
+ Exposure is capped by the smart contract:
338
+ - Maximum spend = channel deposit (you choose the amount)
339
+ - Channel has a fixed duration (you choose)
340
+ - After expiry, unspent funds are reclaimable via `drain_close_channel`
341
+ - No recurring charges, no stored payment methods
342
+
343
+ ### What Leaves Your Machine
344
+ - Public API queries to handshake58.com (provider list, config, channel status)
345
+ - Request messages to providers (sent to the provider's apiUrl, NOT to Handshake58)
346
+ - Signed payment vouchers (contain a cryptographic signature, not the key)
347
+ - Signed on-chain transactions (broadcast to Polygon RPC)
348
+
349
+ ### What Stays Local
350
+ - Private key (never transmitted)
351
+ - All cryptographic operations (signing happens in-process)
352
+
353
+ ### External Endpoints
354
+
355
+ Every network request the MCP server makes:
356
+
357
+ | Endpoint | Method | Data Sent | Key Transmitted? |
358
+ |---|---|---|---|
359
+ | handshake58.com/api/mcp/providers | GET | Nothing (public catalog) | No |
360
+ | handshake58.com/api/directory/config | GET | Nothing (reads fee wallet) | No |
361
+ | handshake58.com/api/channels/status | GET | channelId (public on-chain data) | No |
362
+ | handshake58.com/api/gas-station | POST | Wallet address | No |
363
+ | Provider apiUrl /v1/docs | GET | Nothing (fetches usage docs) | No |
364
+ | Provider apiUrl /v1/chat/completions | POST | Request messages + signed voucher | No |
365
+ | Provider apiUrl /v1/close-channel | POST | channelId + close signature | No |
366
+ | Polygon RPC (on-chain tx) | POST | Signed transactions | No |
367
+
368
+ ### Safeguards
369
+ - Use a **dedicated wallet** with $1–5 USDC. Never reuse your main wallet.
370
+ - Always generate keys **locally**. The key stays on your machine.
371
+ - **Open source**: [github.com/kimbo128/DRAIN](https://github.com/kimbo128/DRAIN) (MIT licensed)
372
+ - Open channels with small deposits. Close promptly when done.
373
+
374
+ ---
375
+
376
+ ## Links
377
+
378
+ - NPM: https://www.npmjs.com/package/drain-mcp
379
+ - GitHub: https://github.com/kimbo128/DRAIN
380
+ - Marketplace: https://handshake58.com
381
+ - Provider Directory: https://handshake58.com/directory
382
+ - Contract: `0x0C2B3aA1e80629D572b1f200e6DF3586B3946A8A` (Polygon Mainnet)