x402-defillama-mcp

x402-defillama-mcp

An x402 pay-per-call proxy for DefiLlama's MCP tools, allowing AI agents to pay USDC per tool call (up to 10 tools) without needing a DefiLlama account or subscription.

Category
访问服务器

README

x402-defillama-mcp

An x402 pay-per-call proxy in front of DefiLlama's agent MCP. An AI agent with a funded Base wallet uses DeFi data with no account, no subscription, no browser login — it pays USDC per tool call. Proxies 10 of DefiLlama's 23 MCP tools as a proof-of-concept.

Verified end-to-end on Base mainnet — real USDC settlement, on-chain.

Problem

DefiLlama runs a live 23-tool agent MCP (mcp.defillama.com/mcp). Today an agent can only use it with a DefiLlama account + active API subscription + browser login. An autonomous agent with a wallet but no account can't onboard. x402 (HTTP 402 + EIP-3009 USDC settlement) fixes exactly this: pay-per-call, no account. This repo proves the flow, and proposes it as an alternative auth to the existing API-key/credit rail — dual-auth, not a replacement.

Demo

demo

The recording is a deterministic replay of a real Base-mainnet run (demo/demo.tape, regenerate with vhs demo.tape). The settlement it shows is a real on-chain transaction — see below.

Verify it yourself

Three independent ways to check this is real, not a mock:

  1. On-chain proof. The settlement tx is on Base mainnet: 0xe58ae46d…abe7. status: success · USDC transfer payer → payTo, 0.001 USDC · gas paid by the facilitator (the payer's ETH is untouched — EIP-3009 is gasless for the payer). from / to / amount / status match the demo exactly.

  2. Run it live against your own wallet. The video is a replay; here's the live path:

    git clone <repo> && cd x402-defillama-mcp && npm i
    cp .env.example .env
    npm run wallets:gen                 # generates burners; or paste your own
    # set PAY_TO_ADDRESS = your treasury, CLIENT_PRIVATE_KEY = your funded burner in .env
    # fund the client with a little USDC on Base (mainnet) or Base Sepolia (testnet)
    npm run dev                         # start the proxy
    npm run demo:script                 # agent pays per call → gets data → prints the settlement tx
    

    Testnet (Base Sepolia, NETWORK=base-sepolia, public facilitator, no keys) proves the identical mechanism with zero mainnet spend.

  3. Real data. The figures returned are live DefiLlama data — e.g. Binance CEX ~$137.7B TVL — independently checkable on defillama.com. The proxy is a pass-through to DefiLlama's REST API, not a mock.

Architecture

flowchart LR
  A["AI agent<br/>(MCP client + Base wallet)"] -- "MCP tools/call" --> P["x402-defillama-mcp<br/>/mcp"]
  P -- "rail A · no key" --> X["402 + accepts[] → agent signs EIP-3009<br/>→ facilitator verifies + settles USDC on Base"]
  X -- "settled" --> P
  P -- "rail B · DefiLlama API-key header" --> S["skip 402 → serve via pro backend<br/>(existing subscription rail)"]
  P -- "resolves tool" --> D["DefiLlama REST<br/>api.llama.fi | pro-api.llama.fi/{KEY}"]
  • Rail A (x402 pay-per-call): built on Coinbase's @x402/mcp (createPaymentWrapper on the MCP SDK). No payment → in-protocol 402 with an accepts[] (scheme exact, network, USDC, payTo, amount = the tool's price). The agent signs EIP-3009 transferWithAuthorization; the facilitator verifies + settles on-chain, then the tool runs. Facilitator = public facilitator on testnet (no keys); Coinbase CDP facilitator on mainnet.
  • Rail B (dual-auth): a request carrying a DefiLlama API-key header skips the 402 and serves via the pro backend with that key. One server, two rails.
  • Data-driven registry: each tool is one declarative row (name → endpoint → price → params); adding a tool = adding a row. The remaining 13 of DefiLlama's 23 tools drop in as rows (some need the Pro backend).
  • Discovery: /.well-known/x402 (advertised networks) + /catalog (tools, prices, params).
  • Price-sanity guard: the server refuses to start if any per-call price exceeds a ceiling (default $0.05) — protects against a decimals/units bug.

Run (2-minute quickstart)

npm i
cp .env.example .env
npm run wallets:gen        # fresh burners; funds/keys stay in .env (gitignored)
npm run dev                # proxy on http://localhost:4021  (POST /mcp)

No-spend checks: npm run backend:check (data layer), npm run smoke (MCP + the 402 gate), npm run dualauth-check (API-key rail skips the 402).

Config (.env): NETWORK (base-sepolia|base) · PAY_TO_ADDRESS · FACILITATOR (public|cdp) · DATA_BACKEND (free|pro) · PRICE_MODE (tiered|flat) · ALLOW_APIKEY_PASSTHROUGH · SESSION_TOKEN_MODE.

Production mapping

This is a PoC of a feature that would live in DefiLlama's own stack, not a data reseller:

  • payTo = DefiLlama's treasury. In the demo it's a burner; in production the settlement USDC lands in your wallet.
  • Backend is a one-env swap. The demo runs on the free public API to stay costless; in production DATA_BACKEND=pro points at your own gated Pro data over the identical path (pro-api.llama.fi/{KEY}/…). The x402 payment layer is byte-identical either way.
  • Dual-auth = additive. x402 is an alternative auth to the API-key/credit flow, so existing keys keep working while agents onboard without a subscription. Nothing is removed.

Design decisions / FAQ

  • Why a demo, not a PR? DefiLlama's MCP server and Pro-API billing are closed-source — there's no public file to patch. The credible move is a working proxy + the dual-auth recommendation.
  • Doesn't this resell DefiLlama's data? No. It's a PoC of DefiLlama's own future feature; in production it fronts their gated data and the settlement goes to their treasury. The open free API stays open.
  • Pay-per-call vs session tokens. Per-call is the demo (clearest: pay each call, no subscription). x402 V2 reusable session tokens (amortize per-call overhead) are scaffolded behind SESSION_TOKEN_MODE, default off.
  • Multi-chain / CCTP. On mainnet the accepts[] can advertise Base + other chains so an agent pays where its treasury sits. Consolidating multi-chain revenue into one treasury is where CCTP would slot in as an optional sweep — noted, not built (Base-only needs none).

Extensions

Payments are rail-agnostic (src/payments/provider.ts) — a second rail drops in behind the same interface. src/payments/mpp.ts (Machine Payments Protocol — Stripe + Tempo's open, IETF-proposed agent-payment standard) is stubbed for now to keep this PoC's single thesis (x402 on the MCP) focused — not for any technical barrier. MPP is open (Apache-2.0/MIT, implementable via the mppx SDK; only Stripe's optional fiat/card path is gated, not the crypto rail) and a planned fast-follow — adding it makes the MCP monetizable on both leading agent-payment rails. Peers are already split across the two: CoinGecko shipped x402, Dune Analytics is on MPP. ACP slots in the same way.

Provenance

x402 API verified against Coinbase's coinbase/x402 examples; @x402/mcp / @x402/core / @x402/evm on the MCP SDK; CDP facilitator via @coinbase/x402. DefiLlama endpoints are live public API. DefiLlama already tracks x402 as a category (dimension-adapters#6181, merged).

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选