Skip to main content
Glama

x402 MCP 工具演示——真实的 402、真实的拒绝与真实的结算

一个极简演示,展示 x402 支付协议 如何保护 MCP 风格的工具端点,并针对真实的 x402==2.16.0 Python 包及其默认公共协调器(https://x402.org/facilitator)在 Base Sepolia 测试网 上运行。这里没有任何手写的协议 JSON——captured_responses/ 中的每个响应都来自库本身,未作编辑。

配套内容:如何为 MCP 工具收费:一小时内实现可用的 x402 端点

内容一览

文件

说明

server.py

FastAPI 端点(POST /mcp/tools/vendor_audit),由 x402ResourceServer 和 EVM exact 方案保护,定价为 Base Sepolia 上的 $0.01 USDC

gen_wallets.py

生成两个一次性 EOA——一个收款地址和一个付款地址。零资金,仅限测试网,切勿重复使用这些密钥。

client_probe.py

充当付款代理:获取真实的 402,使用零余额账户签署真实的付款负载,并展示协调器的真实拒绝

captured_responses/

由此产生的三个响应体,逐字未改——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_responsecreate_payment_payloadencode_payment_signature_header),对于演示来说组件更少,并产生完全相同的线上输出。

安全注意事项

  • gen_wallets.py 使用 eth_account.Account.create() 生成密钥——密码学随机,从未注资,本演示中从不接触主网。

  • wallets.json 已被 gitignore。wallets.example.json 使用一个众所周知的销毁地址展示其结构,以便仓库在全新克隆后即可运行,且 git 历史中不包含机密信息。

  • 协调器 URL、网络和资产合约都是库自带的测试网默认值——这里的一切都不会与 Base 主网通信,也不会转移真实价值。

A
license - permissive license
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    B
    quality
    D
    maintenance
    Production-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
  • A
    license
    Not graded
    quality
    A
    maintenance
    L402 + 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.
    371
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Minimal 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
  • A
    license
    A
    quality
    D
    maintenance
    An 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.
    3
    5
    MIT

View all related MCP servers

Related MCP Connectors

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Thinkflow-ro/x402-mcp-demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server