get_exchange_rate_history
Retrieve historical CHF exchange rate data from the Swiss National Bank for analysis, tracking, or reporting purposes. Access monthly average rates with optional date filtering capabilities.
Instructions
Get historical CHF exchange rates for a currency from the Swiss National Bank (SNB). Returns monthly average rates with optional date filtering. Without date range, returns the most recent 90 months.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| currency | Yes | ISO 4217 currency code (e.g. 'EUR', 'USD', 'GBP'). Use list_currencies to see all available codes. | |
| from | No | Start date in YYYY-MM format (e.g. '2020-01'). Optional. | |
| to | No | End date in YYYY-MM format (e.g. '2026-02'). Optional. |
Implementation Reference
- src/modules/snb.ts:279-342 (handler)The handler function `handleGetExchangeRateHistory` that fetches and processes the SNB exchange rate history.
async function handleGetExchangeRateHistory( currency: string, from?: string, to?: string ): Promise<string> { if (!currency?.trim()) { throw new Error("currency is required (e.g. 'EUR', 'USD', 'GBP')"); } const [currencies, ratesMap] = await Promise.all([fetchDimensions(), fetchRatesMap()]); const info = findSeriesId(currency, currencies); if (!info) { const availableCodes = currencies.map((c) => c.code).join(", "); throw new Error( `Currency '${currency.toUpperCase()}' not found. Available currencies: ${availableCodes}` ); } let entries = ratesMap.get(info.seriesId) ?? []; if (entries.length === 0) { return JSON.stringify( { error: "No historical data available", currency: currency.toUpperCase(), source: "https://data.snb.ch", }, null, 2 ); } // Filter by date range if provided (dates are "YYYY-MM" format) if (from) { entries = entries.filter((e) => e.date >= from); } if (to) { entries = entries.filter((e) => e.date <= to); } // If no date range provided, limit to most recent 90 entries if (!from && !to && entries.length > 90) { entries = entries.slice(-90); } if (entries.length === 0) { return JSON.stringify( { error: "No data in the specified date range", currency: currency.toUpperCase(), from: from ?? null, to: to ?? null, source: "https://data.snb.ch", }, null, 2 ); } // Compute simple stats const rates = entries.map((e) => e.rate); const minRate = Math.min(...rates); const maxRate = Math.max(...rates); - src/modules/snb.ts:466-488 (schema)The tool schema definition for `get_exchange_rate_history` including name, description, and input parameters.
{ name: "get_exchange_rate_history", description: "Get historical CHF exchange rates for a currency from the Swiss National Bank (SNB). Returns monthly average rates with optional date filtering. Without date range, returns the most recent 90 months.", inputSchema: { type: "object" as const, required: ["currency"], properties: { currency: { type: "string", description: "ISO 4217 currency code (e.g. 'EUR', 'USD', 'GBP'). Use list_currencies to see all available codes.", }, from: { type: "string", description: "Start date in YYYY-MM format (e.g. '2020-01'). Optional.", }, to: { type: "string", description: "End date in YYYY-MM format (e.g. '2026-02'). Optional.", }, }, }, },