Crypto MCP Server 🚀
2025 年 7 月 · MCP 协议发布之初的首批实践之一
在 Model Context Protocol 刚公开时就投入开发的早期 MCP Server,国内较早的 MCP 探索项目之一。
一套完整的加密货币市场数据 MCP 服务端 + 客户端工具链。提供实时行情、K 线、资金费率、市场深度、行业新闻和深度搜索能力,可直接被任何 MCP 兼容的 Agent 作为工具调用。
背景
2025 年 7 月,Anthropic 开源了 MCP(Model Context Protocol)协议。当时市面上几乎没有现成的 MCP Server 实现,文档和工具链都不完善。这个项目就是在那个阶段开始的——从零搭建 MCP Server 框架、实现 stdio 传输、注册工具、编写客户端库,算是国内最早一批把 MCP 跑起来的尝试。
项目结构保留了早期的探索痕迹:有专门的 MCP Client 库(mcp_client.py),有 LLM 编排层,也有后来追加的 WebUI 管理后台。不是最优雅的代码,但每一行都是当时真实踩坑的记录。
功能一览
系统架构
Agent / 用户
│ stdio (MCP 协议)
▼
┌───────────────────────┐ ┌──────────────────┐
│ MCP Client 库 │◄──────►│ FastAPI WebUI │
│ (mcp_client.py) │ │ (可视化管理) │
└──────────┬────────────┘ └──────────────────┘
│ stdio
▼
┌───────────────────────┐
│ MCP Server 层 │
│ FastMCP / @mcp.tool │
├───────────────────────┤
│ crypto_mcp_server │ 行情 / K 线 / 资金费率 / 深度
│ deepsearch_server │ 深度搜索 + LLM 总结
│ weather_server │ 模块化扩展示例
└───────────────────────┘
│
▼
┌───────────────────────┐
│ LLM 编排层 │ Moonshot / OpenRouter
│ (重试退避) │ exponential backoff
└───────────────────────┘
项目结构
crypto_mcp_server.py # 行情 MCP Server(核心)
deepsearch_mcp_server.py # 深度搜索 MCP Server
weather_mcp_server.py # 天气 MCP Server(示例)
mcp_client.py # MCP Client 库(LLM 编排 + 重试退避)
webui_fastapi.py # FastAPI WebUI 后台
static/ # WebUI 前端资源
templates/ # WebUI 页面模板
config.json # LLM 参数配置
mcp.json # MCP Server 启动配置
.env.example # 环境变量模板
requirements.txt # Python 依赖
快速开始
1. 安装
git clone https://github.com/Ibook000/crypto_mcp_server.git
cd crypto_mcp_server
pip install -r requirements.txt
2. 配置密钥
cp .env.example .env
编辑 .env 填入你的密钥:
⚠️
.env已加入.gitignore,不会提交到仓库。代码统一通过os.environ.get()读取。
3. 启动 MCP Server
python crypto_mcp_server.py # 加密货币行情
python deepsearch_mcp_server.py # 深度搜索
python weather_mcp_server.py # 天气
每个 Server 以 stdio 方式运行,可被任意 MCP 兼容的 Agent(Claude Code、Cursor、Codex 等)调用。
4. 通过 Client 调用
from mcp_client import MCPClient
import asyncio
client = MCPClient()
async def main():
for name in client.mcp_servers:
await client.connect_to_server(name)
result = await client.process_query("BTC 现在的价格是多少?")
print(result)
asyncio.run(main())
5. 启动 WebUI
python webui_fastapi.py
# 打开 http://localhost:8000
MCP 工具一览
crypto_mcp_server.py
deepsearch_mcp_server.py
WebUI 截图
WebUI 管理后台基于 FastAPI 构建,支持多 Server 连接监控和对话式查询。


技术要点
- MCP 协议:基于
stdio传输,使用FastMCP+@mcp.tool()注册工具 - 异步 I/O:
httpx+asyncio,单币种和批量请求都支持 - 重试退避:Client 内置指数退避 + 随机抖动,应对 API 限流
- 容错设计:所有数据获取函数返回结构化错误,格式化函数有边界校验
- 模块化扩展:新增数据源只需实现
fetch_xxx+format_xxx+@mcp.tool()三步
License
MIT