Supports containerized deployment with ready-to-use Docker and Docker Compose configuration for running the IBKR TWS MCP server
Uses Pydantic data models for structured data validation and serialization in the TWS API integration
Built using Python with the official modelcontextprotocol/python-sdk for MCP compliance and ib_async for TWS API integration
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
Quick Start
1. Install Dependencies
2. Configure Environment
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 likehttps://test.lizhao.netAllowed 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
5. Test the Server
WebSocket Streaming Examples
Market Data Stream (Python)
Market Data Stream (JavaScript/Browser)
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
HTTP Streaming Migration - Migration plan and architecture
HTTP Streaming Examples - Complete client examples
HTTP Streaming Summary - Changes and best practices
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.
Documentation
Setup Guide - Installation and configuration
API Reference - Complete tool documentation
Prompts Guide - MCP prompts quick reference
Design Document - Architecture and design decisions
HTTP Streaming Guide - Real-time streaming architecture
Streaming Examples - Client code examples
Project Structure - Detailed project organization
Migration Guide - ib-insync to ib_async migration