关于 Defi Copilot
Defi Copilot 是面向 AI Agent 的 Stellar DeFi 情报服务,提供收益与兑换建议,以及钱包和协议操作。其 MCP 工具按请求处理 x402 付款,并可执行受支持的 Stellar 与 Blend 操作。
主要功能
- 01比较 Blend Finance 收益策略
- 02比较 Stellar 兑换路线
- 03执行兑换及 Blend 存取款
- 04查看持仓、行情与交易记录
- 05管理 Stellar 钱包、转账和网络
适用场景
- 01根据 Blend Finance 实时数据寻找收益策略
- 02兑换 Stellar 资产前比较路线
- 03向 Blend Finance 存款或取款
- 04查看钱包活动与市场状况
MCP 相关
MCP 服务向 MCP 客户端提供 DeFi 查询、兑换、钱包和 Blend Finance 工具,并为付费查询自动处理 x402 付款。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
13收录的工具列表 · 更新于 3 周前
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| network | enum | 是 | 要切换到的网络testnet | mainnet |
详细介绍
DeFi Copilot
为 Stellar 上的 AI Agent 提供按决策付费的 DeFi 智能服务。基于 x402 protocol 构建——Agent 购买决策,而非订阅。
概述
DeFi Copilot 是一个原生支持 Stellar 的 DeFi 智能层,允许 AI Agent 自主查询 DeFi 数据、执行兑换并与 DeFi 协议交互,通过 x402 protocol 按请求付费。无需 API 密钥。无需订阅。无需人工审批。
两种使用方式:
- MCP 服务 — 安装到 Claude Desktop 或任何兼容 MCP 的 AI 中,Agent 可自动查询并付款
- REST API — 使用 x402 付款协议的标准 HTTP 接口,适用于任何 HTTP 客户端
在线后端: https://api.fxjrin.com
工作原理
User / AI Agent
|
| (natural language or direct API call)
v
MCP Server (defi-copilot-mcp)
|
| GET /defi/yield/best
v
Backend API (port 3000)
|
| 402 Payment Required
v
Agent builds + signs Stellar USDC tx
|
| x-payment header
v
Facilitator verifies + settles (port 4022)
|
| on-chain settlement on Stellar
v
Return DeFi intelligence
项目结构
defi-copilot/
src/
server.ts # Express server
routes/
yield.ts # GET /defi/yield/best
swap.ts # GET /defi/swap/best
services/
scorer.ts # Market data + Blend on-chain APY + DEX quotes
utils/
x402-middleware.ts # x402 payment verification
network.ts # Network config (testnet/mainnet)
http.ts # IPv4-forced HTTP client (undici)
init.ts # Server wallet setup script
facilitator/
index.ts # Local x402 facilitator (port 4022)
agent/
client.ts # Agent payment client with auto-create wallet
demo.ts # Demo script
mcp-server/
src/
index.ts # MCP tools + auto-create wallet
network.ts # Network config for MCP
http.ts # IPv4-forced HTTP client (undici)
前置条件
- Node.js v18+
- npm v9+
- Git
安装
1. 克隆仓库
git clone https://github.com/fxjrin/defi-copilot.git
cd defi-copilot
2. 安装后端依赖
npm install
3. 安装 MCP 服务依赖
cd mcp-server
npm install
cd ..
配置
后端 .env
运行 npm run init 会自动生成 .env 文件。如果你想手动设置,请在 defi-copilot/ 根目录下创建 .env:
# Stellar network: testnet or mainnet
STELLAR_NETWORK=testnet
# Server wallet — receives x402 payments
# Generated automatically by: npm run init
SERVER_SECRET_KEY=
SERVER_PUBLIC_KEY=
PORT=3000
快速开始
步骤 1 — 初始化服务钱包
运行设置脚本,自动生成并为服务钱包充值:
npm run init
此操作将:
- 为服务生成新的 Stellar 密钥对
- 通过 Friendbot 充值 XLM(仅限测试网)
- 设置 USDC 信任线
- 将凭据保存到
.env
预期输出:
DeFi Copilot — Initial Setup
Network: testnet
Generating server wallet...
Public key : GXXXX...
Secret key : SXXX*...XXXX (saved to .env)
Credentials saved to .env
Funding via Friendbot...
XLM funded successfully
Waiting for account activation...
Setting up USDC trustline...
USDC trustline created
Final balances:
USDC: 0.0000
XLM: 10000.0000
Setup complete!
Run: npm start
步骤 2 — 启动服务
npm start
此操作会同时启动 API 服务(端口 3000)和 x402 facilitator(端口 4022)。
预期输出:
[api] 🚀 DeFi Copilot API running on port 3000
[api] Network : testnet
[api] Wallet : GXXXX...
[facilitator] ⚡ Local x402 Facilitator running on port 4022
步骤 3 — 运行演示
确保 npm start 正在另一个终端中运行,然后:
npm run demo
演示 Agent 会自动创建自己的钱包,为其充值 USDC,并自动付款。
预期输出:
DeFi Copilot Agent Demo
Initializing agent wallet...
No wallet found — creating new agent wallet on Stellar testnet...
Funding with XLM via Friendbot...
XLM funded successfully
Setting up USDC trustline...
Swapping XLM to USDC...
USDC funded successfully
Agent wallet ready: GXXXX...
DEMO 1: Where should I put my USDC?
Agent requesting: http://localhost:3000/defi/yield/best
Payment required: 0.0010 USDC
Payment accepted!
Strategy : Deposit to Fixed V2 on Blend Finance
APY : 9.47% (live on-chain)
Risk : low
Confidence : 95%
DEMO 2: Best route to swap 500 XLM to USDC?
Agent requesting: http://localhost:3000/defi/swap/best
Payment accepted!
Route : SDEX Orderbook
Expected output: 75.88 USDC
Slippage : 0.005%
Confidence : 99%
API 参考
GET /health
免费。返回服务状态。
{
"status": "ok",
"network": "testnet",
"server_wallet": "GXXXX..."
}
GET /defi/yield/best
费用:通过 x402 支付 0.001 USDC
查询参数:
asset—USDC或XLM(默认值:USDC)amount— 数值(默认值:1000)
响应:
{
"strategy": "Deposit to Fixed V2 on Blend Finance",
"protocol": "Blend Finance",
"apy": "9.47%",
"risk": "low",
"reasoning": "Live Blend Finance on-chain APY. Pool comparison: Fixed V2: USDC 9.47% | YieldBlox V2: USDC 0.07%",
"confidence": 0.95,
"marketData": {
"xlmPrice": "0.1518 USDC",
"spread": "0.3945%",
"volume24h": "4531.6K XLM",
"source": "Blend Finance (on-chain Soroban) + Stellar mainnet DEX"
},
"alternatives": [...]
}
GET /defi/swap/best
费用:通过 x402 支付 0.001 USDC
查询参数:
from— 源资产(默认值:XLM)to— 目标资产(默认值:USDC)amount— 数值(默认值:100)
响应:
{
"from": "XLM",
"to": "USDC",
"amount": 500,
"route": ["SDEX Orderbook"],
"expectedOutput": "75.88 USDC",
"slippage": "0.005%",
"priceImpact": "low",
"marketPrice": "1 XLM = 0.1518 USDC",
"confidence": 0.99,
"reasoning": "DEX comparison: SDEX Orderbook: 75.88 USDC | Stellar DEX: 75.80 USDC | Stellar AMM: 75.70 USDC"
}
x402 付款流程
1. Client → GET /defi/yield/best
2. Server ← 402 Payment Required
{
"x402Version": 1,
"accepts": [{
"scheme": "exact",
"network": "stellar-testnet",
"maxAmountRequired": "10000",
"asset": "CBIELTK6...",
"payTo": "GXXXX..."
}]
}
3. Client builds + signs Stellar USDC payment tx
4. Client → GET /defi/yield/best
x-payment: <base64-encoded-payload>
5. Facilitator verifies transaction
6. Facilitator settles on Stellar
7. Server → 200 OK with DeFi intelligence
MCP 服务
MCP 服务允许 AI Agent(Claude、Cursor 等)在对话中直接使用 DeFi Copilot 工具,并自动处理付款。
通过 Claude Code 安装
claude mcp add defi-copilot -- npx -y defi-copilot-mcp
通过 Claude Desktop 配置安装
将以下内容添加到你的 claude_desktop_config.json:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
Linux: ~/.config/Claude/claude_desktop_config.json
{
"mcpServers": {
"defi-copilot": {
"command": "npx",
"args": ["-y", "defi-copilot-mcp"],
"env": {
"STELLAR_NETWORK": "testnet"
}
}
}
}
注意: DEFI_COPILOT_API 默认值为 https://api.fxjrin.com——除非你运行自己的后端,否则无需设置。
可用工具
| 工具 | 费用 | 描述 |
|---|---|---|
get_best_yield | 0.001 USDC | 最佳收益策略 — 来自 Blend Finance 链上(Soroban)的实时数据 |
get_best_swap | 0.001 USDC | 最优兑换路径 — 比较 SDEX Orderbook、Stellar DEX 和 AMM |
execute_swap | 免费 | 在 Stellar DEX 上执行真实兑换 |
deposit_blend | 免费 | 向 Blend Finance 存入 USDC 以赚取收益(主网) |
withdraw_blend | 免费 | 从 Blend Finance 提取 USDC 和利息(主网) |
get_blend_position | 免费 | 查看你的 Blend Finance 存款及已赚取的利息(主网) |
transfer | 免费 | 向任意 Stellar 地址发送 XLM 或 USDC |
get_market_data | 免费 | 来自 Stellar 主网 DEX 的实时 XLM 价格、交易量和价差 |
get_agent_wallet | 免费 | 查看或创建 Agent 钱包 |
fund_wallet | 免费 | 显示用于充值的钱包地址和二维码 URL |
set_network | 免费 | 在测试网和主网之间切换 |
get_transaction_history | 免费 | 查看近期钱包交易记录 |
get_api_info | 免费 | API 信息和定价 |
Agent 钱包行为
首次使用时,MCP 服务会自动:
- 生成新的 Stellar 密钥对
- 通过 Friendbot 为钱包充值 XLM(仅限测试网)
- 设置 USDC 信任线
- 将 50 XLM 兑换为 USDC,以便进行支付
- 将钱包保存到
~/.defi-copilot/wallet-testnet.json
每位用户的钱包都存储在其自己的机器本地。私钥绝不会离开用户的设备。
在主网上,钱包会创建,但不会自动充值 — 用户必须手动存入 XLM。这是有意为之:主网使用真实资金。
网络切换
在对话中切换网络:
User: "Switch to mainnet"
Claude: calls set_network(network="mainnet")
User: "Deposit 10 USDC to Blend Finance"
Claude: calls deposit_blend(amount=10, confirmed=true)
也可以通过环境变量设置(在多个会话之间保持生效):
"env": {
"STELLAR_NETWORK": "mainnet"
}
数据来源
| 数据 | 来源 | 网络 |
|---|---|---|
| 收益 APY | Blend Finance 智能合约(Soroban) | 主网(链上) |
| 兑换报价 | SDEX Orderbook + Stellar DEX 路径查找器 + AMM 池 | 主网(真实报价) |
| XLM 价格 | Stellar Horizon 交易汇总 | 主网(真实价格) |
| 订单簿价差 | Stellar Horizon 订单簿 | 主网 |
| 24 小时交易量 | Stellar Horizon 交易汇总 | 主网 |
| 支付 | x402 协议 | 测试网或主网 |
无论支付使用哪个网络,所有价格和收益数据都直接来自 Stellar 主网,以确保准确性。
环境变量
后端(defi-copilot/.env)
| 变量 | 必填 | 描述 |
|---|---|---|
STELLAR_NETWORK | 否 | testnet 或 mainnet(默认:testnet) |
SERVER_SECRET_KEY | 是 | 服务端钱包的私钥(用于接收付款) |
SERVER_PUBLIC_KEY | 是 | 服务端钱包的公钥 |
PORT | 否 | 服务端端口(默认:3000) |
MCP 服务(通过配置或 shell 环境变量)
| 变量 | 必填 | 描述 |
|---|---|---|
STELLAR_NETWORK | 否 | testnet 或 mainnet(默认:testnet) |
DEFI_COPILOT_API | 否 | 后端 API URL(默认:https://api.fxjrin.com) |
npm 脚本
后端(defi-copilot/)
npm run init # Generate and fund server wallet
npm start # Start API + facilitator together
npm run dev # Start API only (port 3000)
npm run facilitator # Start facilitator only (port 4022)
npm run demo # Run agent payment demo
npm run build # Compile TypeScript
MCP 服务(defi-copilot/mcp-server/)
npm run build # Compile TypeScript
npm run dev # Run without compiling (tsx)
部署
在 VPS 上部署后端
git clone https://github.com/fxjrin/defi-copilot.git
cd defi-copilot
npm install
# Initialize server wallet
npm run init
# Install pm2 for process management
npm install -g pm2
# Start both api and facilitator
pm2 start npm --name defi-copilot -- start
pm2 save
pm2 startup
部署后,建议使用 nginx 反向代理,并通过 certbot 配置 SSL:
apt install -y nginx certbot python3-certbot-nginx
# configure nginx to proxy port 3000
certbot --nginx -d your-domain.com
许可证
MIT
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。