关于 🪙 MCP Crypto Price Lookup Server using ALPACA API
该项目介绍了一个兼容 MCP 的服务,通过 Alpaca API 获取加密货币价格和市场数据,供需要格式化行情查询的 AI Agent 或客户端使用。
主要功能
- 01获取实时加密货币价格
- 02通过 Alpaca REST API 访问市场数据
- 03向 Agent 返回格式化结果
适用场景
- 01查询 BTC/USD 当前价格
- 02通过 AI 客户端查看加密货币市场数据
MCP 相关
该服务向 MCP 客户端提供加密货币价格和市场数据查询工具,并使用 Alpaca API。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
🪙 使用 ALPACA API 查询加密货币价格的 MCP 服务
本项目使用 ALPACA API 搭建加密货币价格查询服务,旨在与 **MCP(多 Agent 控制协议)**集成。
该服务可让 AI Agent 或客户端通过 Alpaca 的 API 高效获取实时加密货币价格和市场数据。
📦 第 1 步:使用 uv 设置环境
我们将使用 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 服务
设置好环境后,就可以构建并运行加密货币价格查询工具了。 从 scripts 文件夹复制服务脚本:
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 Agent 提供快速、格式化的响应
第 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 和一点 nerdy 雄心构建自己的股票服务
尽情学习!
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。