DEV Community

goldbean
goldbean

Posted on

From Free to Paid: Engineering a Monetized MCP Server with 16 AI Tools in Production

Last month we launched GoldBean, a pay-per-call MCP server with 100+ AI tools. The most common question I get: 'How did you actually build the payment layer?'

Here's a technical deep dive into how we implemented x402 micropayments on top of an MCP server.

The Problem

Most MCP servers are free. That's great for open source, but it creates a tragedy of the commons: nobody maintains them, API costs go unpaid, and quality degrades.

We wanted to prove that a pay-per-call model could work for AI tool aggregation — where developers pay only for what they use, and the underlying API costs are transparently passed through.

Architecture Overview

Claude Desktop → MCP Client → GoldBean Server
                                    ↓
                              Payment Gateway
                                    ↓
                          x402 (Base USDC) / PayPal / Alipay
                                    ↓
                            Baidu AI / Other Providers
Enter fullscreen mode Exit fullscreen mode

Step 1: The MCP Server Skeleton

We built on top of the standard MCP SDK. Each tool gets a price metadata field:

{
  name: "ocr",
  description: "Extract text from images",
  price: 0.01,  // USD
  inputSchema: { ... }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: x402 Payment Verification

The x402 protocol is elegant: HTTP 402 Payment Required, natively.

Flow:

  1. First request — no payment header → Server returns 402 + payment details
  2. Second request — client includes X-PAYMENT header with EIP-3009 signed transfer
  3. Verification — server checks on-chain that the transfer is valid and not replayed
  4. Execution — if payment OK, run the tool and return results

Key code snippet (Node.js):

const verifyPayment = async (paymentHeader) => {
  const { transferWithAuth } = decodePayment(paymentHeader);

  const valid = await contract.methods.validateTransfer(transferWithAuth).call();
  const used = await db.get(`nonce:${transferWithAuth.nonce}`);
  if (used) throw new Error('Replay detected');
  await db.set(`nonce:${transferWithAuth.nonce}`, true);

  return valid;
};
Enter fullscreen mode Exit fullscreen mode

Step 3: PayPal and Alipay Fallback

Not everyone wants crypto. We added traditional payment rails:

  • PayPal — Standard checkout flow, credits added to account
  • Alipay — QR code payment, same credit system

Credits are stored server-side and deducted per call. When credits run out, the client gets a 402 response with instructions to top up.

Step 4: MCP Tool Registration

server.setRequestHandler(ListToolsRequestSchema, async () => ({
  tools: [
    { name: "ocr", description: "Extract text", price: 0.01, freeQuota: 50, inputSchema: { ... } },
    // ... 15 more tools
  ]
}));
Enter fullscreen mode Exit fullscreen mode

Step 5: The Free Tier Hook

Data so far:

  • ~80% of users stay within free tier
  • ~15% exceed free tier but don't pay (wait for next day)
  • ~5% convert to paid

The free tier is a loss leader, but it builds trust.

Challenges We Hit

  1. Nonce replay attacks — Fixed with Redis-backed nonce storage
  2. Chain reorgs on Base — Wait for 2 block confirmations
  3. Price volatility — Daily price cache updates
  4. Client UX — Claude Desktop doesn't show prices, added tool-info endpoint

Lessons Learned

  1. MCP is the perfect distribution channel for APIs
  2. x402 micropayments can work when invisible to users
  3. Free tiers are essential — 80% never pay, but spread the word
  4. Aggregation > Individual APIs

Try It Out

Top comments (0)