加密货币价格查询服务

一个基于ALPACA API的加密货币价格查询服务,用于实时获取加密货币价格和市场数据,适用于AI代理或客户端集成。

  • by Adity-star
  • Python
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 Adity-star/mcp-crypto-server

使用说明

🪙 使用ALACA API的MCP加密价格查找服务器

该项目设立了一个 加密货币价格查询服务 由 阿尔卑斯API 为与集成而构建 MCP(多代理控制协议).

服务器允许AI代理或客户端获取 实时加密货币价格和市场数据 有效使用Alpaca的API。


📦 步骤1:设置环境 uv

我们将使用 紫外线 --一个快速、现代的Python包管理器——用于创建和管理项目环境。

🛠️ 安装和设置

运行这些命令 在您的终端 (不在Jupyter中):

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh 

# Create and navigate to your project directory
mkdir mcp-crypto-server
cd mcp-crypto-server

# Initialize a new project
uv init

# Create and activate the virtual environment
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install required dependencies
uv add "mcp[cli]" httpx

步骤2:运行MCP服务器

一旦环境设置好,我们就可以构建和运行我们的加密货币价格查找工具。 从脚本文件夹复制服务器脚本:

cp ../mcp_server.py .

Start the MCP server:
uv run mcp_server.py

💡 结账 mcp_server.py 了解有关该工具如何与Alpaca API接口的实现细节。

关于服务器

MCP兼容服务器包括以下工具:

  • 获取实时加密货币价格(例如BTC/USD、ETH/USD)
  • 使用Alpaca的REST API访问市场数据
  • 为AI代理提供快速、格式化的响应

步骤3:配置MCP服务器

在MCP配置中添加以下内容以注册服务器:

{
    "mcpServers": {
        "crypto-price-tracker": {
            "command": "/ABSOLUTE/PATH/TO/uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/GenAI_Agents//mcp-crypto-server",
                "run",
                "mcp_server.py"
            ]
        }
    }
}

🔁 替换/绝对/路径/TO/。..包含uv二进制文件和项目文件夹的完整路径。

步骤4:重新启动Claude Desktop以使更改生效。

第五步:尝试询问比特币的价格

  • 您可以将自己的工具添加到mcp_server.py
"""
This script demonstrates how to create a simple MCP server that fetches
the current price of a cryptocurrency using the CoinGecko API.
It uses the FastMCP library to create the server and handle requests.
"""
import httpx
from dotenv import load_dotenv
from mcp.server.fastmcp import FastMCP

load_dotenv()

COINGECKO_BASE_URL = "https://api.coingecko.com/api/v3"

# Create our MCP server with a descriptive name
mcp = FastMCP("crypto_price_tracker")

# Now let's define our first tool - getting the current price of a cryptocurrency
@mcp.tool()
async def get_crypto_price(crypto_id: str, currency: str = "usd") -> str:
    """
    Get the current price of a cryptocurrency in a specified currency.
    
    Parameters:
    - crypto_id: The ID of the cryptocurrency (e.g., 'bitcoin', 'ethereum')
    - currency: The currency to display the price in (default: 'usd')
    
    Returns:
    - Current price information as a formatted string
    """
    # Construct the API URL
    url = f"{COINGECKO_BASE_URL}/simple/price"
    
    # Set up the query parameters
    params = {
        "ids": crypto_id,
        "vs_currencies": currency
    }
    
    try:
        # Make the API call
        async with httpx.AsyncClient() as client:
            response = await client.get(url, params=params)
            response.raise_for_status()  # Raise an exception for HTTP errors
            
            # Parse the response
            data = response.json()
            
            # Check if we got data for the requested crypto
            if crypto_id not in data:
                return f"Cryptocurrency '{crypto_id}' not found. Please check the ID and try again."
            
            # Format and return the price information
            price = data[crypto_id][currency]
            return f"The current price of {crypto_id} is {price} {currency.upper()}"
            
    except httpx.HTTPStatusError as e:
        return f"API Error: {e.response.status_code} - {e.response.text}"
    except Exception as e:
        return f"Error fetching price data: {str(e)}"

# You can add more tools here, following the same pattern as above

# Run the MCP server
# This will start the server and listen for incoming requests
if __name__ == "__main__":
    mcp.run()

📈 然后升级:构建yFinance股票服务器

一旦你熟悉了流程,就可以继续使用这个更高级的股票跟踪器💹

🔗 GitHub回购: https://github.com/Adity-star/mcp-yfinance-server

📝 详细博客: 👉 我如何用Python、yFinance和一点书呆子的野心构建自己的股票服务器

享受学习!