Skip to content

Commit 1463280

Browse files
authored
Merge branch 'master' into glama-badge
2 parents b129372 + 594be95 commit 1463280

162 files changed

Lines changed: 14575 additions & 1683 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [main, master, develop]
6+
pull_request:
7+
branches: [main, master, develop]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20.x'
21+
cache: 'npm'
22+
23+
- name: Install dependencies
24+
run: npm ci
25+
26+
- name: Run tests
27+
run: npm test
28+

IB-MCP.gif

34.4 MB
Loading

IB-MCP.mp4

7.97 MB
Binary file not shown.

README.md

Lines changed: 163 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,52 @@
44
<img src="https://www.interactivebrokers.com/images/web/logos/ib-logo-text-black.svg" alt="Interactive Brokers" width="300">
55
</div>
66

7-
> **⚠️ DISCLAIMER**: This is an **unofficial**, community-developed MCP server and is **NOT** affiliated with or endorsed by Interactive Brokers. This software is in **Alpha state** and may not work perfectly.
7+
> **DISCLAIMER**: This is an **unofficial**, community-developed MCP server
8+
> and is **NOT** affiliated with or endorsed by Interactive Brokers. This
9+
> software is in **Alpha state** and may not work perfectly.
810
9-
A Model Context Protocol (MCP) server that provides integration with Interactive Brokers' trading platform. This server allows AI assistants to interact with your IB account to retrieve market data, check positions, and place trades.
11+
A Model Context Protocol (MCP) server that provides integration with Interactive
12+
Brokers' trading platform. This server allows AI assistants to interact with
13+
your IB account to retrieve market data, check positions, and place trades.
1014

1115
<a href="https://glama.ai/mcp/servers/@code-rabi/interactive-brokers-mcp">
1216
<img width="380" height="200" src="https://glama.ai/mcp/servers/@code-rabi/interactive-brokers-mcp/badge" alt="Interactive Brokers Server MCP server" />
1317
</a>
1418

1519
## 🔒 Security Notice
20+
![Showcase of Interactive Brokers MCP](./IB-MCP.gif)
1621

17-
**This MCP server is designed to run locally only** for security reasons. Never deploy this to remote servers or cloud platforms as it handles sensitive trading credentials and financial data.
1822

19-
## Features
23+
## Features
2024

21-
- **Account Management**: Get account information and balances
22-
- **Position Tracking**: View current positions and P&L
23-
- **Market Data**: Real-time market data for stocks and instruments
24-
- **Order Management**: Place market, limit, and stop orders
25-
- **Order Monitoring**: Check order status and execution details
25+
- **Interactive Brokers API Integration**: Full trading capabilities including account management, position tracking, real-time market data, and order management (market, limit, and stop orders)
26+
- **Flex Query Support**: Execute Flex Queries to retrieve account statements, trade confirmations, and historical data. Queries are automatically remembered for easy reuse
27+
- **Flexible Authentication**: Choose between browser-based OAuth authentication or headless mode with credentials for automated environments
28+
- **Simple Setup**: Run directly with `npx` - no Docker or additional installations required. Includes pre-configured IB Gateway and Java runtime for all platforms
29+
30+
## Security Notice
31+
32+
**IMPORTANT WARNINGS:**
33+
34+
- **Financial Risk**: Trading involves substantial risk of loss. Always test
35+
with paper trading first.
36+
- **Security**: This software handles sensitive financial data. Only run
37+
locally, never on public servers.
38+
- **No Warranty**: This unofficial software comes with no warranties. Use at
39+
your own risk.
40+
- **Not Financial Advice**: This tool is for automation only, not financial
41+
advice.
2642

2743
## Prerequisites
2844

2945
**No additional installations required!** This package includes:
46+
3047
- Pre-configured IB Gateway for all platforms (Linux, macOS, Windows)
3148
- Java Runtime Environment (JRE) for IB Gateway
3249
- All necessary dependencies
3350

3451
You only need:
52+
3553
- Interactive Brokers account (paper or live trading)
3654
- Node.js 18+ (for running the MCP server)
3755

