x402 MCP Gateway
A gateway that forwards tool calls to downstream MCP servers, handles x402 payment challenges with automatic wallet payment, and supports compliance checks.
README
x402 MCP Gateway
x402 MCP Gateway 是一个 MCP 网关组件。
它作为上游 Agent 的单一入口,将工具调用转发到下游 MCP,并在下游出现 x402 收费挑战时由网关钱包自动支付。
若配置了 Agentry Dashboard 合规接口,还会在支付前执行 KYC/KYT;若下游在挑战中携带 AP2 CartMandate,会额外走 保障验签与预算校验。
当前模式
当前示例仅支持 Command(stdio)模式,用于在本地运行并被 Cursor 等 Agent 通过命令拉起。
不对外提供 HTTP endpoint(如 /mcp、/sse)。
当前能力
- 多下游注册(一个或多个 endpoint)
- 下游工具转发调用(
call_service_tool) - 自动支付重试(网关钱包代付)
- 支付前合规闸门(KYC/KYT,需配置 Dashboard;未配置时默认拒绝支付)
- 可选 AP2 保障链(
extra.cartMandate存在时启用) - 支付相关结果可异步上报 Dashboard 交易记录
- 下游连接支持两种 transport:
- Streamable HTTP
- SSE(仅用于连接下游)
- 自动连接策略:先 Streamable HTTP,失败回退 SSE
- 启动连接重试(默认 3 次,可配置)
对外工具
list_gateway_services
列出当前已连接下游服务:
serviceIdurltransporttools
call_service_tool
通过网关调用下游工具:
- 输入:
serviceId(可选,默认service-1)toolName(必填)args(可选)
- 返回为 JSON 信封(
ok、stage、code、message、traceId、detail),detail中除下游内容外还包含例如paymentMade、paymentResponse、complianceDecision、assuranceResult、paymentContext等字段,便于 Agent 结构化解析。
环境变量(推荐直接写在 Cursor MCP 配置)
你可以使用本仓库根目录的 .env 文件,也可以直接在 Cursor 的 mcp.json 里写 env。
对于「私钥只保留在本机配置」的场景,推荐直接写在 mcp.json 的 env 中。
必填
EVM_PRIVATE_KEY=0x...
DOWNSTREAM_MCP_URLS=https://vendor.example/mcp,http://localhost:4022/mcp
或使用单地址:
DOWNSTREAM_MCP_URL=https://vendor.example/mcp
下游连接(可选)
DOWNSTREAM_MCP_TRANSPORT=auto
DOWNSTREAM_CONNECT_RETRIES=3
Dashboard 合规与记账(可选但强烈建议生产开启)
未同时配置 COMPLIANCE_BASE_URL 与 COMPLIANCE_API_KEY 时,网关会对所有支付请求 直接拒绝(fail-close)。
COMPLIANCE_BASE_URL=https://your-dashboard.example.com
COMPLIANCE_API_KEY=agt_...
COMPLIANCE_CHAIN=base
COMPLIANCE_CHAIN:KYT 等接口使用的链标识,默认base。- 交易异步上报与 AP2 验签与上述两项共用同一
baseUrl/apiKey,无需额外变量。
字段说明汇总
EVM_PRIVATE_KEY:网关支付钱包私钥(须0x前缀)DOWNSTREAM_MCP_URLS:多个下游地址(逗号分隔)DOWNSTREAM_MCP_URL:单下游地址(URLS未配置时使用)DOWNSTREAM_MCP_TRANSPORT:auto(默认,先 Streamable HTTP 再 SSE)streamable-httpsse
DOWNSTREAM_CONNECT_RETRIES:下游连接重试次数(默认3,范围1-10)
HTTP 接口字段与示例见仓库内 api-integration-guide.md。
启动方式
在本仓库根目录执行(请将 cd 路径换为你本机克隆位置):
cd /path/to/mcp-gateway
pnpm install
pnpm dev
生产或本地验证构建产物:
pnpm run build
pnpm start
Cursor 接入(Command + env)
在 Cursor MCP 配置中,将 --dir 后的路径改为 本仓库根目录的绝对路径:
{
"mcpServers": {
"gateway-mcp": {
"command": "pnpm",
"args": ["--dir", "/path/to/mcp-gateway", "dev"],
"env": {
"EVM_PRIVATE_KEY": "0x你的私钥",
"DOWNSTREAM_MCP_URLS": "https://vendor.example/mcp,http://localhost:4022/mcp",
"DOWNSTREAM_MCP_TRANSPORT": "auto",
"DOWNSTREAM_CONNECT_RETRIES": "3",
"COMPLIANCE_BASE_URL": "https://your-dashboard.example.com",
"COMPLIANCE_API_KEY": "agt_你的密钥"
}
}
}
}
说明:
command/args用于拉起本地网关进程。env中的变量会注入该进程,等价于运行命令前设置环境变量。- 适合在本机
mcp.json中管理私钥与 Dashboard API Key。
对外分发(npm 包方式)
若希望他人不依赖你的本地仓库路径,可发布为 npm CLI 包(package.json 中已配置 bin:x402-mcp-gateway)。
- 在本仓库根目录执行打包自检:
cd /path/to/mcp-gateway
pnpm install
pnpm lint:check
pnpm exec tsc --noEmit
pnpm pack
- 验证无误后发布:
npm publish
建议发布前把
package.json中的name改成你自己的 npm scope(例如@your-scope/x402-mcp-gateway),避免包名冲突。
发给别人什么
最少发这三项信息即可:
- npm 包名(例如
@your-scope/x402-mcp-gateway) - 可直接复制的 Cursor
mcp.json配置 - 必填环境变量说明(
EVM_PRIVATE_KEY、DOWNSTREAM_MCP_URL或DOWNSTREAM_MCP_URLS;若需自动支付通过合规闸门,还需 Dashboard 两项)
发布后 Cursor 配置模板示例:
{
"mcpServers": {
"gateway-mcp": {
"command": "npx",
"args": ["-y", "@your-scope/x402-mcp-gateway"],
"env": {
"EVM_PRIVATE_KEY": "0x你的私钥",
"DOWNSTREAM_MCP_URLS": "https://vendor.example/mcp,http://localhost:4022/mcp",
"DOWNSTREAM_MCP_TRANSPORT": "auto",
"DOWNSTREAM_CONNECT_RETRIES": "3"
}
}
}
}
常见问题
1) tsx: command not found
说明依赖未安装。在本仓库根目录执行:
pnpm install
2) 启动时报 Failed to connect downstream
通常是下游不可达或 transport 不匹配:
- 先验证下游 URL 是否可访问
- 使用
DOWNSTREAM_MCP_TRANSPORT=auto - 适当提高
DOWNSTREAM_CONNECT_RETRIES
3) Cursor 里显示 MCP 连接失败
重点检查:
command是否可执行(先在终端里手动跑一遍同样命令)args里--dir是否指向本仓库根目录env.EVM_PRIVATE_KEY是否为0x开头私钥env.DOWNSTREAM_MCP_URLS(或DOWNSTREAM_MCP_URL)是否可访问
4) 日志提示合规配置不完整、支付全部被拒绝
需要同时配置 COMPLIANCE_BASE_URL 与 COMPLIANCE_API_KEY,且 Dashboard 可访问;否则网关按设计拒绝代付。
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.