coingecko.ts•2.06 kB
import { BaseApiClient } from "../../core/base-api.js";
import { ToolRegistration } from "../../core/types.js";
class CoinGeckoClient extends BaseApiClient {
constructor() {
super("https://api.coingecko.com/api/v3");
}
getPrice(coinId: string, vsCurrency: string) {
return this.request(`/simple/price`, { query: { ids: coinId, vs_currencies: vsCurrency } });
}
getTrending() {
return this.request(`/search/trending`);
}
getMarketData(coinId: string) {
return this.request(`/coins/${coinId}`);
}
}
export function registerCoinGecko(): ToolRegistration {
const client = new CoinGeckoClient();
return {
tools: [
{
name: "get_coin_price",
description: "Get current price for a coin in a given currency",
inputSchema: {
type: "object",
properties: { coin_id: { type: "string" }, vs_currency: { type: "string" } },
required: ["coin_id", "vs_currency"],
},
},
{
name: "get_trending_coins",
description: "Get trending coins on CoinGecko",
inputSchema: { type: "object", properties: {} },
},
{
name: "get_market_data",
description: "Get market data for a coin",
inputSchema: {
type: "object",
properties: { coin_id: { type: "string" } },
required: ["coin_id"],
},
},
],
handlers: {
async get_coin_price(args: Record<string, unknown>) {
const coinId = String(args.coin_id || "");
const vsCurrency = String(args.vs_currency || "");
if (!coinId || !vsCurrency) throw new Error("coin_id and vs_currency are required");
return client.getPrice(coinId, vsCurrency);
},
async get_trending_coins() {
return client.getTrending();
},
async get_market_data(args: Record<string, unknown>) {
const coinId = String(args.coin_id || "");
if (!coinId) throw new Error("coin_id is required");
return client.getMarketData(coinId);
},
},
};
}