
Futu Stock
STDIOMCP server for Futu securities trading with real-time market data access
MCP server for Futu securities trading with real-time market data access
基于模型上下文协议(MCP)的富途证券行情交易接口服务器。将富途OpenAPI功能以标准化的MCP协议提供给AI模型使用,支持行情订阅、数据查询等功能。
在使用本项目之前,您需要:
.env
文件已添加到.gitignore
中本项目是一个开源工具,旨在简化富途OpenAPI的接入流程。使用本项目时请注意:
git clone https://github.com/yourusername/futu-stock-mcp-server.git cd futu-stock-mcp-server
# macOS/Linux curl -LsSf https://astral.sh/uv/install.sh | sh # Windows (PowerShell) powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Create virtual environment uv venv # Activate virtual environment # On macOS/Linux: source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install in editable mode uv pip install -e .
cp .env.example .env
Edit the .env
file with your server settings:
HOST=0.0.0.0
PORT=8000
FUTU_HOST=127.0.0.1
FUTU_PORT=11111
Add new dependencies to pyproject.toml
:
[project] dependencies = [ # ... existing dependencies ... "new-package>=1.0.0", ]
Then update your environment:
uv pip install -e .
This project uses Ruff for code linting and formatting. The configuration is in pyproject.toml
:
[tool.ruff] line-length = 100 target-version = "py38" [tool.ruff.lint] select = ["E", "F", "I", "N", "W", "B", "UP"]
Run linting:
uv pip install ruff ruff check .
Run formatting:
ruff format .
python -m futu_stock_mcp_server.server
from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client async def main(): server_params = StdioServerParameters( command="python", args=["src/server.py"] ) async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: # Initialize the connection await session.initialize() # List available tools tools = await session.list_tools() # Call a tool result = await session.call_tool( "get_stock_quote", arguments={"symbols": ["HK.00700"]} ) # Access a resource content, mime_type = await session.read_resource( "market://HK.00700" ) # Get a prompt prompt = await session.get_prompt( "market_analysis", arguments={"symbol": "HK.00700"} ) if __name__ == "__main__": import asyncio asyncio.run(main())
get_stock_quote
: Get stock quote dataget_market_snapshot
: Get market snapshotget_cur_kline
: Get current K-line dataget_history_kline
: Get historical K-line dataget_rt_data
: Get real-time dataget_ticker
: Get ticker dataget_order_book
: Get order book dataget_broker_queue
: Get broker queue datasubscribe
: Subscribe to real-time dataunsubscribe
: Unsubscribe from real-time dataget_option_chain
: Get option chain dataget_option_expiration_date
: Get option expiration datesget_option_condor
: Get option condor strategy dataget_option_butterfly
: Get option butterfly strategy dataget_account_list
: Get account listget_asset_info
: Get asset informationget_asset_allocation
: Get asset allocation informationget_market_state
: Get market stateget_security_info
: Get security informationget_security_list
: Get security listFilter stocks based on various conditions.
Parameters:
base_filters
(optional): List of basic stock filters
{ "field_name": int, # StockField enum value "filter_min": float, # Optional minimum value "filter_max": float, # Optional maximum value "is_no_filter": bool, # Optional, whether to skip filtering "sort_dir": int # Optional, sort direction }
accumulate_filters
(optional): List of accumulate filters
{ "field_name": int, # AccumulateField enum value "filter_min": float, "filter_max": float, "is_no_filter": bool, "sort_dir": int, "days": int # Required, number of days to accumulate }
financial_filters
(optional): List of financial filters
{ "field_name": int, # FinancialField enum value "filter_min": float, "filter_max": float, "is_no_filter": bool, "sort_dir": int, "quarter": int # Required, financial quarter }
market
(optional): Market code (e.g. "HK.Motherboard", "US.NASDAQ")page
(optional): Page number, starting from 1 (default: 1)page_size
(optional): Number of results per page, max 200 (default: 200)Supported Market Codes:
HK.Motherboard
: Hong Kong Main BoardHK.GEM
: Hong Kong GEMHK.BK1911
: H-Share Main BoardHK.BK1912
: H-Share GEMUS.NYSE
: NYSEUS.AMEX
: AMEXUS.NASDAQ
: NASDAQSH.3000000
: Shanghai Main BoardSZ.3000001
: Shenzhen Main BoardSZ.3000004
: Shenzhen ChiNextExample:
# Get stocks with price between 10 and 50 HKD in Hong Kong Main Board filters = { "base_filters": [{ "field_name": 5, # Current price "filter_min": 10.0, "filter_max": 50.0 }], "market": "HK.Motherboard" } result = await client.get_stock_filter(**filters)
Notes:
market://{symbol}
: Get market data for a symbolkline://{symbol}/{ktype}
: Get K-line data for a symbolmarket_analysis
: Create a market analysis promptoption_strategy
: Create an option strategy analysis promptThe server follows the MCP 2.0 error response format:
{ "jsonrpc": "2.0", "id": "request_id", "error": { "code": -32000, "message": "Error message", "data": null } }
To add a new tool, use the @mcp.tool()
decorator:
@mcp.tool() async def new_tool(param1: str, param2: int) -> Dict[str, Any]: """Tool description""" # Implementation return result
To add a new resource, use the @mcp.resource()
decorator:
@mcp.resource("resource://{param1}/{param2}") async def new_resource(param1: str, param2: str) -> Dict[str, Any]: """Resource description""" # Implementation return result
To add a new prompt, use the @mcp.prompt()
decorator:
@mcp.prompt() async def new_prompt(param1: str) -> str: """Prompt description""" return f"Prompt template with {param1}"
MIT License
Get stock quote data for given symbols.
symbols = ["HK.00700", "US.AAPL", "SH.600519"] result = await session.call_tool("get_stock_quote", {"symbols": symbols})
Returns quote data including price, volume, turnover, etc.
Get market snapshot for given symbols.
symbols = ["HK.00700", "US.AAPL", "SH.600519"] result = await session.call_tool("get_market_snapshot", {"symbols": symbols})
Returns comprehensive market data including price, volume, bid/ask prices, etc.
Get current K-line data.
result = await session.call_tool("get_cur_kline", { "symbol": "HK.00700", "ktype": "K_1M", # K_1M, K_5M, K_15M, K_30M, K_60M, K_DAY, K_WEEK, K_MON "count": 100 })
Get historical K-line data.
result = await session.call_tool("get_history_kline", { "symbol": "HK.00700", "ktype": "K_DAY", "start": "2024-01-01", "end": "2024-03-31" })
Get real-time trading data.
result = await session.call_tool("get_rt_data", {"symbol": "HK.00700"})
Get ticker data (detailed trades).
result = await session.call_tool("get_ticker", {"symbol": "HK.00700"})
Get order book data.
result = await session.call_tool("get_order_book", {"symbol": "HK.00700"})
Get broker queue data.
result = await session.call_tool("get_broker_queue", {"symbol": "HK.00700"})
Subscribe to real-time data.
result = await session.call_tool("subscribe", { "symbols": ["HK.00700", "US.AAPL"], "sub_types": ["QUOTE", "TICKER", "K_1M"] })
Subscription types:
Unsubscribe from real-time data.
result = await session.call_tool("unsubscribe", { "symbols": ["HK.00700", "US.AAPL"], "sub_types": ["QUOTE", "TICKER"] })
Get option chain data.
result = await session.call_tool("get_option_chain", { "symbol": "HK.00700", "start": "2024-04-01", "end": "2024-06-30" })
Get option expiration dates.
result = await session.call_tool("get_option_expiration_date", { "symbol": "HK.00700" })
Get option condor strategy data.
result = await session.call_tool("get_option_condor", { "symbol": "HK.00700", "expiry": "2024-06-30", "strike_price": 350.0 })
Get option butterfly strategy data.
result = await session.call_tool("get_option_butterfly", { "symbol": "HK.00700", "expiry": "2024-06-30", "strike_price": 350.0 })
Get account list.
result = await session.call_tool("get_account_list", {"random_string": "dummy"})
Get account funds information.
result = await session.call_tool("get_funds", {"random_string": "dummy"})
Get account positions.
result = await session.call_tool("get_positions", {"random_string": "dummy"})
Get maximum trading power.
result = await session.call_tool("get_max_power", {"random_string": "dummy"})
Get margin ratio for a security.
result = await session.call_tool("get_margin_ratio", {"symbol": "HK.00700"})
Get market state.
result = await session.call_tool("get_market_state", {"market": "HK"})
Available markets: "HK", "US", "SH", "SZ"
Get security information.
result = await session.call_tool("get_security_info", { "market": "HK", "code": "00700" })
Get security list for a market.
result = await session.call_tool("get_security_list", {"market": "HK"})
Get filtered stock list based on conditions.
result = await session.call_tool("get_stock_filter", { "market": "HK.Motherboard", "base_filters": [{ "field_name": 1, # Price "filter_min": 10.0, "filter_max": 50.0, "sort_dir": 1 # Ascending }], "page": 1, "page_size": 50 })
Get current server time.
result = await session.call_tool("get_current_time", {"random_string": "dummy"})
Returns timestamp, formatted datetime, date, time and timezone.