Skip to main content
Glama
jianchundev

Binance Cryptocurrency MCP

by jianchundev

get_avg_price

Calculate the average price for a cryptocurrency trading pair on Binance to analyze market trends and inform trading decisions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesTrading pair symbol, e.g. BTCUSDT

Implementation Reference

  • src/index.ts:212-235 (registration)
    Tool registration for get_avg_price using server.tool() with zod schema definition and async handler function
    server.tool(
        "get_avg_price",
        {
            symbol: z.string().describe("Trading pair symbol, e.g. BTCUSDT")
        },
        async (args: { symbol: string }) => {
            try {
                const response = await axios.get(`${BASE_URL}/api/v3/avgPrice`, {
                    params: {
                        symbol: args.symbol
                    },
                    ...getProxyConfig(),
                });
                return {
                    content: [{ type: "text", text: JSON.stringify(response.data, null, 2) }]
                };
            } catch (error: any) {
                return {
                    content: [{ type: "text", text: `Failed to get average price: ${error.message}` }],
                    isError: true
                };
            }
        }
    );
  • Handler function that executes get_avg_price by making a GET request to Binance API /api/v3/avgPrice endpoint with symbol parameter, handles errors and returns JSON response
    async (args: { symbol: string }) => {
        try {
            const response = await axios.get(`${BASE_URL}/api/v3/avgPrice`, {
                params: {
                    symbol: args.symbol
                },
                ...getProxyConfig(),
            });
            return {
                content: [{ type: "text", text: JSON.stringify(response.data, null, 2) }]
            };
        } catch (error: any) {
            return {
                content: [{ type: "text", text: `Failed to get average price: ${error.message}` }],
                isError: true
            };
        }
    }
  • Zod schema definition for get_avg_price input parameters: symbol (string, required) describing trading pair symbol like BTCUSDT
    {
        symbol: z.string().describe("Trading pair symbol, e.g. BTCUSDT")
    },
  • getProxyConfig() helper function that configures SOCKS5 or HTTP proxy for axios requests based on environment variables
    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