x402 MCP Demo
x402 MCP 工具演示——真实的 402、真实的拒绝与真实的结算
一个极简演示,展示 x402 支付协议 如何保护 MCP 风格的工具端点,并针对真实的 x402==2.16.0 Python 包及其默认公共协调器(https://x402.org/facilitator)在 Base Sepolia 测试网 上运行。这里没有任何手写的协议 JSON——captured_responses/ 中的每个响应都来自库本身,未作编辑。
配套内容:如何为 MCP 工具收费:一小时内实现可用的 x402 端点。
内容一览
文件 | 说明 |
| FastAPI 端点( |
| 生成两个一次性 EOA——一个收款地址和一个付款地址。零资金,仅限测试网,切勿重复使用这些密钥。 |
| 充当付款代理:获取真实的 402,使用零余额账户签署真实的付款负载,并展示协调器的真实拒绝 |
| 由此产生的三个响应体,逐字未改——402 要求、余额不足拒绝和已结算的 200 |
Related MCP server: 402-mcp
运行方法
pip install -r requirements.txt
python gen_wallets.py # writes wallets.json (gitignored) -- two fresh, empty keys
python server.py # starts on 127.0.0.1:8402
python client_probe.py # in a second terminal你会看到什么
第 1 轮——无付款。 一个带有 payment-required 头(base64 JSON)的 402:包含方案、网络、资产合约、以代币最小单位计量的金额,以及收款地址。
第 2 轮——真实签名付款,余额为零。 客户端使用一次性密钥构建并签署实际的 EIP-3009 授权,将其作为 X-PAYMENT 附加,协调器会对照真实的 Base Sepolia 链状态进行检查。它返回:
{ "error": "invalid_exact_evm_insufficient_balance" }该字符串是协调器真实的错误代码,而非猜测——当协议完全按设计运行且付款人根本没有钱时,你得到的就是这个。
第 3 轮——已注资,真实结算。 agent_address 已从 Circle 公共水龙头 获得 20 枚测试网 USDC,然后再次对同一服务器运行 client_probe.py。这一次,第 2 轮返回 200:
{
"url": "https://example.com/mcp",
"recommendation": "expose as a priced x402 tool at $0.01/call",
"paid": true
}带有一个可解码为以下内容的 payment-response 头:
{ "success": true, "payer": "0x3214cB6C...E0E9", "transaction": "0x9530b663...0dd86", "network": "eip155:84532" }该交易哈希是真实且可独立验证的——在 Base Sepolia Blockscout 上查看:状态为 ok,发送到 USDC 合约,方法为 transferWithAuthorization——正是付款人签署的 EIP-3009 无 Gas 转账模式。任何人都可以验证这一点,无需信任本 README。
为什么 client_probe.py 需要手动往返
x402 2.16.0 自带 PaymentRoundTripper,但它是一个回调(handle_response(...)),旨在接入自定义的 httpx/requests 传输层——它本身并不是可直接替换的传输层,尽管文档字符串暗示如此。client_probe.py 直接调用其内部使用的同样三个原语(get_payment_required_response → create_payment_payload → encode_payment_signature_header),对于演示来说组件更少,并产生完全相同的线上输出。
安全注意事项
gen_wallets.py使用eth_account.Account.create()生成密钥——密码学随机,从未注资,本演示中从不接触主网。wallets.json已被 gitignore。wallets.example.json使用一个众所周知的销毁地址展示其结构,以便仓库在全新克隆后即可运行,且 git 历史中不包含机密信息。协调器 URL、网络和资产合约都是库自带的测试网默认值——这里的一切都不会与 Base 主网通信,也不会转移真实价值。
This server cannot be deployed
Maintenance
Related MCP Connectors
Pay-per-action access to APIs and MCP tools over Lightning L402 and Base USDC x402.
Monetize any MCP server: x402 paywall, pay-per-call billing in USDC on Base, agent marketplace.
Test any x402 paid URL without paying: 402, PAYMENT-REQUIRED, USDC, amounts, payTo, EIP-712, CORS.
Agent x402 Paywall MCP — Coinbase HTTP 402 protocol + on-chain settlement. Agents pay per-call
Related MCP Servers
- FlicenseBqualityDmaintenanceProduction-ready MCP server template for consuming X402-protected APIs with gasless micropayments. Enables AI agents like Claude to make micropayment-based API calls seamlessly using EIP-712 signatures and USDC transfers.3-
- AlicenseAqualityBmaintenanceL402 + x402 client MCP. AI agents discover, pay for, and consume any payment-gated API autonomously. Supports Lightning (NWC), Cashu ecash, stablecoins, and human-in-the-loop payments.14302 npmMIT
- AlicenseNot gradedqualityDmaintenanceMinimal MCP server demonstrating L402 pay-per-call with Depth-of-Identity reputation gating, providing a bitcoin data tool that fetches BTC price and mempool fees.MIT
- AlicenseAqualityDmaintenanceAn MCP server that lets your AI coding agent (Claude Code, OpenClaw, Codex, Cursor, etc.) discover and pay on-chain agents registered on ERC-8004, using Coinbase's official x402 protocol. No smart account. No bundler. No relay. Just your EOA, an HTTPS request, and an automatic 402 → sign → retry flow.35MIT