futu-stock-mcp-server 0.1.9__py3-none-any.whl
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.
- futu_stock_mcp_server/__init__.py +0 -0
- futu_stock_mcp_server/server.py +1850 -0
- futu_stock_mcp_server-0.1.9.dist-info/METADATA +842 -0
- futu_stock_mcp_server-0.1.9.dist-info/RECORD +7 -0
- futu_stock_mcp_server-0.1.9.dist-info/WHEEL +4 -0
- futu_stock_mcp_server-0.1.9.dist-info/entry_points.txt +2 -0
- futu_stock_mcp_server-0.1.9.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: futu-stock-mcp-server
|
|
3
|
+
Version: 0.1.9
|
|
4
|
+
Summary: A Model Context Protocol (MCP) server for accessing Futu OpenAPI functionality
|
|
5
|
+
Project-URL: Homepage, https://github.com/shuizhengqi1/futu-stock-mcp-server
|
|
6
|
+
Project-URL: Documentation, https://github.com/shuizhengqi1/futu-stock-mcp-server#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/shuizhengqi1/futu-stock-mcp-server
|
|
8
|
+
Project-URL: Issues, https://github.com/shuizhengqi1/futu-stock-mcp-server/issues
|
|
9
|
+
Author-email: shuizhengqi <shuizhengqi1@163.com>
|
|
10
|
+
Maintainer-email: shuizhengqi <shuizhengqi1@163.com>
|
|
11
|
+
License: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: api,finance,futu,mcp,stock,trading
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: aiohttp
|
|
26
|
+
Requires-Dist: fastapi
|
|
27
|
+
Requires-Dist: futu-api
|
|
28
|
+
Requires-Dist: loguru
|
|
29
|
+
Requires-Dist: mcp[cli]>=1.6.0
|
|
30
|
+
Requires-Dist: psutil
|
|
31
|
+
Requires-Dist: pydantic
|
|
32
|
+
Requires-Dist: python-dotenv
|
|
33
|
+
Requires-Dist: uvicorn
|
|
34
|
+
Requires-Dist: websockets
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: black; extra == 'dev'
|
|
37
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
38
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
39
|
+
Requires-Dist: pytest-asyncio; extra == 'dev'
|
|
40
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Futu Stock MCP Server
|
|
45
|
+
|
|
46
|
+
[](https://www.python.org)
|
|
47
|
+
[](LICENSE)
|
|
48
|
+
[](https://openapi.futunn.com/futu-api-doc/)
|
|
49
|
+
|
|
50
|
+
基于[模型上下文协议(MCP)](https://github.com/cursor-ai/model-context-protocol)的富途证券行情交易接口服务器。将富途OpenAPI功能以标准化的MCP协议提供给AI模型使用,支持行情订阅、数据查询等功能。
|
|
51
|
+
|
|
52
|
+
## 🌟 特性
|
|
53
|
+
|
|
54
|
+
- 🔌 完全兼容 MCP 2.0 协议标准
|
|
55
|
+
- 📊 支持港股、美股、A股等市场的实时行情
|
|
56
|
+
- 🔄 支持实时数据订阅和推送
|
|
57
|
+
- 📈 支持K线、逐笔、买卖盘等多维度数据
|
|
58
|
+
- 🔒 安全的API调用和数据访问机制
|
|
59
|
+
- 🛠 提供完整的开发工具和示例代码
|
|
60
|
+
|
|
61
|
+
## ⚠️ 前置要求
|
|
62
|
+
|
|
63
|
+
在使用本项目之前,您需要:
|
|
64
|
+
|
|
65
|
+
1. 拥有富途证券账户并开通OpenAPI权限
|
|
66
|
+
2. 安装并运行富途的OpenD网关程序([官方文档](https://openapi.futunn.com/futu-api-doc/intro/intro.html))
|
|
67
|
+
3. 根据您的需求订阅相应的行情权限
|
|
68
|
+
|
|
69
|
+
## 🔒 安全提示
|
|
70
|
+
|
|
71
|
+
- 请勿在代码中硬编码任何账号密码信息
|
|
72
|
+
- 确保`.env`文件已添加到`.gitignore`中
|
|
73
|
+
- 妥善保管您的API访问凭证
|
|
74
|
+
- 遵守富途OpenAPI的使用条款和限制
|
|
75
|
+
|
|
76
|
+
## 📝 免责声明
|
|
77
|
+
|
|
78
|
+
本项目是一个开源工具,旨在简化富途OpenAPI的接入流程。使用本项目时请注意:
|
|
79
|
+
|
|
80
|
+
1. 遵守相关法律法规和富途OpenAPI的使用条款
|
|
81
|
+
2. 自行承担使用本项目进行交易的风险
|
|
82
|
+
3. 本项目不提供任何投资建议
|
|
83
|
+
4. 使用本项目前请确保您已获得所需的行情权限
|
|
84
|
+
|
|
85
|
+
## Features
|
|
86
|
+
|
|
87
|
+
- Standard MCP 2.0 protocol compliance
|
|
88
|
+
- Comprehensive Futu API coverage
|
|
89
|
+
- Real-time data subscription support
|
|
90
|
+
- Market data access
|
|
91
|
+
- Derivatives information
|
|
92
|
+
- Account query capabilities
|
|
93
|
+
- Resource-based data access
|
|
94
|
+
- Interactive prompts for analysis
|
|
95
|
+
|
|
96
|
+
## Prerequisites
|
|
97
|
+
|
|
98
|
+
- Python 3.10+
|
|
99
|
+
- Futu OpenAPI SDK
|
|
100
|
+
- Model Context Protocol SDK
|
|
101
|
+
- uv (recommended)
|
|
102
|
+
|
|
103
|
+
## 🚀 快速开始
|
|
104
|
+
|
|
105
|
+
### 方式一:通过 pipx 安装(推荐)
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
# 安装 pipx(如果还没有安装)
|
|
109
|
+
brew install pipx # macOS
|
|
110
|
+
# 或者 pip install --user pipx # 其他系统
|
|
111
|
+
|
|
112
|
+
# 安装包
|
|
113
|
+
pipx install futu-stock-mcp-server
|
|
114
|
+
|
|
115
|
+
# 运行服务器
|
|
116
|
+
futu-mcp-server
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> **为什么使用 pipx?**
|
|
120
|
+
> - pipx 专门用于安装 Python 应用程序到全局环境
|
|
121
|
+
> - 自动管理独立的虚拟环境,避免依赖冲突
|
|
122
|
+
> - 命令直接可用,无需激活虚拟环境
|
|
123
|
+
|
|
124
|
+
### 方式二:通过 Docker 运行
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
# 拉取镜像
|
|
128
|
+
docker pull your-registry/futu-stock-mcp-server:latest
|
|
129
|
+
|
|
130
|
+
# 运行容器
|
|
131
|
+
docker run -d \
|
|
132
|
+
--name futu-mcp-server \
|
|
133
|
+
-p 8000:8000 \
|
|
134
|
+
-e FUTU_HOST=127.0.0.1 \
|
|
135
|
+
-e FUTU_PORT=11111 \
|
|
136
|
+
your-registry/futu-stock-mcp-server:latest
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### 方式三:从源码安装
|
|
140
|
+
|
|
141
|
+
1. Clone the repository:
|
|
142
|
+
```bash
|
|
143
|
+
git clone https://github.com/yourusername/futu-stock-mcp-server.git
|
|
144
|
+
cd futu-stock-mcp-server
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
2. Install uv:
|
|
148
|
+
```bash
|
|
149
|
+
# macOS/Linux
|
|
150
|
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
151
|
+
|
|
152
|
+
# Windows (PowerShell)
|
|
153
|
+
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
3. Create and activate a virtual environment:
|
|
157
|
+
```bash
|
|
158
|
+
# Create virtual environment
|
|
159
|
+
uv venv
|
|
160
|
+
|
|
161
|
+
# Activate virtual environment
|
|
162
|
+
# On macOS/Linux:
|
|
163
|
+
source .venv/bin/activate
|
|
164
|
+
# On Windows:
|
|
165
|
+
.venv\Scripts\activate
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
4. Install dependencies:
|
|
169
|
+
```bash
|
|
170
|
+
# Install in editable mode
|
|
171
|
+
uv pip install -e .
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
5. Copy the environment file and configure:
|
|
175
|
+
```bash
|
|
176
|
+
cp .env.example .env
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Edit the `.env` file with your server settings:
|
|
180
|
+
```
|
|
181
|
+
HOST=0.0.0.0
|
|
182
|
+
PORT=8000
|
|
183
|
+
FUTU_HOST=127.0.0.1
|
|
184
|
+
FUTU_PORT=11111
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Development
|
|
188
|
+
|
|
189
|
+
### Managing Dependencies
|
|
190
|
+
|
|
191
|
+
Add new dependencies to `pyproject.toml`:
|
|
192
|
+
```toml
|
|
193
|
+
[project]
|
|
194
|
+
dependencies = [
|
|
195
|
+
# ... existing dependencies ...
|
|
196
|
+
"new-package>=1.0.0",
|
|
197
|
+
]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Then update your environment:
|
|
201
|
+
```bash
|
|
202
|
+
uv pip install -e .
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Code Style
|
|
206
|
+
|
|
207
|
+
This project uses Ruff for code linting and formatting. The configuration is in `pyproject.toml`:
|
|
208
|
+
|
|
209
|
+
```toml
|
|
210
|
+
[tool.ruff]
|
|
211
|
+
line-length = 100
|
|
212
|
+
target-version = "py38"
|
|
213
|
+
|
|
214
|
+
[tool.ruff.lint]
|
|
215
|
+
select = ["E", "F", "I", "N", "W", "B", "UP"]
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Run linting:
|
|
219
|
+
```bash
|
|
220
|
+
uv pip install ruff
|
|
221
|
+
ruff check .
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Run formatting:
|
|
225
|
+
```bash
|
|
226
|
+
ruff format .
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## 🔧 MCP Server 配置
|
|
230
|
+
|
|
231
|
+
### 在 Claude Desktop 中配置
|
|
232
|
+
|
|
233
|
+
1. **找到配置文件位置**:
|
|
234
|
+
- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
235
|
+
- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
236
|
+
|
|
237
|
+
2. **添加服务器配置**:
|
|
238
|
+
```json
|
|
239
|
+
{
|
|
240
|
+
"mcpServers": {
|
|
241
|
+
"futu-stock": {
|
|
242
|
+
"command": "futu-mcp-server",
|
|
243
|
+
"env": {
|
|
244
|
+
"FUTU_HOST": "127.0.0.1",
|
|
245
|
+
"FUTU_PORT": "11111"
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
3. **故障排除配置**:
|
|
253
|
+
如果上述配置不工作,可以尝试使用完整路径:
|
|
254
|
+
```json
|
|
255
|
+
{
|
|
256
|
+
"mcpServers": {
|
|
257
|
+
"futu-stock": {
|
|
258
|
+
"command": "/Users/your-username/.local/bin/futu-mcp-server",
|
|
259
|
+
"env": {
|
|
260
|
+
"FUTU_HOST": "127.0.0.1",
|
|
261
|
+
"FUTU_PORT": "11111"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
> **提示**:使用 `which futu-mcp-server` 命令查看完整路径
|
|
269
|
+
|
|
270
|
+
### 在其他 MCP 客户端中配置
|
|
271
|
+
|
|
272
|
+
#### 使用 Python MCP 客户端
|
|
273
|
+
```python
|
|
274
|
+
from mcp import ClientSession, StdioServerParameters
|
|
275
|
+
from mcp.client.stdio import stdio_client
|
|
276
|
+
|
|
277
|
+
async def main():
|
|
278
|
+
server_params = StdioServerParameters(
|
|
279
|
+
command="futu-mcp-server",
|
|
280
|
+
env={
|
|
281
|
+
"FUTU_HOST": "127.0.0.1",
|
|
282
|
+
"FUTU_PORT": "11111"
|
|
283
|
+
}
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
async with stdio_client(server_params) as (read, write):
|
|
287
|
+
async with ClientSession(read, write) as session:
|
|
288
|
+
# Initialize the connection
|
|
289
|
+
await session.initialize()
|
|
290
|
+
|
|
291
|
+
# List available tools
|
|
292
|
+
tools = await session.list_tools()
|
|
293
|
+
print("Available tools:", [tool.name for tool in tools.tools])
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
#### 使用 Node.js MCP 客户端
|
|
297
|
+
```javascript
|
|
298
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
299
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
300
|
+
|
|
301
|
+
const transport = new StdioClientTransport({
|
|
302
|
+
command: "futu-mcp-server",
|
|
303
|
+
env: {
|
|
304
|
+
FUTU_HOST: "127.0.0.1",
|
|
305
|
+
FUTU_PORT: "11111"
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const client = new Client({
|
|
310
|
+
name: "futu-stock-client",
|
|
311
|
+
version: "1.0.0"
|
|
312
|
+
}, {
|
|
313
|
+
capabilities: {}
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
await client.connect(transport);
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## 📋 使用方法
|
|
320
|
+
|
|
321
|
+
### 1. 启动服务器(独立运行)
|
|
322
|
+
```bash
|
|
323
|
+
# 通过 pip 安装后
|
|
324
|
+
futu-mcp-server
|
|
325
|
+
|
|
326
|
+
# 或从源码运行
|
|
327
|
+
python -m futu_stock_mcp_server.server
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### 2. 环境变量配置
|
|
331
|
+
创建 `.env` 文件或设置环境变量:
|
|
332
|
+
```bash
|
|
333
|
+
FUTU_HOST=127.0.0.1
|
|
334
|
+
FUTU_PORT=11111
|
|
335
|
+
LOG_LEVEL=INFO
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### 3. 验证连接
|
|
339
|
+
启动服务器后,你应该看到类似的日志:
|
|
340
|
+
```
|
|
341
|
+
2024-10-02 14:20:52 | INFO | Initializing Futu connection...
|
|
342
|
+
2024-10-02 14:20:52 | INFO | Successfully initialized Futu connection
|
|
343
|
+
2024-10-02 14:20:52 | INFO | Starting MCP server in stdio mode...
|
|
344
|
+
2024-10-02 14:20:52 | INFO | Press Ctrl+C to stop the server
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### 4. 在 AI 工具中使用
|
|
348
|
+
配置完成后,重启 Claude Desktop 或其他 MCP 客户端,你就可以:
|
|
349
|
+
- 查询股票实时行情
|
|
350
|
+
- 获取历史K线数据
|
|
351
|
+
- 订阅股票数据推送
|
|
352
|
+
- 查询账户信息
|
|
353
|
+
- 执行交易操作(需要交易权限)
|
|
354
|
+
|
|
355
|
+
## 🔧 故障排除
|
|
356
|
+
|
|
357
|
+
### 常见问题
|
|
358
|
+
|
|
359
|
+
#### 1. 命令 `futu-mcp-server` 找不到
|
|
360
|
+
```bash
|
|
361
|
+
# 确保已正确安装
|
|
362
|
+
pipx install futu-stock-mcp-server
|
|
363
|
+
|
|
364
|
+
# 检查命令是否可用
|
|
365
|
+
which futu-mcp-server
|
|
366
|
+
|
|
367
|
+
# 如果还是找不到,检查 PATH
|
|
368
|
+
echo $PATH | grep -o '[^:]*\.local/bin[^:]*'
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
#### 2. Ctrl+C 无法退出服务器
|
|
372
|
+
- 新版本已修复此问题
|
|
373
|
+
- 如果仍然遇到,可以使用 `kill -9 <pid>` 强制终止
|
|
374
|
+
|
|
375
|
+
#### 3. 连接富途 OpenD 失败
|
|
376
|
+
```bash
|
|
377
|
+
# 检查 OpenD 是否运行
|
|
378
|
+
netstat -an | grep 11111
|
|
379
|
+
|
|
380
|
+
# 检查环境变量
|
|
381
|
+
echo $FUTU_HOST
|
|
382
|
+
echo $FUTU_PORT
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
#### 4. Claude Desktop 无法识别服务器
|
|
386
|
+
- 确保配置文件路径正确
|
|
387
|
+
- 检查 JSON 格式是否有效
|
|
388
|
+
- 重启 Claude Desktop
|
|
389
|
+
- 查看 Claude Desktop 的日志文件
|
|
390
|
+
|
|
391
|
+
#### 5. 权限问题
|
|
392
|
+
```bash
|
|
393
|
+
# 确保有执行权限
|
|
394
|
+
chmod +x ~/.local/bin/futu-mcp-server
|
|
395
|
+
|
|
396
|
+
# 或者使用完整路径
|
|
397
|
+
python -m futu_stock_mcp_server.server
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### 日志调试
|
|
401
|
+
|
|
402
|
+
本项目已根据 [MCP 官方文档](https://github.com/modelcontextprotocol/python-sdk) 的最佳实践配置了日志系统:
|
|
403
|
+
|
|
404
|
+
#### MCP 兼容的日志配置
|
|
405
|
+
- **文件日志**: 所有日志写入 `logs/futu_server.log`,自动轮转和清理
|
|
406
|
+
- **MCP Context 日志**: 工具执行期间通过 MCP Context 发送日志给客户端
|
|
407
|
+
- **stdout 保护**: 确保 stdout 仅用于 MCP JSON 通信,避免污染
|
|
408
|
+
|
|
409
|
+
#### 调试模式(仅开发时使用)
|
|
410
|
+
```bash
|
|
411
|
+
# 启用调试模式(会向 stderr 输出日志)
|
|
412
|
+
export FUTU_DEBUG_MODE=1
|
|
413
|
+
futu-mcp-server
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
**注意**: 在 MCP 客户端中不要启用调试模式,因为它会向 stderr 输出日志。
|
|
417
|
+
|
|
418
|
+
#### 日志文件位置
|
|
419
|
+
- 主日志文件:`./logs/futu_server.log`
|
|
420
|
+
- 自动轮转:500 MB 后轮转
|
|
421
|
+
- 自动清理:保留 10 天
|
|
422
|
+
|
|
423
|
+
详细的日志配置说明请参考 [docs/LOGGING.md](docs/LOGGING.md)。
|
|
424
|
+
tools = await session.list_tools()
|
|
425
|
+
|
|
426
|
+
# Call a tool
|
|
427
|
+
result = await session.call_tool(
|
|
428
|
+
"get_stock_quote",
|
|
429
|
+
arguments={"symbols": ["HK.00700"]}
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
# Access a resource
|
|
433
|
+
content, mime_type = await session.read_resource(
|
|
434
|
+
"market://HK.00700"
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
# Get a prompt
|
|
438
|
+
prompt = await session.get_prompt(
|
|
439
|
+
"market_analysis",
|
|
440
|
+
arguments={"symbol": "HK.00700"}
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
if __name__ == "__main__":
|
|
444
|
+
import asyncio
|
|
445
|
+
asyncio.run(main())
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
## Available API Methods
|
|
449
|
+
|
|
450
|
+
### Market Data Tools
|
|
451
|
+
- `get_stock_quote`: Get stock quote data
|
|
452
|
+
- `get_market_snapshot`: Get market snapshot
|
|
453
|
+
- `get_cur_kline`: Get current K-line data
|
|
454
|
+
- `get_history_kline`: Get historical K-line data
|
|
455
|
+
- `get_rt_data`: Get real-time data
|
|
456
|
+
- `get_ticker`: Get ticker data
|
|
457
|
+
- `get_order_book`: Get order book data
|
|
458
|
+
- `get_broker_queue`: Get broker queue data
|
|
459
|
+
|
|
460
|
+
### Subscription Tools
|
|
461
|
+
- `subscribe`: Subscribe to real-time data
|
|
462
|
+
- `unsubscribe`: Unsubscribe from real-time data
|
|
463
|
+
|
|
464
|
+
### Derivatives Tools
|
|
465
|
+
- `get_option_chain`: Get option chain data
|
|
466
|
+
- `get_option_expiration_date`: Get option expiration dates
|
|
467
|
+
- `get_option_condor`: Get option condor strategy data
|
|
468
|
+
- `get_option_butterfly`: Get option butterfly strategy data
|
|
469
|
+
|
|
470
|
+
### Account Query Tools
|
|
471
|
+
- `get_account_list`: Get account list
|
|
472
|
+
- `get_asset_info`: Get asset information
|
|
473
|
+
- `get_asset_allocation`: Get asset allocation information
|
|
474
|
+
|
|
475
|
+
### Market Information Tools
|
|
476
|
+
- `get_market_state`: Get market state
|
|
477
|
+
- `get_security_info`: Get security information
|
|
478
|
+
- `get_security_list`: Get security list
|
|
479
|
+
|
|
480
|
+
### Stock Filter Commands
|
|
481
|
+
|
|
482
|
+
#### get_stock_filter
|
|
483
|
+
Filter stocks based on various conditions.
|
|
484
|
+
|
|
485
|
+
Parameters:
|
|
486
|
+
- `base_filters` (optional): List of basic stock filters
|
|
487
|
+
```python
|
|
488
|
+
{
|
|
489
|
+
"field_name": int, # StockField enum value
|
|
490
|
+
"filter_min": float, # Optional minimum value
|
|
491
|
+
"filter_max": float, # Optional maximum value
|
|
492
|
+
"is_no_filter": bool, # Optional, whether to skip filtering
|
|
493
|
+
"sort_dir": int # Optional, sort direction
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
- `accumulate_filters` (optional): List of accumulate filters
|
|
497
|
+
```python
|
|
498
|
+
{
|
|
499
|
+
"field_name": int, # AccumulateField enum value
|
|
500
|
+
"filter_min": float,
|
|
501
|
+
"filter_max": float,
|
|
502
|
+
"is_no_filter": bool,
|
|
503
|
+
"sort_dir": int,
|
|
504
|
+
"days": int # Required, number of days to accumulate
|
|
505
|
+
}
|
|
506
|
+
```
|
|
507
|
+
- `financial_filters` (optional): List of financial filters
|
|
508
|
+
```python
|
|
509
|
+
{
|
|
510
|
+
"field_name": int, # FinancialField enum value
|
|
511
|
+
"filter_min": float,
|
|
512
|
+
"filter_max": float,
|
|
513
|
+
"is_no_filter": bool,
|
|
514
|
+
"sort_dir": int,
|
|
515
|
+
"quarter": int # Required, financial quarter
|
|
516
|
+
}
|
|
517
|
+
```
|
|
518
|
+
- `market` (optional): Market code (e.g. "HK.Motherboard", "US.NASDAQ")
|
|
519
|
+
- `page` (optional): Page number, starting from 1 (default: 1)
|
|
520
|
+
- `page_size` (optional): Number of results per page, max 200 (default: 200)
|
|
521
|
+
|
|
522
|
+
Supported Market Codes:
|
|
523
|
+
- `HK.Motherboard`: Hong Kong Main Board
|
|
524
|
+
- `HK.GEM`: Hong Kong GEM
|
|
525
|
+
- `HK.BK1911`: H-Share Main Board
|
|
526
|
+
- `HK.BK1912`: H-Share GEM
|
|
527
|
+
- `US.NYSE`: NYSE
|
|
528
|
+
- `US.AMEX`: AMEX
|
|
529
|
+
- `US.NASDAQ`: NASDAQ
|
|
530
|
+
- `SH.3000000`: Shanghai Main Board
|
|
531
|
+
- `SZ.3000001`: Shenzhen Main Board
|
|
532
|
+
- `SZ.3000004`: Shenzhen ChiNext
|
|
533
|
+
|
|
534
|
+
Example:
|
|
535
|
+
```python
|
|
536
|
+
# Get stocks with price between 10 and 50 HKD in Hong Kong Main Board
|
|
537
|
+
filters = {
|
|
538
|
+
"base_filters": [{
|
|
539
|
+
"field_name": 5, # Current price
|
|
540
|
+
"filter_min": 10.0,
|
|
541
|
+
"filter_max": 50.0
|
|
542
|
+
}],
|
|
543
|
+
"market": "HK.Motherboard"
|
|
544
|
+
}
|
|
545
|
+
result = await client.get_stock_filter(**filters)
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
Notes:
|
|
549
|
+
- Limited to 10 requests per 30 seconds
|
|
550
|
+
- Each page returns maximum 200 results
|
|
551
|
+
- Recommended to use no more than 250 filter conditions
|
|
552
|
+
- Maximum 10 accumulate conditions of the same type
|
|
553
|
+
- Dynamic data sorting (like current price) may change between pages
|
|
554
|
+
- Cannot compare different types of indicators (e.g. MA5 vs EMA10)
|
|
555
|
+
|
|
556
|
+
## Resources
|
|
557
|
+
|
|
558
|
+
### Market Data
|
|
559
|
+
- `market://{symbol}`: Get market data for a symbol
|
|
560
|
+
- `kline://{symbol}/{ktype}`: Get K-line data for a symbol
|
|
561
|
+
|
|
562
|
+
## Prompts
|
|
563
|
+
|
|
564
|
+
### Analysis
|
|
565
|
+
- `market_analysis`: Create a market analysis prompt
|
|
566
|
+
- `option_strategy`: Create an option strategy analysis prompt
|
|
567
|
+
|
|
568
|
+
## Error Handling
|
|
569
|
+
|
|
570
|
+
The server follows the MCP 2.0 error response format:
|
|
571
|
+
|
|
572
|
+
```json
|
|
573
|
+
{
|
|
574
|
+
"jsonrpc": "2.0",
|
|
575
|
+
"id": "request_id",
|
|
576
|
+
"error": {
|
|
577
|
+
"code": -32000,
|
|
578
|
+
"message": "Error message",
|
|
579
|
+
"data": null
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
## Security
|
|
585
|
+
|
|
586
|
+
- The server uses secure WebSocket connections
|
|
587
|
+
- All API calls are authenticated through the Futu OpenAPI
|
|
588
|
+
- Environment variables are used for sensitive configuration
|
|
589
|
+
|
|
590
|
+
## Development
|
|
591
|
+
|
|
592
|
+
### Adding New Tools
|
|
593
|
+
|
|
594
|
+
To add a new tool, use the `@mcp.tool()` decorator:
|
|
595
|
+
|
|
596
|
+
```python
|
|
597
|
+
@mcp.tool()
|
|
598
|
+
async def new_tool(param1: str, param2: int) -> Dict[str, Any]:
|
|
599
|
+
"""Tool description"""
|
|
600
|
+
# Implementation
|
|
601
|
+
return result
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### Adding New Resources
|
|
605
|
+
|
|
606
|
+
To add a new resource, use the `@mcp.resource()` decorator:
|
|
607
|
+
|
|
608
|
+
```python
|
|
609
|
+
@mcp.resource("resource://{param1}/{param2}")
|
|
610
|
+
async def new_resource(param1: str, param2: str) -> Dict[str, Any]:
|
|
611
|
+
"""Resource description"""
|
|
612
|
+
# Implementation
|
|
613
|
+
return result
|
|
614
|
+
```
|
|
615
|
+
|
|
616
|
+
### Adding New Prompts
|
|
617
|
+
|
|
618
|
+
To add a new prompt, use the `@mcp.prompt()` decorator:
|
|
619
|
+
|
|
620
|
+
```python
|
|
621
|
+
@mcp.prompt()
|
|
622
|
+
async def new_prompt(param1: str) -> str:
|
|
623
|
+
"""Prompt description"""
|
|
624
|
+
return f"Prompt template with {param1}"
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
## License
|
|
628
|
+
|
|
629
|
+
MIT License
|
|
630
|
+
|
|
631
|
+
## Available MCP Functions
|
|
632
|
+
|
|
633
|
+
### Market Data Functions
|
|
634
|
+
|
|
635
|
+
#### get_stock_quote
|
|
636
|
+
Get stock quote data for given symbols.
|
|
637
|
+
```python
|
|
638
|
+
symbols = ["HK.00700", "US.AAPL", "SH.600519"]
|
|
639
|
+
result = await session.call_tool("get_stock_quote", {"symbols": symbols})
|
|
640
|
+
```
|
|
641
|
+
Returns quote data including price, volume, turnover, etc.
|
|
642
|
+
|
|
643
|
+
#### get_market_snapshot
|
|
644
|
+
Get market snapshot for given symbols.
|
|
645
|
+
```python
|
|
646
|
+
symbols = ["HK.00700", "US.AAPL", "SH.600519"]
|
|
647
|
+
result = await session.call_tool("get_market_snapshot", {"symbols": symbols})
|
|
648
|
+
```
|
|
649
|
+
Returns comprehensive market data including price, volume, bid/ask prices, etc.
|
|
650
|
+
|
|
651
|
+
#### get_cur_kline
|
|
652
|
+
Get current K-line data.
|
|
653
|
+
```python
|
|
654
|
+
result = await session.call_tool("get_cur_kline", {
|
|
655
|
+
"symbol": "HK.00700",
|
|
656
|
+
"ktype": "K_1M", # K_1M, K_5M, K_15M, K_30M, K_60M, K_DAY, K_WEEK, K_MON
|
|
657
|
+
"count": 100
|
|
658
|
+
})
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
#### get_history_kline
|
|
662
|
+
Get historical K-line data.
|
|
663
|
+
```python
|
|
664
|
+
result = await session.call_tool("get_history_kline", {
|
|
665
|
+
"symbol": "HK.00700",
|
|
666
|
+
"ktype": "K_DAY",
|
|
667
|
+
"start": "2024-01-01",
|
|
668
|
+
"end": "2024-03-31"
|
|
669
|
+
})
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
#### get_rt_data
|
|
673
|
+
Get real-time trading data.
|
|
674
|
+
```python
|
|
675
|
+
result = await session.call_tool("get_rt_data", {"symbol": "HK.00700"})
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
#### get_ticker
|
|
679
|
+
Get ticker data (detailed trades).
|
|
680
|
+
```python
|
|
681
|
+
result = await session.call_tool("get_ticker", {"symbol": "HK.00700"})
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
#### get_order_book
|
|
685
|
+
Get order book data.
|
|
686
|
+
```python
|
|
687
|
+
result = await session.call_tool("get_order_book", {"symbol": "HK.00700"})
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
#### get_broker_queue
|
|
691
|
+
Get broker queue data.
|
|
692
|
+
```python
|
|
693
|
+
result = await session.call_tool("get_broker_queue", {"symbol": "HK.00700"})
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
### Subscription Functions
|
|
697
|
+
|
|
698
|
+
#### subscribe
|
|
699
|
+
Subscribe to real-time data.
|
|
700
|
+
```python
|
|
701
|
+
result = await session.call_tool("subscribe", {
|
|
702
|
+
"symbols": ["HK.00700", "US.AAPL"],
|
|
703
|
+
"sub_types": ["QUOTE", "TICKER", "K_1M"]
|
|
704
|
+
})
|
|
705
|
+
```
|
|
706
|
+
Subscription types:
|
|
707
|
+
- "QUOTE": Basic quote
|
|
708
|
+
- "ORDER_BOOK": Order book
|
|
709
|
+
- "TICKER": Trades
|
|
710
|
+
- "RT_DATA": Real-time data
|
|
711
|
+
- "BROKER": Broker queue
|
|
712
|
+
- "K_1M" to "K_MON": K-line data
|
|
713
|
+
|
|
714
|
+
#### unsubscribe
|
|
715
|
+
Unsubscribe from real-time data.
|
|
716
|
+
```python
|
|
717
|
+
result = await session.call_tool("unsubscribe", {
|
|
718
|
+
"symbols": ["HK.00700", "US.AAPL"],
|
|
719
|
+
"sub_types": ["QUOTE", "TICKER"]
|
|
720
|
+
})
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
### Options Functions
|
|
724
|
+
|
|
725
|
+
#### get_option_chain
|
|
726
|
+
Get option chain data.
|
|
727
|
+
```python
|
|
728
|
+
result = await session.call_tool("get_option_chain", {
|
|
729
|
+
"symbol": "HK.00700",
|
|
730
|
+
"start": "2024-04-01",
|
|
731
|
+
"end": "2024-06-30"
|
|
732
|
+
})
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
#### get_option_expiration_date
|
|
736
|
+
Get option expiration dates.
|
|
737
|
+
```python
|
|
738
|
+
result = await session.call_tool("get_option_expiration_date", {
|
|
739
|
+
"symbol": "HK.00700"
|
|
740
|
+
})
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
#### get_option_condor
|
|
744
|
+
Get option condor strategy data.
|
|
745
|
+
```python
|
|
746
|
+
result = await session.call_tool("get_option_condor", {
|
|
747
|
+
"symbol": "HK.00700",
|
|
748
|
+
"expiry": "2024-06-30",
|
|
749
|
+
"strike_price": 350.0
|
|
750
|
+
})
|
|
751
|
+
```
|
|
752
|
+
|
|
753
|
+
#### get_option_butterfly
|
|
754
|
+
Get option butterfly strategy data.
|
|
755
|
+
```python
|
|
756
|
+
result = await session.call_tool("get_option_butterfly", {
|
|
757
|
+
"symbol": "HK.00700",
|
|
758
|
+
"expiry": "2024-06-30",
|
|
759
|
+
"strike_price": 350.0
|
|
760
|
+
})
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
### Account Functions
|
|
764
|
+
|
|
765
|
+
#### get_account_list
|
|
766
|
+
Get account list.
|
|
767
|
+
```python
|
|
768
|
+
result = await session.call_tool("get_account_list", {"random_string": "dummy"})
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
#### get_funds
|
|
772
|
+
Get account funds information.
|
|
773
|
+
```python
|
|
774
|
+
result = await session.call_tool("get_funds", {"random_string": "dummy"})
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
#### get_positions
|
|
778
|
+
Get account positions.
|
|
779
|
+
```python
|
|
780
|
+
result = await session.call_tool("get_positions", {"random_string": "dummy"})
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
#### get_max_power
|
|
784
|
+
Get maximum trading power.
|
|
785
|
+
```python
|
|
786
|
+
result = await session.call_tool("get_max_power", {"random_string": "dummy"})
|
|
787
|
+
```
|
|
788
|
+
|
|
789
|
+
#### get_margin_ratio
|
|
790
|
+
Get margin ratio for a security.
|
|
791
|
+
```python
|
|
792
|
+
result = await session.call_tool("get_margin_ratio", {"symbol": "HK.00700"})
|
|
793
|
+
```
|
|
794
|
+
|
|
795
|
+
### Market Information Functions
|
|
796
|
+
|
|
797
|
+
#### get_market_state
|
|
798
|
+
Get market state.
|
|
799
|
+
```python
|
|
800
|
+
result = await session.call_tool("get_market_state", {"market": "HK"})
|
|
801
|
+
```
|
|
802
|
+
Available markets: "HK", "US", "SH", "SZ"
|
|
803
|
+
|
|
804
|
+
#### get_security_info
|
|
805
|
+
Get security information.
|
|
806
|
+
```python
|
|
807
|
+
result = await session.call_tool("get_security_info", {
|
|
808
|
+
"market": "HK",
|
|
809
|
+
"code": "00700"
|
|
810
|
+
})
|
|
811
|
+
```
|
|
812
|
+
|
|
813
|
+
#### get_security_list
|
|
814
|
+
Get security list for a market.
|
|
815
|
+
```python
|
|
816
|
+
result = await session.call_tool("get_security_list", {"market": "HK"})
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
#### get_stock_filter
|
|
820
|
+
Get filtered stock list based on conditions.
|
|
821
|
+
```python
|
|
822
|
+
result = await session.call_tool("get_stock_filter", {
|
|
823
|
+
"market": "HK.Motherboard",
|
|
824
|
+
"base_filters": [{
|
|
825
|
+
"field_name": 1, # Price
|
|
826
|
+
"filter_min": 10.0,
|
|
827
|
+
"filter_max": 50.0,
|
|
828
|
+
"sort_dir": 1 # Ascending
|
|
829
|
+
}],
|
|
830
|
+
"page": 1,
|
|
831
|
+
"page_size": 50
|
|
832
|
+
})
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
### Time Function
|
|
836
|
+
|
|
837
|
+
#### get_current_time
|
|
838
|
+
Get current server time.
|
|
839
|
+
```python
|
|
840
|
+
result = await session.call_tool("get_current_time", {"random_string": "dummy"})
|
|
841
|
+
```
|
|
842
|
+
Returns timestamp, formatted datetime, date, time and timezone.
|