get_klines
Retrieve historical candlestick data for specified trading pairs and time intervals from Binance exchange to analyze market trends and price movements.
Instructions
获取K线历史数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| interval | Yes | 时间间隔 | |
| limit | No | 数量限制,默认500 | |
| symbol | Yes | 交易对符号,如 BTCUSDT |
Implementation Reference
- src/tools/market-data.ts:115-147 (handler)The core handler function implementing the get_klines tool logic: validates input, fetches K-line (candlestick) data from Binance API, formats the response with mapped fields, and handles errors.handler: async (binanceClient: any, args: unknown) => { const input = validateInput(GetKlinesSchema, args); validateSymbol(input.symbol); try { const klines = await binanceClient.candles({ symbol: input.symbol, interval: input.interval, limit: input.limit, }); return { symbol: input.symbol, interval: input.interval, data: klines.map((kline: any) => ({ openTime: kline.openTime, open: kline.open, high: kline.high, low: kline.low, close: kline.close, volume: kline.volume, closeTime: kline.closeTime, quoteAssetVolume: kline.quoteAssetVolume, numberOfTrades: kline.numberOfTrades, takerBuyBaseAssetVolume: kline.takerBuyBaseAssetVolume, takerBuyQuoteAssetVolume: kline.takerBuyQuoteAssetVolume, })), timestamp: Date.now(), }; } catch (error) { handleBinanceError(error); } },
- src/tools/market-data.ts:92-148 (registration)Tool registration object within marketDataTools array, defining name, description, inputSchema (JSON Schema for MCP), and handler reference.{ name: 'get_klines', description: '获取K线历史数据', inputSchema: { type: 'object', properties: { symbol: { type: 'string', description: '交易对符号,如 BTCUSDT', }, interval: { type: 'string', enum: ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M'], description: '时间间隔', }, limit: { type: 'number', description: '数量限制,默认500', default: 500, }, }, required: ['symbol', 'interval'], }, handler: async (binanceClient: any, args: unknown) => { const input = validateInput(GetKlinesSchema, args); validateSymbol(input.symbol); try { const klines = await binanceClient.candles({ symbol: input.symbol, interval: input.interval, limit: input.limit, }); return { symbol: input.symbol, interval: input.interval, data: klines.map((kline: any) => ({ openTime: kline.openTime, open: kline.open, high: kline.high, low: kline.low, close: kline.close, volume: kline.volume, closeTime: kline.closeTime, quoteAssetVolume: kline.quoteAssetVolume, numberOfTrades: kline.numberOfTrades, takerBuyBaseAssetVolume: kline.takerBuyBaseAssetVolume, takerBuyQuoteAssetVolume: kline.takerBuyQuoteAssetVolume, })), timestamp: Date.now(), }; } catch (error) { handleBinanceError(error); } }, },
- src/types/mcp.ts:12-16 (schema)Zod schema (GetKlinesSchema) used for input validation inside the handler.export const GetKlinesSchema = z.object({ symbol: z.string().describe('交易对符号,如 BTCUSDT'), interval: z.enum(['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M']).describe('时间间隔'), limit: z.number().optional().default(500).describe('数量限制,默认500'), });
- src/server.ts:41-51 (registration)Top-level MCP server registration: spreads marketDataTools (including get_klines) into allTools and registers each by name in the server's tools Map.private setupTools(): void { const allTools = [ ...marketDataTools, ...accountTools, ...tradingTools, ]; for (const tool of allTools) { this.tools.set(tool.name, tool); } }