IBKR TWS MCP Server

IBKR TWS MCP Server

MCP server for Interactive Brokers TWS API, enabling automated financial workflows like portfolio rebalancing through 51+ tools for market data, account management, and order execution, plus real-time WebSocket streaming.

Category
访问服务器

README

IBKR TWS MCP Server

This project implements a Model Context Protocol (MCP) server for the Interactive Brokers (IBKR) Trader Workstation (TWS) API. It uses the official modelcontextprotocol/python-sdk and ib_async to expose key TWS functionalities as MCP tools, enabling seamless integration with LLM clients for automated financial workflows, such as portfolio rebalancing.

The server supports HTTP Streaming for MCP protocol and WebSocket streaming for real-time data subscriptions (market data, portfolio updates, news bulletins).

Features

  • MCP Compliance: Built with FastMCP for full adherence to the Model Context Protocol with HTTP streaming support.
  • Asynchronous TWS Integration: Leverages ib_async (maintained fork of ib-insync) for non-blocking, asynchronous interaction with the TWS API.
  • Comprehensive Toolset: 51+ tools for connection management, market data retrieval (historical and streaming), account and portfolio querying, and order management (placing and canceling orders).
  • MCP Prompts: 6 guided workflows that combine multiple tools into expert-level trading strategies (bracket orders, portfolio rebalancing, risk assessment, options strategies, market analysis, and workspace setup).
  • Real-time WebSocket Streaming: Three dedicated WebSocket endpoints for continuous real-time data:
    • Market Data Stream - Real-time quotes for multiple symbols
    • Portfolio Stream - Live portfolio and account updates
    • News Stream - IBKR news bulletins and system messages
  • HTTP Streaming Transport: Uses chunked transfer encoding for efficient MCP tool responses.
  • CORS Support: Configured to allow cross-origin requests from browser-based MCP clients (e.g., Goflow, web applications).
  • Containerized Deployment: Ready-to-use Docker and Docker Compose configuration.

Architecture

┌─────────────────────────────────────────────────┐
│  Client Application                              │
├─────────────────────────────────────────────────┤
│  1. MCP Client (HTTP Streaming)                 │
│     POST /api/v1/mcp                             │
│     - Tool calls: connect, orders, positions     │
│                                                   │
│  2. WebSocket Clients (real-time streams)       │
│     WS /api/v1/stream/market-data                │
│     WS /api/v1/stream/portfolio                  │
│     WS /api/v1/stream/news                       │
└─────────────────────────────────────────────────┘
         │                           │
         │ HTTP POST                 │ WebSocket
         ▼                           ▼
┌─────────────────────────────────────────────────┐
│  IBKR TWS MCP Server                             │
├─────────────────────────────────────────────────┤
│  • FastMCP Server (HTTP streaming)               │
│  • WebSocket Handlers (real-time streaming)     │
│  • Shared TWS Client (ib_async)                  │
└─────────────────────────────────────────────────┘
         │
         │ IB Client Protocol
         ▼
┌─────────────────────────────────────────────────┐
│  TWS / IB Gateway                                │
│  (127.0.0.1:7497 or :4001)                      │
└─────────────────────────────────────────────────┘

Quick Start

1. Install Dependencies

# Using uv (recommended)
uv sync

# Or using pip
pip install -r requirements.txt

2. Configure Environment

# Copy example environment file
cp .env.example .env

# Edit .env with your settings
TWS_HOST=127.0.0.1
TWS_PORT=7497
TWS_CLIENT_ID=1
TWS_PAPER_ACCOUNT=DU2515295
SERVER_HOST=0.0.0.0
SERVER_PORT=8000

3. CORS Configuration (Browser Clients)

The server is configured to allow cross-origin requests from browser-based MCP clients:

  • Allowed Origins: All origins (*) - supports external domains like https://test.lizhao.net
  • Allowed Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
  • Allowed Headers: All headers including MCP-specific headers (Mcp-Session-Id, Mcp-Initialize-Request)
  • Credentials: Enabled for authenticated requests
  • Preflight Cache: 24 hours

This enables integration with browser-based MCP clients like Goflow without CORS restrictions.

4. Start TWS/Gateway

