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
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Trading pair symbol (e.g., BTCUSDT) | |
| type | Yes | Market type | |
| streams | Yes | List of data streams to subscribe to |
Implementation Reference
- src/index.ts:251-270 (handler)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"] } }
- src/types/api-types.ts:13-17 (schema)TypeScript interface defining the input parameters for the subscribe_market_data tool.export interface StreamParams { symbol: string; type: 'spot' | 'futures'; streams: string[]; }
- src/types/api-types.ts:49-56 (schema)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) ); }
- src/connectors/binance-ws.ts:44-65 (helper)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); }