UNPKG

@bujaayjaay/mcp-coincap-jj

Version:

A Model Context Protocol (MCP) server that provides real-time cryptocurrency data and analysis through CoinCap's API v3. Features include price tracking, market analysis, and historical trends. This is a fork of the original repo by truss44, with updates

61 lines (60 loc) 1.73 kB
import { COINCAP_API_BASE, CACHE_TTL } from '../config/index.js'; const cache = new Map(); // Expose cache clear function for testing export function clearCache() { cache.clear(); } function getCachedData(key) { const entry = cache.get(key); if (!entry) return null; const now = Date.now(); if (now - entry.timestamp > CACHE_TTL) { cache.delete(key); return null; } return entry.data; } function setCacheData(key, data) { cache.set(key, { data, timestamp: Date.now() }); } async function makeCoinCapRequest(endpoint) { // Check cache first const cachedData = getCachedData(endpoint); if (cachedData) { return cachedData; } const headers = {}; const apiKey = process.env.COINCAP_API_KEY; if (apiKey) { headers['Authorization'] = `Bearer ${apiKey}`; } try { const response = await fetch(`${COINCAP_API_BASE}${endpoint}`, { headers }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // Cache the successful response setCacheData(endpoint, data); return data; } catch (error) { console.error("Error making CoinCap request:", error); return null; } } export async function getAssets() { return makeCoinCapRequest('/assets'); } export async function getMarkets(assetId) { return makeCoinCapRequest(`/assets/${assetId}/markets`); } export async function getHistoricalData(assetId, interval, start, end) { return makeCoinCapRequest(`/assets/${assetId}/history?interval=${interval}&start=${start}&end=${end}`); }