UNPKG

asset-price-mcp

Version:

An MCP-compatible service that provides real-time asset prices including precious metals, cryptocurrencies, and more

51 lines (50 loc) 1.93 kB
import { fetchJson } from "../utils.js"; import { apiCache } from "../cache.js"; const BASE_URL = 'https://data-asg.goldprice.org/dbXRates/USD'; export class GoldPriceOrgService { getName() { return "GoldPriceOrg"; } async getSupportedAssets() { return [ { name: "Gold", symbol: "XAU" }, { name: "Silver", symbol: "XAG" } ]; } async getPrice(symbol, _currency = 'USD') { const upperSymbol = symbol.toUpperCase(); if (upperSymbol !== 'XAU' && upperSymbol !== 'XAG') { return null; } // This API primarily supports USD. We return USD here and let the tool handle conversion. const cacheKey = "GOLD_PRICE_ORG_DATA"; let data = apiCache.get(cacheKey); if (!data) { data = await fetchJson(BASE_URL); if (data) { apiCache.set(cacheKey, data); } } if (!data || !data.items || data.items.length === 0) return null; const item = data.items[0]; const isGold = upperSymbol === 'XAU'; const price = isGold ? item.xauPrice : item.xagPrice; // const changeAmount = isGold ? item.chgXau : item.chgXag; const changePercent = isGold ? item.pcXau : item.pcXag; return { name: isGold ? "Gold" : "Silver", symbol: upperSymbol, price: price, currency: "USD", updatedAt: new Date().toISOString(), // The API returns a formatted date string, using current time for simplicity or could parse `data.date` updatedAtReadable: data.date, change24h: changePercent }; } async getPrices(symbols, currency = 'USD') { const promises = symbols.map(s => this.getPrice(s, currency)); const results = await Promise.all(promises); return results.filter((p) => p !== null); } }