CoinGecko 服务器是一个模型上下文协议(MCP)服务器和 OpenAI 函数调用服务,用于与 CoinGecko Pro API 进行交互,为用户提供加密货币相关的数据服务。
npm install coingecko-server
在项目根目录中创建一个 .env 文件:
COINGECKO_API_KEY=your_api_key_here
Claude Desktop 完全支持 MCP 功能。要使用此服务器,可按以下步骤操作:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json{
"mcpServers": {
"coingecko": {
"command": "node",
"args": ["/path/to/coingecko-server/build/index.js"],
"env": {
"COINGECKO_API_KEY": "your-api-key-here"
}
}
}
}
import { CoinGeckoService } from 'coingecko-server';
import OpenAI from 'openai';
const openai = new OpenAI();
const coinGeckoService = new CoinGeckoService(process.env.COINGECKO_API_KEY);
// 获取函数定义
const functions = CoinGeckoService.getOpenAIFunctionDefinitions();
// 示例:获取历史数据
const response = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages: [{ role: "user", content: "获取过去一周的比特币价格历史" }],
functions: [functions[2]], // get_historical_data 函数
function_call: "auto",
});
if (response.choices[0].message.function_call) {
const args = JSON.parse(response.choices[0].message.function_call.arguments);
const history = await coinGeckoService.getHistoricalData(
args.id,
args.vs_currency,
args.from,
args.to,
args.interval
);
}
该服务器提供以下工具:
get-coins:获取分页支持的硬币列表find-coin-ids:查找加密货币名称或符号对应的 CoinGecko IDget-historical-data:获取历史价格、市值和成交量数据get-ohlc-data:获取 OHLC 烛台数据refresh-cache:刷新本地硬币列表缓存interface OHLCData {
open: number;
high: number;
low: number;
close: number;
volume?: number;
timestamp: string;
}
interface HistoricalData {
[key: string]: OHLCData[];
}
interface CoinInfo {
id: string;
symbol: string;
name: string;
priceUsd: string;
marketCapUsd: string;
volumeUsd24Hr: string;
latestUpdate: string;
}
有关 API 调用的速率限制,请参考 CoinGecko Pro API 文档。
该库在 MIT 许可证下发布。