by rcontesti
This project provides an Interactive Brokers (IB) API interface using the Model Context Protocol (MCP).
# Add to your Claude Code skills
git clone https://github.com/rcontesti/IB_MCP
[!Note]
Project Status
This project is currently under active development. Features may be incomplete, and breaking changes may occur.
This project is built exclusively on the Interactive Brokers Web API, and this is a deliberate design choice. While the TWS API is powerful, the Web API provides a more modern and flexible foundation for the goals of this project.
Why not using the TWS API? The short answer is that the WEB API is planned to be more comprehensive. By more comprehensive I mean, it brings account management, specifically reporting, into the same place. The two other important reasons are: 1. The Web API is standalone. It does not require you to run the TWS desktop software or IB Gateway. 2. Model context protocols (MCPs) are easier to build on top of HTTPS communication rather than TCP/IP sockets.
If it is more comprehensive why not using just the WEB API? Three reasons: 1. WEB API is in beta (see Limitations) 2. TWS is faster and more reliable for trading. 3. TWS has some trading functionalities that the WEB API does not have.
While other projects act as a "bridge" to make the TWS API look like a web API, they introduce an unofficial, third-party dependency. This project avoids that risk by using the official IBKR Web API directly, ensuring better long-term stability and support.
In summary:
For a more detailed, side-by-side breakdown, please see the TWS vs. Web API Comparison section.
This project provides an Interactive Brokers (IB) API interface using the Model Context Protocol (MCP). There are several ways to interact with Interactive Brokers, like the TWS API, the WEB API, Excel RTD and FIX among others. This project is built on top of Interactive Brokers WEB API.
This development uses the retail authentication process which is managed using the Client Portal Gateway, a small Java program used to route local web requests with appropriate authentication.
See a quick walkthrough in YOUTUBE
Clone the repo, set env variables and build the images
# Clone the repository
git clone https://github.com/rcontesti/IB_MCP.git
# Navigate to the project directory
cd IB_MCP
# Copy the .env.example file to .env and edit as needed
cp .env.example .env
# Build the image
docker compose up --build -d
Auth with your IB account and credentials to:
After the image is up and running, navigate to https://{GATEWAY_BASE_URL}:{GATEWAY_PORT} (e.g.: https://localhost:5055/) to login.
You will also find the login path in the logs of the API gateway container:
If successful you should be redirected to a URL that reads: "Client login succeeds".
Add the MCP server config file to your VS Code settings.json.
Given the following environment parameters
MCP_SERVER_HOST=0.0.0.0
MCP_SERVER_PORT=5002
MCP_SERVER_PATH=/mcp
MCP_TRANSPORT_PROTOCOL=streamable-http
the VS Code MCP server snippet in settings.json would look like:
{
...
},
"chat.mcp.discovery.enabled": true,
"mcp": {
"inputs": [],
"servers": {
"time": {
"command": "docker",
"args": ["run", "-i", "--rm", "mcp/time"]
},
"ib-web": {
"type": "http",
"url": "http://localhost:5002/mcp/",
}
}
},
"workbench.colorTheme": "Tomorrow Night Blue"
}
Alternatively, you can create a .vscode/mcp.json file at the root of your project with the following content:
{
"servers": {
"ib-mcp-server": {
"type": "http",
"url": "http://localhost:5002/mcp/"
}
},
"inputs": []
}
Check Use MCP servers in VS Code (Preview) for further reference.
Start the MCP in Copilot
The additional /iserver/auth/ssodh/init endpoint is used to subsequently reopen a brokerage session with the backend, through which you can access the protected /iserver endpoints.
Sessions will time out after approximately 6 minutes without sending new requests or maintaining the /tickle endpoint at least every 5 minutes.
In order to prevent the session from timing out, the endpoint /tickle should be called on a regular basis. It is recommended to call this endpoint approximately every minute.
If the brokerage session has timed out but the session is still connected to the IBKR backend, the response to /auth/status returns ‘connected’:true and ‘authenticated’:false. Calling the /iserver/auth/ssodh/init endpoint will initialize a new brokerage session.
Automatically generate endpoints
Currently the IB REST API (2.16.0) OpenAPI specification fails validation, and the automated router generation feature is currently failing to generate routers. You can try to validate yourself here:
https://oas-validation.com/
https://editor.swagger.io/
The spec currently has 351 errors. Therefore, router endpoints are currently being built manually, and their status is updated upon completion.
Add OAuth
Endpoints are currently manually built.
👉 See the full list of API Endpoints Staus
| Feature | TWS API | Web API (Client Portal API) | | :------------------- | :------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------- | | Technology | TCP/IP Socket, Proprietary binary protocol | RESTful, HTTPS, JSON format | | Dependency | Requires TWS or IB Gateway to be running | Standalone (does not require TWS/Gateway) | | Ease of Use | Steep learning curve, complex | Much easier, standard for web developers | | Performance | Very high speed, low latency. Ideal for high-frequency data and fast order execution. | Slower due to HTTPS overhead. Not for HFT. | | Functionality | Extremely comprehensive. Access to virtually every feature in TWS, including complex order types, combos, alg
No comments yet. Be the first to share your thoughts!