Skip to main content
Glama
Interactive Brokers (IBKR) MCP Server

Interactive Brokers (IBKR) MCP Server

README.md
# Interactive Brokers (IBKR) MCP Server

A lightweight, zero-framework-dependency **Model Context Protocol (MCP)** server in Python that connects AI agents to Interactive Brokers. Implements MCP JSON-RPC 2.0 over `stdio` natively.

## Auto-Detection Across IBKR Apps

Automatically probes and connects to whichever local IBKR interface is running:
1. **TWS (Paper Trading)** — Port `7497`
2. **IB Gateway (Paper Trading)** — Port `4002`
3. **TWS (Live Trading)** — Port `7496`
4. **IB Gateway (Live Trading)** — Port `4001`
5. **Client Portal Web API Gateway** — Port `5000` (`https://127.0.0.1:5000/v1/api`)

> **Tip:** If you use **IBKR Desktop** as your main screen, run **IB Gateway** or **Client Portal Web API Gateway** alongside it so the local API is available.

## Setup

1. Install dependencies:
   ```bash
   pip install -r requirements.txt
   ```
2. In **TWS** or **IB Gateway**, open **File → Global Configuration → API → Settings**:
   - Check **Enable ActiveX and Socket Clients**
   - Uncheck **Read-Only API** *(only if you want to stage or place orders)*
   - Verify the **Socket port** (`7497` TWS Paper / `7496` TWS Live / `4002` Gateway Paper / `4001` Gateway Live)
   - Ensure `127.0.0.1` is allowed under Trusted IPs / *"Allow connections from localhost only"*
3. Add the server to your MCP client config:
   ```json
   {
     "mcpServers": {
       "ibkr": {
         "command": "python3",
         "args": ["/path/to/ibkr-mcp/server.py"]
       }
     }
   }
   ```

## Available MCP Tools

| Tool | Description |
| :--- | :--- |
| `ibkr_status` | Scans local ports (`7497`, `4002`, `7496`, `4001`, `5000`) and reports which IBKR app is running |
| `ibkr_account_summary` | Net Liquidation, Cash, Buying Power, Available Funds, Margin, and Realized/Unrealized P&L |
| `ibkr_positions` | Current portfolio holdings, average cost, market price, market value, and P&L |
| `ibkr_market_quote` | Live or delayed bid/ask/last/high/low/volume quote for stocks, ETFs, forex, options, or futures |
| `ibkr_historical_bars` | Historical OHLCV candlestick bars (`1 D`, `1 M`, `1 Y` at `1 min`, `5 mins`, `1 hour`, `1 day`, etc.) |
| `ibkr_search_contract` | Search contracts by ticker or company name (`conId`, exchange, available derivative types) |
| `ibkr_option_chain` | Fetch option expirations and strikes for an underlying ticker |
| `ibkr_open_orders` | List all currently open or working orders |
| `ibkr_place_order` | Stage (`transmit=False` by default) or transmit (`transmit=True`) `LMT`, `MKT`, or `STP` orders |
| `ibkr_cancel_order` | Cancel an open order by `orderId` |