AI 代理MCP 与 x402
您的 AI 代理可以自行付费的地址验证。
一个 MCP 工具 validate_address,加上按次计费的 x402 支付:您的代理免费发现服务,在 Base 网络上每次验证支付 0.02 USDC,并获得标准化地址。无需账户,无需 API 密钥,无需人工介入。
- MCP 2025-06-18
- x402 v1 · v2
- 每次调用 0.02 USDC
- USDC · Base
三次调用完成
与 HTTP 相同的服务,接入您的代理已经在用的协议。
免费发现
/.well-known/mcp-server卡片描述服务器。在/mcp上调用initialize和tools/list完全免费。获取报价
未付款的
tools/call会返回一个错误工具结果(isError),其中是准确的 x402 v2 报价:网络、USDC 代币、金额(20000 个单位,即 0.02 USDC)和收款地址。付款,然后获得结果
代理签署授权(EIP-3009),并将付款放入
params._meta["x402/payment"]再次调用(v1 客户端也可使用X-PAYMENT请求头)。结算完成后返回结果与收据。
MCP 客户端配置
{
"mcpServers": {
"address-validator": { "url": "https://kder-ai.com/mcp" }
}
}
调用工具
POST https://kder-ai.com/mcp
{
"jsonrpc": "2.0", "id": 1, "method": "tools/call",
"params": {
"name": "validate_address",
"arguments": { "address": {
"street": "1600 Amphitheatre Parkway", "city": "Mountain View",
"state": "CA", "zip": "94043", "country": "US" } }
}
}
工具结果中的报价
"accepts": [{
"scheme": "exact", "network": "eip155:8453",
"amount": "20000",
"asset": "USDC", "payTo": "0x…"
}]
从代码中付款
x402 官方库会自动读取报价、签名并付款,x402 v1 和 v2 均适用。设置支出上限,然后像调用其他 API 一样调用即可。
Python · x402 + httpx
# pip install "x402[httpx,evm]"
import asyncio, os
from eth_account import Account
from x402 import x402Client
from x402.http.clients import x402HttpxClient
from x402.mechanisms.evm import EthAccountSigner
from x402.mechanisms.evm.exact.register import register_exact_evm_client
async def main():
client = x402Client().set_spend_controls({"max_amount_per_payment": "$0.05"})
signer = EthAccountSigner(Account.from_key(os.environ["EVM_PRIVATE_KEY"]))
register_exact_evm_client(client, signer)
async with x402HttpxClient(client) as http:
response = await http.post("https://kder-ai.com/validate", json={"address": {
"street": "1600 Amphitheatre Parkway", "city": "Mountain View",
"state": "CA", "zip": "94043", "country": "US"}})
await response.aread()
print(response.status_code, response.json())
asyncio.run(main())
JavaScript · @x402/fetch
// npm install @x402/fetch @x402/evm viem, then run as an ES module (pay.mjs)
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
client.setSpendControls({ maxAmountPerPayment: "$0.05" });
client.register("eip155:*", new ExactEvmScheme(privateKeyToAccount(process.env.EVM_PRIVATE_KEY)));
const pay = wrapFetchWithPayment(fetch, client);
const response = await pay("https://kder-ai.com/validate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ address: {
street: "1600 Amphitheatre Parkway", city: "Mountain View",
state: "CA", zip: "94043", country: "US" } }),
});
console.log(response.status, await response.json());
代理收到的内容
JSON 响应,格式始终一致。
validated地址是否被识别standardizedline1、line2、city、state、zip、countrylatitude · longitude地址的 GPS 坐标deliverabilityresidential、commercial…confidence0 到 1 的置信度degraded为 true:数据提供方未响应,请重试
为什么代理适合用 x402
无需创建账户,无需保存密钥,无需订阅:代理只为实际用量付费,用多少付多少。已使用过的授权会被拒绝:同一笔付款绝不会计算两次。
须知
- 美国地址由 USPS(DPV)核验,法国及海外省由国家地址库(BAN)核验;其他地区仍为部分验证。
degraded为 true 表示数据提供方没有响应:这不是错误地址,请稍后再调用。- 每个 IP 地址每秒不超过 10 个请求。
- 地址不会保存在日志中;结果会在缓存中保留 30 天,以便更快响应。
- 没有钱包的网店可以用 API 密钥和订阅使用同一服务。
常见问题
我的代理需要创建账户吗?
不需要。x402 支付就够了:无需注册,无需 API 密钥。
需要什么钱包?
一个在 Base 网络上持有 USDC、能签署 EIP-3009 授权的钱包,例如 x402 库提供的钱包。
发现服务要花钱吗?
不要钱。initialize、tools/list 和发现卡片都是免费的;只有 tools/call 收费,0.02 USDC。
如果数据提供方没有响应怎么办?
不会扣款:响应为 503,并带有 Retry-After,你的授权仍然有效。不要拦截订单:稍后再调用一次。
Copy-paste: pay and call validate_address (MCP)
Python, three steps: read the offer (free), sign EIP-3009, send the same tools/call with params._meta["x402/payment"]. The code reads amount, network and payTo from the live offer: nothing is hard-coded.
Python - MCP
# pip install requests eth-account
import os, time, requests
from eth_account import Account
BASE = "https://kder-ai.com"
ARGS = {"address": {"street": "1600 Amphitheatre Parkway", "city": "Mountain View",
"state": "CA", "zip": "94043", "country": "US"}}
def mcp(method, params):
body = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
return requests.post(f"{BASE}/mcp", json=body, timeout=30).json()
# 1) FREE: read the offer. An unpaid tools/call returns result.isError + structuredContent.
offer = mcp("tools/call", {"name": "validate_address", "arguments": ARGS})["result"]["structuredContent"]
accept = offer["accepts"][0]
# 2) SIGN: EIP-3009 TransferWithAuthorization on Base (USDC). Domain comes from the offer.
key = os.environ["EVM_PRIVATE_KEY"]
auth = {"from": Account.from_key(key).address, "to": accept["payTo"],
"value": accept["amount"], "validAfter": int(time.time()) - 60,
"validBefore": int(time.time()) + 240, "nonce": "0x" + os.urandom(32).hex()}
typed = {
"primaryType": "TransferWithAuthorization",
"domain": {"name": accept["extra"]["name"], "version": accept["extra"]["version"],
"chainId": 8453, "verifyingContract": accept["asset"]},
"types": {"EIP712Domain": [{"name": "name", "type": "string"}, {"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"}, {"name": "verifyingContract", "type": "address"}],
"TransferWithAuthorization": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"},
{"name": "value", "type": "uint256"}, {"name": "validAfter", "type": "uint256"},
{"name": "validBefore", "type": "uint256"}, {"name": "nonce", "type": "bytes32"}]},
"message": {"from": auth["from"], "to": auth["to"], "value": int(auth["value"]),
"validAfter": auth["validAfter"], "validBefore": auth["validBefore"], "nonce": auth["nonce"]},
}
signature = Account.sign_typed_data(key, full_message=typed).signature
payment = {"x402Version": 2, "resource": offer["resource"], "accepted": accept,
"payload": {"signature": "0x" + signature.hex().removeprefix("0x"),
"authorization": auth}}
# 3) PAY + CALL: the same tools/call, with the payment in params._meta.
paid = mcp("tools/call", {"name": "validate_address", "arguments": ARGS,
"_meta": {"x402/payment": payment}})["result"]
print(paid["structuredContent"]) # validated address; receipt in paid["_meta"]