rugmuncher-backend / backend /X402_ARCHITECTURE.md
Netcup Server
ops: commit netcup-specific docs, config, and M3 Bayesian reputation on netcup mainline
846b6be
|
Raw
History Blame Contribute Delete
10.6 kB

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

# Test all 59 tools (expect 403 = x402 enforcement working):
bash /root/backend/scripts/test_all_tools.sh

# Status dashboard:
python3 /root/scripts/rmi-status

# Pre-commit check:
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

  1. Add definition to x402-gateway/base/index.ts (and solana if applicable)
  2. 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."""
        # Implementation using existing connectors
    
  3. Restart backend: docker restart rmi-backend
  4. Verify: curl http://localhost:8000/api/v1/x402-tools/my_new_tool
  5. Check catalog auto-updated: curl http://localhost:8000/api/v1/x402/tools-catalog