Labsco
code-rabi logo

IBKR MCP

β˜… 189

from code-rabi

Unofficial local MCP server for Interactive Brokers market data, account positions, and trading workflows. Use paper trading and review permissions before connecting live accounts.

πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedAccount requiredNeeds API keys

Interactive Brokers MCP Server

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.

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.

πŸ”’ Security Notice

Features

  • Interactive Brokers API Integration: Full trading capabilities including account management, position tracking, real-time market data, and order management (market, limit, and stop orders)

  • Flex Query Support: Execute Flex Queries to retrieve account statements, trade confirmations, and historical data. Queries are automatically remembered for easy reuse

  • Flexible Authentication: Choose between browser-based OAuth authentication or headless mode with credentials for automated environments

  • Simple Setup: Run directly with npx - no Docker or additional installations required. Includes pre-configured IB Gateway and Java runtime for all platforms

Security Notice

IMPORTANT WARNINGS:

  • Financial Risk: Trading involves substantial risk of loss. Always test with paper trading first.

  • Security: This software handles sensitive financial data. Only run locally, never on public servers.

  • No Warranty: This unofficial software comes with no warranties. Use at your own risk.

  • Not Financial Advice: This tool is for automation only, not financial advice.

Gateway Lifecycle

On startup, the MCP first probes reachable local Gateway endpoints on the configured port and common Client Portal Gateway ports. If a healthy existing Gateway is found, the MCP attaches to it and does not start another bundled Gateway.

When no suitable existing Gateway is reachable, the MCP starts the bundled Java Gateway as a durable detached process. Runtime coordination files are stored under ib-gateway/.runtime/:

  • gateway-session.json records the MCP-managed Gateway pid, port, version, and log paths.

  • gateway-session.lock prevents two MCP processes from starting duplicate managed Gateways at the same time.

  • gateway.stdout.log and gateway.stderr.log receive the Gateway process output.

Normal MCP shutdown detaches from the Gateway and leaves it running so later MCP runs can reuse it. If IB_FORCE_STANDALONE_GATEWAY=true is set, the MCP skips unrelated external Gateway discovery, but it still reuses or coordinates through the durable MCP-managed session metadata and lock files.

To reset the managed Gateway session, stop the Gateway process recorded in ib-gateway/.runtime/gateway-session.json, then remove ib-gateway/.runtime/gateway-session.json and any stale ib-gateway/.runtime/gateway-session.lock. The MCP automatically removes stale metadata when the recorded pid no longer exists.

Available MCP Tools

Trading & Account Management

Tool Description get_account_info Retrieve account information and balances get_positions Get current positions and P&L get_market_data Real-time market data for symbols place_order Place market, limit, or stop orders (only if read-only mode is disabled) get_order_status Check order execution status get_live_orders Get all live/open orders for monitoring

Flex Queries (Requires IB_FLEX_TOKEN)

Tool Description get_flex_query Execute a Flex Query and retrieve statements (auto-saves for reuse) list_flex_queries List all previously used Flex Queries forget_flex_query Remove a saved Flex Query from memory

Support

  • This Server: Open an issue in this repository.

License

MIT License - see LICENSE file for details.

Thanks to our contributors

A big thank you to everyone who has contributed to making this project better.