Skip to main content
Glama
wn01011

llm-token-tracker

get_exchange_rate

Retrieve current USD to KRW exchange rate with optional cache refresh for accurate currency conversion tracking in token usage monitoring.

Instructions

Get current USD to KRW exchange rate with cache info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
force_refreshNoForce refresh from API (default: false)

Implementation Reference

  • Primary handler for 'get_exchange_rate' tool: processes input, fetches rate via tracker, formats and returns formatted text response.
    private async getExchangeRate(args: any) { const { force_refresh = false } = args; try { let rate: number; let info: any; if (force_refresh) { rate = await this.tracker.refreshExchangeRate(); info = await this.tracker.getExchangeRateInfo(); } else { info = await this.tracker.getExchangeRateInfo(); rate = info.rate; } const lastUpdated = info.lastUpdated ? new Date(info.lastUpdated).toLocaleString() : 'Never'; const timeSinceUpdate = info.lastUpdated ? Math.round((Date.now() - new Date(info.lastUpdated).getTime()) / (1000 * 60 * 60)) : null; let result = `πŸ’± Exchange Rate (USD to KRW)\n`; result += `━━━━━━━━━━━━━━━━━━━━━━\n`; result += `πŸ’΅ Current Rate: β‚©${rate.toFixed(2)}\n`; result += `πŸ“… Last Updated: ${lastUpdated}\n`; if (timeSinceUpdate !== null) { result += `⏰ ${timeSinceUpdate} hours ago\n`; } result += `πŸ”„ Source: ${info.source || 'fallback'}\n`; result += `━━━━━━━━━━━━━━━━━━━━━━\n\n`; result += `πŸ’‘ Rate updates automatically every 24 hours\n`; result += ` Cache location: ~/.llm-token-tracker/exchange-rate.json`; return { content: [{ type: 'text', text: result }] }; } catch (error) { return { content: [ { type: 'text', text: `❌ Failed to get exchange rate: ${error instanceof Error ? error.message : 'Unknown error'}` } ] }; } }
  • Tool registration entry: defines name, description, and input schema for get_exchange_rate.
    name: 'get_exchange_rate', description: 'Get current USD to KRW exchange rate with cache info', inputSchema: { type: 'object', properties: { force_refresh: { type: 'boolean', description: 'Force refresh from API (default: false)', default: false } } } }
  • TokenTracker helper method getExchangeRateInfo() that retrieves current exchange rate and cache metadata via ExchangeRateManager.
    async getExchangeRateInfo(): Promise<{ rate: number; lastUpdated: string | null; source: string | null; }> { const cached = this.exchangeRateManager.getCacheInfo(); const currentRate = await this.exchangeRateManager.getUSDtoKRW(); return { rate: currentRate, lastUpdated: cached?.lastUpdated || null, source: cached?.source || null }; }
  • Core ExchangeRateManager.getUSDtoKRW(): handles caching, API fetch (exchangerate-api.com), fallback logic for USD->KRW rate.
    async getUSDtoKRW(): Promise<number> { // Try to get from cache first const cached = this.loadFromCache(); if (cached && !this.isCacheExpired(cached.lastUpdated)) { return cached.rate; } // Fetch fresh rate try { const rate = await this.fetchExchangeRate(); this.saveToCache(rate, 'exchangerate-api'); return rate; } catch (error) { console.error('Failed to fetch exchange rate:', error); // Use cached rate even if expired if (cached) { console.log('Using expired cache due to API failure'); return cached.rate; } // Fall back to default rate console.log(`Using fallback rate: ${this.fallbackRate}`); return this.fallbackRate; } }
  • Actual API fetch implementation for USD to KRW exchange rate using exchangerate-api.com.
    private async fetchExchangeRate(): Promise<number> { // Using exchangerate-api.com (free tier, no API key required) const url = 'https://api.exchangerate-api.com/v4/latest/USD'; const response = await fetch(url); if (!response.ok) { throw new Error(`API request failed: ${response.status}`); } const data: any = await response.json(); if (!data.rates || !data.rates.KRW) { throw new Error('Invalid API response format'); } return data.rates.KRW as number; }

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/wn01011/llm-token-tracker'

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