fetchTokenPriceByAddress
Retrieve token prices by providing contract addresses and blockchain networks, enabling quick and accurate token value queries across multiple chains via Alchemy MCP Server.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| addresses | Yes | A list of token contract address and network pairs |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"addresses": {
"description": "A list of token contract address and network pairs",
"items": {
"additionalProperties": false,
"properties": {
"address": {
"description": "The token contract address to query. e.g. \"0x1234567890123456789012345678901234567890\"",
"type": "string"
},
"network": {
"description": "The blockchain network to query. e.g. \"eth-mainnet\" or \"base-mainnet\"",
"type": "string"
}
},
"required": [
"address",
"network"
],
"type": "object"
},
"type": "array"
}
},
"required": [
"addresses"
],
"type": "object"
}
Implementation Reference
- api/alchemyApi.ts:29-46 (handler)Core handler function that executes the logic for fetching token price by address using Alchemy Prices API via POST to /by-address endpoint.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; } },
- types/types.d.ts:6-13 (schema)TypeScript interfaces defining the input parameters for the fetchTokenPriceByAddress tool.export interface TokenPriceByAddress { addresses: TokenPriceByAddressPair[]; } export interface TokenPriceByAddressPair { address: string; network: string; }
- index.ts:41-65 (registration)Registers the fetchTokenPriceByAddress tool with Zod input schema and wrapper handler that calls the core alchemyApi function 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 }; } });
- api/alchemyClients.ts:9-16 (helper)Helper function to create Axios client configured for Alchemy Prices API, used in the token price handler.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 }, });