Interactive Brokers MCP Server

Interactive Brokers MCP Server

Enables interaction with Interactive Brokers via the TWS API or IB Gateway to manage accounts, retrieve market data, and execute trades. It supports features like real-time market data, historical data queries, and order management for stocks and options.

Category
访问服务器

README

Interactive Brokers MCP Server for Windows

A Model Context Protocol (MCP) server that provides tools for interacting with Interactive Brokers (IBKR) through the TWS API or IB Gateway.

Features

  • Account Management: Get account summary, positions, and portfolio information
  • Market Data: Real-time and historical market data for stocks
  • Order Management: Place market and limit orders, cancel orders, view open orders
  • Trade Execution: View recent trade executions
  • Options Trading: Get option chains for stocks

Prerequisites

  1. Interactive Brokers Account: You need an active IBKR account

  2. TWS or IB Gateway: Install either:

    • Trader Workstation (TWS) - Full trading platform
    • IB Gateway - Lightweight API gateway

    Download from: https://www.interactivebrokers.com/en/trading/tws.php

  3. Python 3.8+: Required for running the MCP server

  4. Enable API Access: In TWS/Gateway settings:

    • File → Global Configuration → API → Settings
    • Enable "Enable ActiveX and Socket Clients"
    • Note the Socket port (7497 for paper trading, 7496 for live)
    • Add 127.0.0.1 to trusted IPs

Installation

Step 1: Install Python Dependencies

Open Command Prompt or PowerShell and run:

pip install -r requirements.txt

Or install packages individually:

pip install mcp ib_insync pydantic

Step 2: Configure TWS/IB Gateway

  1. Start TWS or IB Gateway
  2. Log in with your credentials
  3. Go to: File → Global Configuration → API → Settings
  4. Check "Enable ActiveX and Socket Clients"
  5. Uncheck "Read-Only API" if you want to place orders
  6. Note the Socket Port:
    • 7497 for Paper Trading
    • 7496 for Live Trading
  7. Add 127.0.0.1 to Trusted IPs
  8. Click OK and restart TWS/Gateway

Usage

Starting the MCP Server

python server.py

The server will run and wait for MCP client connections via stdio.

Integrating with Claude Desktop

Add this configuration to your Claude Desktop config file:

Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "ibkr": {
      "command": "python",
      "args": ["C:\\path\\to\\ibkr-mcp-server\\server.py"]
    }
  }
}

Replace C:\\path\\to\\ibkr-mcp-server\\ with the actual path where you saved this project.

Available Tools

1. connect_ibkr

Connect to Interactive Brokers TWS or Gateway.

{
  "host": "127.0.0.1",
  "port": 7497,
  "client_id": 1
}
  • port: 7497 (paper trading) or 7496 (live trading)
  • Must be called before using other tools

2. disconnect_ibkr

Disconnect from IBKR.

3. get_account_summary

Get account summary including cash, equity, and P&L.

Returns:

{
  "TotalCashValue": "50000.00 USD",
  "NetLiquidation": "52000.00 USD",
  "BuyingPower": "200000.00 USD",
  "RealizedPnL": "500.00 USD",
  "UnrealizedPnL": "2000.00 USD"
}

4. get_positions

Get all current positions.

Returns array of positions with symbol, quantity, avg cost, market value, and P&L.

5. get_market_data

Get real-time market data for a symbol.

{
  "symbol": "AAPL",
  "exchange": "SMART",
  "currency": "USD"
}

Returns bid, ask, last, volume, high, low prices.

6. get_historical_data

Get historical market data.

{
  "symbol": "AAPL",
  "duration": "1 D",
  "bar_size": "1 hour",
  "what_to_show": "TRADES"
}

Duration options: "1 D", "1 W", "1 M", "1 Y" Bar size options: "1 min", "5 mins", "1 hour", "1 day"

7. place_order

Place a trading order.

Market Order:

{
  "symbol": "AAPL",
  "action": "BUY",
  "quantity": 100,
  "order_type": "MKT"
}

Limit Order:

{
  "symbol": "AAPL",
  "action": "SELL",
  "quantity": 100,
  "order_type": "LMT",
  "limit_price": 150.50
}

8. cancel_order

Cancel an existing order.

{
  "order_id": 123
}

9. get_open_orders

Get all open orders.

10. get_executions

Get recent trade executions.

{
  "days": 1
}

11. get_option_chain

Get option chain for a symbol.

{
  "symbol": "AAPL",
  "exchange": "SMART"
}

Example Conversations with Claude

Once configured, you can ask Claude:

  • "Connect to my IBKR paper trading account"
  • "What's my current account balance?"
  • "Show me all my positions"
  • "Get the current price of AAPL"
  • "Show me historical data for TSLA over the last week"
  • "Place a market order to buy 10 shares of MSFT"
  • "What are my open orders?"
  • "Get the option chain for SPY"

Security Notes

⚠️ Important Security Considerations:

  1. Paper Trading First: Always test with paper trading (port 7497) before using live account
  2. Order Confirmation: The server allows order placement - use with caution
  3. API Access: Only enable API access when actively using it
  4. Firewall: Ensure your firewall allows connections to TWS/Gateway
  5. Credentials: Never share your IBKR credentials or API keys

Troubleshooting

Connection Failed

  • Ensure TWS or IB Gateway is running
  • Verify API settings are enabled
  • Check port number (7497 for paper, 7496 for live)
  • Confirm 127.0.0.1 is in trusted IPs

No Market Data

  • Your IBKR account needs market data subscriptions
  • Real-time data requires active subscriptions
  • Delayed/snapshot data may be available without subscription

Order Placement Fails

  • Ensure "Read-Only API" is unchecked in TWS settings
  • Verify you have sufficient buying power
  • Check that the market is open for the security

Module Not Found

pip install --upgrade mcp ib_insync pydantic

Development

Running Tests

python -m pytest tests/

Logging

Logs are written to console. Adjust logging level in server.py:

logging.basicConfig(level=logging.DEBUG)

License

MIT License - See LICENSE file for details

Disclaimer

This software is for educational purposes. Trading involves risk. Use at your own discretion. The authors are not responsible for any financial losses.

Resources

Support

For issues related to:

  • IBKR API: Contact Interactive Brokers support
  • MCP Server: Open an issue on GitHub
  • ib_insync: Check the ib_insync documentation

Changelog

Version 1.0.0

  • Initial release
  • Support for basic account operations
  • Market data retrieval
  • Order management
  • Historical data queries
  • Option chain retrieval

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选