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
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: { ... }
}
Step 2: x402 Payment Verification
The x402 protocol is elegant: HTTP 402 Payment Required, natively.
Flow:
- First request — no payment header → Server returns 402 + payment details
-
Second request — client includes
X-PAYMENTheader with EIP-3009 signed transfer - Verification — server checks on-chain that the transfer is valid and not replayed
- 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;
};
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
]
}));
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
- Nonce replay attacks — Fixed with Redis-backed nonce storage
- Chain reorgs on Base — Wait for 2 block confirmations
- Price volatility — Daily price cache updates
-
Client UX — Claude Desktop doesn't show prices, added
tool-infoendpoint
Lessons Learned
- MCP is the perfect distribution channel for APIs
- x402 micropayments can work when invisible to users
- Free tiers are essential — 80% never pay, but spread the word
- Aggregation > Individual APIs
Try It Out
-
MCP endpoint:
https://goldbean-api.xyz/sse - Docs: https://goldbean-api.xyz
-
npm SDK:
npm install @goldbean/x402-sdk - Server source: https://github.com/wuzenghai616-lang/goldbean
Top comments (0)