Skip to main content
Glama

get_exchange_rate

Retrieve current CHF exchange rates from the Swiss National Bank for any currency using ISO 4217 codes. Provides monthly average rates and currency details.

Instructions

Get the current CHF exchange rate for a currency from the Swiss National Bank (SNB). Returns the latest monthly average rate and currency details.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
currencyYesISO 4217 currency code (e.g. 'EUR', 'USD', 'GBP', 'JPY'). Use list_currencies to see all available codes.

Implementation Reference

  • The `handleGetExchangeRate` function performs the core logic for fetching and formatting exchange rate data from the SNB.
    async function handleGetExchangeRate(currency: 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}`
        );
      }
    
      const entries = ratesMap.get(info.seriesId);
      if (!entries || entries.length === 0) {
        return JSON.stringify(
          {
            error: "No exchange rate data available",
            currency: currency.toUpperCase(),
            hint: "The SNB may not publish rates for this currency for recent periods.",
            source: "https://data.snb.ch",
          },
          null,
          2
        );
      }
    
      // Latest entry (CSV is in chronological order, last = most recent)
      const latest = entries[entries.length - 1];
    
      return JSON.stringify(
        {
          currency: info.code,
          currencyName: info.name,
          date: latest.date,
          rate: latest.rate,
          units: info.units,
          description:
            info.units === 100
              ? `1 CHF = ${(info.units / latest.rate).toFixed(4)} ${info.code} | 100 ${info.code} = ${latest.rate} CHF`
              : `1 ${info.code} = ${latest.rate} CHF`,
          note:
            info.units === 100
              ? `Rate is CHF per 100 ${info.code} (monthly average)`
              : `Rate is CHF per 1 ${info.code} (monthly average)`,
          source: "https://data.snb.ch",
        },
        null,
        2
      );
    }
  • Definition of the `get_exchange_rate` tool, including its input schema and description for MCP.
    {
      name: "get_exchange_rate",
      description:
        "Get the current CHF exchange rate for a currency from the Swiss National Bank (SNB). Returns the latest monthly average rate and currency details.",
      inputSchema: {
        type: "object" as const,
        required: ["currency"],
        properties: {
          currency: {
            type: "string",
            description: "ISO 4217 currency code (e.g. 'EUR', 'USD', 'GBP', 'JPY'). Use list_currencies to see all available codes.",
          },
        },
  • The switch case in the MCP tool handler routes the `get_exchange_rate` request to `handleGetExchangeRate`.
    case "get_exchange_rate":
      return handleGetExchangeRate(args.currency as string);
    case "get_exchange_rate_history":

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