Binance Cryptocurrency Data
STDIOMCP server exposing Binance cryptocurrency exchange data to LLM agents.
MCP server exposing Binance cryptocurrency exchange data to LLM agents.
A Model Context Protocol (MCP) server that exposes Binance cryptocurrency exchange data to Large Language Model (LLM) agents. This server allows LLMs to access real-time and historical market data from Binance through a standardized interface.
mcp
package with CLI tools (mcp[cli]
)requests
library for REST APIwebsockets
library for WebSocket streamsuvicorn
for serving (optional)git clone https://github.com/yourusername/binance_mcp_server.git cd binance_mcp_server
pip install -r requirements.txt
To run the server in standalone mode:
python run_server.py
This will start the MCP server, which will listen for connections via STDIO.
For development and testing, use the MCP Inspector:
mcp dev run_server.py
This opens the MCP Inspector interface where you can test the server's tools interactively.
To use this server with Claude Desktop:
pip install "mcp[cli]"
mcp install run_server.py
An example client script is provided to demonstrate programmatic usage:
python example_client.py
This script connects to the server and retrieves various types of market data.
ping_binance(): Test connectivity to the Binance API server
ping_binance()
get_server_time(): Get the current server time from Binance
get_server_time()
get_price(symbol): Get the current price for a trading pair
get_price(symbol="BTCUSDT")
get_order_book(symbol, depth=10): Get the current order book
get_order_book(symbol="ETHUSDT", depth=5)
get_historical_prices(symbol, interval="1d", limit=100): Get historical OHLCV data
get_historical_prices(symbol="BTCUSDT", interval="1h", limit=24)
get_ui_klines(symbol, interval="1d", limit=100): Get UI-optimized candlestick data
get_ui_klines(symbol="BTCUSDT", interval="1h", limit=24)
get_recent_trades(symbol, limit=20): Get the most recent trades for a symbol
get_recent_trades(symbol="BTCUSDT", limit=50)
get_historical_trades(symbol, limit=20, from_id=None): Get older trades for a symbol
get_historical_trades(symbol="BTCUSDT", limit=100, from_id=12345)
get_aggregate_trades(symbol, limit=20): Get compressed/aggregate trades
get_aggregate_trades(symbol="ETHUSDT", limit=30)
get_24hr_ticker(symbol): Get 24-hour price change statistics
get_24hr_ticker(symbol="BNBUSDT")
get_all_24hr_tickers(): Get 24-hour statistics for all symbols
get_all_24hr_tickers()
get_trading_day_ticker(symbol, type="FULL"): Get trading day price change statistics
get_trading_day_ticker(symbol="BTCUSDT", type="FULL")
get_all_trading_day_tickers(type="FULL"): Get trading day statistics for all symbols
get_all_trading_day_tickers(type="MINI")
get_rolling_window_ticker(symbol, window_size="1d", type="FULL"): Get rolling window price statistics
get_rolling_window_ticker(symbol="BTCUSDT", window_size="4h")
get_all_rolling_window_tickers(window_size="1d", type="FULL"): Get rolling window stats for all symbols
get_all_rolling_window_tickers(window_size="4h", type="MINI")
get_average_price(symbol): Get current average price (5-minute weighted average)
get_average_price(symbol="BTCUSDT")
get_book_ticker(symbol): Get best bid/ask prices and quantities
get_book_ticker(symbol="ETHBTC")
get_all_book_tickers(): Get best bid/ask for all symbols
get_all_book_tickers()
get_exchange_info(): Get comprehensive exchange information including trading rules and symbol list
get_trading_fees(): Get the default trading fee rates (note: for demonstration purposes, returns default public fees)
subscribe_to_trade_stream(symbol): Subscribe to real-time trade events
subscribe_to_trade_stream(symbol="BTCUSDT")
subscribe_to_kline_stream(symbol, interval="1m"): Subscribe to candlestick/kline updates
subscribe_to_kline_stream(symbol="BTCUSDT", interval="5m")
subscribe_to_ticker_stream(symbol): Subscribe to 24hr ticker updates
subscribe_to_ticker_stream(symbol="ETHUSDT")
subscribe_to_book_ticker_stream(symbol): Subscribe to best bid/ask updates
subscribe_to_book_ticker_stream(symbol="BNBUSDT")
subscribe_to_depth_stream(symbol, levels=10): Subscribe to order book updates
subscribe_to_depth_stream(symbol="BTCUSDT", levels=5)
list_active_subscriptions(): List all active WebSocket subscriptions
list_active_subscriptions()
get_latest_stream_data(stream_name): Get the latest data from a stream
get_latest_stream_data(stream_name="btcusdt@trade")
unsubscribe_from_stream(stream_name): Unsubscribe from a stream
unsubscribe_from_stream(stream_name="btcusdt@kline_1m")
cleanup_all_streams(): Close all WebSocket connections and clean up resources
cleanup_all_streams()
binance_mcp_server/
├── binance_api.py # Core REST API interaction module
├── binance_ws_api.py # WebSocket connection management
├── commands/ # MCP command definitions
│ ├── __init__.py
│ ├── market_data.py # Price/order book/historical data commands
│ ├── market_info.py # Exchange info and metadata commands
│ └── websocket_streams.py # WebSocket stream commands
└── server.py # Main MCP server setup and initialization
# Check if Binance API is reachable is_connected = ping_binance() # Get the current server time (milliseconds since epoch) server_time = get_server_time()
# Get the current price of Bitcoin btc_price = get_price(symbol="BTCUSDT") # Get detailed 24-hour statistics btc_stats = get_24hr_ticker(symbol="BTCUSDT") print(f"BTC price change: {btc_stats['priceChangePercent']}%") print(f"BTC 24h volume: {btc_stats['volume']} BTC") # Get rolling window statistics (4-hour window) btc_4h_stats = get_rolling_window_ticker(symbol="BTCUSDT", window_size="4h") print(f"BTC 4h price change: {btc_4h_stats['priceChangePercent']}%")
# Subscribe to real-time trade updates trade_sub = subscribe_to_trade_stream(symbol="BTCUSDT") # After some time, get the latest trade data latest_trade = get_latest_stream_data(stream_name="btcusdt@trade") print(f"Latest trade price: {latest_trade['data']['p']}") # Subscribe to candlestick updates for chart data kline_sub = subscribe_to_kline_stream(symbol="ETHUSDT", interval="5m") # Clean up when done unsubscribe_from_stream(stream_name="btcusdt@trade") unsubscribe_from_stream(stream_name="ethusdt@kline_5m")
For a detailed overview of the implemented and planned API endpoints, refer to the API Implementation Status document.
To add new tools, create appropriate functions in the Binance API module and then register them as MCP tools in one of the command modules.
To support authenticated API calls:
binance_api.py
to include authenticationserver.py
This project is licensed under the MIT License - see the LICENSE file for details.