|
| 1 | +#!/usr/bin/env node |
| 2 | + |
1 | 3 | import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
2 | 4 | import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
3 | 5 | import { z } from 'zod'; |
4 | 6 | import { |
5 | | - createWalletClient, |
6 | | - http, |
7 | | - isAddress, |
8 | | - type Account, |
9 | | - type Hex, |
| 7 | + createWalletClient, |
| 8 | + http, |
| 9 | + isAddress, |
| 10 | + type Hex, |
10 | 11 | } from 'viem'; |
11 | 12 | import { privateKeyToAccount } from 'viem/accounts'; |
12 | 13 | import { |
13 | | - createPaymentHeader, |
14 | | - selectPaymentRequirements, |
15 | | -} from 'x402/dist/esm/client/index.mjs'; |
| 14 | + createPaymentHeader, |
| 15 | +} from 'x402/client'; |
16 | 16 | import debug from 'debug'; |
17 | 17 |
|
18 | 18 | import pkg from '../package.json' with { type: 'json' }; |
19 | 19 | import { base } from 'viem/chains'; |
20 | 20 |
|
21 | 21 | const X402_VERSION = 1; |
| 22 | +const BASE_USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; |
22 | 23 |
|
23 | 24 | // Load the environment variables |
24 | 25 | const PRIVATE_KEY = process.env.PRIVATE_KEY as Hex; |
25 | 26 | const MAX_PAYMENT_AMOUNT_USDC = process.env.MAX_PAYMENT_AMOUNT_USDC; |
26 | 27 |
|
27 | 28 | if (!PRIVATE_KEY) { |
28 | | - throw new Error('PRIVATE_KEY is not set'); |
| 29 | + throw new Error('PRIVATE_KEY is not set'); |
29 | 30 | } |
30 | 31 |
|
31 | 32 | if (!MAX_PAYMENT_AMOUNT_USDC) { |
32 | | - throw new Error('MAX_PAYMENT_AMOUNT_USDC is not set'); |
| 33 | + throw new Error('MAX_PAYMENT_AMOUNT_USDC is not set'); |
33 | 34 | } |
34 | 35 |
|
35 | 36 | const log = debug('payments-mcp'); |
36 | 37 |
|
37 | 38 | const walletClient = createWalletClient({ |
38 | | - chain: base, |
39 | | - transport: http(), |
40 | | - account: privateKeyToAccount(PRIVATE_KEY), |
| 39 | + chain: base, |
| 40 | + transport: http(), |
| 41 | + account: privateKeyToAccount(PRIVATE_KEY), |
41 | 42 | }); |
42 | 43 |
|
43 | 44 | const server = new McpServer({ |
44 | | - name: pkg.name, |
45 | | - version: pkg.version, |
| 45 | + name: pkg.name, |
| 46 | + version: pkg.version, |
46 | 47 | }); |
47 | 48 |
|
48 | 49 | server.tool( |
49 | | - 'create_payment', |
50 | | - 'Create a payment to use with paid MCP servers.', |
51 | | - { |
52 | | - tool: z.string().describe('The MCP tool to pay for'), |
53 | | - amount: z |
54 | | - .number() |
55 | | - .max(Number(MAX_PAYMENT_AMOUNT_USDC)) |
56 | | - .describe('The payment amount in USDC'), |
57 | | - asset: z |
58 | | - .string() |
59 | | - .refine(isAddress, { message: 'Invalid address' }) |
60 | | - .describe('The asset to pay with'), |
61 | | - recipient: z |
62 | | - .string() |
63 | | - .refine(isAddress, { message: 'Invalid address' }) |
64 | | - .describe('The recipient of the payment'), |
65 | | - }, |
66 | | - async ({ amount, asset, recipient, tool }) => { |
67 | | - log(`Creating payment of ${amount} ${asset} to ${recipient}`); |
| 50 | + 'create_payment', |
| 51 | + 'Create a payment to use with paid MCP servers.', |
| 52 | + { |
| 53 | + tool: z.string().describe('The MCP tool to pay for').optional(), |
| 54 | + amount: z |
| 55 | + .number() |
| 56 | + .max(Number(MAX_PAYMENT_AMOUNT_USDC)) |
| 57 | + .describe('The payment amount in USDC'), |
| 58 | + recipient: z |
| 59 | + .string() |
| 60 | + .refine(isAddress, { message: 'Invalid address' }) |
| 61 | + .describe('The recipient of the payment'), |
| 62 | + }, |
| 63 | + async ({ amount, recipient, tool }) => { |
| 64 | + log(`Creating payment of ${amount} to ${recipient}`); |
68 | 65 |
|
69 | | - const header = await createPaymentHeader( |
70 | | - walletClient as any, |
71 | | - X402_VERSION, |
72 | | - { |
73 | | - scheme: 'exact', |
74 | | - description: 'Payment for MCP server', |
75 | | - network: 'base', |
76 | | - maxAmountRequired: amount.toString(), |
77 | | - resource: tool, |
78 | | - mimeType: 'application/json', |
79 | | - payTo: recipient, |
80 | | - maxTimeoutSeconds: 3600, |
81 | | - asset, |
82 | | - } |
83 | | - ); |
| 66 | + const header = await createPaymentHeader( |
| 67 | + walletClient as any, |
| 68 | + X402_VERSION, |
| 69 | + { |
| 70 | + scheme: 'exact', |
| 71 | + description: 'Payment for MCP server', |
| 72 | + network: 'base', |
| 73 | + maxAmountRequired: amount.toString(), |
| 74 | + resource: tool ?? 'unknown', |
| 75 | + mimeType: 'application/json', |
| 76 | + payTo: recipient, |
| 77 | + maxTimeoutSeconds: 3600, |
| 78 | + asset: BASE_USDC_ADDRESS, |
| 79 | + } |
| 80 | + ); |
84 | 81 |
|
85 | | - log(`Payment header: ${header}`); |
| 82 | + log(`Payment header: ${header}`); |
86 | 83 |
|
87 | | - return { |
88 | | - content: [ |
89 | | - { |
90 | | - type: 'text', |
91 | | - text: header, |
92 | | - }, |
93 | | - ], |
94 | | - }; |
95 | | - } |
| 84 | + return { |
| 85 | + content: [ |
| 86 | + { |
| 87 | + type: 'text', |
| 88 | + text: header, |
| 89 | + }, |
| 90 | + ], |
| 91 | + }; |
| 92 | + } |
96 | 93 | ); |
97 | 94 |
|
98 | 95 | const transport = new StdioServerTransport(); |
99 | 96 |
|
| 97 | +log('Starting MCP server transport=stdio'); |
100 | 98 | await server.connect(transport); |
0 commit comments