🪙 MCP Crypto Price Lookup Server using ALPACA API

🪙 MCP Crypto Price Lookup Server using ALPACA API

Adity-star

研究与数据
访问服务器

README

🪙 使用 ALPACA API 的 MCP 加密货币价格查询服务器

本项目搭建了一个加密货币价格查询服务,由 ALPACA API 提供支持,并构建用于与 MCP (多代理控制协议) 集成。

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


📦 步骤 1:使用 uv 设置环境

我们将使用 uv — 一个快速、现代的 Python 包管理器 — 来创建和管理项目环境。

🛠️ 安装与设置

在您的终端中(而不是在 Jupyter 中)运行以下命令:

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

# 创建并导航到您的项目目录
mkdir mcp-crypto-server
cd mcp-crypto-server

# 初始化一个新项目
uv init

# 创建并激活虚拟环境
uv venv
source .venv/bin/activate  # 在 Windows 上:.venv\Scripts\activate

# 安装所需的依赖项
uv add "mcp[cli]" httpx

步骤 2:运行 MCP 服务器

环境设置完成后,我们就可以构建和运行我们的加密货币价格查询工具了。 从 scripts 文件夹复制服务器脚本:

cp ../mcp_server.py .

启动 MCP 服务器:
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"
            ]
        }
    }
}

🔁 将 /ABSOLUTE/PATH/TO/... 替换为您的 uv 二进制文件和项目文件夹的完整路径。

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

步骤 5:尝试询问比特币的价格

  • 您可以将自己的工具添加到 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 和一点书呆子野心构建自己的股票服务器

享受学习的乐趣!

推荐服务器

Crypto Price & Market Analysis MCP Server

Crypto Price & Market Analysis MCP Server

一个模型上下文协议 (MCP) 服务器,它使用 CoinCap API 提供全面的加密货币分析。该服务器通过一个易于使用的界面提供实时价格数据、市场分析和历史趋势。 (Alternative, slightly more formal and technical translation): 一个模型上下文协议 (MCP) 服务器,利用 CoinCap API 提供全面的加密货币分析服务。该服务器通过用户友好的界面,提供实时价格数据、市场分析以及历史趋势数据。

精选
TypeScript
MCP PubMed Search

MCP PubMed Search

用于搜索 PubMed 的服务器(PubMed 是一个免费的在线数据库,用户可以在其中搜索生物医学和生命科学文献)。 我是在 MCP 发布当天创建的,但当时正在度假。 我看到有人在您的数据库中发布了类似的服务器,但还是决定发布我的服务器。

精选
Python
mixpanel

mixpanel

连接到您的 Mixpanel 数据。 从 Mixpanel 分析查询事件、留存和漏斗数据。

精选
TypeScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Nefino MCP Server

Nefino MCP Server

为大型语言模型提供访问德国可再生能源项目新闻和信息的能力,允许按地点、主题(太阳能、风能、氢能)和日期范围进行筛选。

官方
Python
Vectorize

Vectorize

将 MCP 服务器向量化以实现高级检索、私有深度研究、Anything-to-Markdown 文件提取和文本分块。

官方
JavaScript
Mathematica Documentation MCP server

Mathematica Documentation MCP server

一个服务器,通过 FastMCP 提供对 Mathematica 文档的访问,使用户能够从 Wolfram Mathematica 检索函数文档和列出软件包符号。

本地
Python
kb-mcp-server

kb-mcp-server

一个 MCP 服务器,旨在实现便携性、本地化、简易性和便利性,以支持对 txtai “all in one” 嵌入数据库进行基于语义/图的检索。任何 tar.gz 格式的 txtai 嵌入数据库都可以被加载。

本地
Python
Research MCP Server

Research MCP Server

这个服务器用作 MCP 服务器,与 Notion 交互以检索和创建调查数据,并与 Claude Desktop Client 集成以进行和审查调查。

本地
Python
Cryo MCP Server

Cryo MCP Server

一个API服务器,实现了模型补全协议(MCP),用于Cryo区块链数据提取,允许用户通过任何兼容MCP的客户端查询以太坊区块链数据。

本地
Python