Skip to main content
Glama
ethancod1ng
by ethancod1ng

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
NameRequiredDescriptionDefault
intervalYes时间间隔
limitNo数量限制,默认500
symbolYes交易对符号,如 BTCUSDT

Implementation Reference

  • 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); } },
  • 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); } }, },
  • 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); } }

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/ethancod1ng/binance-mcp-server'

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