为 AI 代理接入 x402 支付能力,实现预算控制、支出管理与非托管钱包支付。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "agent-payment-x402" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/docs/zh-CN/skills/agent-payment-x402/SKILL.md 2. 保存为 ~/.claude/skills/agent-payment-x402/SKILL.md 3. 装好后重载技能,告诉我可以用了
帮我为一个 AI 代理设计 x402 支付接入方案:每个任务预算上限 5 美元,超出需中止并记录原因;使用非托管钱包支付第三方 API 费用。请给出配置思路、控制流程和错误处理建议。
一份代理支付接入方案,包含预算限制、支付流程、停止条件与异常处理建议。
我有一个会调用搜索 API、数据 API 和其他代理的 AI 工作流。请用 x402 设计支出控制策略,要求按服务分别计费、记录每次支付,并在总预算达到 20 美元时停止后续调用。
按服务分账的支付控制方案,包含记账规则、总预算阈值和终止逻辑。
请说明如何把非托管钱包接入我的 AI 代理工具链,使代理在调用付费 MCP 工具或外部服务时能自动完成 x402 支付,同时保留人工审核开关。
一套钱包接入与支付授权设计,说明自动支付与人工审批如何配合。
让AI代理能够自主支付并内置消费控制。使用x402 HTTP支付协议和MCP工具,使代理能够为外部服务、API或其他代理支付,无需托管风险。
适用于:代理需要支付API调用、购买服务、与其他代理结算、强制执行每项任务消费限额,或管理非托管钱包。与成本感知LLM流水线和安全审查技能自然搭配。
x402将HTTP 402(需要付款)扩展为机器可协商的流程。当服务器返回402时,代理的支付工具会自动协商价格、检查预算、签署交易并重试——无需人工干预。
每次支付工具调用都会强制执行SpendingPolicy:
代理通过ERC-4337智能账户持有自己的密钥。编排器在委托前设置策略;代理只能在限定范围内支出。无资金池,无托管风险。
支付层暴露标准MCP工具,可无缝接入任何Claude Code或代理框架设置。
安全提示:务必锁定包版本。此工具管理私钥——未锁定的
npx安装会引入供应链风险。
{
"mcpServers": {
"agentpay": {
"command": "npx",
"args": ["[email protected]"]
}
}
}
| 工具 | 用途 |
|---|---|
get_balance | 检查代理钱包余额 |
send_payment | 向地址或ENS发送付款 |
check_spending | 查询剩余预算 |
list_transactions | 所有付款的审计追踪 |
注意:消费策略由编排器在委托给代理之前设置——而非代理本身。这防止代理自行提高消费限额。通过编排层或任务前钩子中的
set_policy配置策略,切勿将其作为代理可调用工具。
在构建调用agentpay MCP服务器的编排器时,在分派付费工具调用前强制执行预算。
前提条件:在添加MCP配置前安装包——
npx不带-y会在非交互环境中提示确认,导致服务器挂起:npm install -g [email protected]
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
async function main() {
// 1. Validate credentials before constructing the transport.
// A missing key must fail immediately — never let the subprocess start without auth.
const walletKey = process.env.WALLET_PRIVATE_KEY;
if (!walletKey) {
throw new Error("WALLET_PRIVATE_KEY is not set — refusing to start payment server");
}
// Connect to the agentpay MCP server via stdio transport.
// Whitelist only the env vars the server needs — never forward all of process.env
// to a third-party subprocess that manages private keys.
const transport = new StdioClientTransport({
command: "npx",
args: ["[email protected]"],
env: {
PATH: process.env.PATH ?? "",
NODE_ENV: process.env.NODE_ENV ?? "production",
WALLET_PRIVATE_KEY: walletKey,
},
});
const agentpay = new Client({ name: "orchestrator", version: "1.0.0" });
await agentpay.connect(transport);
// 2. Set spending policy before delegating to the agent.
// Always verify success — a silent failure means no controls are active.
const policyResult = await agentpay.callTool({
name: "set_policy",
arguments: {
per_task_budget: 0.50,
per_session_budget: 5.00,
allowlisted_recipients: ["api.example.com"],
},
});
if (policyResult.isError) {
throw new Error(
`Failed to set spending policy — do not delegate: ${JSON.stringify(policyResult.content)}`
);
}
// 3. Use preToolCheck before any paid action
await preToolCheck(agentpay, 0.01);
}
// Pre-tool hook: fail-closed budget enforcement with four distinct error paths.
async function preToolCheck(agentpay: Client, apiCost: number): Promise<void> {
// Path 1: Reject invalid input (NaN/Infinity bypass the < comparison)
if (!Number.isFinite(apiCost) || apiCost < 0) {
throw new Error(`Invalid apiCost: ${apiCost} — action blocked`);
}
// Path 2: Transport/connectivity failure
let result;
try {
result = await agentpay.callTool({ name: "check_spending" });
} catch (err) {
throw new Error(`Payment service unreachable — action blocked: ${err}`);
}
// Path 3: Tool returned an error (e.g., auth failure, wallet not initialised)
if (result.isError) {
throw new Error(
`check_spending failed — action blocked: ${JSON.stringify(result.content)}`
);
}
// Path 4: Parse and validate the response shape
let remaining: number;
try {
const parsed = JSON.parse(
(result.content as Array<{ text: string }>)[0].text
);
if (!Number.isFinite(parsed?.remaining)) {
throw new TypeError("missing or non-finite 'remaining' field");
}
…
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
为 AI 代理提供支付、密钥托管与额度治理,确保在授权范围内安全执行操作。