x402 Protocol β Complete System Architecture
MUST READ for all future RMI developers
Auto-audited: May 23, 2026 β 59 tools, 7 chains, all endpoints verified
SYSTEM OVERVIEW
INTERNET
β
βΌ
Cloudflare Tunnel (rmi-cloudflare)
βββββββββββββββββββββββββββββββ
β rugmunch.io β
β mcp.rugmunch.io β
β n8n.rugmunch.io β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββΌββββββββββββββββ
β nginx (:80, :443) β
β Routes: β
β /api/* β :8000 β
β /.well-known/* β :8000 β
β /mcp/* β :8000 β
β /health β :8000 β
β / β static β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββββββββΌββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββ ββββββββββββ ββββββββββββ
β Backend β β Orchestratorβ β n8n β
β :8000 β β :8081 β β :5678 β
β 59 toolsβ β 9 agents β β 2 flows β
ββββββ¬βββββ ββββββββββββ ββββββββββββ
β
ββββββΌβββββββββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββ ββββββββ ββββββββββββ
βRedis β βSupabaseβ β Langfuse β
β:6379 β β (API) β β :3100 β
ββββββββ ββββββββ ββββββββββββ
FILE MAP β Every x402 file and what it does
Core Backend (Python/FastAPI)
| File |
Lines |
Purpose |
app/routers/x402_enforcement.py |
1290 |
Payment gatekeeper β intercepts all /api/v1/x402-tools/*, verifies x402 payment headers, enforces trials, builds 402 Payment Required responses. 7-chain support. |
app/routers/x402_tools.py |
3581 |
Tool handlers β 48 route implementations. Each @router.post("/audit") is a tool. Also serves AI framework adapters (OpenAI, Anthropic, Gemini, LangChain formats). |
app/routers/x402_catalog.py |
255 |
Auto-discovery β parses gateway index.ts files + scans route decorators. Builds unified catalog. No hardcoded tool lists. |
app/routers/x402_forensic_tools.py |
237 |
Forensic bundles β 3 premium tools: forensic_valuation, osint_identity_hunt, investigation_report |
app/routers/x402_dashboard.py |
493 |
Analytics β usage tracking, revenue per tool, top users, trial exhaustion stats |
app/routers/x402_middleware.py |
685 |
Anti-abuse β device fingerprinting, trial tracking per device/wallet, rate limiting |
app/mcp/x402_mcp_server.py |
682 |
MCP protocol server β translates x402 tools into MCP format for Claude/Cursor/Windsurf |
Cloudflare Workers (TypeScript)
| File |
Lines |
Purpose |
x402-gateway/base/index.ts |
2650 |
Base + EVM gateway β Payment verification via PayAI facilitator. 44 tool definitions. Routes to backend. |
x402-gateway/solana/index.ts |
2650 |
Solana gateway β Payment verification via PayAI facilitator. 35 tool definitions. Routes to backend. |
x402-twitter-view/src/index.ts |
~200 |
Twitter data worker β profiles, timelines, search. Self-healing with failover. |
GitHub Repos (public)
| Repo |
Purpose |
rug-munch-intelligence-mcp |
Public pip package. Thin MCP wrapper around x402 API. |
x402-gateway-solana |
Solana gateway source β deploys to Cloudflare Workers |
x402-gateway-base |
Base + EVM gateway source β deploys to Cloudflare Workers |
x402-twitter-view |
Twitter data worker source |
PAYMENT FLOW β Step by step
1. User/bot calls POST /api/v1/x402-tools/{tool}
β
2. x402_enforcement middleware intercepts
βββ Check: Has user paid? (x-pay header with tx hash)
βββ Check: Is trial available? (device fingerprint + wallet)
βββ If unpaid AND no trials β build 402 Payment Required
β βββ Returns: payment addresses per chain, amounts, timeout
β
3. If paid or trial available β forward to tool handler
β
4. Tool handler (x402_tools.py) executes
βββ Call backend connectors (Helius, Etherscan, DeFiLlama, etc.)
βββ Aggregate multi-source data
βββ Return JSON response
β
5. Payment verification (if paid):
βββ Base/Solana β PayAI facilitator verifies USDC transfer
βββ ETH/BSC/ARB/OPT/POL β Self-verify via Etherscan on-chain check
Payment Addresses
- All EVM chains:
0x1E3AC01d0fdb976179790BDD02823196A92705C9
- Solana:
Gix4P9AmwcZRGzr2hCEME5m2QAvY86dBfm8c7e7MpFzv
- Token: USDC on all chains
- Amounts: $0.01 - $0.50 per tool (defined in gateway index.ts)
TOOL DISCOVERY β How the catalog works
MCP Catalog (/api/v1/x402/tools-catalog)
β
βββ Step 1: parse_gateway_tools()
β βββ Reads x402-gateway/{base,solana}/index.ts
β βββ Regex extracts each tool from RMI_TOOLS object
β βββ Finds: name, description, price, category, trialFree, method
β βββ Result: 44 tools (22 unique to base, 0 unique to solana)
β
βββ Step 2: discover_route_tools()
β βββ Scans x402_tools.py + x402_forensic_tools.py
β βββ Finds @router.get/post decorators
β βββ Extracts docstrings as descriptions
β βββ Result: 5 unique tools not in gateways
β
βββ Step 3: Merge + Deduplicate
βββ Same tool ID = merge chains
βββ Result: 59 total tools
Output formats
| Format |
Endpoint |
For |
| Full catalog |
/api/v1/x402/tools-catalog |
Humans, dashboards |
| x402 protocol |
/.well-known/x402 |
AI agents, protocol discovery |
| OpenAI |
/api/v1/x402-tools/openai-tools |
ChatGPT, OpenAI-compatible |
| Anthropic |
/api/v1/x402-tools/anthropic-tools |
Claude, Cursor |
| Gemini |
/api/v1/x402-tools/gemini-tools |
Google Gemini |
| LangChain |
/api/v1/x402-tools/langchain-tools |
LangChain agents |
PRICING & TRIALS
| Tier |
Calls |
Requirement |
| Anonymous |
1 free per tool |
Device fingerprint |
| Wallet connected |
3 free per tool |
MetaMask/Phantom |
| Paid |
Unlimited |
USDC payment per call |
Refund: Full refund if tool returns no data. POST /api/v1/x402/refund with tx hash within 48h.
Anti-abuse: Device fingerprinting survives VPN/incognito. Identity hierarchy: wallet > device_id > turnstile > fingerprint.
CHAIN SUPPORT MATRIX
| Chain |
Network ID |
USDC Address |
Verification |
| Base |
eip155:8453 |
0x833589...a02913 |
PayAI facilitator |
| Solana |
solana:5eykt4... |
EPjFWdd5...TDt1v |
PayAI facilitator |
| Ethereum |
eip155:1 |
0xA0b869...eB48 |
Self-verify |
| BSC |
eip155:56 |
0x8AC76a...d580d |
Self-verify |
| Arbitrum |
eip155:42161 |
0xaf88d0...5831 |
Self-verify |
| Optimism |
eip155:10 |
0x0b2C63...Ff85 |
Self-verify |
| Polygon |
eip155:137 |
0x3c499c...3359 |
Self-verify |
CONNECTOR APIS β What data we have
| Connector |
API Key |
Status |
Used By |
| Helius |
β
Working |
Solana RPC, webhooks, transactions |
wallet, cluster, whale, forensics |
| Etherscan |
β
Working |
Contract source, ABI, TX history |
contract_inspect, tx_decoder |
| DeFiLlama |
π Free |
TVL, protocols, yields |
protocol_research, yield_scanner |
| Birdeye |
β
Working |
Trending, token data |
trending_tokens |
| CoinGecko |
β
Working |
Prices, categories, trending |
market_price, market_sectors |
| DexScreener |
π Free |
Pairs, liquidity, volume |
dex_activity, market_price |
| Moralis |
β Key invalid |
Multi-chain wallet/token data |
NOT USED |
| GMGN |
β Access denied |
KOL tracking, trending |
NOT USED |
| Arkham |
β οΈ Untested |
Entity labeling |
NOT USED |
| Nansen |
β οΈ Untested |
Smart money, token god mode |
NOT USED |
| Dune |
β οΈ Untested |
Custom queries |
NOT USED |
TESTING
bash /root/backend/scripts/test_all_tools.sh
python3 /root/scripts/rmi-status
bash /root/backend/scripts/pre-commit.sh
COMMON ISSUES & FIXES
| Issue |
Symptom |
Fix |
| Gateway files missing |
Catalog shows 0 tools |
Clone gateways to /root/backend/x402-gateway/ |
| Tool returns 403 |
x402 enforcement active |
Expected β tools require payment or trial |
| Catalog stale after adding tools |
Old count |
Restart backend: docker restart rmi-backend |
| Payment verification fails |
402 responses |
Check USDC addresses, network config |
| Self-signed cert on external |
curl fails without -k |
Cloudflare provides edge cert β use -k or browser |
WHEN ADDING NEW TOOLS
- Add definition to
x402-gateway/base/index.ts (and solana if applicable)
- Add route handler to
x402_tools.py:@router.post("/my_new_tool")
async def my_new_tool(req: SomeRequest):
"""Description of what this tool does."""
- Restart backend:
docker restart rmi-backend
- Verify:
curl http://localhost:8000/api/v1/x402-tools/my_new_tool
- Check catalog auto-updated:
curl http://localhost:8000/api/v1/x402/tools-catalog