MIGRATION_TO_IB_ASYNC.md•3.17 kB
# Migration from ib-insync to ib_async
## Summary
The IBKR TWS MCP Server has been migrated from `ib-insync` to `ib_async` (v2.0.1) to resolve event loop compatibility issues.
## Why the Change?
**Problem:** The original `ib-insync` library has known issues with event loop management in modern async Python applications:
- Captures event loops at initialization time
- Causes "Future attached to a different loop" errors
- Not actively maintained (last update 2021)
**Solution:** `ib_async` is a maintained fork that:
- Properly handles multiple event loops
- Actively maintained by the community
- API-compatible with ib-insync (drop-in replacement)
- Repository: https://github.com/ib-api-reloaded/ib_async
## What Changed?
### Dependencies (pyproject.toml)
```toml
# OLD
dependencies = [
"ib-insync>=0.9.86",
...
]
# NEW
dependencies = [
"ib-async==2.0.1",
"aeventkit==2.1.0", # Required by ib_async
...
]
```
### Imports
```python
# OLD
from ib_insync import IB, Stock, Contract, ...
# NEW
from ib_async import IB, Stock, Contract, ...
```
### Code Changes
**No other code changes required!** The `ib_async` API is 100% compatible with `ib_insync`.
## Installation
```bash
# Install the new dependencies
uv pip install ib-async==2.0.1 aeventkit==2.1.0
# Or reinstall from pyproject.toml
uv sync
```
## Files Updated
1. **Source Code:**
- `src/tws_client.py` - Main TWS client implementation
2. **Tests:**
- `tests/unit/test_tws_client.py` - Unit tests
3. **Utility Scripts:**
- `inspect_ib_structure.py`
- `check_ib_loop_attrs.py`
- `check_ib_client_loop.py`
4. **Dependencies:**
- `pyproject.toml` - Project dependencies
5. **Documentation:**
- All docs updated to reference `ib_async` instead of `ib-insync`
## Benefits
1. **Event Loop Compatibility:** Fixes "Future attached to a different loop" errors
2. **Active Maintenance:** Receives bug fixes and updates
3. **Better FastMCP Integration:** Works seamlessly with multiple event loops
4. **No Breaking Changes:** API-compatible drop-in replacement
## Verification
All unit tests pass with `ib_async`:
```bash
$ uv run pytest tests/unit/test_tws_client.py -v
# Result: 6 passed in 1.15s ✅
```
## References
- **ib_async GitHub:** https://github.com/ib-api-reloaded/ib_async
- **ib_async PyPI:** https://pypi.org/project/ib-async/
- **API Documentation:** https://ib-api-reloaded.github.io/ib_async/ (compatible with ib-insync docs)
## Migration Checklist
- [x] Update pyproject.toml dependencies
- [x] Change all import statements from `ib_insync` to `ib_async`
- [x] Run unit tests to verify compatibility
- [x] Update documentation
- [ ] Test with actual TWS connection
- [ ] Verify all MCP tools work without event loop errors
## Troubleshooting
If you encounter import errors after migrating:
```bash
# Clear any cached packages
rm -rf .venv
uv venv
uv sync
# Verify installation
uv pip list | grep ib-async
# Should show: ib-async 2.0.1
```
## Next Steps
1. Test the `ibkr_connect` tool with a running TWS instance
2. Verify no event loop errors occur during MCP tool invocations
3. Run full integration tests (requires TWS)