CANCELLEDERROR_TROUBLESHOOTING.md•4.79 kB
# CancelledError Troubleshooting Guide
## Current Issue
When calling `ibkr_connect` through the MCP protocol, you're getting:
```
CancelledError("Cancelled via cancel scope...")
```
## What This Means
**CancelledError** is different from the previous "Future attached to different loop" error. This error means:
- The MCP session cancelled the request before it completed
- The connection to TWS is taking too long
- OR TWS isn't running/accepting connections
## Changes Made to Fix
### 1. Reduced Timeout ✅
Changed connection timeout from 10s to 5s to avoid MCP session cancellation:
```python
# Old: timeout=10 seconds
await self.ib.connectAsync(host, port, clientId=client_id, timeout=10)
# New: timeout=5 seconds with 8s overall timeout
await asyncio.wait_for(
self.ib.connectAsync(host, port, clientId=client_id, timeout=5),
timeout=8.0
)
```
### 2. Better Error Messages ✅
Added specific handling for:
- `asyncio.TimeoutError` - Connection takes too long
- `asyncio.CancelledError` - Request cancelled by MCP client
- Other exceptions - Generic connection failures
### 3. Created Diagnostic Tool ✅
**`test_tws_connection.py`** - Test TWS connection directly (outside MCP):
```bash
# Test with default settings (TWS Live on 7496)
uv run python test_tws_connection.py
# Test with custom port (TWS Paper)
uv run python test_tws_connection.py --port 7497
# Test IB Gateway
uv run python test_tws_connection.py --port 4001
```
## Debugging Steps
### Step 1: Test TWS Connection Directly
```bash
# This bypasses the MCP layer to test if TWS is reachable
uv run python test_tws_connection.py
```
**Expected outcomes:**
- ✅ **Success** → TWS is running and accepting connections (problem is in MCP layer)
- ❌ **Timeout** → TWS not running or wrong port
- ❌ **Connection Refused** → TWS not listening on that port
### Step 2: Check TWS Configuration
In TWS/IB Gateway:
1. Go to: **Edit → Global Configuration → API → Settings**
2. Verify:
- ✅ "Enable ActiveX and Socket Clients" is checked
- ✅ "Socket Port" shows the correct port:
- TWS Live: 7496
- TWS Paper: 7497
- IB Gateway Live: 4001
- IB Gateway Paper: 4002
- ✅ "Allow connections from localhost only" (unless using remote)
3. **Restart TWS after any changes**
### Step 3: Check Your .env File
```bash
# Check current settings
cat .env
```
Should contain:
```env
TWS_HOST=127.0.0.1
TWS_PORT=7496 # Or 7497 for paper trading
TWS_CLIENT_ID=1
```
### Step 4: Test Through MCP
After verifying TWS is working with the diagnostic tool:
1. Start the MCP server:
```bash
uv run python main.py
```
2. Connect via Claude Desktop or MCP Inspector
3. Call `ibkr_connect` tool
## Common Issues & Solutions
### Issue: "Connection timeout"
**Cause:** TWS not running or wrong port
**Solution:**
- Start TWS/IB Gateway
- Verify port number matches your TWS settings
- Use diagnostic tool to test: `uv run python test_tws_connection.py`
### Issue: "Connection refused"
**Cause:** TWS not listening on that port
**Solution:**
- Check TWS API settings (see Step 2 above)
- Verify port number in .env matches TWS configuration
- Try different port (7496 vs 7497)
### Issue: "Connection cancelled" (CancelledError)
**Cause:** MCP session timeout or client disconnection
**Solution:**
- Connection is taking too long (see timeout issue above)
- OR TWS is slow to respond (start TWS earlier)
- Try the diagnostic tool to verify TWS is responsive
### Issue: Still getting event loop errors
**Cause:** Not using ib_async library
**Solution:**
- Verify `uv pip list | grep ib-async` shows version 2.0.1
- Not `ib-insync`
- Run `uv sync` to reinstall dependencies
## Port Reference
| Application | Mode | Port |
|-------------|------|------|
| TWS | Live | 7496 |
| TWS | Paper | 7497 |
| IB Gateway | Live | 4001 |
| IB Gateway | Paper | 4002 |
## Next Steps
1. **Run the diagnostic tool:**
```bash
uv run python test_tws_connection.py
```
2. **If diagnostic succeeds:**
- TWS connection works ✅
- Problem is in MCP layer
- Try restarting the MCP server
- Check MCP client timeout settings
3. **If diagnostic fails:**
- Fix TWS configuration (see Step 2)
- Verify correct port
- Ensure TWS is running
- Check firewall settings
## Files Modified
- ✅ `src/tws_client.py` - Reduced timeout, better error handling
- ✅ `tests/unit/test_tws_client.py` - Updated test expectations
- ✅ `test_tws_connection.py` - NEW diagnostic tool
## Status
- [x] Migrated to ib_async (fixes event loop errors)
- [x] Reduced connection timeout (5s)
- [x] Added better error messages
- [x] Created diagnostic tool
- [ ] **NEED TO TEST**: Run diagnostic tool with TWS
- [ ] **NEED TO TEST**: Test via MCP after diagnostic passes