get_klines
Retrieve historical candlestick chart data for cryptocurrency trading pairs from Binance to analyze price movements and market trends.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Trading pair symbol, e.g. BTCUSDT | |
| interval | Yes | K-line interval, e.g. 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M | |
| startTime | No | Start timestamp (milliseconds) | |
| endTime | No | End timestamp (milliseconds) | |
| timeZone | No | Time zone, default UTC | |
| limit | No | Number of K-lines to return, default 500, max 1000 |
Implementation Reference
- src/index.ts:150-172 (handler)The handler function for get_klines tool that executes the logic - makes an API call to Binance's /api/v3/klines endpoint with the provided parameters (symbol, interval, startTime, endTime, timeZone, limit) and returns formatted K-line/candlestick data or handles errors.
async (args: { symbol: string; interval: string; startTime?: number; endTime?: number; timeZone?: string; limit?: number }) => { try { const response = await axios.get(`${BASE_URL}/api/v3/klines`, { params: { symbol: args.symbol, interval: args.interval, startTime: args.startTime, endTime: args.endTime, timeZone: args.timeZone, limit: args.limit }, ...getProxyConfig(), }); return { content: [{ type: "text", text: JSON.stringify(response.data, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to get K-line data: ${error.message}` }], isError: true }; } } - src/index.ts:140-173 (registration)Complete registration of the get_klines tool with the MCP server, including schema definition (parameters: symbol, interval, startTime, endTime, timeZone, limit) and the handler function.
server.tool( "get_klines", { symbol: z.string().describe("Trading pair symbol, e.g. BTCUSDT"), interval: z.string().describe("K-line interval, e.g. 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M"), startTime: z.number().optional().describe("Start timestamp (milliseconds)"), endTime: z.number().optional().describe("End timestamp (milliseconds)"), timeZone: z.string().optional().describe("Time zone, default UTC"), limit: z.number().optional().describe("Number of K-lines to return, default 500, max 1000") }, async (args: { symbol: string; interval: string; startTime?: number; endTime?: number; timeZone?: string; limit?: number }) => { try { const response = await axios.get(`${BASE_URL}/api/v3/klines`, { params: { symbol: args.symbol, interval: args.interval, startTime: args.startTime, endTime: args.endTime, timeZone: args.timeZone, limit: args.limit }, ...getProxyConfig(), }); return { content: [{ type: "text", text: JSON.stringify(response.data, null, 2) }] }; } catch (error: any) { return { content: [{ type: "text", text: `Failed to get K-line data: ${error.message}` }], isError: true }; } } ); - src/index.ts:406-434 (helper)Helper function getProxyConfig that configures proxy settings for HTTP requests - supports SOCKS5 proxy (priority) and HTTP proxy fallback, used by the get_klines handler.
function getProxyConfig(): any { // 优先使用SOCKS5代理 if (socksProxyURL) { try { const agent = new SocksProxyAgent(socksProxyURL); return { httpsAgent: agent, httpAgent: agent }; } catch (error) { console.error(`Failed to create SOCKS proxy agent: ${error}`); } } // 回退到HTTP代理 if (httpProxyURL) { try { const urlInfo = new URL(httpProxyURL); return { proxy: { host: urlInfo.hostname, port: parseInt(urlInfo.port) || (urlInfo.protocol === 'https:' ? 443 : 80), protocol: urlInfo.protocol.replace(":", "") } }; } catch (error) { console.error(`Failed to parse HTTP proxy URL: ${error}`); } } return {}; }