bybit-official-trading-server 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -0
  3. package/README.zh.md +203 -0
  4. package/package.json +41 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 bybit-exchange
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,203 @@
1
+ # @bybit-exchange/mcp-server
2
+
3
+ A [Model Context Protocol (MCP)](https://modelcontextprotocol.io) server that exposes Bybit REST and WebSocket APIs as MCP tools, enabling AI assistants (Claude, Cursor, etc.) to query market data and manage accounts on Bybit.
4
+
5
+ [中文文档](./README.zh.md)
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - **Market Data** — Klines, orderbook, tickers, funding rates, open interest, risk limits, and more (22 tools, no auth required)
12
+ - **Account** — Wallet balance, transaction logs, fee rates, collateral info, MMP state, DCP config, SMP group, and more (11 tools, auth required)
13
+ - **Asset** — Asset overview, portfolio margin, delivery/settlement records, total member assets (5 tools, auth required)
14
+ - **User** — API key info, sub-account listing, member account type, referral queries (7 tools, auth required)
15
+ - **WebSocket** — Subscribe-snapshot pattern for real-time orderbook, tickers, klines, executions, positions, wallet, RFQ, spread trading, and more (26 tools)
16
+ - **HMAC-SHA256 signing** — Automatic request signing for all authenticated endpoints
17
+ - **Rate limiting** — Per-endpoint rate limiting built in
18
+ - **Mainnet / Testnet** — Switch via environment variable
19
+
20
+ ---
21
+
22
+ ## Requirements
23
+
24
+ - Node.js >= 20.6
25
+
26
+ ---
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install -g @bybit-exchange/mcp-server
32
+ ```
33
+
34
+ Or run directly with `npx`:
35
+
36
+ ```bash
37
+ npx @bybit-exchange/mcp-server
38
+ ```
39
+
40
+ ---
41
+
42
+ ## Configuration
43
+
44
+ Set the following environment variables before starting the server:
45
+
46
+ | Variable | Required | Default | Description |
47
+ |----------|----------|---------|-------------|
48
+ | `BYBIT_API_KEY` | For auth endpoints | — | Your Bybit API key |
49
+ | `BYBIT_API_SECRET` | For auth endpoints | — | Your Bybit API secret |
50
+ | `BYBIT_TESTNET` | No | `false` | Set to `true` to use the testnet |
51
+
52
+ Market data tools work without credentials. Account, asset, user, and WebSocket private channel tools require `BYBIT_API_KEY` and `BYBIT_API_SECRET`.
53
+
54
+ ---
55
+
56
+ ## Usage with Claude Desktop
57
+
58
+ > **First-time setup:** Claude Desktop will show an authorization prompt the first time each tool is called. Click **"Always allow"** to permanently approve it — you won't be asked again.
59
+
60
+ Add the following to your `claude_desktop_config.json`:
61
+
62
+ ```json
63
+ {
64
+ "mcpServers": {
65
+ "bybit": {
66
+ "command": "npx",
67
+ "args": ["-y", "@bybit-exchange/mcp-server"],
68
+ "env": {
69
+ "BYBIT_API_KEY": "your_api_key",
70
+ "BYBIT_API_SECRET": "your_api_secret"
71
+ }
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ For testnet:
78
+
79
+ ```json
80
+ {
81
+ "mcpServers": {
82
+ "bybit": {
83
+ "command": "npx",
84
+ "args": ["-y", "@bybit-exchange/mcp-server"],
85
+ "env": {
86
+ "BYBIT_API_KEY": "your_testnet_api_key",
87
+ "BYBIT_API_SECRET": "your_testnet_api_secret",
88
+ "BYBIT_TESTNET": "true"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ ---
96
+
97
+ ## Usage with Cursor / VS Code
98
+
99
+ Add to your MCP settings file:
100
+
101
+ ```json
102
+ {
103
+ "mcpServers": {
104
+ "bybit": {
105
+ "command": "npx",
106
+ "args": ["-y", "@bybit-exchange/mcp-server"],
107
+ "env": {
108
+ "BYBIT_API_KEY": "your_api_key",
109
+ "BYBIT_API_SECRET": "your_api_secret"
110
+ }
111
+ }
112
+ }
113
+ }
114
+ ```
115
+
116
+ ---
117
+
118
+ ## Available Tool Categories
119
+
120
+ | Category | Auth | Description |
121
+ |----------|------|-------------|
122
+ | `market` | No | Klines, orderbook, tickers, funding rates, open interest, volatility, risk limits, long/short ratio, delivery price, insurance pool, and more (22 tools) |
123
+ | `account` | Yes | Wallet balance, transaction log, fee rates, collateral settings, option Greeks, MMP state, DCP config, SMP group, account instruments, and more (11 tools) |
124
+ | `asset` | Yes | Asset overview, portfolio margin, delivery/settlement records, aggregated parent+sub account assets (5 tools) |
125
+ | `user` | Yes | API key info & permissions, sub-account listing, member account types, referral/invitation queries (7 tools) |
126
+ | `websocket` | Mixed | Real-time snapshots via subscribe-snapshot pattern: orderbook, tickers, klines, trades, liquidations, executions, positions, wallet, option Greeks, RFQ block trades, spread trading (26 tools) |
127
+
128
+ ---
129
+
130
+ ## Example Prompts
131
+
132
+ Once connected to an AI assistant, you can use natural language:
133
+
134
+ **Market data:**
135
+ - "What is the current BTC/USDT price?"
136
+ - "Show me the order book for ETHUSDT with depth 50"
137
+ - "Get the last 10 BTC perpetual klines on the 1-hour interval"
138
+ - "What are the current funding rates for the top 5 perpetual contracts?"
139
+ - "What's the open interest for BTCUSDT?"
140
+
141
+ **Account & Asset:**
142
+ - "What's my wallet balance?"
143
+ - "Show me my recent transaction log"
144
+ - "What are my maker/taker fee rates?"
145
+ - "Show me my total assets across all sub-accounts"
146
+ - "What's my portfolio margin status?"
147
+
148
+ **User & Sub-accounts:**
149
+ - "List all my sub-accounts"
150
+ - "Show me the permissions and VIP level of my current API key"
151
+ - "What account types do my sub-accounts use?"
152
+ - "Who have I invited through the referral program?"
153
+
154
+ **WebSocket / Real-time:**
155
+ - "Subscribe to the BTCUSDT orderbook and give me a snapshot"
156
+ - "Get the latest execution records from my account"
157
+ - "What are my current open positions?"
158
+
159
+ ---
160
+
161
+ ## WebSocket Pattern Details
162
+
163
+ WebSocket tools are compatible with MCP's request/response model:
164
+
165
+ 1. The tool opens a WebSocket connection to Bybit's streaming endpoint
166
+ 2. Subscribes to the requested channel (with auth handshake for private channels)
167
+ 3. Collects the specified number of messages (default: 1) or waits up to `timeoutMs` (default: 5000 ms)
168
+ 4. Returns the collected snapshot and closes the connection
169
+
170
+ This makes real-time data accessible in a single tool call without managing persistent connections.
171
+
172
+ ---
173
+
174
+ ## Security Notes
175
+
176
+ - API keys are read from environment variables at call time, never hardcoded
177
+ - All authenticated requests use HMAC-SHA256 signing per Bybit's V5 API specification
178
+ - Never share your API secret or commit it to source control
179
+ - Use API keys with minimal required permissions (read-only where possible)
180
+
181
+ ---
182
+
183
+ ## Local Development
184
+
185
+ ```bash
186
+ # Install dependencies
187
+ npm install
188
+
189
+ # Start the server in development mode
190
+ npm run dev
191
+
192
+ # Type check
193
+ npm run typecheck
194
+
195
+ # Build for production
196
+ npm run build
197
+ ```
198
+
199
+ ---
200
+
201
+ ## License
202
+
203
+ MIT
package/README.zh.md ADDED
@@ -0,0 +1,203 @@
1
+ # @bybit-exchange/mcp-server
2
+
3
+ 将 Bybit REST 和 WebSocket API 封装为 [MCP(模型上下文协议)](https://modelcontextprotocol.io) 工具的服务器,让 AI 助手(Claude、Cursor 等)可以直接用自然语言查询行情数据、管理 Bybit 账户。
4
+
5
+ [English Documentation](./README.md)
6
+
7
+ ---
8
+
9
+ ## 功能特性
10
+
11
+ - **行情数据** — K 线、订单薄、Ticker、资金费率、持仓量、风险限额等(22 个工具,无需鉴权)
12
+ - **账户管理** — 钱包余额、交易日志、手续费率、抵押品信息、MMP 状态、DCP 配置、SMP 分组等(11 个工具,需要鉴权)
13
+ - **资产管理** — 资产总览、组合保证金、交割/结算记录、全账户聚合资产(5 个工具,需要鉴权)
14
+ - **用户管理** — API Key 信息、子账户列表、账户类型查询、邀请返佣查询(7 个工具,需要鉴权)
15
+ - **WebSocket** — 快照模式实时订阅订单薄、Ticker、K 线、成交、仓位、钱包、RFQ、价差合约等(26 个工具)
16
+ - **HMAC-SHA256 签名** — 鉴权请求自动签名,无需手动处理
17
+ - **频率限制** — 内置按接口的频率控制
18
+ - **主网 / 测试网** — 通过环境变量切换
19
+
20
+ ---
21
+
22
+ ## 环境要求
23
+
24
+ - Node.js >= 20.6
25
+
26
+ ---
27
+
28
+ ## 安装
29
+
30
+ ```bash
31
+ npm install -g @bybit-exchange/mcp-server
32
+ ```
33
+
34
+ 或直接通过 `npx` 运行(无需全局安装):
35
+
36
+ ```bash
37
+ npx @bybit-exchange/mcp-server
38
+ ```
39
+
40
+ ---
41
+
42
+ ## 配置
43
+
44
+ 启动前设置以下环境变量:
45
+
46
+ | 变量名 | 是否必填 | 默认值 | 说明 |
47
+ |--------|----------|--------|------|
48
+ | `BYBIT_API_KEY` | 鉴权接口必填 | — | Bybit API Key |
49
+ | `BYBIT_API_SECRET` | 鉴权接口必填 | — | Bybit API Secret |
50
+ | `BYBIT_TESTNET` | 否 | `false` | 设为 `true` 使用测试网 |
51
+
52
+ 行情类工具无需填写 API 凭证。账户、资产、用户类工具以及 WebSocket 私有频道需要同时设置 `BYBIT_API_KEY` 和 `BYBIT_API_SECRET`。
53
+
54
+ ---
55
+
56
+ ## 在 Claude Desktop 中使用
57
+
58
+ > **首次使用提示:** Claude Desktop 会在每个工具第一次调用时弹出授权确认框。点击 **"Always allow"** 即可永久免授权,后续调用不再弹出。
59
+
60
+ 在 `claude_desktop_config.json` 中添加以下配置:
61
+
62
+ ```json
63
+ {
64
+ "mcpServers": {
65
+ "bybit": {
66
+ "command": "npx",
67
+ "args": ["-y", "@bybit-exchange/mcp-server"],
68
+ "env": {
69
+ "BYBIT_API_KEY": "你的 API Key",
70
+ "BYBIT_API_SECRET": "你的 API Secret"
71
+ }
72
+ }
73
+ }
74
+ }
75
+ ```
76
+
77
+ 使用测试网:
78
+
79
+ ```json
80
+ {
81
+ "mcpServers": {
82
+ "bybit": {
83
+ "command": "npx",
84
+ "args": ["-y", "@bybit-exchange/mcp-server"],
85
+ "env": {
86
+ "BYBIT_API_KEY": "你的测试网 API Key",
87
+ "BYBIT_API_SECRET": "你的测试网 API Secret",
88
+ "BYBIT_TESTNET": "true"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ ---
96
+
97
+ ## 在 Cursor / VS Code 中使用
98
+
99
+ 在 MCP 配置文件中添加:
100
+
101
+ ```json
102
+ {
103
+ "mcpServers": {
104
+ "bybit": {
105
+ "command": "npx",
106
+ "args": ["-y", "@bybit-exchange/mcp-server"],
107
+ "env": {
108
+ "BYBIT_API_KEY": "你的 API Key",
109
+ "BYBIT_API_SECRET": "你的 API Secret"
110
+ }
111
+ }
112
+ }
113
+ }
114
+ ```
115
+
116
+ ---
117
+
118
+ ## 工具分类
119
+
120
+ | 分类 | 是否需要鉴权 | 说明 |
121
+ |------|------------|------|
122
+ | `market` | 否 | K 线、订单薄、Ticker、资金费率、持仓量、历史波动率、风险限额、多空比、交割价格、保险基金等(22 个工具) |
123
+ | `account` | 是 | 钱包余额、交易日志、手续费率、抵押品设置、期权希腊值、MMP 状态、断线保护、防自成交分组、可划转金额等(11 个工具) |
124
+ | `asset` | 是 | 资产总览、组合保证金、交割/结算记录、母子账户聚合资产(5 个工具) |
125
+ | `user` | 是 | API Key 信息与权限、子账户列表、成员账户类型、邀请返佣查询(7 个工具) |
126
+ | `websocket` | 混合 | 订阅-快照模式实时数据:订单薄、Ticker、K 线、成交、强平、仓位、钱包、期权希腊值、RFQ 大宗交易、价差合约等(26 个工具) |
127
+
128
+ ---
129
+
130
+ ## 使用示例
131
+
132
+ 接入 AI 助手后,可以用自然语言提问:
133
+
134
+ **行情数据:**
135
+ - "BTC/USDT 现在多少钱?"
136
+ - "给我看一下 ETHUSDT 深度为 50 的订单薄"
137
+ - "查一下 BTC 永续合约最近 10 根小时 K 线"
138
+ - "前 5 大永续合约当前资金费率分别是多少?"
139
+ - "BTCUSDT 当前的未平仓量是多少?"
140
+
141
+ **账户与资产:**
142
+ - "我的钱包余额是多少?"
143
+ - "查一下我最近的交易日志"
144
+ - "我的 Maker/Taker 费率是多少?"
145
+ - "列出我所有账户的聚合资产"
146
+ - "查看我的组合保证金状态"
147
+
148
+ **用户与子账户:**
149
+ - "列出我所有的子账户"
150
+ - "查看当前 API Key 的权限和 VIP 等级"
151
+ - "我的子账户分别是什么账户类型?"
152
+ - "查看我通过邀请码邀请的用户"
153
+
154
+ **WebSocket 实时数据:**
155
+ - "订阅 BTCUSDT 订单薄,给我一个快照"
156
+ - "获取我账户最新的成交记录"
157
+ - "查看我当前的仓位状态"
158
+
159
+ ---
160
+
161
+ ## WebSocket 工具说明
162
+
163
+ WebSocket 工具与 MCP 的请求/响应模型兼容:
164
+
165
+ 1. 工具建立 WebSocket 连接到 Bybit 行情推送地址
166
+ 2. 订阅指定频道(私有频道会先完成鉴权握手)
167
+ 3. 收集指定数量的消息(默认 1 条),或等待 `timeoutMs`(默认 5000 ms)后超时返回
168
+ 4. 返回快照数据并关闭连接
169
+
170
+ 这样一来,实时数据可在单次工具调用中完整返回,无需维护长连接。
171
+
172
+ ---
173
+
174
+ ## 安全注意事项
175
+
176
+ - API Key 从环境变量读取,不会硬编码在任何地方
177
+ - 所有鉴权请求遵循 Bybit V5 API 规范,使用 HMAC-SHA256 签名
178
+ - 请勿泄露 API Secret,也不要将其提交到代码仓库
179
+ - 建议为 API Key 设置最小权限(只读权限优先)
180
+
181
+ ---
182
+
183
+ ## 本地开发
184
+
185
+ ```bash
186
+ # 安装依赖
187
+ npm install
188
+
189
+ # 开发模式启动
190
+ npm run dev
191
+
192
+ # 类型检查
193
+ npm run typecheck
194
+
195
+ # 生产构建
196
+ npm run build
197
+ ```
198
+
199
+ ---
200
+
201
+ ## License
202
+
203
+ MIT
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "bybit-official-trading-server",
3
+ "version": "2.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Bybit trading MCP server — exposes Bybit REST and WebSocket APIs as MCP tools",
8
+ "type": "module",
9
+ "main": "dist/index.js",
10
+ "bin": {
11
+ "trading-mcp": "dist/index.js"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsup",
18
+ "dev": "node --env-file=.env --import tsx/esm src/index.ts",
19
+ "typecheck": "tsc --noEmit",
20
+ "bump": "bash scripts/bump-version.sh",
21
+ "gen-manifest": "bash scripts/gen-manifest.sh"
22
+ },
23
+ "dependencies": {
24
+ "@modelcontextprotocol/sdk": "^1.10.2",
25
+ "ws": "^8.18.0",
26
+ "zod": "^3.24.2",
27
+ "zod-to-json-schema": "^3.24.5"
28
+ },
29
+ "devDependencies": {
30
+ "js-yaml": "^4.1.0",
31
+ "@types/js-yaml": "^4.0.9",
32
+ "@types/node": "^22.0.0",
33
+ "@types/ws": "^8.5.13",
34
+ "tsup": "^8.4.0",
35
+ "tsx": "^4.19.3",
36
+ "typescript": "^5.7.3"
37
+ },
38
+ "engines": {
39
+ "node": ">=20.6"
40
+ }
41
+ }