fetchTokenPriceByAddress
Get real-time cryptocurrency token prices by providing contract addresses and blockchain networks. Query multiple tokens simultaneously across different networks for accurate market data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| addresses | Yes | A list of token contract address and network pairs |
Implementation Reference
- api/alchemyApi.ts:29-46 (handler)Main handler function that executes the tool logic by making a POST request to Alchemy's /prices/v1/{key}/tokens/by-address endpoint using the created axios client.async getTokenPriceByAddress(params: TokenPriceByAddress) { try { const client = createPricesClient(); const response = await client.post('/by-address', { addresses: params.addresses.map((pair: TokenPriceByAddressPair) => ({ address: pair.address, network: pair.network })) }); console.log('Successfully fetched token price:', response.data); return response.data; } catch (error) { console.error('Error fetching token price:', error); throw error; } },
- index.ts:41-65 (registration)MCP tool registration using server.tool(), including Zod input schema and error-handling wrapper that calls the main handler and formats response.server.tool('fetchTokenPriceByAddress', { addresses: z.array(z.object({ address: z.string().describe('The token contract address to query. e.g. "0x1234567890123456789012345678901234567890"'), network: z.string().describe('The blockchain network to query. e.g. "eth-mainnet" or "base-mainnet"') })).describe('A list of token contract address and network pairs'), }, async (params) => { try { const result = await alchemyApi.getTokenPriceByAddress(params); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (error) { if (error instanceof Error) { console.error('Error in getTokenPriceByAddress:', 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:6-13 (schema)TypeScript interface definitions for the tool's input parameters: TokenPriceByAddress and nested TokenPriceByAddressPair.export interface TokenPriceByAddress { addresses: TokenPriceByAddressPair[]; } export interface TokenPriceByAddressPair { address: string; network: string; }
- api/alchemyClients.ts:9-16 (helper)Helper function to create an axios client configured for Alchemy Prices API, used in 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 }, });