|
| 1 | +# @xmcp-dev/x402 |
| 2 | + |
| 3 | +x402 payment protocol plugin for xmcp. Add paid tools with USDC on Base. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pnpm add @xmcp-dev/x402 |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Middleware |
| 14 | + |
| 15 | +```typescript |
| 16 | +// src/middleware.ts |
| 17 | +import { x402Middleware } from "@xmcp-dev/x402"; |
| 18 | + |
| 19 | +export default x402Middleware({ |
| 20 | + wallet: process.env.X402_WALLET!, |
| 21 | + defaults: { |
| 22 | + price: 0.01, |
| 23 | + network: "base-sepolia", |
| 24 | + }, |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +### Paid tools |
| 29 | + |
| 30 | +```typescript |
| 31 | +// src/tools/my-tool.ts |
| 32 | +import { paid } from "@xmcp-dev/x402"; |
| 33 | + |
| 34 | +export default paid(async ({ input }) => { |
| 35 | + return { content: [{ type: "text", text: `Result: ${input}` }] }; |
| 36 | +}); |
| 37 | +``` |
| 38 | + |
| 39 | +```typescript |
| 40 | +// src/tools/premium-tool.ts |
| 41 | +import { paid } from "@xmcp-dev/x402"; |
| 42 | + |
| 43 | +export default paid({ price: 0.10 }, async ({ input }) => { |
| 44 | + return { content: [{ type: "text", text: `Premium: ${input}` }] }; |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +### Payment context |
| 49 | + |
| 50 | +```typescript |
| 51 | +import { paid, payment } from "@xmcp-dev/x402"; |
| 52 | + |
| 53 | +export default paid(async ({ input }) => { |
| 54 | + const { payer, amount, network } = payment(); |
| 55 | + return { content: [{ type: "text", text: `Paid by ${payer}` }] }; |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +## API |
| 60 | + |
| 61 | +### `x402Middleware(config)` |
| 62 | + |
| 63 | +```typescript |
| 64 | +interface X402Config { |
| 65 | + wallet: string; |
| 66 | + facilitator?: string; |
| 67 | + debug?: boolean; |
| 68 | + defaults?: { |
| 69 | + price?: number; |
| 70 | + currency?: string; |
| 71 | + network?: string; |
| 72 | + maxPaymentAge?: number; |
| 73 | + }; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### `paid(options?, handler)` |
| 78 | + |
| 79 | +```typescript |
| 80 | +interface X402ToolOptions { |
| 81 | + price?: number; |
| 82 | + currency?: string; |
| 83 | + network?: string; |
| 84 | + maxPaymentAge?: number; |
| 85 | + description?: string; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### `payment()` |
| 90 | + |
| 91 | +```typescript |
| 92 | +interface X402PaymentContext { |
| 93 | + payer: string; |
| 94 | + amount: string; |
| 95 | + network: string; |
| 96 | + asset: string; |
| 97 | + toolName: string; |
| 98 | + transactionHash?: string; |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Networks |
| 103 | + |
| 104 | +| Network | USDC Address | |
| 105 | +|---------|--------------| |
| 106 | +| `base` | `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913` | |
| 107 | +| `base-sepolia` | `0x036CbD53842c5426634e7929541eC2318f3dCF7e` | |
| 108 | + |
| 109 | +## License |
| 110 | + |
| 111 | +MIT |
0 commit comments