Skip to main content
Glama

IBKR TWS MCP Server

by haymant
CURRENT_ISSUE_TWS_API_BLOCKED.mdโ€ข3.58 kB
# ๐Ÿ”ด CRITICAL ISSUE: TWS API Handshake Timeout ## Current Status: Connection Blocked ### Diagnosis Complete โœ… Your diagnostic output shows: ``` INFO:ib_async.client:Connecting to 127.0.0.1:7496 with clientId 0... INFO:ib_async.client:Connected โ† Socket works โœ… ERROR:ib_async.client:API connection failed: TimeoutError() โ† API fails โŒ ``` **Translation:** - โœ… TWS is running on port 7496 - โœ… Network/socket connection works (telnet succeeds) - โœ… ib_async can connect to the socket - โŒ **TWS is NOT completing the IB API handshake** ## The Problem The IB API requires a specific handshake protocol after the socket connects. TWS is not responding to this handshake, which means: **TWS API is either:** 1. Not enabled properly in settings 2. Blocking the connection for security reasons 3. In a bad state and needs restart ## The Solution ### ๐Ÿ”ฅ MOST LIKELY FIX: TWS API Settings **Go to TWS:** ``` File โ†’ Global Configuration โ†’ API โ†’ Settings ``` **Make sure these are set:** - โœ… **"Enable ActiveX and Socket Clients"** = CHECKED - โœ… **Socket port** = 7496 - โœ… **"Read-Only API"** = UNCHECKED (unless you only want read-only) - โœ… If "Allow connections from localhost only" = CHECKED - Then **"Trusted IP Addresses"** MUST include `127.0.0.1` **CRITICAL:** After changing ANY settings: 1. Click "OK" 2. **Completely QUIT TWS** (File โ†’ Exit) 3. Wait 10 seconds 4. **Restart TWS** 5. Log in and wait for it to fully load 6. Try the connection again ### ๐Ÿงช Test After Restart ```bash # Run the minimal test uv run python test_minimal_tws.py ``` **Expected when working:** ``` โœ… SUCCESS! API connection established. ๐Ÿ“Š Connected accounts: ['DU123456'] โœ… TWS API IS WORKING CORRECTLY ``` ## Files Created for You 1. **`test_minimal_tws.py`** - Simplest possible connection test ```bash uv run python test_minimal_tws.py ``` 2. **`test_tws_connection.py`** - Full diagnostic with verbose logging ```bash uv run python test_tws_connection.py --client-id 0 ``` 3. **`docs/TWS_API_HANDSHAKE_TIMEOUT.md`** - Complete troubleshooting guide ## Quick Actions ### Action 1: Verify TWS Settings โšก ``` TWS โ†’ File โ†’ Global Configuration โ†’ API โ†’ Settings โœ“ Enable ActiveX and Socket Clients โœ“ Socket port = 7496 โœ“ Trusted IPs includes 127.0.0.1 โ†’ Click OK โ†’ RESTART TWS (completely quit and restart) ``` ### Action 2: Test Again ```bash uv run python test_minimal_tws.py ``` ### Action 3: If Still Failing Check TWS logs for error messages: - Windows: `C:\Users\<you>\Jts\<you>\log\api.*.log` - Mac/Linux: `~/Jts/<you>/log/api.*.log` Look for messages about rejected connections or authentication errors. ## Alternative: Try IB Gateway If TWS continues to fail, IB Gateway is more reliable for API access: 1. Download IB Gateway (lighter than TWS) 2. Configure for API connections 3. Use port 4001 (live) or 4002 (paper) 4. Update .env: `TWS_PORT=4001` ## What We've Ruled Out - โŒ Event loop issues (fixed by migrating to ib_async) - โŒ Network/firewall blocking (telnet works) - โŒ Wrong port (7496 is confirmed listening) - โŒ ib_async library issues (connects to socket fine) - โŒ Client ID conflicts (tried multiple IDs) ## What It Is - โœ… **TWS API configuration or state issue** The handshake requires TWS to actively respond. It's listening but not responding = configuration or TWS state problem. ## Status ๐Ÿ”ด **BLOCKED** - Waiting for TWS API to be properly configured **Next Step:** Fix TWS API settings, restart TWS, run `test_minimal_tws.py`

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/haymant/tws-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server