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
| Name | Required | Description | Default |
|---|---|---|---|
| force_refresh | No | Force refresh from API (default: false) |
Implementation Reference
- src/mcp-server.ts:357-407 (handler)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'}` } ] }; } }
- src/mcp-server.ts:142-154 (registration)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 } } } }
- src/tracker.ts:306-319 (helper)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 }; }
- src/exchange-rate.ts:35-60 (helper)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; } }
- src/exchange-rate.ts:65-82 (helper)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; }