An Interactive Brokers (IBKR) MCP server implementation based on FastMCP 2.0 and MCP StreamableHTTP, providing account management, trading operations, and market data query functionality.
- 🔗 Connection Management: Stable connection with IBKR TWS/Gateway
- 📊 Account Information: Query account summary, positions, and balances
- 💹 Trading Operations: Place orders, cancel orders, query order status
- 📈 Market Data: Real-time and historical market data retrieval
- 🛡️ Type Safety: Data validation using Pydantic
- ⚡ Async Architecture: High-performance asynchronous I/O operations
- 📝 Rich Logging: Structured logging
- 🔧 Flexible Configuration: Support for environment variables and configuration files
git clone https://github.com/yourusername/ibkr-mcp-server.git
cd ibkr-mcp-server
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install the package
pip install -e .# Install development dependencies
pip install -e ".[dev]"
# Setup pre-commit hooks
pre-commit installCreate a .env file in the project root:
# MCP Server Settings
MCP__HOST=0.0.0.0
MCP__PORT=8080
# IBKR Connection Settings
IBKR__HOST=127.0.0.1
IBKR__PORT=7497
IBKR__CLIENT_ID=1
IBKR__READONLY=false
# Logging Settings
LOGGING__LEVEL=INFO# Test IBKR connection
python -m ibkr_mcp_server.cli test --host 127.0.0.1 --port 7497# Start server
python -m ibkr_mcp_server.cli serve
# Or with custom parameters
python -m ibkr_mcp_server.cli serve --host 0.0.0.0 --port 8080| Variable | Default | Description |
|---|---|---|
MCP__HOST |
0.0.0.0 |
MCP server listen address |
MCP__PORT |
8080 |
MCP server port |
IBKR__HOST |
127.0.0.1 |
IBKR TWS/Gateway address |
IBKR__PORT |
7497 |
IBKR TWS/Gateway port |
IBKR__CLIENT_ID |
1 |
IBKR client ID |
IBKR__READONLY |
false |
Read-only mode |
LOGGING__LEVEL |
INFO |
Logging level |
| Platform | Demo Port | Live Port |
|---|---|---|
| TWS | 7497 | 7496 |
| Gateway | 4002 | 4001 |
The server provides 9 MCP tools:
get_account_summary: Get account summary informationget_positions: Get position information
place_order: Place an ordercancel_order: Cancel an orderget_open_orders: Get open orders
get_market_data: Get real-time market dataget_historical_data: Get historical data
connection_status: Check connection statusreconnect: Reconnect to IBKR
{
"tool": "place_order",
"arguments": {
"contract": {
"symbol": "AAPL",
"sec_type": "STK",
"exchange": "SMART",
"currency": "USD"
},
"order": {
"action": "BUY",
"total_quantity": 100,
"order_type": "LMT",
"lmt_price": 150.0
}
}
}{
"tool": "get_positions",
"arguments": {}
}{
"tool": "get_historical_data",
"arguments": {
"contract": {
"symbol": "AAPL",
"sec_type": "STK",
"exchange": "SMART",
"currency": "USD"
},
"duration": "1 D",
"bar_size": "1 min"
}
}┌─────────────────────┐
│ MCP Client │
│ (Claude Desktop, │
│ Custom Client) │
└─────────┬───────────┘
│ HTTP/WebSocket
┌─────────┴───────────┐
│ FastMCP Server │
│ (MCP Protocol Layer)│
├─────────────────────┤
│ IBKR MCP Server │
│ (Business Logic) │
├─────────────────────┤
│ IBKR Client │
│ (API Wrapper) │
└─────────┬───────────┘
│ TWS API
┌─────────┴───────────┐
│ TWS/Gateway │
│ (IBKR Platform) │
└─────────────────────┘
src/ibkr_mcp_server/
├── __init__.py # Package initialization
├── server.py # MCP server implementation
├── client.py # IBKR client wrapper
├── models.py # Data models
├── config.py # Configuration management
├── exceptions.py # Exception definitions
└── cli.py # Command line interface
- Use
blackfor code formatting - Use
isortfor import sorting - Use
flake8for code linting - Use
mypyfor type checking
# Run tests
pytest
# Generate coverage report
pytest --cov=src --cov-report=html# Build image
docker build -t ibkr-mcp-server .
# Run container
docker run -p 8080:8080 --env-file .env ibkr-mcp-server# Start services
docker-compose up -d
# View logs
docker-compose logs -fAdd to your Claude Desktop MCP configuration:
{
"mcpServers": {
"ibkr": {
"command": "python",
"args": ["-m", "ibkr_mcp_server.cli", "serve"],
"env": {
"IBKR__HOST": "127.0.0.1",
"IBKR__PORT": "7497",
"IBKR__CLIENT_ID": "1"
}
}
}
}- TWS/Gateway: Ensure IBKR TWS or Gateway is running with API connection enabled
- Port Configuration: Make sure TWS/Gateway API port matches your configuration
- Permissions: Ensure your account has appropriate trading permissions
- Risk Management: Please implement proper risk controls in production environments
- Market Data: Some market data may require subscriptions
- Connection Failed: Check if TWS/Gateway is running and API is enabled
- Client ID Conflict: Use different client IDs for multiple connections
- Port Issues: Verify the correct port for your TWS/Gateway setup
- Market Data Errors: Ensure you have proper market data subscriptions
Enable debug logging for troubleshooting:
LOGGING__LEVEL=DEBUG python -m ibkr_mcp_server.cli serveMIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues, please file an Issue.
中文文档: README_zh_CN.md