Skip to main content

Interactive Brokers MCP Server for account management, trading operations, and market data

Project description

IBKR MCP Server

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.

Features

  • ๐Ÿ”— 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

Installation

Install from Source

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 .

Development Setup

# Install development dependencies
pip install -e ".[dev]"

# Setup pre-commit hooks
pre-commit install

Quick Start

1. Configure Environment

Create 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=4002
IBKR__CLIENT_ID=1
IBKR__READONLY=false

# Logging Settings
LOGGING__LEVEL=INFO

2. Test Connection

# Test IBKR connection
python -m ibkr_mcp_server.cli test --host 127.0.0.1 --port 4002

3. Start Server

# 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

Configuration

Environment Variables

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 4002 IBKR TWS/Gateway port
IBKR__CLIENT_ID 1 IBKR client ID
IBKR__READONLY false Read-only mode
LOGGING__LEVEL INFO Logging level

IBKR Port Configuration

Platform Demo Port Live Port
TWS 4002 7496
Gateway 4002 4001

MCP Tools

The server provides 9 MCP tools:

Account Management

  • get_account_summary: Get account summary information
  • get_positions: Get position information

Trading Operations

  • place_order: Place an order
  • cancel_order: Cancel an order
  • get_open_orders: Get open orders

Market Data

  • get_market_data: Get real-time market data
  • get_historical_data: Get historical data

Connection Management

  • connection_status: Check connection status
  • reconnect: Reconnect to IBKR

Usage Examples

Place Order

{
    "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
        }
    }
}

Get Positions

{
    "tool": "get_positions",
    "arguments": {}
}

Get Historical Data

{
    "tool": "get_historical_data",
    "arguments": {
        "contract": {
            "symbol": "AAPL",
            "sec_type": "STK",
            "exchange": "SMART",
            "currency": "USD"
        },
        "duration": "1 D",
        "bar_size": "1 min"
    }
}

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   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)     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Development

Project Structure

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

Code Standards

  • Use black for code formatting
  • Use isort for import sorting
  • Use flake8 for code linting
  • Use mypy for type checking

Testing

# Run tests
pytest

# Generate coverage report
pytest --cov=src --cov-report=html

Deployment

Docker Deployment

# Build image
docker build -t ibkr-mcp-server .

# Run container
docker run -p 8080:8080 --env-file .env ibkr-mcp-server

Docker Compose

# Start services
docker-compose up -d

# View logs
docker-compose logs -f

Integration with Claude Desktop

Add 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": "4002",
        "IBKR__CLIENT_ID": "1"
      }
    }
  }
}

Important Notes

  1. TWS/Gateway: Ensure IBKR TWS or Gateway is running with API connection enabled
  2. Port Configuration: Make sure TWS/Gateway API port matches your configuration
  3. Permissions: Ensure your account has appropriate trading permissions
  4. Risk Management: Please implement proper risk controls in production environments
  5. Market Data: Some market data may require subscriptions

Troubleshooting

Common Issues

  1. Connection Failed: Check if TWS/Gateway is running and API is enabled
  2. Client ID Conflict: Use different client IDs for multiple connections
  3. Port Issues: Verify the correct port for your TWS/Gateway setup
  4. Market Data Errors: Ensure you have proper market data subscriptions

Logging

Enable debug logging for troubleshooting:

LOGGING__LEVEL=DEBUG python -m ibkr_mcp_server.cli serve

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues, please file an Issue.


ไธญๆ–‡ๆ–‡ๆกฃ: README_zh_CN.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iflow_mcp_gaochx_ibkr_mcp_server-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file iflow_mcp_gaochx_ibkr_mcp_server-0.1.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_gaochx_ibkr_mcp_server-0.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_gaochx_ibkr_mcp_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9ef6b6742a4e699f2ed8780b4ceb35720332c001dd7dcba110b484da1ee47ccc
MD5 45b2acab2176628c005c74682b3f936e
BLAKE2b-256 642fc8243c45429c6f7e24b618b5e4d27c870b924beab5a76c64f747eab7c22a

See more details on using hashes here.

File details

Details for the file iflow_mcp_gaochx_ibkr_mcp_server-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_gaochx_ibkr_mcp_server-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_gaochx_ibkr_mcp_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b9bbd4eb5d67ca36630ed1528603990258055a57568dea35c04c05b311012c3
MD5 0b1aa77d5586fce2dcdea66b5ca2dee9
BLAKE2b-256 e630bdb93f5be461788cc3851fd0f5492c27c07edbf7ce34bc36c5aeff86baa8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page