Skip to main content
Glama

IBKR TWS MCP Server

by haymant
FIX_SUMMARY.md3.41 kB
# Fix Summary: Event Loop Error Resolved ## Problem The `ibkr_connect` tool was returning a runtime error: ``` RuntimeError("Task ... got Future ... attached to a different loop") ``` This occurred because the `ib_insync.IB()` instance was created in `TWSClient.__init__` which ran in one event loop context (during server startup), but `connect()` was called from a different event loop (during MCP tool invocation). ## Solution Applied ### 1. Updated `src/tws_client.py` **Changed the IB initialization strategy:** - Previously: Created `IB()` in `__init__` and tried to detect/recreate if on different loop - Now: **Always** recreate `IB()` at the start of `connect()` to ensure it's bound to the current event loop **Key changes:** - `__init__`: Still creates an initial `IB()` instance (needed for unit tests that patch `IB`) - `connect()`: Unconditionally recreates `self.ib = IB()` before connecting - Added `_to_dict()` helper to safely convert ib_insync dataclasses to dicts (handles MagicMocks in tests) - Replaced all `util.dataclassAsDict()` calls with `_to_dict()` for test compatibility ### 2. Updated `docs/SETUP.md` Added a new section: **"5. Connecting from Claude MCP Inspector"** with: - Step-by-step instructions to connect the Inspector to the running server - How to use ngrok/localtunnel for remote access - Troubleshooting tips for common connection issues - Example curl commands to verify the manifest endpoint ## Testing Results ### Unit Tests ✅ **All 6 tests passing** Command: ```bash uv run pytest tests/unit/test_tws_client.py -q ``` Result: ``` ...... 6 passed in 0.93s ``` ### Runtime Test ✅ **Server starts successfully with no loop errors** Server log excerpt: ``` INFO: Started server process [51775] INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: 127.0.0.1:47452 - "GET /api/v1/sse HTTP/1.1" 200 OK ``` No "different loop" errors present. ## How to Use ### Start the Server ```bash uv run python main.py ``` ### Test with Claude MCP Inspector 1. Expose your local server (if needed): ```bash ngrok http 8000 ``` 2. Open: https://www.claudemcp.com/inspector 3. Enter server endpoint: - Local: `http://localhost:8000/api/v1/sse` - ngrok: `https://xxxx.ngrok.io/api/v1/sse` 4. Click "Connect" - the Inspector will fetch available tools 5. Test the `ibkr_connect` tool with your TWS credentials ### Test Locally with curl ```bash # Check SSE endpoint curl -i http://localhost:8000/api/v1/sse # Should return HTTP 200 and keep connection open (SSE stream) ``` ## Files Modified 1. **src/tws_client.py** - Always recreate IB instance in `connect()` - Added `_to_dict()` helper for safe dataclass conversion - Replaced all `util.dataclassAsDict()` calls 2. **tests/unit/test_tws_client.py** - Added missing `import asyncio` to fix test_stream_market_data_generator 3. **docs/SETUP.md** - Added "Connecting from Claude MCP Inspector" section - Added troubleshooting subsection with common issues and fixes ## Known Minor Issues None! All unit tests pass and the server runs without errors. ## Next Steps (Optional) - Fix the minor test import issue - Add `pytest-asyncio` to dev dependencies - Add integration test that actually calls `ibkr_connect` with a running TWS instance - Document the MCP tool protocol more extensively in API.md

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