get_avg_price
Calculate the average price of a cryptocurrency trading pair using Binance market data. Provide the trading pair symbol (e.g., BTCUSDT) to retrieve precise price averages for informed decision-making.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Trading pair symbol, e.g. BTCUSDT |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"symbol": {
"description": "Trading pair symbol, e.g. BTCUSDT",
"type": "string"
}
},
"required": [
"symbol"
],
"type": "object"
}
Implementation Reference
- src/index.ts:214-231 (handler)The handler function implementing the core logic for the 'get_avg_price' tool. It performs an HTTP GET request to the Binance API's /api/v3/avgPrice endpoint with the provided symbol, formats the response as JSON, and handles errors gracefully.async (args: { symbol: string }) => { try { const response = await axios.get(`${BASE_URL}/api/v3/avgPrice`, { params: { symbol: args.symbol }, proxy: getProxy(), }); 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:211-213 (schema)Input schema for the 'get_avg_price' tool, validating the 'symbol' parameter using Zod.{ symbol: z.string().describe("Trading pair symbol, e.g. BTCUSDT") },
- src/index.ts:210-232 (registration)Registration of the 'get_avg_price' tool on the MCP server using server.tool(), specifying the tool name, input schema, and handler function."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 }, proxy: getProxy(), }); 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:403-412 (helper)Helper utility function 'getProxy()' that configures proxy settings from environment variables, used in the tool's API request.function getProxy():any { const proxy: any = {} if (proxyURL) { const urlInfo = new URL(proxyURL); proxy.host = urlInfo.hostname; proxy.port = urlInfo.port; proxy.protocol = urlInfo.protocol.replace(":", ""); } return proxy }