Skip to main content
Glama

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
NameRequiredDescriptionDefault
currencyYesISO 4217 currency code (e.g. 'EUR', 'USD', 'GBP'). Use list_currencies to see all available codes.
fromNoStart date in YYYY-MM format (e.g. '2020-01'). Optional.
toNoEnd date in YYYY-MM format (e.g. '2026-02'). Optional.

Implementation Reference

  • 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);
  • 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.",
          },
        },
      },
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vikramgorla/mcp-swiss'

If you have feedback or need assistance with the MCP directory API, please join our Discord server