IBKR TWS MCP Server
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@IBKR TWS MCP ServerWhat's the current price for AAPL?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
IBKR TWS MCP Server
基于 ib_async + fastmcp 的可扩展 Interactive Brokers MCP Server。
48 个工具,8 个插件分类,与现有 8 个 IB skills 平滑融合。
目录
Related MCP server: FinClaw
架构概览
┌─────────────────────────────────────────────┐
│ 用户 / AI Agent │
└──────────────┬──────────────────────────────┘
│ MCP 协议 (stdio / HTTP)
┌──────────▼──────────┐
│ MCP Server │ ← 48 个工具,8 个插件分类
│ ┌────────────────┐ │
│ │ 插件注册中心 │ │ ← 新增工具只需 1 个装饰器
│ └────────────────┘ │
│ ┌────────────────┐ │
│ │ 中间件层 │ │ ← 只读模式 / 风控 / 限流
│ └────────────────┘ │
│ ┌────────────────┐ │
│ │ 连接管理器 │ │ ← 单例 + 自动重连
│ └────────────────┘ │
└──────────┬──────────┘
│ Socket (TCP)
┌──────────▼──────────┐
│ IB Gateway / TWS │ ← 必须运行
└──────────┬──────────┘
│
┌──────────▼──────────┐
│ IBKR 服务器 │
└─────────────────────┘前置条件
必须
项目 | 版本 | 说明 |
Python | 3.12+ | 需要较新版本以支持类型提示语法 |
uv | latest | 包管理工具,替代 pip/poetry |
IB 账户 | 任意类型 | 模拟盘或实盘均可 |
IB Gateway 或 TWS | 稳定版或最新版 | 必须运行并接受 API 连接 |
安装 IB Gateway
配置 IB Gateway API
在 IB Gateway / TWS 中:
进入
Configure → Settings → API → Settings勾选 Enable ActiveX and Socket Clients
勾选 Allow connections from localhost only
勾选 Download open orders on connection
记下端口号:
平台 | 模拟盘 | 实盘 |
IB Gateway | 4002 | 4001 |
TWS | 7497 | 7496 |
建议:
Configure → Settings → Memory Allocation设为 4096 MB 以上
安装
# 克隆或进入项目目录
cd /Users/x/ibkr-tws-mcp
# 安装依赖
uv sync配置
环境变量
cp .env.example .env编辑 .env:
# IB 连接
IB_HOST=127.0.0.1
IB_PORT=7497 # 根据上表修改
IB_CLIENT_ID=1
IB_ACCOUNT_ID= # 留空自动检测
IB_READ_ONLY=true # 生产环境建议 true
IB_REALTIME_DATA=false # 无实时数据订阅时设 false(使用延迟免费数据)
# 风控
RISK_MAX_ORDER_QTY=1000
RISK_MAX_ORDER_NOTIONAL=100000
RISK_DAILY_LOSS_LIMIT=10000
RISK_SYMBOL_DENYLIST= # 逗号分隔,如 "GME,AMC"
RISK_SYMBOL_ALLOWLIST= # 留空=不限制
# MCP 服务
MCP_TRANSPORT=stdio # stdio 或 http
MCP_HOST=0.0.0.0
MCP_PORT=8765
# 日志
LOG_LEVEL=info # debug / info / warn / error插件配置
编辑 config/plugins.yaml:
plugins:
enabled:
- market_data # 行情 (10 工具)
- contracts # 合约 (7 工具)
- account # 账户 (8 工具)
- orders # 订单 (9 工具)
- risk # 风控 (5 工具)
- scanner # 扫描器 (3 工具)
- news # 新闻 (3 工具)
optional:
strategies: # 策略 (3 工具,融合现有 skills)
enabled: false # 按需开启
requires: [pandas, numpy]
disabled: [] # 填入要禁用的插件名启动
stdio 模式(推荐,用于 AI Agent 接入)
uv run ibkr-mcpHTTP 模式(用于调试或远程接入)
uv run ibkr-mcp --transport http --host 0.0.0.0 --port 8765启动后访问 http://localhost:8765 查看工具列表。
接入 AI Agent
OpenCode / Claude Code
在项目或全局配置中添加 MCP Server:
{
"mcpServers": {
"ibkr-tws": {
"command": "uv",
"args": ["run", "--directory", "/Users/x/ibkr-tws-mcp", "ibkr-mcp"],
"env": {
"IB_HOST": "127.0.0.1",
"IB_PORT": "7497",
"IB_READ_ONLY": "true"
}
}
}
}Claude Desktop
编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"ibkr-tws": {
"command": "uv",
"args": ["run", "--directory", "/Users/x/ibkr-tws-mcp", "ibkr-mcp"],
"env": {
"IB_PORT": "7497",
"IB_READ_ONLY": "true"
}
}
}
}Cursor
编辑 .cursor/mcp.json:
{
"mcpServers": {
"ibkr-tws": {
"command": "uv",
"args": ["run", "--directory", "/Users/x/ibkr-tws-mcp", "ibkr-mcp"]
}
}
}工具参考
所有工具返回统一格式:
{
"success": true,
"data": { ... },
"error": null,
"meta": { "conId": 265598 }
}📊 Market Data (10 工具)
get_quote
获取实时报价快照。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 交易所 |
| string |
| 货币 |
返回:{ symbol, bid, ask, last, close, high, low, volume, marketCap }
get_quotes_bulk
批量获取多个标的报价。
参数 | 类型 | 默认 | 说明 |
| list[str] | 必填 | 股票代码列表 |
| string |
| 交易所 |
| string |
| 货币 |
返回:报价列表
get_history
获取历史 K 线数据。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 时间跨度,如 |
| string |
| K 线周期,如 |
| string |
| 数据类型: |
| string |
| 交易所 |
| string |
| 货币 |
| bool |
| 仅交易时段数据 |
返回:[{ date, open, high, low, close, volume, average, barCount }]
get_ticks
获取历史逐笔成交数据。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 起始时间,格式 |
| int |
| 返回笔数 |
返回:[{ time, price, size, tickAttribLast }]
get_depth
获取 Level 2 盘口数据。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| int |
| 盘口深度 |
返回:{ bids: [{ price, size, side, position }], asks: [...] }
subscribe_stream
订阅实时行情流。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
返回:{ subscription_id, symbol, status }
unsubscribe_stream
取消实时行情订阅。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 订阅 ID |
list_subscriptions
查看当前所有活跃订阅。
subscribe_pnl
订阅实时盈亏推送。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
返回:{ subscription_id, account, dailyPnL, unrealizedPnL, realizedPnL, status }
subscribe_account
订阅账户更新推送(余额、持仓变化等)。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
📝 Contracts (7 工具)
search_contract
搜索并解析合约信息。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 类型: |
| string |
| 交易所 |
| string |
| 货币 |
返回:{ conId, symbol, secType, exchange, currency, primaryExchange }
contract_details
通过 conId 获取合约详情。
参数 | 类型 | 默认 | 说明 |
| int | 必填 | 合约 ID |
get_option_chain
获取期权链(到期日、行权价)。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 标的代码 |
| string |
| 交易所 |
| string |
| 货币 |
返回:[{ exchange, underlyingConId, tradingClass, multiplier, expirations, strikes }]
get_futures_chain
获取期货合约链。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 品种代码 |
| string |
| 交易所 |
symbol_to_conid
股票代码转 conId。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 类型 |
返回:{ conId, symbol, secType }
get_trading_hours
获取标的交易时段信息。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 交易所 |
| string |
| 货币 |
返回:{ conId, symbol, timeZone, tradingHours, liquidHours, marketName }
get_option_expirations
获取标的可用期权到期日列表。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 标的代码 |
| string |
| 交易所 |
| string |
| 货币 |
返回:{ symbol, conId, expirations: [...], count }
👤 Account (8 工具)
get_account_summary
获取账户摘要(净值、购买力等)。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号(留空自动检测) |
| string |
| 指定标签(留空返回全部) |
返回:{ NetLiquidation: "...", TotalCashValue: "...", ... }
get_positions
获取持仓列表。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
返回:[{ symbol, secType, position, avgCost, marketValue, unrealizedPNL, realizedPNL }]
get_portfolio
获取投资组合(含 P&L)。
参数同 get_positions。
get_buying_power
获取购买力详情。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
返回:{ BuyingPower: "...", CashBalance: "...", NetLiquidation: "..." }
get_pnl
获取实时盈亏。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
返回:{ dailyPnL, unrealizedPnL, realizedPnL, value }
get_cash_balances
获取多币种现金余额。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
get_watchlist
获取自选股实时行情。不传 symbols 时默认返回当前持仓。
参数 | 类型 | 默认 | 说明 |
| list[str] |
| 股票代码列表 |
| string |
| 交易所 |
| string |
| 货币 |
返回:[{ symbol, conId, last, bid, ask, volume, high, low, close }]
calculate_rebalance
计算调仓建议。
参数 | 类型 | 默认 | 说明 |
| dict | 必填 | 目标配置比例,如 |
| string |
| 账户号 |
返回:{ netLiquidation, currentAllocations, targetAllocations, rebalanceActions }
💰 Orders (9 工具)
⚠️ 交易类工具受
IB_READ_ONLY环境变量控制,默认只读模式下会被阻止。
place_order
下单。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string | 必填 |
|
| int | 必填 | 数量 |
| string |
|
|
| float |
| 限价(LMT 必填) |
| string |
| 交易所 |
| string |
| 货币 |
返回:{ orderId, status, filledQty, avgFillPrice }
cancel_order
撤单。
参数 | 类型 | 默认 | 说明 |
| int | 必填 | 订单 ID |
cancel_all
撤销所有未成交订单。
get_open_orders
获取未成交订单列表。
返回:[{ orderId, symbol, action, type, quantity, price, status, filled, remaining, avgFillPrice }]
get_executions
获取已成交记录。
返回:[{ execId, symbol, side, shares, price, time, cumQty }]
place_bracket
下括号单(入场 + 止损 + 止盈)。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string | 必填 |
|
| int | 必填 | 数量 |
| float | 必填 | 入场价 |
| float | 必填 | 止损价 |
| float | 必填 | 止盈价 |
返回:3 个订单 ID(entry / stop_loss / take_profit)
modify_order
修改未成交订单。
参数 | 类型 | 默认 | 说明 |
| int | 必填 | 订单 ID |
| int |
| 新数量(0=不变) |
| float |
| 新价格(0=不变) |
| float |
| 新止损价(0=不变) |
place_conditional
条件单(价格触发)。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string | 必填 |
|
| int | 必填 | 数量 |
| string |
|
|
| float |
| 限价 |
| float |
| 触发价格 |
place_algo_order
算法订单(VWAP / TWAP 等)。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string | 必填 |
|
| int | 必填 | 数量 |
| string |
|
|
| dict |
| 算法参数,如 |
🛡️ Risk (5 工具)
get_delta_exposure
获取按标的分类的 Delta 敞口。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
返回:{ by_symbol: { AAPL: 100, ... }, total_delta: 150 }
get_greeks
获取期权希腊值。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 标的代码 |
| string | 必填 | 到期日,格式 |
| float | 必填 | 行权价 |
| string | 必填 |
|
| string |
| 交易所 |
| string |
| 货币 |
返回:{ delta, gamma, theta, vega, rho, impliedVol, optPrice, pvDividend }
get_margin
获取保证金使用情况。
参数 | 类型 | 默认 | 说明 |
| string |
| 账户号 |
simulate_trade
模拟交易,预估保证金/购买力影响。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string | 必填 |
|
| int | 必填 | 数量 |
| float |
| 价格(留空取当前价) |
返回:{ estimatedPrice, estimatedNotional, marginImpactPct, netLiquidation, riskFlags }
get_corporate_actions
获取分红/拆股历史。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
返回:[{ payDate, dividend, annualDividend, yield }]
🔍 Scanner (3 工具)
run_scanner
运行市场扫描。
参数 | 类型 | 默认 | 说明 |
| string |
| 扫描类型 |
| string |
| 扫描范围 |
| string |
| 品种类型 |
| int |
| 最大结果数 |
可用 scanType:TOP_PERC_GAIN, TOP_PERC_LOSE, MOST_ACTIVE, TOP_VOLUME, TOP_PRICE_RANGE, HOT_BY_PRICE, HOT_BY_VOLUME
scanner_presets
获取可用扫描预设列表。
scan_options_volume
扫描期权异动(高成交量)。
参数 | 类型 | 默认 | 说明 |
| int |
| 最小成交量 |
| int |
| 最小持仓量 |
| int |
| 最大结果数 |
📰 News (3 工具)
get_news
获取标的相关新闻。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| string |
| 交易所 |
| string |
| 货币 |
| int |
| 返回条数 |
get_earnings_calendar
获取财报日历。
参数 | 类型 | 默认 | 说明 |
| list[str] |
| 股票代码列表(默认蓝筹股) |
| int |
| 未来天数 |
get_dividend_calendar
获取分红日历。
参数 | 类型 | 默认 | 说明 |
| list[str] |
| 股票代码列表(默认高息股) |
🧠 Strategies (3 工具)
融合现有 IB skills 的策略分析工具。需在
config/plugins.yaml中开启strategies插件。
get_short_roll_candidates
寻找滚动空头/备兑开仓机会。
分析当前持仓中的空头股票,返回可滚动或备兑的候选列表。
analyze_collar
Collar 保护策略分析。
参数 | 类型 | 默认 | 说明 |
| string | 必填 | 股票代码 |
| int | 必填 | 持仓数量 |
| float |
| Put 行权价偏移(下行保护比例) |
| float |
| Call 行权价偏移(上行封顶比例) |
| string |
| 到期日 |
返回:{ currentPrice, putStrike, callStrike, maxLoss, maxProfit, putProtection, callCap }
get_portfolio_actions
持仓行动建议(财报风险、盈亏review)。
分析持仓盈亏百分比,对亏损超过 10% 或盈利超过 30% 的标的给出 review 建议。
插件扩展
新增工具(3 步)
Step 1: 在 src/ibkr_mcp/plugins/ 下新建或编辑文件
Step 2: 使用装饰器注册
# src/ibkr_mcp/plugins/news.py
from ibkr_mcp.core.registry import ToolRegistry
from ibkr_mcp.core.connection import IBConnection
from ibkr_mcp.core.response import MCPResponse
@ToolRegistry.register("news")
async def get_news(conid: int, limit: int = 10) -> dict:
"""获取标的新闻"""
ib = await IBConnection.get()
# ... 实现逻辑
return MCPResponse.ok(data=news)Step 3: 在 config/plugins.yaml 中启用(如果是新分类)
plugins:
enabled:
- news # 新增重启即可。零核心代码修改。
与现有 Skills 融合
融合策略
现有 Skill | 融合方式 | 状态 |
| → MCP | 可替代 |
| → MCP | 可替代 |
| → MCP | 可替代 |
| 保留 skill,数据源改 MCP | 策略逻辑保留 |
| 保留 skill,数据源改 MCP | 策略逻辑保留 |
| → MCP | 可替代 |
| → MCP | 可替代 |
| 保留(不依赖 IB 连接) | 不受影响 |
迁移建议
数据类 skills(
ib-account,ib-portfolio,ib-option-chain)→ 直接用 MCP 工具替代策略类 skills(
ib-find-short-roll,ib-collar)→ 保留策略逻辑,底层数据改为调用 MCP分析类 skills(
ib-report-delta-exposure)→ 已迁入 MCPget_delta_exposure
常见问题
连接失败
Q: Connection refused
确保 IB Gateway / TWS 正在运行,且 API 端口匹配:
# TWS 模拟盘
IB_PORT=7497
# IB Gateway 模拟盘
IB_PORT=4002Q: Client ID conflict
每个连接需要唯一的 Client ID。修改 .env 中的 IB_CLIENT_ID。
市场数据
Q: 返回延迟数据而非实时数据
设置 IB_REALTIME_DATA=false(默认),系统自动使用延迟免费数据(type 3)。
如需实时数据,需订阅 IB 市场数据包并设置 IB_REALTIME_DATA=true。
Q: 某些标的数据为空
确保合约代码正确。先用 search_contract 或 symbol_to_conid 验证。
交易
Q: 下单被拒绝
检查 IB_READ_ONLY=true。生产环境默认只读,如需交易设为 false。
Q: 订单一直未成交
检查价格是否合理。限价单价格偏离市场价太远不会成交。
性能
Q: Gateway 崩溃
增加 Java 内存:Configure → Settings → Memory Allocation → 4096 MB。
Q: 请求超时
增加 API timeout:在 IB Gateway 设置中调大超时时间。
项目结构
ibkr-tws-mcp/
├── pyproject.toml # 项目配置
├── .env.example # 环境变量模板
├── config/plugins.yaml # 插件配置
├── README.md # 本文档
├── src/ibkr_mcp/
│ ├── config.py # 配置管理
│ ├── main.py # MCP Server 入口
│ ├── core/
│ │ ├── connection.py # 连接管理(单例 + 自动重连)
│ │ ├── registry.py # 插件注册中心
│ │ ├── middleware.py # 中间件(只读/风控/限流)
│ │ └── response.py # 统一响应信封
│ ├── plugins/
│ │ ├── market_data.py # 行情 (10 工具)
│ │ ├── contracts.py # 合约 (7 工具)
│ │ ├── account.py # 账户 (8 工具)
│ │ ├── orders.py # 订单 (9 工具)
│ │ ├── risk.py # 风控 (5 工具)
│ │ ├── scanner.py # 扫描器 (3 工具)
│ │ ├── strategies.py # 策略 (3 工具)
│ │ └── news.py # 新闻 (3 工具)
│ └── shared/
│ ├── formatters.py # 数据格式化
│ └── validators.py # 参数验证
└── tests/许可证
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
Trade across 22+ exchanges and brokers from any MCP-capable AI agent, no install required.
Trade 16 crypto exchanges + MetaTrader 5 from your AI assistant via one MCP connection.
MCP server exposing the Backtest360 engine API as tools for AI agents.
Real SEC, 13F, insider, congress & macro data your AI agent can cite. Hosted MCP, 24 tools.
Related MCP Servers
- AlicenseBqualityBmaintenanceEnables AI assistants to interact with Interactive Brokers trading accounts to retrieve market data, check positions, and place trades. Includes pre-configured IB Gateway and handles OAuth authentication automatically.141,188 npm216MIT
- AlicenseNot gradedqualityDmaintenanceMCP server that provides AI agents with financial tools including real-time quotes, backtesting, technical analysis, and multi-exchange data via a simple CLI interface.1MIT
- AlicenseNot gradedqualityDmaintenanceMCP server for Interactive Brokers that exposes portfolio data, market quotes, trading, and analysis to any MCP-compatible AI client, with support for EU investors and safety-gated trading.1MIT
- AlicenseAqualityAmaintenanceEnables AI agents to trade on Interactive Brokers: check quotes, balance, positions, and execute buy/sell orders (including fractional shares by dollar amount) via the Client Portal API.2013MIT