x402 End-to-End Deployment Guide: Enable Your AI Agent to Pay for Data Using USDC
→ x402 Data Services · For B2B clients(including service scope and reference pricing)
〇 What is this system?
Two-layer protocol, both mandatory: MCP enables AI Agents to discover and invoke your tools via standardized interfaces (discovery layer); x402 ensures the Agent pays before accessing data (settlement layer, Base Mainnet USDC). Buyers only need USDC in their wallet — no ETH required. Gas is covered by the facilitator (EIP-3009 gasless authorization). No account registration, no API Key, no Coinbase account needed to receive payments.
① Preparation
- A server capable of running Python (this site runs on Tencent Cloud 2C4G Ubuntu 24.04)
- One Base mainnet USDC receiving address (the one whose private key you control).
- A domain + HTTPS (required for both Agent discovery and x402)
- Optional: A test buyer wallet pre-funded with a small amount of Base-mainnet USDC for verification
Install dependencies
python3 -m venv venv && source venv/bin/activate pip install "mcp<2" "x402==2.25.0" web3 httpx
mcp must be pinned to <2: FastMCP was renamed MCPServer in version 2.x; x402 2.25 still uses 1.x syntax—installing 2.x will cause immediate import failure.② Write the server-side script server.py (core skeleton)
import os
from mcp.server.fastmcp import FastMCP
from x402 import x402ResourceServer
from x402.http import FacilitatorConfig, HTTPFacilitatorClient
from x402.mechanisms.evm.exact import ExactEvmServerScheme
from x402.mcp import create_payment_wrapper
from x402.schemas.config import ResourceConfig
# 1. Chain and facilitator: Base mainnet + PayAI (no registration required; supports x402 v2 mainnet)
NETWORK = os.environ.get("X402_NETWORK", "eip155:8453")
FACILITATOR_URL = os.environ.get("X402_FACILITATOR",
"https://facilitator.payai.network")
# 2. Payment address: Replace with your own Base mainnet address! Do not copy the example.
PAY_TO = os.environ.get("PAY_TO") or "0xYOUR_PAYOUT_ADDRESS"
# 3. Pricing (per call, USD) — These are illustrative values for demonstration only and bear no relation to actual prices on this site
PRICE = {"my_data_tool": "$1.00"}
facilitator = HTTPFacilitatorClient(FacilitatorConfig(url=FACILITATOR_URL))
resource_server = x402ResourceServer(facilitator)
resource_server.register(NETWORK, ExactEvmServerScheme())
resource_server.initialize()
mcp = FastMCP("my-data-service")
def paid(tool_name: str):
return create_payment_wrapper(
resource_server,
accepts=resource_server.build_payment_requirements(
ResourceConfig(scheme="exact", pay_to=PAY_TO,
price=PRICE[tool_name], network=NETWORK)),
resource=None,
)
@mcp.tool(name="list_items") # Free tool: Let your Agent see your inventory first
def list_items() -> str:
return '[{"name": "item-a"}]'
@mcp.tool(name="my_data_tool") # paid tool
@paid("my_data_tool")
def my_data_tool(item: str) -> str:
return '{"item": "%s", "price": 1.24}' % item
if __name__ == "__main__":
mcp.run(transport="streamable-http", host="127.0.0.1", port=8797)③ Run as a systemd service
# /etc/systemd/system/x402.service [Unit] Description=My x402 paid MCP server After=network.target [Service] Type=simple WorkingDirectory=/opt/my-x402 Environment=X402_NETWORK=eip155:8453 Environment=X402_FACILITATOR=https://facilitator.payai.network Environment=PORT=8797 ExecStart=/opt/my-x402/venv/bin/python server.py Restart=always RestartSec=5 [Install] WantedBy=multi-user.target # systemctl daemon-reload && systemctl enable --now x402
④ Configure nginx reverse proxy for /mcp
location ^~ /mcp {
proxy_pass http://127.0.0.1:8797;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off; # streamable-http uses SSE and requires buffering to be disabled
proxy_cache off;
proxy_read_timeout 300s;
}proxy_buffering off is mandatory: streamable-http delivers SSE streams, and enabling buffering will stall delivery.⑤ Zero-cost verification (credible without spending real money)
# Zero-cost validation (no real money spent): Call paid tools without payment — should return HTTP 402 Payment Required
curl -s https://your-domain/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1"}}}'
# Then call tools/call with the session ID (without X-PAYMENT header).
# The response’s accepts field should include: network=eip155:8453, asset=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
# payTo=your address&amount=50000 (i.e., 0.05 USDC; example value)⑥ Real-money verification (top up USDC + on-chain validation)
# Real-money verification: Top up the test buyer’s wallet with USDC, run the client, then query on-chain USDC Transfer events
# (Do not rely on balance differences—RPC sync delays may yield false deltas; only trust Transfer events)
from web3 import Web3
w3 = Web3(Web3.HTTPProvider(”https://base-rpc.publicnode.com”)) # mainnet.base.org returns 429
USDC = ”0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913”
topic = w3.keccak(text=”Transfer(address,address,uint256)”).hex()
logs = w3.eth.get_logs({
”fromBlock”: w3.eth.block_number - 3000,
”toBlock”: ”latest”,
”address”: USDC,
”topics”: [topic,
”0x” + ”0”*24 + buyer[2:].lower(),
”0x” + ”0”*24 + seller[2:].lower()],
})
for lg in logs:
print(int(lg[”data”].hex(), 16) / 1e6, ”USDC received”)⑦ Make yourself discoverable (discovery layer)
- /.well-known/mcp.json and /.well-known/x402.json: Machine-readable tool list and pricing, placed in the site root directory
- MCP Official Registry: Use the mcp-publisher binary from GitHub Releases (not the identically named npm package, which is counterfeit). Domain verification uses HTTP file hosting (place a file at /.well-known/mcp-registry-auth containing v=MCPv1); no DNS changes required.
- llms.txt: Declares MCP endpoint and payment methods — parsed by AI crawlers
7 pitfalls we’ve encountered (not documented elsewhere)
| Pitfall | Phenomenon | Solution |
|---|---|---|
| The x402.org facilitator supports testnets only. | Payments always fail after switching to mainnet | Switch to PayAI / openx402 / CDP |
| Mogami Offers Only x402 v1 | Incompatible with x402 SDK v2.25 | Switch to a facilitator supporting v2 + EIP-155:8453 |
| Upgrade MCP to Version 2.x | import Fails Immediately | Lock mcp<2 |
| On-chain verification used the testnet USDC contract | A Transfer record cannot be found — incorrectly flagged as unpaid | Mainnet contract 0x833589fC…2913 |
| Public RPC at mainnet.base.org Is Rate-Limited | eth_getLogs Returns HTTP 429 | Switch to base-rpc.publicnode.com |
| Verify using balance delta | RPC Sync Latency Causes False Balance Discrepancies | Only trust on-chain Transfer events |
| Writing Prices Containing $ in Shell | $0 expanded into bash, polluting the JSON | Use single-quote heredoc; after modification, validate with grep |
Public transaction counter
| Time | Type | Amount | On-chain proof |
|---|---|---|---|
| 2026-10-26 19:11 | Self-hosted wallet verification | 0.01 USDC | 0x5e492eee6572… |
| 2026-10-26 19:06 | Self-hosted wallet verification | 0.01 USDC | 0x67a0d45f7183… |
This article’s verification cases (self-hosted wallet, verifiable on-chain)
· Payment address:
0x230cd9cd33589bcf33cd32e5aa4f9e0c3994a63b· Test buyer address:
0x6274A5e39E84168c5DE57110A6e0ab1FBBE0a89E· Transaction hash: tx
0x67a0d45f7183032600feb4fa7c53c5348eb84b05fa586927bf3d7d6718faed12(0.01 USDC)、tx 0x5e492eee6572e42d934f7b4082b525036346c5a2761ff534ce16a8bd9c781bc9(0.01 USDC)· MCP Registry name:
link.rentgpu/gpu-market· Online endpoint:https://rentgpu.link/mcp(Integration instructions and real response examples see /agents)
FAQ
You must enter your own Base mainnet payout address (PAY_TO). The addresses shown on this page are sample addresses from this site, provided only for on-chain record verification; copying them will route others’ payments into this site’s wallet. This page contains no private keys.
No KYC required for receiving payments—facilitators like PayAI and openx402 require no registration. Only when converting received USDC to RMB for withdrawal will you need to complete identity verification via an exchange.
Not required. x402 uses EIP-3009 gasless authorization (transferWithAuthorization); the buyer’s wallet only needs Base-mainnet USDC, and gas is covered by the facilitator.
Calling a paid tool without payment should return HTTP 402 Payment Required. Verify that the challenge contains: network=eip155:8453, asset=Circle’s official USDC contract, payTo=your address, and correct amount. Real-money validation is performed separately.
None of these can be directly used for the mainnet scenario in this tutorial: x402.org officially supports testnet only; Mogami supports x402 v1 only. For mainnet, use PayAI, openx402 (no registration required), or Coinbase CDP (API Key required).
x402 Full Deployment Tutorial|Data source: GPU Compute Radar rentgpu.link (updated 2026-11-23, auto-scraping public pricing from all platforms every 45 minutes)|https://rentgpu.link/x402|CC BY 4.0
rentgpu.link