DEV Community

goldbean
goldbean

Posted on • Originally published at dev.to

Build a Pay-Per-Call MCP Server: x402 + 84 Tools + USDC on Base

How I Built a Pay-Per-Call MCP Server with x402 (84 Tools, USDC on Base)

TL;DR: I built GoldBean — an AI API marketplace with 84 pay-per-call tools. AI agents discover it, pay USDC on Base via x402, and get results in <2 seconds. No API keys, no signups for consumers.


The Problem

I run an MCP server with 84+ tools. Like 95% of MCP servers out there, I was earning $0.

The reason? Subscriptions don't work for AI agents. Agents don't have credit cards. They don't click "Subscribe" buttons.

What they can do is sign crypto transactions. That's where x402 (HTTP 402 Payment Required + crypto) comes in.

What is x402?

x402 is a protocol that repurposes HTTP's old 402 Payment Required status code for modern micropayments:

  1. An AI agent calls your API
  2. Instead of returning data, you return 402 with payment requirements
  3. The agent signs a USDC transfer (EIP-3009) and retries
  4. You verify on-chain (2 seconds on Base) and return the data

Think of it as Stripe for AI agents — except there's no signup, no API key management, no invoices. Just USDC on Base mainnet.

The Architecture

Here's what the GoldBean backend looks like:

const express = require('express');
const app = express();

// x402 middleware — intercepts paid routes
app.use('/paid/*', async (req, res, next) => {
  const paymentHeader = req.headers['x-payment'];
  if (!paymentHeader) {
    return res.status(402).json({
      error: 'x402 payment required',
      amount_usd: 0.01,
      payment_options: {
        x402: { method: 'x402', desc: 'Pay per call with USDC', amount_usd: 0.01 }
      }
    });
  }
  // Verify on-chain via Base RPC
  const verified = await verifyOnChainTx(paymentHeader);
  if (!verified) return res.status(402).json({ error: 'payment verification failed' });
  next();
});

// Route modules
app.use('/paid/ocr', baiduOcrRouter);
app.use('/paid/weather', weatherRouter);
app.use('/paid/translate', translateRouter);
// ...84+ routes divided across modules
Enter fullscreen mode Exit fullscreen mode

Key Design Decisions

1. Middleware-first approach

All paid routes are behind the x402 middleware. Free routes like /health and /api/routes are open. This keeps the code clean.

2. Manual route registry

Express 5 has a bug where _router.stack is inaccessible. I built a _routeRegistry array and a printRegisteredRoutes() function that logs all 146 routes at startup.

3. Dual payment model

  • Free trial: New users get 20 free credits
  • x402: Agents auto-pay per-call (USDC on Base)

Deployment

# Install
git clone https://github.com/wuzenghai616-lang/goldbean
cd goldbean
npm install

# Configure
export PRIVATE_KEY=your_wallet_key
export RPC_URL=https://mainnet.base.org

# Start
node server.js

# As MCP server
npx goldbean-mcp
Enter fullscreen mode Exit fullscreen mode

The server runs on a VPS behind nginx with Let's Encrypt SSL. Systemd keeps it alive.

Results

  • 🟢 84 tools live and working
  • 🟢 USDC on Base — 2 second settlement
  • 🟢 Free tier for developers to try
  • 🟢 Listed on Glama.ai, Smithery.ai, MCP Market
  • 🟢 discoverable via /.well-known/x402-bazaar

What's Next

  • MCPize marketplace integration (native x402 support)
  • OCR demo page — upload Chinese text, get results
  • Developer-focused content series

Try It


Built by GoldBean 🫘 — Wishing You Good Fortune & Prosperity

Top comments (0)