x402-Gated MCP Server
x402-Gated MCP Server š°
A Model Context Protocol (MCP) server that charges AI agents per-call via the x402 micropayment protocol (USDC on Base).
Agents pay $0.05 USDC per tool call ā no API keys, no subscriptions, just pay-per-use crypto.
How It Works
Agent ā POST /mcp/tools/call (no payment)
ā 402 + pricing payload (how much, where to pay)
Agent ā signs USDC tx on Base
ā POST /mcp/tools/call + X-Payment-Signature: 0x...
ā 200 + tool results ā
Quick Start
# Install dependencies
pip install -r requirements.txt
# Configure (copy and edit .env)
cp .env.example .env
# Set PAYMENT_WALLET_ADDRESS=0xYourWallet
# Run server
python server.py
# Test the payment flow (in another terminal)
python client.pyAvailable Tools
Tool | Description | Price |
| Generates user profiles, transactions, or IoT sensor data | 0.05 USDC |
| Deep DEX liquidity, 24h volume, and sentiment score on Base network | 0.05 USDC |
| Clean markdown summaries, key takeaways, and structured JSON from URLs | 0.05 USDC |
Endpoints
Method | Path | Auth | Description |
GET |
| None | Health check + payment config |
GET |
| x402 | MCP SSE connection |
POST |
| x402 | MCP JSON-RPC messages |
POST |
| x402 | REST tool invocation |
Deploy to Railway
# Or via CLI:
railway login
railway init
railway up
# Set env vars in Railway dashboard:
# PAYMENT_WALLET_ADDRESS=0x...
# VERIFY_MODE=onchainEnvironment Variables
Variable | Required | Default | Description |
| Yes | - | Your Base wallet for receiving USDC |
| No |
| Price per call in USDC |
| No |
|
|
| No |
| Base RPC endpoint |
| No |
| Server port |
Architecture
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā FastAPI App ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā ā x402 Payment Middleware (ASGI) ā ā
ā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā ā
ā ā ā MCP SSE Transport ā ā ā
ā ā ā āā /mcp/sse ā ā ā
ā ā ā āā /mcp/messages/ ā ā ā
ā ā ā REST Endpoint ā ā ā
ā ā ā āā /mcp/tools/call ā ā ā
ā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā ā
ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā
ā Health Check (public) ā
ā āā / ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāTech Stack
FastAPI + uvicorn ā ASGI web server
MCP SDK v2 ā Model Context Protocol
web3.py ā On-chain USDC verification on Base
httpx ā Async HTTP client
License
MIT