内容
[](https://mseep.ai/app/badger3000-okx-mcp-server)
# OKX MCP Server
这个项目创建了一个模型上下文协议(MCP)服务器,用于从OKX交易所获取实时加密货币数据。它允许像Claude这样的人工智能助手通过定义的工具访问最新的加密货币价格信息和历史数据,并具有增强的可视化功能和WebSocket实时更新。
## Features
- `get_price`:获取加密货币交易对的最新价格数据,并进行可视化格式化
- `get_candlesticks`:检索历史K线数据,并提供包括ASCII图表在内的可视化选项
- `subscribe_ticker`:订阅交易对的实时WebSocket更新
- `get_live_ticker`:从WebSocket连接检索最新的实时数据
- `unsubscribe_ticker`:停止接收特定交易对的更新
## Prerequisites
- Node.js (v16 or higher recommended)
- npm or yarn
- VSCode with Claude extension (if using VSCode integration)
- Claude Desktop (if not using VSCode)
## Installation
1. 克隆存储库
```bash
git clone https://github.com/yourusername/okx-mcp-server.git
cd okx-mcp-server
```
2. 安装依赖项
```bash
npm install
```
3. 添加WebSocket依赖项
```bash
npm install ws
npm install --save-dev @types/ws
```
4. 构建项目
```bash
npm run build
```
5. 使编译后的脚本可执行
```bash
chmod +x build/index.js
```
## Usage
### Running the Server Directly
你可以直接运行服务器:
```bash
npm start
```
Or:
```bash
node build/index.js
```
### Testing with MCP Inspector
在集成之前测试你的 MCP 服务器:
```bash
npx @modelcontextprotocol/inspector node build/index.js
```
在检查器中,你可以测试:
- `get_price` with input: `{ "instrument": "BTC-USDT", "format": "markdown" }`
- `get_candlesticks` with input: `{ "instrument": "BTC-USDT", "bar": "1m", "limit": 10, "format": "markdown" }`
- `subscribe_ticker` with input: `{ "instrument": "BTC-USDT" }`
- `get_live_ticker` with input: `{ "instrument": "BTC-USDT", "format": "markdown" }`
- `unsubscribe_ticker` with input: `{ "instrument": "BTC-USDT" }`
每个工具支持不同的可视化格式:
- `format`:在 `markdown`、`json` 或 `table`(用于K线图)之间选择
### Integration with VSCode
1. 安装用于VSCode的Claude扩展
2. 在VSCode设置中配置MCP服务器:
- 创建或编辑以下文件:
```
~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json
```
- 添加以下配置:
```json
{
"mcpServers": {
"okx": {
"command": "node",
"args": ["/absolute/path/to/okx-mcp-server/build/index.js"],
"disabled": false,
"autoApprove": []
}
}
}
```
- 将 `/absolute/path/to/okx-mcp-server/build/index.js` 替换为你的实际文件路径
3. 重新启动VSCode或重新加载Claude扩展
### Integration with Claude Desktop
如果使用Claude Desktop,请查看其文档,了解放置MCP配置设置的适当位置。
## Example Prompts
集成后,你可以问Claude:
1. "What's the current price of Bitcoin (BTC-USDT)? Show me a nicely formatted display."
2. "Show me the price trend of Ethereum (ETH-USDT) over the last hour using 5-minute intervals with a visual representation."
3. "Compare the current prices of BTC-USDT and ETH-USDT."
4. "Analyze the most recent 20 candlesticks for SOL-USDT with 1-minute intervals and display them in a table format."
5. "Is the current price of BTC-USDT higher or lower than its 24-hour high? Visualize the price range."
6. "Subscribe to live updates for Bitcoin price and tell me when it changes."
7. "Show me the latest real-time data for Ethereum."
8. "Monitor BTC-USDT in real-time and alert me if the price moves by more than 1% in either direction."
## Data Visualization Features
增强的 MCP 服务器提供丰富的数据可视化:
### Price Data Visualization
- 格式化的markdown输出,包含清晰的价格信息
- 可视化的价格范围条,显示当前价格在24小时高点/低点之间的位置
- 价格变动的方向指示器(▲/▼)
- 格式化的数字,以提高可读性
### Candlestick Data Visualization
- **Markdown format**: Includes summary statistics, ASCII chart of price movements, and recent price action table
- **Table format**: Clean tabular presentation of candlestick data
- **JSON format**: Raw data for programmatic use
### Format Options
- **get_price**: Supports `markdown` (default) or `json` formats
- **get_candlesticks**: Supports `markdown` (default), `table`, or `json` formats
- **get_live_ticker**: Supports `markdown` (default) or `json` formats
## WebSocket Real-time Updates
此服务器包括WebSocket支持,用于接收来自OKX的实时数据:
### How WebSockets Work
- 建立与OKX的WebSocket API的持久连接
- 只要价格发生变化,就会收到推送更新
- 维护每个订阅工具的最新数据缓存
- 如果连接丢失,自动重新连接
### WebSocket Tools
- **subscribe_ticker**: Starts a subscription for real-time ticker updates
- **get_live_ticker**: Gets the latest data from active subscriptions
- **unsubscribe_ticker**: Ends a subscription when monitoring is no longer needed
### Use Cases for Real-time Updates
- 实时监控价格变动
- 设置价格警报
- 跟踪市场波动
- 根据最新数据做出明智的交易决策
## Environment Variables (Optional)
这个基本实现使用OKX的公共API端点,不需要身份验证。如果你扩展服务器以使用经过身份验证的端点,你可能需要添加以下环境变量:
```
OKX_API_KEY=your_api_key
OKX_API_SECRET=your_api_secret
OKX_API_PASSPHRASE=your_api_passphrase
```
## Security Notes
- 当前的实现仅使用公共OKX API端点,因此不需要API密钥
- 代码库中没有存储敏感数据
- 可以安全地将此代码提交到Git
## Extending the Server
你可以通过以下方式扩展此 MCP 服务器:
1. 为其他OKX API端点添加更多工具
2. 使用API密钥实现经过身份验证的端点
3. 添加对其他交易所的支持
4. 通过更复杂的图表进一步增强可视化效果
5. 添加技术分析指标(RSI、MACD、移动平均线)
6. 为多种加密货币实现价格比较工具
7. 为更广泛的市场分析添加市场摘要功能
## License
MIT