get_exchange_rate
Retrieve current USD to KRW exchange rates with optional cache refresh for accurate currency conversion tracking.
Instructions
Get current USD to KRW exchange rate with cache info
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force_refresh | No | Force refresh from API (default: false) |
Implementation Reference
- src/mcp-server.ts:141-154 (registration)Registration of the 'get_exchange_rate' tool in the ListToolsRequestSchema handler, including name, description, and input schema.
{ 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/mcp-server.ts:357-407 (handler)Primary MCP tool handler for 'get_exchange_rate' that delegates to tracker and formats the user-friendly 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/tracker.ts:306-319 (helper)Helper method in TokenTracker that retrieves current exchange rate and cache info from 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 method that gets USD->KRW rate from cache, API, or fallback.
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)Private method that fetches fresh exchange rate from exchangerate-api.com API.
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; }