fetchTokenPriceHistoryBySymbol
Retrieve historical price data for a specific cryptocurrency by symbol, date range, and interval using this Alchemy MCP Server tool. Ideal for analyzing token price trends over time.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| endTime | Yes | The end time date to query. e.g. "2021-01-01" | |
| interval | Yes | The interval to query. e.g. "1d" or "1h" | |
| startTime | Yes | The start time date to query. e.g. "2021-01-01" | |
| symbol | Yes | The token symbol to query. e.g. "BTC" or "ETH" |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"endTime": {
"description": "The end time date to query. e.g. \"2021-01-01\"",
"type": "string"
},
"interval": {
"description": "The interval to query. e.g. \"1d\" or \"1h\"",
"type": "string"
},
"startTime": {
"description": "The start time date to query. e.g. \"2021-01-01\"",
"type": "string"
},
"symbol": {
"description": "The token symbol to query. e.g. \"BTC\" or \"ETH\"",
"type": "string"
}
},
"required": [
"symbol",
"startTime",
"endTime",
"interval"
],
"type": "object"
}
Implementation Reference
- api/alchemyApi.ts:48-63 (handler)Core implementation of the tool logic: makes HTTP POST request to Alchemy's /prices/v1/{key}/tokens/historical endpoint with the provided parameters.async getTokenPriceHistoryBySymbol(params: TokenPriceHistoryBySymbol) { console.log('Fetching token price history for symbol:', params.symbol); try { const client = createPricesClient(); const response = await client.post('/historical', { ...params }); console.log('Successfully fetched token price history:', response.data); return response.data; } catch (error) { console.error('Error fetching token price history:', error); throw error; } },
- index.ts:68-96 (registration)MCP server tool registration, including Zod input schema validation and inline error-handling handler that preprocesses dates using toISO8601 and delegates to alchemyApi.getTokenPriceHistoryBySymbolserver.tool('fetchTokenPriceHistoryBySymbol', { symbol: z.string().describe('The token symbol to query. e.g. "BTC" or "ETH"'), startTime: z.string().describe('The start time date to query. e.g. "2021-01-01"'), endTime: z.string().describe('The end time date to query. e.g. "2021-01-01"'), interval: z.string().describe('The interval to query. e.g. "1d" or "1h"') }, async (params) => { try { const result = await alchemyApi.getTokenPriceHistoryBySymbol({ ...params, startTime: toISO8601(params.startTime), endTime: toISO8601(params.endTime) }); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (error) { if (error instanceof Error) { console.error('Error in getTokenPriceHistoryBySymbol:', 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:15-20 (schema)TypeScript interface defining the input parameters for getTokenPriceHistoryBySymbol, matching the Zod schema.export interface TokenPriceHistoryBySymbol { symbol: string; startTime: string; endTime: string; interval: string; }
- api/alchemyClients.ts:9-16 (helper)Factory function creating Axios HTTP client configured for Alchemy Prices API, used in the handler to make the historical price request.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 }, });
- utils/dateUtils.ts:137-192 (helper)Utility function toISO8601 used in the tool handler to convert startTime and endTime parameters to ISO 8601 format required by the API.export function toISO8601(dateStr: string): string { // Handle keywords first const normalizedStr = dateStr.toLowerCase(); const now = new Date(); switch (normalizedStr) { case 'today': { const date = new Date(now); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'yesterday': { const date = new Date(now); date.setDate(date.getDate() - 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'last-week': { const date = new Date(now); date.setDate(date.getDate() - 7); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'last-month': { const date = new Date(now); date.setMonth(date.getMonth() - 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'start-of-year': { const date = new Date(now); date.setMonth(0, 1); date.setHours(0, 0, 0, 0); return date.toISOString(); } case 'now': { return now.toISOString(); } } // If already in ISO format, return as is if (dateStr.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/)) { return dateStr; } // For any other date string, parse it normally try { const date = new Date(dateStr); return date.toISOString(); } catch (error) { console.error('Error parsing date:', error); // Return current time if parsing fails return now.toISOString(); } }