get_orderbook
Retrieve real-time order book depth data for specified trading pairs from Binance to analyze market liquidity and price levels.
Instructions
获取订单簿深度数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | 深度限制,默认100 | |
| symbol | Yes | 交易对符号,如 BTCUSDT |
Implementation Reference
- src/tools/market-data.ts:63-89 (handler)The main handler function for the 'get_orderbook' tool. It validates the input using GetOrderBookSchema, fetches the order book data from the Binance client, slices and maps the bids and asks to the requested limit, and returns formatted data with timestamp. Handles errors using handleBinanceError.handler: async (binanceClient: any, args: unknown) => { const input = validateInput(GetOrderBookSchema, args); validateSymbol(input.symbol); try { const orderBook = await binanceClient.book({ symbol: input.symbol, limit: input.limit, }); return { symbol: input.symbol, lastUpdateId: orderBook.lastUpdateId, bids: orderBook.bids.slice(0, input.limit).map((bid: any) => ({ price: bid.price, quantity: bid.quantity, })), asks: orderBook.asks.slice(0, input.limit).map((ask: any) => ({ price: ask.price, quantity: ask.quantity, })), timestamp: Date.now(), }; } catch (error) { handleBinanceError(error); } },
- src/types/mcp.ts:7-10 (schema)Zod schema definition for GetOrderBook input validation, used in the handler to parse and validate arguments: requires symbol (string), optional limit (number, default 100).export const GetOrderBookSchema = z.object({ symbol: z.string().describe('交易对符号,如 BTCUSDT'), limit: z.number().optional().default(100).describe('深度限制,默认100'), });
- src/tools/market-data.ts:45-90 (registration)Tool registration within marketDataTools array: defines name 'get_orderbook', description, JSON inputSchema compatible with MCP, and references the handler function.{ name: 'get_orderbook', description: '获取订单簿深度数据', inputSchema: { type: 'object', properties: { symbol: { type: 'string', description: '交易对符号,如 BTCUSDT', }, limit: { type: 'number', description: '深度限制,默认100', default: 100, }, }, required: ['symbol'], }, handler: async (binanceClient: any, args: unknown) => { const input = validateInput(GetOrderBookSchema, args); validateSymbol(input.symbol); try { const orderBook = await binanceClient.book({ symbol: input.symbol, limit: input.limit, }); return { symbol: input.symbol, lastUpdateId: orderBook.lastUpdateId, bids: orderBook.bids.slice(0, input.limit).map((bid: any) => ({ price: bid.price, quantity: bid.quantity, })), asks: orderBook.asks.slice(0, input.limit).map((ask: any) => ({ price: ask.price, quantity: ask.quantity, })), timestamp: Date.now(), }; } catch (error) { handleBinanceError(error); } }, },