🪙 MCP Crypto Price Lookup Server using ALPACA API について
このプロジェクトは、Alpaca APIを通じて暗号資産の価格や市場データを取得するMCP対応サーバーです。整形された相場情報を必要とするAIエージェントやクライアントを対象としています。
主な機能
- 01暗号資産のリアルタイム価格取得
- 02Alpaca REST API経由の市場データ取得
- 03エージェント向けの整形済み応答
活用シーン
- 01BTC/USDの現在価格を調べる
- 02AIクライアントから暗号資産市場データを確認
MCP 対応
Alpaca APIを利用し、暗号資産の価格や市場データを検索するツールをMCPクライアントに提供します。
設定
ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。
このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。
ツール
ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。
詳細
🪙 ALPACA API を使用した MCP 暗号資産価格検索サーバー
このプロジェクトでは、ALPACA API を活用し、MCP(Multi-Agent Control Protocol) との統合を目的とした 暗号資産価格検索サービス を構築します。
このサーバーを使用すると、AI エージェントやクライアントは Alpaca の API を利用して、リアルタイムの暗号資産価格と市場データを効率よく取得できます。
📦 ステップ 1:uv で環境をセットアップする
高速で最新の Python パッケージマネージャーである uv を使用して、プロジェクト環境を作成・管理します。
🛠️ インストールとセットアップ
これらのコマンドは、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
💡 ツールが Alpaca API とどのように連携するかについては、mcp_server.py をご覧ください。
サーバーについて
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、そしてちょっとしたオタクの野心で自分だけの株価サーバーを構築した方法
学習を楽しみましょう!
コメント
AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。