Santhosh-dev-lab/crypto_mcp_server
If you are the rightful owner of crypto_mcp_server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The Cryptocurrency Market Data Protocol (MCP) Server provides real-time and historical cryptocurrency market data with a focus on caching, clear data contracts, and high reliability.
💰 Cryptocurrency Market Data Protocol (MCP) Server
A robust, asynchronous, and fully tested server that provides real-time and historical cryptocurrency market data.
The system is designed following modern Market Data Protocol (MCP) principles: caching, clear data contracts, and high reliability.
📘 Introduction
This project implements a high-performance cryptocurrency market data server using FastAPI and CCXT, delivering a stable API for real-time and historical price data.
It emphasizes:
- Efficiency (async ASGI stack)
- Abstraction (CCXT unified API)
- Reliability (caching + strict validation)
- Test coverage (7 automated tests)
🏛 Architectural Overview
Core Strategy
A professional market data API requires reliability, low latency, and rate-limit awareness.
This system achieves that using:
- Asynchronous FastAPI for high throughput
- Pydantic for enforcement of strict and predictable data contracts
- CCXT for safe and unified access to exchange data
- 5-second TTL caching to reduce API load and avoid hitting rate limits
🔧 Key Components
| Component | Technology | Rationale |
|---|---|---|
| Server Framework | FastAPI | Fast, async-ready, auto validation + docs |
| Market Data Source | CCXT | Unified exchange API abstraction |
| Caching | Custom DataCacher with TTL | Prevents excessive external API calls |
| Routing | {symbol:path} | Supports symbols like BTC/USDT |
Setup and Validation (Evaluator’s Guide)
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
The project includes a comprehensive test suite (test_crypto_data.py) covering:
-
Real-time data retrieval
-
Historical OHLCV retrieval
-
Cache refresh behavior
-
Invalid symbol error handling
-
API stability
Run tests:
pytest
Expected output:
9 passed
API Usage Guide:
Start the Server
uvicorn crypto_data_server:app --reload
Interactive Documentation
Open Swagger UI:
http://127.0.0.1:8000/docs
Authentication Requirement
All /realtime and /historical endpoints are protected and require the following HTTP Header:
-
Header: X-API-Key
-
Value: SECURE_DEV_KEY_12345
Testing the Secured Endpoint (API Key Authentication)
🛑 Browser Test (Access Denied)
-
If you try to access the route directly through a browser, it will fail because the browser cannot send custom headers on simple GET requests.
-
Action: Paste the URL below into your browser's address bar.
http://127.0.0.1:8000/realtime/BTC/USDT
- Result: You will receive a 403 Forbidden error, with the JSON response:
{"detail": "Invalid API Key. Access denied."}
✅ Terminal Test (curl Success)
-
You must use a tool like curl to manually inject the required header, proving the security mechanism works.
-
Action: Run the following command in your terminal (VS Code terminal is perfect for this).
curl -H "X-API-Key: SECURE_DEV_KEY_12345" http://127.0.0.1:8000/realtime/BTC/USDT
- Result: The request will succeed, returning the full JSON market data (price, timestamp, etc.).
Endpoint Examples
| Feature | Example URL | Description |
|---|---|---|
| Health Check | http://127.0.0.1:8000/ | Confirms API + Binance connectivity |
| Real-Time BTC/USDT Price | http://127.0.0.1:8000/realtime/BTC/USDT | Returns current price (cached 5s) |
| Real-Time ETH/USDT Price | http://127.0.0.1:8000/realtime/ETH/USDT | Works for any valid trading pair |
| Historical OHLCV | http://127.0.0.1:8000/historical/SOL/USDT?timeframe=4h&limit=5 | Fetches 5 candlestick entries |
| Invalid Pair Test | http://127.0.0.1:8000/realtime/NONEXISTENT/PAIR | Returns 404 with clean JSON error |
Caching & Price Discrepancy Explanation
You may observe slight price differences vs. Binance's website.
| Platform | Data Source | Behavior |
|---|---|---|
| Binance Website | WebSocket stream | Instant, real-time price updates |
| This API | REST API + caching | 5-second TTL for reliability |
This difference confirms that the caching layer is functioning correctly — a deliberate design choice to prevent rate limiting and ensure stability
⭐ Features
-
⚡ Async FastAPI backend
-
🛡 API Key Authentication
-
🔄 5-second TTL caching
-
🔌 CCXT-powered unified exchange abstraction
-
🧪 Automated 7-test suite
-
🛡 Clean structured error handling
-
🔗 Supports symbols containing / (e.g., BTC/USDT)
-
📘 Automatic Swagger documentation
🐞 Troubleshooting
| Issue | Explanation / Fix |
|---|---|
| Price seems outdated | Cache TTL still active (wait 5 seconds). |
| 404 for trading pair | Symbol may not exist on Binance. |
| Tests failing | Ensure stable internet (CCXT fetches live data). |
| Server doesn't start | Use correct module path: crypto_data_server:app |
| 403 Forbidden | Missing or incorrect X-API-Key header. |