加密货币交易API服务

Binance MCP Server提供Binance API访问,用于构建基于AI的加密货币交易代理,支持市场数据获取、账户管理、现货和期货交易以及风险分析。

  • by format37
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 format37/mcp-binance

使用说明

币安MCP服务器

MCP服务器提供Binance API访问,通过模型上下文协议构建AI驱动的加密货币交易代理。

建筑

CSV第一数据设计

所有工具都返回结构化的CSV文件,而不是原始的JSON响应,从而鼓励系统的数据分析工作流程:

  • 输出:每个工具都将结果保存到服务器的CSV文件中 data/mcp-binance/ 文件夹
  • 分析:CSV文件使用 py_eval 预装pandas/numpy的工具
  • 模式:获取数据→ 接收CSV路径→ 用Python分析→ 做决定
  • 益处:促进数据驱动推理,支持复杂的多步骤分析,支持历史跟踪

模块化设计

每个工具都在单独的文件中实现(backend/binance_tools/*.py)通过全面的参数文档,最大限度地减少令牌使用并提高可维护性。

可用工具

市场数据(只读)

  • binance_get_ticker -24小时价格变动统计
  • binance_get_orderbook -当前买卖订单簿
  • binance_get_recent_trades -最近的市场交易
  • binance_get_price -符号的最新价格
  • binance_get_book_ticker -最佳买卖价格
  • binance_get_avg_price -时间窗口内的平均价格

帐户管理(只读)

  • binance_get_account -投资组合余额与USDT估值
  • binance_get_open_orders -当前活跃订单
  • binance_spot_trade_history -使用损益数据执行交易历史
  • binance_portfolio_performance -当前投资组合与基准投资组合的比较

现货交易

  • binance_spot_market_order -执行市场买入/卖出订单
  • binance_spot_limit_order -下达限价订单(GTC/IOC/FOK)
  • binance_spot_oco_order -一个订单取消其他订单(止盈+止损)
  • binance_cancel_order -取消单个或所有订单

期货交易

  • binance_set_futures_leverage -配置符号杠杆
  • binance_manage_futures_positions -打开/关闭/修改期货头寸
  • binance_calculate_liquidation_risk -计算清算价格和风险

分析与风险管理

  • binance_calculate_spot_pnl -带费用跟踪的损益分析
  • trading_notes -保存和检索交易决策/观察结果
  • py_eval -使用pandas/numpy执行Python代码进行数据分析

典型工作流程

# 1. Fetch account data
binance_get_account()
# Returns: "✓ Data saved to CSV\nFile: account_a1b2c3d4.csv..."

# 2. Analyze the CSV file with Python
py_eval("""
import pandas as pd
df = pd.read_csv('data/mcp-binance/account_a1b2c3d4.csv')

# Calculate portfolio metrics
total_value = df['value_usdt'].sum()
top_holdings = df.nlargest(5, 'value_usdt')[['asset', 'value_usdt']]

print(f"Total Portfolio: ${total_value:,.2f}")
print("\nTop 5 Holdings:")
print(top_holdings.to_string(index=False))
""")

# 3. Make data-driven trading decisions based on analysis

设置

需求

  • Docker和Docker Compose
  • 币安API凭证(启用现货和/或期货)

环境配置

创建 .env.local 文件:

# Binance API Credentials
BINANCE-API-KEY=your_api_key_here
BINANCE-API-SECRET=your_api_secret_here

# MCP Configuration
MCP_NAME=binance
MCP_TOKENS=your_secure_token_here
MCP_REQUIRE_AUTH=true
PORT=8010

# Optional: Sentry error tracking
SENTRY_DSN=your_sentry_dsn
CONTAINER_NAME=mcp-binance-local

部署

# Local development
./compose.local.sh

# Service runs on http://localhost:8010/binance/

安全说明

  • API权限:配置具有适当限制的Binance API密钥
  • 只读工具:所有市场数据和账户信息工具都是只读的
  • 交易工具:在工具说明中明确标记;需要明确的行动
  • 令牌身份验证:生产使用所需(MCP_REGUIR_AUTH=true)
  • 风险管理:执行交易前始终分析数据

CSV数据持久化

所有CSV文件都存储在 data/mcp-binance/ 具有唯一标识符,能够:

  • 历史投资组合跟踪
  • 随时间变化的性能分析
  • 交易决策的审计跟踪
  • 系统化的回溯测试工作流程

工具文档

每个工具都包括全面的内联文档,其中包括:

  • 详细的参数描述和约束
  • 返回具有数据类型的值模式
  • 用例和示例
  • 交易操作风险警告
  • Python分析建议

运行不带参数的工具以查看完整文档。

Claude桌面配置

"binance_local": {
    "command": "npx",
    "args": [
        "mcp-remote",
        "http://localhost:8010/binance/"
    ]
}

Claude代码配置

claude mcp add-json binance_local '{"command":"npx","args":["mcp-remote","http://localhost:8010/binance/"]}'

许可证

MIT许可证-有关详细信息,请参阅许可证文件。