返回市场
MCP服务器_bn

MCP服务器_bn

作者:kydlikebtc14 星标更新:2025-03-24

项目介绍

mcp-server-cex-bn

smithery 徽章

此MCP服务器提供了与币安现货和期货交易操作的全面集成。

中文说明

功能

现货交易操作

  • 执行现货交易操作(限价订单/LIMIT、市价订单/MARKET)
  • 监控账户余额
  • 跟踪和管理未完成订单
  • 取消现有订单

期货交易操作

  • 创建各种类型的期货订单(限价订单LIMIT、市价订单MARKET、止损订单STOP、止盈订单TAKE_PROFIT等)
  • 管理杠杆设置(1-125倍)
  • 监控期货仓位和账户信息
  • 跟踪资金费率
  • 支持单向和对冲模式仓位
  • 高级订单类型包括追踪止损和仅减仓订单

工具

API配置

configure_api_keys

安全存储您的币安API凭证:

await configureBinanceApiKeys({
  apiKey: 'your-api-key',
  apiSecret: 'your-api-secret'
});

现货交易工具

create_spot_order

创建限价或市价订单:

// 限价订单
await createSpotOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'LIMIT',
  quantity: '0.001',
  price: '40000'
});

// 市价订单
await createSpotOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'MARKET',
  quantity: '0.001'
});
cancel_order

取消现有订单:

await cancelOrder({
  symbol: 'BTCUSDT',
  orderId: '12345678'
});
get_balances

检查您的账户余额:

const balances = await getBalances();
// 返回:{ BTC: '0.1', USDT: '1000', ... }
get_open_orders

列出所有未完成订单:

const orders = await getOpenOrders({
  symbol: 'BTCUSDT' // 可选:指定符号
});

期货交易工具

create_futures_order

创建各种类型的期货订单:

// 限价订单
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'BUY',
  type: 'LIMIT',
  quantity: '0.001',
  price: '40000',
  timeInForce: 'GTC'
});

// 止损市价订单
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'SELL',
  type: 'STOP_MARKET',
  quantity: '0.001',
  stopPrice: '38000'
});

// 追踪止损市价订单
await createFuturesOrder({
  symbol: 'BTCUSDT',
  side: 'SELL',
  type: 'TRAILING_STOP_MARKET',
  quantity: '0.001',
  callbackRate: '1.0' // 1% 回调率
});
set_futures_leverage

调整交易对的杠杆:

await setFuturesLeverage({
  symbol: 'BTCUSDT',
  leverage: 10  // 1-125倍
});
get_futures_positions

获取所有开放的期货仓位:

const positions = await getFuturesPositions();
get_futures_account

获取详细的期货账户信息:

const account = await getFuturesAccount();
get_funding_rate

获取期货符号的资金费率:

const fundingRate = await getFundingRate({
  symbol: 'BTCUSDT'
});
cancel_futures_order

取消现有的期货订单:

await cancelFuturesOrder({
  symbol: 'BTCUSDT',
  orderId: '12345678'
});

期货交易详情

仓位模式

  • 单向模式:每个符号只有一个仓位
    • 默认模式,更简单的仓位管理
    • 总仓位规模是所有订单之和
  • 对冲模式:独立的多头和空头仓位
    • 允许同时持有多头和空头仓位
    • 每个仓位有独立的保证金要求

保证金类型

  • 隔离保证金:每个仓位固定保证金
    • 风险限制在分配的保证金内
    • 每个仓位有自己的杠杆设置
  • 交叉保证金:仓位间共享保证金
    • 更高的资本效率
    • 所有仓位共享风险

资金费率

永续期货合约使用资金费率来保持期货价格与现货价格一致:

  • 正利率:多头支付空头
  • 负利率:空头支付多头
  • 每8小时支付一次

安全注意事项

现货交易安全

  • 切勿将API密钥提交到版本控制
  • 使用环境变量或安全密钥存储
  • 限制API密钥权限仅用于所需操作
  • 定期轮换您的API密钥

期货交易安全

  • 根据风险承受能力设置适当的杠杆限制
  • 始终使用止损订单以限制潜在损失
  • 仔细监控清算价格
  • 定期检查仓位风险和保证金比率
  • 考虑使用仅减仓订单进行风险管理
  • 小心使用交叉保证金,因为存在共享风险

请求速率限制

  • 遵守币安API请求速率限制
  • 默认请求速率限制:
    • 订单操作每分钟1200次请求
    • 市场数据每秒100次请求
  • 实现适当的错误处理以应对速率限制错误

错误处理

常见错误场景

  • 无效的API凭证
  • 余额不足或保证金不足
  • 无效的订单参数
  • 超过速率限制
  • 网络连接问题

期货特定错误

  • InsufficientMarginError:操作所需的保证金不足
  • InvalidPositionModeError:错误的仓位模式设置
  • OrderValidationError:无效的期货订单参数

示例错误处理:

try {
  await createFuturesOrder({
    symbol: 'BTCUSDT',
    side: 'BUY',
    type: 'LIMIT',
    quantity: '0.001',
    price: '40000',
    timeInForce: 'GTC'
  });
} catch (error) {
  if (error instanceof InsufficientMarginError) {
    console.error('可用保证金不足');
  } else if (error instanceof InvalidPositionModeError) {
    console.error('无效的仓位模式');
  } else if (error instanceof OrderValidationError) {
    console.error('无效的订单参数');
  }
}

项目结构

.
├── src/
│   ├── index.ts                 # 服务器入口点
│   ├── services/
│   │   ├── binance.ts          # 币安API集成
│   │   ├── keystore.ts         # API密钥管理
│   │   └── tools.ts            # 交易工具实现
│   └── types/
│       ├── binance.ts          # 币安类型
│       └── binance-connector.d.ts  # API客户端类型
├── README.md
├── README_CN.md
├── package.json
├── pnpm-lock.yaml
└── tsconfig.json

开发

  1. 设置环境变量:

在根目录创建.env文件,并设置您的币安API凭证:

BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_secret_key_here
  1. 安装依赖:
pnpm install

构建服务器:

pnpm build

开发时自动重建:

pnpm watch

安装

通过Smithery安装

要通过Smithery自动安装Claude Desktop的币安交易服务器:

npx -y @smithery/cli install mcp-server-cex-bn --client claude

手动安装

  1. 克隆仓库
  2. 安装依赖:
pnpm install
  1. .env中配置您的币安API凭证
  2. 构建并启动服务器:
pnpm build
pnpm start

调试

由于MCP服务器通过stdio通信,调试可能具有挑战性。我们推荐使用作为包脚本提供的MCP Inspector

pnpm inspector

Inspector将提供一个URL,以便您可以在浏览器中访问调试工具。