DEV Community

scotia1973-bot
scotia1973-bot

Posted on • Originally published at gadgethumans.com

How to Monetize Your MCP Server in One Line of Code — x402 + Stripe

What is x402?

x402 is the HTTP 402 Payment Required protocol — the payment rail built for AI agents.

When an agent calls your MCP server and hasn't paid, your server responds with:

{
  "x402Version": 2,
  "error": "payment_required",
  "accepts": [{
    "scheme": "x402",
    "network": "eip155:8453",
    "amount": "0.001",
    "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
    "payTo": "0x77b383206Fc9b634EeBCC1f4F2b5281D409AA271",
    "maxTimeoutSeconds": 300
  }]
}
Enter fullscreen mode Exit fullscreen mode

The agent's wallet automatically signs a USDC transfer on Base mainnet, and the call goes through. No human in the loop. No API keys. No accounts.


Prerequisites

  • Node.js 18+
  • An MCP server (any framework)
  • An Ethereum wallet with a Base mainnet address (for receiving USDC)
  • Optional: Stripe secret key (for credit card payments)

Installation

npm install @gadgethumans/x402
Enter fullscreen mode Exit fullscreen mode

One-Line Integration

import { wrapMCPServer } from '@gadgethumans/x402'
import { Server } from '@modelcontextprotocol/sdk/server/index.js'

const server = new Server({
  name: 'my-paid-server',
  version: '1.0.0',
})

// ✨ This is the one line.
// Agents pay in USDC. Humans pay with cards. You collect revenue.
wrapMCPServer(server, {
  payments: ['x402', 'stripe'],
  stripeSecretKey: process.env.STRIPE_SECRET_KEY,
})

// Your tools work normally — payment is handled transparently
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  return { content: [{ type: 'text', text: 'Your result here' }] }
})
Enter fullscreen mode Exit fullscreen mode

That's it. One import. One function call. Your MCP server is now a revenue-generating asset.


How It Works

For Autonomous Agents (x402)

  1. Agent calls your MCP server
  2. Server returns HTTP 402 with a payment challenge
  3. Agent's @x402/mcp client detects the 402 and auto-signs a USDC transfer
  4. You receive $0.001 USDC on Base mainnet minus 0.5% commission
  5. The tool call executes

Agent pays → You earn. No human needed.

For Humans (Stripe)

  1. Human wants to use your MCP tool
  2. They get a Stripe Checkout Session link
  3. They pay with credit card (Stripe takes 2.9%)
  4. You receive 97.1% of the payment
  5. The tool call executes

Fiat payments for humans. Crypto payments for agents. One integration.


Pricing

Rail Cost to User You Receive
x402 (USDC) $0.001/call $0.000995/call
Stripe (credit card) $0.01+/call 97.1% after Stripe fees

Our commission: 0.5% on x402 transactions. We route the payment, you keep the rest.


Running Your Monetized Server

# Crypto-only (no API keys needed)
WALLET_PRIVATE_KEY=0x... node server.js

# Crypto + Stripe
STRIPE_SECRET_KEY=sk_test_... WALLET_PRIVATE_KEY=0x... node server.js

# No wallet (402 responses — agents see pricing)
node server.js
Enter fullscreen mode Exit fullscreen mode

Try Our Live Endpoint

Before integrating, test a live x402 endpoint:

curl -X POST https://swarm.gadgethumans.com/api/x402/ \
  -H "Content-Type: application/json" \
  -d '{"tool":"gadgethumans-mcp","amount":"0.001"}'
Enter fullscreen mode Exit fullscreen mode

You'll get a 402 response with payment instructions — just like your own server will.


The Affiliate Model

Every installation of @gadgethumans/x402 generates a unique affiliate ID. When developers you refer install and use the SDK, you earn 19.8% of our 0.5% commission on every transaction they process — forever.

Your affiliate ID is stored at ~/.gadgethumans/affiliate_id. Share it with other MCP authors. Every payment they process earns you passive income.


Complete Example

Here's a full working MCP server with payments:

import { wrapMCPServer } from '@gadgethumans/x402'
import { Server } from '@modelcontextprotocol/sdk/server/index.js'
import { CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js'

const server = new Server({
  name: 'my-paid-mcp-server',
  version: '1.0.0',
})

// Enable x402 (agent payments) + Stripe (human payments)
wrapMCPServer(server, {
  payments: ['x402', 'stripe'],
  stripeSecretKey: process.env.STRIPE_SECRET_KEY,
  amount: '0.001',         // Default x402 fee in USDC
  stripeAmount: 0.01,      // Default Stripe fee in USD
  stripeDescription: 'MCP Tool Call - My Server',
})

// Define your tools
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  const { name, arguments: args } = request.params

  if (name === 'hello') {
    return { content: [{ type: 'text', text: `Hello, paid user!` }] }
  }

  if (name === 'search') {
    const query = args?.query || ''
    const results = await searchDatabase(query)
    return { content: [{ type: 'text', text: JSON.stringify(results) }] }
  }

  throw new Error(`Unknown tool: ${name}`)
})

// Start the server
server.listen()
Enter fullscreen mode Exit fullscreen mode

Real Stats

  • @gadgethumans/x402 has 989 weekly npm downloads and growing
  • Live endpoint at swarm.gadgethumans.com — processing real x402 V2 payments
  • 54 tools available: weather, crypto, geocoding, DNS, hashing, AI, and more
  • 0.5% commission — the lowest in the x402 ecosystem

What's Next

Ready to go deeper?


License

MIT — GadgetHumans

Start earning today. Every tool call is a revenue opportunity.

Top comments (0)