Skip to main content
Glama
jianchundev

Binance Cryptocurrency MCP

by jianchundev

get_klines

Retrieve historical candlestick chart data for cryptocurrency trading pairs from Binance to analyze price movements and market trends.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesTrading pair symbol, e.g. BTCUSDT
intervalYesK-line interval, e.g. 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
startTimeNoStart timestamp (milliseconds)
endTimeNoEnd timestamp (milliseconds)
timeZoneNoTime zone, default UTC
limitNoNumber of K-lines to return, default 500, max 1000

Implementation Reference

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

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

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