Skip to main content
Glama
qeinfinity

Binance MCP Server

by qeinfinity

subscribe_market_data

Subscribe to real-time cryptocurrency market data from Binance for trading pairs, including spot and futures markets. Receive live updates on ticker prices, trades, order book depth, candlestick charts, and other market indicators through WebSocket streams.

Instructions

Subscribe to real-time market data updates

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesTrading pair symbol (e.g., BTCUSDT)
typeYesMarket type
streamsYesList of data streams to subscribe to

Implementation Reference

  • Tool handler that validates input parameters using isStreamParams, calls wsManager.subscribe to start WebSocket subscription, sets up a logging callback for stream data, and returns a success message.
    case "subscribe_market_data": { if (!isStreamParams(request.params.arguments)) { throw new Error('Invalid stream parameters'); } const { symbol, type, streams } = request.params.arguments; wsManager.subscribe(symbol, type, streams); // Set up message handler wsManager.onStreamData(symbol, streams[0], (data: StreamEventData) => { // Handle real-time data updates logger.info(`Received WebSocket data for ${symbol}:`, data); }); return { content: [{ type: "text", text: `Successfully subscribed to ${streams.join(", ")} for ${symbol}` }] }; }
  • src/index.ts:136-162 (registration)
    Tool registration in ListToolsRequestHandler, defining name, description, and input schema for validation.
    { name: "subscribe_market_data", description: "Subscribe to real-time market data updates", inputSchema: { type: "object", properties: { symbol: { type: "string", description: "Trading pair symbol (e.g., BTCUSDT)" }, type: { type: "string", enum: ["spot", "futures"], description: "Market type" }, streams: { type: "array", items: { type: "string", enum: ["ticker", "trade", "kline", "depth", "forceOrder", "markPrice", "openInterest"] }, description: "List of data streams to subscribe to" } }, required: ["symbol", "type", "streams"] } }
  • TypeScript interface defining the input parameters for the subscribe_market_data tool.
    export interface StreamParams { symbol: string; type: 'spot' | 'futures'; streams: string[]; }
  • Type guard function used in the handler to validate input parameters match StreamParams.
    export function isStreamParams(params: any): params is StreamParams { return ( typeof params === 'object' && typeof params.symbol === 'string' && (params.type === 'spot' || params.type === 'futures') && Array.isArray(params.streams) ); }
  • Core subscription method in BinanceWebSocketManager that initializes subscription state, sets up callbacks, and triggers WebSocket connection.
    public subscribe(symbol: string, type: 'spot' | 'futures', streams: StreamEventType[]): void { const subscription: StreamSubscription = { symbol, type, streams, reconnectAttempts: 0 }; if (!this.messageCallbacks.has(symbol)) { this.messageCallbacks.set(symbol, new Map()); } const symbolCallbacks = this.messageCallbacks.get(symbol)!; streams.forEach(stream => { if (!symbolCallbacks.has(stream)) { symbolCallbacks.set(stream, []); } }); this.subscriptions.set(symbol, subscription); this.connectWebSocket(subscription); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qeinfinity/binance-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server