使用Encore.ts实现的用于MEXC数字货币交易所集成的模型上下文协议(MCP)服务器。
git clone https://github.com/RyanLisse/mexc-mcp-server.git
cd mexc-mcp-server
bun install
在项目根目录创建一个.env文件:
# MEXC API凭证
MEXC_API_KEY=your_mexc_api_key_here
MEXC_SECRET_KEY=your_mexc_secret_key_here
# 启动开发服务器
bun run dev
# 运行测试(带监视模式)
bun run test:watch
# 运行所有质量检查
bun run check
# 格式化和代码检查
bun run format
bun run lint:fix
# 运行所有测试
bun test
# 运行带有覆盖率的测试
bun run test:coverage
# 运行特定测试文件
bun test auth/auth.test.ts
# 测试MEXC API连接
bun run test:mexc
| 工具名称 | 描述 | 输入参数 |
|---|---|---|
mexc_get_ticker | 获取当前报价和24小时统计数据 | symbol: string, convert?: string |
mexc_get_order_book | 获取当前订单簿(买/卖) | symbol: string, limit?: number |
mexc_get_24h_stats | 获取24小时交易统计数据 | symbol?: string |
mexc_test_connectivity | 测试API连接性和服务器时间 | 无 |
mexc_test_authentication | 测试API认证 | 无 |
mexc_get_active_symbols | 获取所有活跃交易符号 | limit?: number |
mexc_place_order | 下买单或卖单 | `symbol: string, side: 'buy' |
mexc_cancel_order | 取消现有订单 | symbol: string, orderId: string |
mexc_get_order_status | 获取订单状态和详情 | symbol: string, orderId: string |
mexc_get_account_balance | 获取账户余额 | 无 |
mexc_get_open_orders | 获取所有未完成订单 | symbol?: string |
mexc_get_order_history | 获取订单历史 | symbol?: string, limit?: number |
mexc_get_trade_history | 获取交易执行历史 | symbol?: string, limit?: number |
安装Encore CLI:
curl -L https://encore.dev/install.sh | bash
身份验证:
encore auth login
设置秘密:
# 对于测试环境
encore secret set --env=staging MEXC_API_KEY "your_api_key"
encore secret set --env=staging MEXC_SECRET_KEY "your_secret_key"
# 对于生产环境
encore secret set --env=production MEXC_API_KEY "your_api_key"
encore secret set --env=production MEXC_SECRET_KEY "your_secret_key"
部署:
# 部署到测试环境
encore deploy --env=staging
# 部署到生产环境
encore deploy --env=production
该项目包括GitHub Actions工作流程以实现自动化部署:
CI流水线(.github/workflows/ci.yml):在所有PR和推送时运行
部署流水线(.github/workflows/deploy.yml):部署到Encore云
在你的GitHub仓库设置中配置这些秘密:
ENCORE_AUTH_TOKEN=your_encore_auth_token
ENCORE_INSTALL_ID=your_encore_install_id
MEXC_API_KEY=your_mexc_api_key
MEXC_SECRET_KEY=your_mexc_secret_key
CODECOV_TOKEN=your_codecov_token # 可选,用于覆盖报告
此项目使用Encore.ts内置的秘密管理来安全存储凭证:
// 在shared/config.ts中
import { secret } from "encore.dev/config";
// 秘密由Encore.ts自动注入
const mexcApiKey = secret("MEXC_API_KEY");
const mexcSecretKey = secret("MEXC_SECRET_KEY");
// 本地开发时使用环境变量
const localConfig = {
apiKey: process.env.MEXC_API_KEY || mexcApiKey(),
secretKey: process.env.MEXC_SECRET_KEY || mexcSecretKey(),
};
# 为不同环境设置秘密
encore secret set --env=development MEXC_API_KEY "your_dev_api_key"
encore secret set --env=development MEXC_SECRET_KEY "your_dev_secret_key"
encore secret set --env=staging MEXC_API_KEY "your_staging_api_key"
encore secret set --env=staging MEXC_SECRET_KEY "your_staging_secret_key"
encore secret set --env=production MEXC_API_KEY "your_prod_api_key"
encore secret set --env=production MEXC_SECRET_KEY "your_prod_secret_key"
Husky预提交钩子确保代码质量:
# 在git提交时自动运行
- Lint-staged(格式化和检查暂存文件)
- 类型检查
- 测试执行
.github/workflows/ci.yml).github/workflows/deploy.yml)GET /health - 服务健康检查GET /mcp/info - MCP协议信息GET / - API概览POST /auth/validate - 验证API密钥GET /auth/status - 认证状态POST /auth/rate-limit - 速率限制状态GET /auth/test-mexc - 测试MEXC凭证POST /market-data/ticker - 获取报价数据POST /market-data/order-book - 获取订单簿POST /market-data/24h-stats - 获取24小时统计数据GET /market-data/test-connectivity - 测试连接GET /market-data/test-auth - 测试认证POST /market-data/active-symbols - 获取活跃符号GET /market-data/health - 市场数据服务健康GET /market-data/mcp/tools - 可用的MCP工具POST /trading/place-order - 下新订单POST /trading/cancel-order - 取消现有订单POST /trading/order-status - 获取订单状态GET /trading/open-orders - 获取未完成订单POST /trading/order-history - 获取订单历史POST /trading/trade-history - 获取交易历史GET /trading/health - 交易服务健康GET /portfolio/balance - 获取账户余额GET /portfolio/positions - 获取开放头寸POST /portfolio/pnl - 获取利润/损失数据GET /portfolio/health - 资产组合服务健康GET /tools/list - 列出所有可用的MCP工具POST /tools/call - 执行MCP工具GET /tools/resources - 列出MCP资源POST /tools/resources/read - 读取MCP资源内容应用程序使用微服务架构构建,包含5个主要服务:
mexc-mcp-server/
├── encore.service.ts # 主服务定义
├── api.ts # 根API端点
├── auth/ # 认证服务
│ ├── encore.service.ts # 服务定义
│ ├── api.ts # 认证端点
│ └── auth.ts # 认证逻辑
├── market-data/ # 市场数据服务
│ ├── encore.service.ts # 服务定义
│ ├── api.ts # 市场数据端点
│ ├── tools.ts # MCP工具实现
│ └── mexc-client.ts # MEXC API客户端
├── trading/ # 交易操作服务
│ ├── encore.service.ts # 服务定义
│ ├── api.ts # 交易端点
│ └── tools.ts # 交易MCP工具
├── portfolio/ # 资产组合管理服务
│ ├── encore.service.ts # 服务定义
│ ├── api.ts # 资产组合端点
│ └── tools.ts # 资产组合MCP工具
└── tools/ # MCP工具聚合服务
├── encore.service.ts # 服务定义
└── api.ts # MCP协议端点
git checkout -b feature/amazing-featurebun testgit commit -m "feat: 添加神奇的功能"git push origin feature/amazing-featureMIT许可证 - 详情见LICENSE文件。