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
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Trading 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 }; } } ); - src/index.ts:217-234 (handler)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 }; } } - src/index.ts:214-216 (schema)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") }, - src/index.ts:406-434 (helper)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 {}; }