fetchTokenPriceBySymbol
Retrieve real-time cryptocurrency prices by providing token symbols like BTC or ETH. Simplify blockchain price queries with this Alchemy MCP Server tool.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbols | Yes | A list of blockchaintoken symbols to query. e.g. ["BTC", "ETH"] |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"symbols": {
"description": "A list of blockchaintoken symbols to query. e.g. [\"BTC\", \"ETH\"]",
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"symbols"
],
"type": "object"
}
Implementation Reference
- api/alchemyApi.ts:11-27 (handler)The main handler function that implements the logic for fetching token prices by symbol using the Alchemy Prices API client. It constructs the query parameters from the input symbols and makes a GET request to the /by-symbol endpoint.async getTokenPriceBySymbol(params: TokenPriceBySymbol) { try { const client = createPricesClient(); const queryParams = new URLSearchParams(); params.symbols.forEach(symbol => { queryParams.append('symbols', symbol.toUpperCase()); }); const response = await client.get(`/by-symbol?${queryParams}`); return response.data; } catch (error) { console.error('Error fetching token prices:', error); throw error; } },
- index.ts:17-38 (registration)Registers the MCP tool 'fetchTokenPriceBySymbol' with input schema and an inline handler that delegates to alchemyApi.getTokenPriceBySymbol and formats the response.server.tool('fetchTokenPriceBySymbol', { symbols: z.array(z.string()).describe('A list of blockchaintoken symbols to query. e.g. ["BTC", "ETH"]'), }, async (params) => { try { const result = await alchemyApi.getTokenPriceBySymbol(params); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (error) { if (error instanceof Error) { console.error('Error in getTokenPriceBySymbol:', error); return { content: [{ type: "text", text: `Error: ${error.message}` }], isError: true }; } return { content: [{ type: "text", text: 'Unknown error occurred' }], isError: true }; } });
- types/types.d.ts:2-4 (schema)TypeScript interface defining the input parameters for getTokenPriceBySymbol, matching the Zod schema used in registration.export interface TokenPriceBySymbol { symbols: string[]; }
- api/alchemyClients.ts:9-16 (helper)Helper function that creates an Axios client configured for the Alchemy Prices API, used by the handler to make HTTP requests.export const createPricesClient = () => axios.create({ baseURL: `https://api.g.alchemy.com/prices/v1/${API_KEY}/tokens`, headers: { 'accept': 'application/json', 'content-type': 'application/json', 'x-alchemy-client-breadcrumb': BREADCRUMB_HEADER }, });