This project provides an Interactive Brokers (IB) API interface using the Model Context Protocol (MCP). It allows you to access real-time and historical market data from IB through a FastAPI-based service, designed to be used with MCP clients like Claude Desktop.
The project consists of two main components:
- ib_client_service: A FastAPI application that connects to the IB API (TWS or IB Gateway) using the
ib_asynclibrary. It exposes REST endpoints for retrieving market data. This service runs in a Docker container. - mcp_server: A FastMCP server that acts as an MCP interface to the
ib_client_service. It exposes tools that can be called by MCP clients, which in turn call the REST endpoints of theib_client_service. This service also runs in a Docker container.
-
Create .env file: Create a file named
.envin the project root directory (/Users/rubencontesti/Code/IB_MCP/) and populate it with your IB API connection details. You can create this file by copying the content of.env.exampleand replacing the placeholder values with your actual IB connection information. The following variables are required:IB_HOST=127.0.0.1 IB_PORT=7497 # Or your TWS/Gateway API port IB_CLIENT_ID=1 # Must be unique -
Install Docker and Docker Compose: Make sure you have Docker and Docker Compose installed on your system.
-
Build and Run: In your terminal, navigate to the project root directory (
/Users/rubencontesti/Code/IB_MCP/) and run:docker-compose up --build
This command will build the Docker images for both services and start the containers.
Once the services are running and connected to IB (check the logs for connection success), you can test the following endpoints:
curl http://localhost:5001/health# Stock Tick Data (Snapshot, Delayed) - Replace conId with a valid Bond conId
curl "http://localhost:5001/data/ticks/STK?conId=265598&exchange=SMART¤cy=USD&marketDataType=3"
# Bond Tick Data (Snapshot, Delayed) - Replace conId with a valid Bond conId
curl "http://localhost:5001/data/ticks/BOND?conId=443719449&exchange=SMART¤cy=USD&marketDataType=3"# Stock Historical Data
curl "http://localhost:5001/data/historical/STK?conId=265598&exchange=SMART¤cy=USD&durationStr=1%20M&barSizeSetting=1%20day&whatToShow=TRADES"
# Bond Historical Data - Replace conId with a valid Bond conId
curl "http://localhost:5001/data/historical/BOND?conId=443719449&exchange=SMART¤cy=USD&durationStr=1%20M&barSizeSetting=1%20day&whatToShow=MIDPOINT"- Implement MCP tools in the
mcp_serverto expose theib_client_serviceendpoints. - Add endpoints for order placement and management.
- Add endpoints for position retrieval.
- Implement streaming tick data via Server-Sent Events (SSE).