@@ -50,40 +68,156 @@ Add this MCP server to your Cursor/Claude configuration:
5068
}
5169
```
5270

53-
When you first use the server, a web browser window will automatically open for the Interactive Brokers OAuth authentication flow. Log in with your IB credentials to authorize the connection.
71+
When you first use the server, a web browser window will automatically open for
72+
the Interactive Brokers OAuth authentication flow. Log in with your IB
73+
credentials to authorize the connection.
74+
75+
## Headless Mode Configuration
76+
77+
For automated environments or when you prefer not to use a browser for
78+
authentication, you can enable headless mode by configuring it in your MCP
79+
server configuration:
80+
81+
```json
82+
{
83+
"mcpServers": {
84+
"interactive-brokers": {
85+
"command": "npx",
86+
"args": ["-y", "interactive-brokers-mcp"],
87+
"env": {
88+
"IB_HEADLESS_MODE": "true",
89+
"IB_USERNAME": "your_ib_username",
90+
"IB_PASSWORD_AUTH": "your_ib_password"
91+
}
92+
}
93+
}
94+
}
95+
96+
```
97+
98+
In headless mode, the server will automatically authenticate using your
99+
credentials without opening a browser window. This is useful for:
100+
101+
- Automated trading systems
102+
- Server environments without a display
103+
- CI/CD pipelines
104+
- Situations where browser interaction is not desired
105+
106+
**Important**: Even in headless mode, Interactive Brokers may still require
107+
two-factor authentication (2FA). When 2FA is triggered, the headless
108+
authentication will wait for you to complete the 2FA process through your
109+
configured method (mobile app, SMS, etc.) before proceeding.
110+
111+
To enable paper trading, add `"IB_PAPER_TRADING": "true"` to your environment variables:
112+
113+
```json
114+
{
115+
"mcpServers": {
116+
"interactive-brokers": {
117+
"command": "npx",
118+
"args": ["-y", "interactive-brokers-mcp"],
119+
"env": {
120+
"IB_HEADLESS_MODE": "true",
121+
"IB_USERNAME": "your_ib_username",
122+
"IB_PASSWORD_AUTH": "your_ib_password",
123+
"IB_PAPER_TRADING": "true"
124+
}
125+
}
126+
}
127+
}
128+
```
129+
130+
**Security Note**: Store credentials securely and never commit them to version
131+
control. Consider using environment variable files or secure credential
132+
management systems.
133+
134+
## Flex Query Configuration (Optional)
135+
136+
To use Flex Queries for retrieving account statements and historical data, you need to configure your Flex Web Service Token:
137+
138+
```json
139+
{
140+
"mcpServers": {
141+
"interactive-brokers": {
142+
"command": "npx",
143+
"args": ["-y", "interactive-brokers-mcp"],
144+
"env": {
145+
"IB_FLEX_TOKEN": "your_flex_token_here"
146+
}
147+
}
148+
}
149+
}
150+
```
151+
152+
### How to Get Your Flex Token:
153+
154+
1. Log in to [Interactive Brokers Account Management](https://www.interactivebrokers.com/portal)
155+
2. Go to **Settings****Account Settings**
156+
3. Navigate to **Reporting****Flex Web Service**
157+
4. Generate or retrieve your Flex Web Service Token
158+
159+
For detailed instructions on enabling Flex Web Service, see the [IB Flex Web Service Guide](https://www.ibkrguides.com/orgportal/performanceandstatements/flex-web-service.htm).
160+
161+
### Creating Flex Queries:
162+
163+
1. Go to **Reports****Flex Queries** in Account Management
164+
2. Create or customize your query template
165+
3. Click the info icon next to your query to find its Query ID
166+
167+
For a complete guide on creating and customizing Flex Queries, see the [IB Flex Queries Guide](https://www.ibkrguides.com/orgportal/performanceandstatements/flex.htm).
168+
169+
**Note**: When you execute a Flex Query for the first time, the MCP server automatically saves it with its name from the API. Future executions can reference the query by either its ID or its saved name.
170+
171+
### Flex Query Features:
172+
173+
- **Automatic Memory**: When you execute a Flex Query, it's automatically saved for future use
174+
- **Easy Reuse**: Previously used queries are remembered - no need to copy query IDs repeatedly
175+
- **Friendly Names**: Optionally provide a friendly name when first executing a query
176+
- **Forget Queries**: Remove queries you no longer need with the `forget_flex_query` tool
177+
178+
## Configuration Variables
179+
180+
| Feature | Environment Variable | Command Line Argument |
181+
|---------|---------------------|----------------------|
182+
| Username | `IB_USERNAME` | `--ib-username` |
183+
| Password | `IB_PASSWORD_AUTH` | `--ib-password-auth` |
184+
| Headless Mode | `IB_HEADLESS_MODE` | `--ib-headless-mode` |
185+
| Paper Trading | `IB_PAPER_TRADING` | `--ib-paper-trading` |
186+
| Auth Timeout | `IB_AUTH_TIMEOUT` | `--ib-auth-timeout` |
187+
| Flex Token | `IB_FLEX_TOKEN` | N/A |
54188

55189
## Available MCP Tools
56190

57-
| Tool | Description |
58-
|------|-------------|
191+
### Trading & Account Management
192+
193+
| Tool | Description |
194+
| ------------------ | ----------------------------------------- |
59195
| `get_account_info` | Retrieve account information and balances |
60-
| `get_positions` | Get current positions and P&L |
61-
| `get_market_data` | Real-time market data for symbols |
62-
| `place_order` | Place market, limit, or stop orders |
63-
| `get_order_status` | Check order execution status |
196+
| `get_positions` | Get current positions and P&L |
197+
| `get_market_data` | Real-time market data for symbols |
198+
| `place_order` | Place market, limit, or stop orders |
199+
| `get_order_status` | Check order execution status |
200+
| `get_live_orders` | Get all live/open orders for monitoring |
201+
202+
### Flex Queries (Requires IB_FLEX_TOKEN)
203+
204+
| Tool | Description |
205+
| ------------------- | -------------------------------------------------------------------- |
206+
| `get_flex_query` | Execute a Flex Query and retrieve statements (auto-saves for reuse) |
207+
| `list_flex_queries` | List all previously used Flex Queries |
208+
| `forget_flex_query` | Remove a saved Flex Query from memory |
64209

65210
## Troubleshooting
66211

67212
**Authentication Problems:**
213+
68214
- Use the web interface that opens automatically
69215
- Complete any required two-factor authentication
70216
- Try paper trading mode if live trading fails
71217

72-
73-
## Security & Risk Disclaimer
74-
75-
⚠️ **IMPORTANT WARNINGS:**
76-
77-
- **Financial Risk**: Trading involves substantial risk of loss. Always test with paper trading first.
78-
- **Security**: This software handles sensitive financial data. Only run locally, never on public servers.
79-
- **No Warranty**: This unofficial software comes with no warranties. Use at your own risk.
80-
- **Not Financial Advice**: This tool is for automation only, not financial advice.
81-
82218
## Support
83219

84-
- **IB Gateway Issues**: [Interactive Brokers API Documentation](https://www.interactivebrokers.com/campus/ibkr-api-page/cpapi-v1/#introduction)
85-
- **MCP Protocol**: [Model Context Protocol Documentation](https://modelcontextprotocol.io/)
86-
- **This Server**: Open an issue in this repository
220+
- **This Server**: Open an issue in this repository.
87221

88222
## License
89223

install/install-chromium-alpine.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/sh
2+
# Alpine-only: install minimal system deps + chromium, prune assets, and report sizes
3+
set -e
4+
5+
echo "🔧 Installing minimal Chromium runtime (Alpine)"
6+
7+
# --- Config ---
8+
CHROMIUM_BIN="/usr/bin/chromium-browser"
9+
CHROMIUM_DIR="/usr/lib/chromium"
10+
11+
# --- Guard: Alpine only ---
12+
if [ ! -f /etc/alpine-release ]; then
13+
echo "❌ This script targets Alpine Linux only."
14+
exit 1
15+
fi
16+
17+
# --- Install system deps + chromium ---
18+
echo "📦 apk add chromium + minimal deps"
19+
apk add --no-cache \
20+
chromium \
21+
nss \
22+
ca-certificates \
23+
freetype \
24+
harfbuzz \
25+
ttf-liberation
26+
27+
update-ca-certificates || true
28+
29+
# --- Size snapshot (pre-prune) ---
30+
echo "📏 Sizes before pruning:"
31+
if [ -x "$CHROMIUM_BIN" ]; then
32+
echo " - $(ls -lh "$CHROMIUM_BIN" | awk '{print $5" "$9}')"
33+
fi
34+
if [ -d "$CHROMIUM_DIR" ]; then
35+
du -sh "$CHROMIUM_DIR" 2>/dev/null | sed 's/^/ - /'
36+
fi
37+
38+
# --- Prune Chromium payload ---
39+
if [ -d "$CHROMIUM_DIR" ]; then
40+
echo "🧹 Pruning Chromium assets in $CHROMIUM_DIR"
41+
42+
# 1) Locales — keep only en-US if available
43+
if [ -d "$CHROMIUM_DIR/locales" ]; then
44+
if [ -f "$CHROMIUM_DIR/locales/en-US.pak" ]; then
45+
mv "$CHROMIUM_DIR/locales/en-US.pak" /tmp/keep-locale.pak
46+
rm -rf "$CHROMIUM_DIR/locales"/*
47+
mkdir -p "$CHROMIUM_DIR/locales"
48+
mv /tmp/keep-locale.pak "$CHROMIUM_DIR/locales/en-US.pak"
49+
echo " • kept locale: en-US"
50+
else
51+
echo " • locale en-US not found, skipping locale prune"
52+
fi
53+
fi
54+
55+
# 2) Remove heavy, non-essential directories for headless auth
56+
rm -rf \
57+
"$CHROMIUM_DIR/swiftshader" \
58+
"$CHROMIUM_DIR/extensions" \
59+
"$CHROMIUM_DIR/MEIPreload" 2>/dev/null || true
60+
61+
# 3) Drop hi-DPI resource packs; keep defaults
62+
if [ -d "$CHROMIUM_DIR/resources" ]; then
63+
find "$CHROMIUM_DIR/resources" -type f -name "*-200-percent.pak" -delete || true
64+
find "$CHROMIUM_DIR/resources" -type f -name "*-300-percent.pak" -delete || true
65+
fi
66+
67+
# 4) Clean any leftover debug/tests if present (defensive)
68+
find "$CHROMIUM_DIR" -type d -name "test*" -prune -exec rm -rf {} + 2>/dev/null || true
69+
fi
70+
71+
# --- Size snapshot (post-prune) ---
72+
echo "📏 Sizes after pruning:"
73+
if [ -x "$CHROMIUM_BIN" ]; then
74+
echo " - $(ls -lh "$CHROMIUM_BIN" | awk '{print $5" "$9}')"
75+
fi
76+
if [ -d "$CHROMIUM_DIR" ]; then
77+
du -sh "$CHROMIUM_DIR" 2>/dev/null | sed 's/^/ - /'
78+
fi
79+
80+
# --- Package-level info (useful when debugging layer size) ---
81+
echo "ℹ️ apk package sizes:"
82+
apk info -s chromium 2>/dev/null | sed 's/^/ - /' || true
83+
apk info -s nss 2>/dev/null | sed 's/^/ - /' || true
84+
apk info -s freetype 2>/dev/null | sed 's/^/ - /' || true
85+
apk info -s harfbuzz 2>/dev/null | sed 's/^/ - /' || true
86+
apk info -s ttf-liberation 2>/dev/null | sed 's/^/ - /' || true
87+
88+
echo "✅ Done. Chromium at: $CHROMIUM_BIN"
89+
echo " Suggested flags: --headless=new --no-sandbox --disable-dev-shm-usage"

mcp-inspector.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"mcpServers": {
3+
"interactive-brokers-mcp": {
4+
"type": "sse",
5+
"url": "http://localhost:8123/mcp"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)