Launch Interactive Brokers TWS or IB Gateway and enable API connections.

5. Run the Server

uv run python main.py

5. Test the Server

# Health check
curl http://localhost:8000/health

# MCP tool call (connect to TWS)
curl -X POST http://localhost:8000/api/v1/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "ibkr_connect",
      "arguments": {"host": "127.0.0.1", "port": 7497, "clientId": 1}
    },
    "id": 1
  }'

WebSocket Streaming Examples

Market Data Stream (Python)

import websockets
import json
import asyncio

async def stream_quotes():
    async with websockets.connect('ws://localhost:8000/api/v1/stream/market-data') as ws:
        # Subscribe to AAPL
        await ws.send(json.dumps({
            "action": "subscribe",
            "symbol": "AAPL",
            "secType": "STK",
            "exchange": "SMART",
            "currency": "USD"
        }))
        
        # Receive real-time updates
        async for message in ws:
            data = json.loads(message)
            if data["type"] == "market_data":
                print(f"{data['symbol']}: ${data['data']['last']}")

asyncio.run(stream_quotes())

Market Data Stream (JavaScript/Browser)

const ws = new WebSocket('ws://localhost:8000/api/v1/stream/market-data');

ws.onopen = () => {
    ws.send(JSON.stringify({
        action: 'subscribe',
        symbol: 'AAPL',
        secType: 'STK'
    }));
};

ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    if (data.type === 'market_data') {
        console.log(`${data.symbol}: $${data.data.last}`);
    }
};

For complete examples including portfolio streams and news bulletins, see HTTP Streaming Examples.

MCP Prompts

The server includes 6 comprehensive prompts that provide guided workflows for complex trading operations:

Portfolio Management

  • setup_trading_workspace - Complete workspace setup with streaming resources
  • rebalance_portfolio - Portfolio rebalancing with tax optimization and OCA groups
  • assess_portfolio_risk - Risk analysis with beta weighting, VaR, and stress testing

Trading Execution

  • execute_bracket_order - Bracket orders with take-profit and stop-loss automation
  • execute_options_strategy - Options strategies (covered calls, protective puts, collars, etc.)

Market Analysis

  • analyze_market_conditions - Multi-dimensional analysis combining technicals, fundamentals, news, and options

Each prompt provides step-by-step workflows with tool call examples, best practices, risk warnings, and sample calculations. See Prompts Guide for details.

Getting Started

Please refer to the Setup Guide for detailed instructions on prerequisites, environment configuration, and running the server locally or in a container.

API Reference

A complete list of all exposed MCP tools, their parameters, and return types can be found in the API Reference.

Streaming Documentation

End-to-End Testing

The server is designed to support the portfolio rebalancing E2E case. You can test all functionalities using the Claude MCP Inspector.

See the dedicated section in the API Reference for a step-by-step guide on how to use the Inspector to interact with your running server.

Project Structure

For a detailed explanation of the project organization, see PROJECT_STRUCTURE.md.

ibkr-tws-mcp-server/
├── src/                   # Source code
│   ├── server.py          # FastMCP server and tool definitions
│   ├── tws_client.py      # TWS client wrapper using ib_async
│   ├── models.py          # Pydantic data models
│   └── streaming/         # WebSocket streaming handlers
│       ├── websocket_manager.py
│       ├── market_data.py
│       ├── portfolio.py
│       └── news.py
├── tests/                 # Test suite
│   ├── unit/              # Unit tests with mocks
│   └── integration/       # Integration test documentation
├── docs/                  # Documentation
│   ├── API.md             # MCP tools API reference
│   ├── SETUP.md           # Setup and deployment guide
│   ├── HTTP_STREAMING_MIGRATION.md  # Streaming migration plan
│   ├── HTTP_STREAMING_EXAMPLES.md   # Streaming examples
│   ├── HTTP_STREAMING_SUMMARY.md    # Streaming summary
│   └── ...                # Additional guides and troubleshooting
├── diagnostics/           # Diagnostic and testing scripts
├── scripts/               # Utility scripts
├── main.py                # Application entry point
├── pyproject.toml         # Project dependencies
└── README.md              # This file

Documentation

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选