CoinGecko 服务器是一个模型上下文协议(MCP)服务器和 OpenAI 函数调用服务,用于与 CoinGecko Pro API 交互,可助力用户获取各类加密货币数据。
CoinGecko 服务器为用户提供了便捷的方式与 CoinGecko Pro API 进行交互。通过以下步骤,你可以快速搭建并使用该服务器。
在项目中安装 CoinGecko 服务器,可使用以下命令:
npm install coingecko-server
在项目根目录中创建一个 .env 文件,并设置 CoinGecko API 密钥:
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"
}
}
}
}
该服务器提供的工具如下:
get-coins:获取分页的加密货币列表find-coin-ids:按名称或符号查找 CoinGecko IDget-historical-data:获取历史价格、市值和交易量数据get-ohlc-data:获取 OHLC 烛台数据refresh-cache:刷新本地硬币列表缓存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",
});
如果 (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
);
}
interface OHLCData {
timestamp: number;
open: number;
high: number;
low: number;
close: number;
}
interface HistoricalData {
prices: [number, number][];
market_caps: [number, number][];
total_volumes: [number, number][];
}
interface CoinInfo {
id: string;
symbol: string;
name: string;
platforms?: Record<string, string>;
}
请参考 CoinGecko Pro API 文档 以获取当前的速率限制和使用指南。
本项目采用 MIT 许可证。