Skip to main content
Glama
simoneb

ibkr-risk-mcp

by simoneb

Server Configuration

Describes the environment variables required to run the server.

NameRequiredDescriptionDefault
IBKR_HOSTNoWhere TWS listens. A remote host must also be in the API's Trusted IPs127.0.0.1
IBKR_PORTNo7496/7497 TWS live/paper, 4001/4002 Gateway live/paper7496
IBKR_ACCOUNTNo**Required** on a multi-account login. Without it every position/account tool refuses rather than combining accounts
IBKR_CLIENT_IDNoMust differ from every other script on this TWS. **Never 0** — TWS reserves that for orders placed by hand17
IBKR_ENABLE_WHATIFNoThe gate on `whatif_order`. Also decides whether the connection itself is opened read-onlyfalse
IBKR_GREEKS_TIMEOUTNoSeconds to wait for greeks on one contract. Short by design — IB answers fast or never, and explicit refusals cut the wait short anyway4
IBKR_RISK_FREE_RATENoUsed to discount and to carry spot to the forward in the local repricing0.04
IBKR_WHATIF_TIMEOUTNoSeconds to wait for a what-if reply. IB sometimes never sends one5
IBKR_CONNECT_TIMEOUTNoSeconds for the API handshake6
IBKR_MARKET_DATA_TYPENo1 live, 2 frozen, 3 delayed, 4 delayed-frozen. 3 works and does carry greeks1
IBKR_MAX_MKT_DATA_LINESNoConcurrent market data subscriptions. IB allows about 5040

Instructions

Guidance the server publishes about itself, which clients place ahead of the tool catalog so the model reads it before choosing anything.

This server publishes no instructions, or was last inspected before Glama recorded them.

Capabilities

Features and capabilities supported by this server

Protocol revision2025-11-25

CapabilityDetails
tools
{
  "listChanged": false
}
prompts
{
  "listChanged": false
}
resources
{
  "subscribe": false,
  "listChanged": false
}
experimental
{}

Tools

Functions exposed to the LLM to take actions

NameDescription
check_connectionA

Check whether TWS or IB Gateway is reachable and an account is logged in.

Call this first whenever another tool fails. It separates the four situations that all present as "cannot connect" and need different fixes:

  • not_listening — nothing is on the port. TWS is not running, or IBKR_PORT points at the wrong one; the response lists which of the four default ports (7496 TWS live, 7497 TWS paper, 4001/4002 Gateway) are answering.

  • api_not_enabled — the port answers but the API handshake never completes. "Enable ActiveX and Socket Clients" is off in TWS.

  • client_id_in_use — another script holds this client id.

  • not_logged_in — the API is up but no account is loaded.

  • connected — everything is in place.

hint says what to do about the state in each case.

get_margin_summaryA

Margin and liquidity, split by segment.

Returns NetLiquidation, EquityWithLoanValue, FullInitMarginReq, FullMaintMarginReq, AvailableFunds, ExcessLiquidity, TotalCashValue, BuyingPower and Leverage — each as the account total and, where IB reports it, separately for the securities (-S) and commodities (-C) segments.

The segments are the point. Futures margin must be met in the commodities segment; IB covers a shortfall there by sweeping cash out of the securities segment. An account whose total excess liquidity looks healthy can still be one bad day away from a forced liquidation if the shortfall lands in commodities while securities is also falling. Report the segment figures, not only the totals, whenever futures are involved.

get_position_greeksA

IB's model greeks for every option position — its numbers, not ones implied locally.

Returns per position: conid, symbol, secType, right, strike, lastTradeDate, settlementDate, daysToExpiry, position, multiplier, undPrice, impliedVol, delta, gamma, vega, theta, optPrice and pvDividend.

Things to know about the values:

  • undPrice is the forward IB used, not the index spot. For a futures option that is the future's price, which differs from the cash index by the basis; any repricing has to start from it.

  • multiplier comes from the contract. ES is 50 and MES is 5, and the difference is a factor of ten in every exposure figure.

  • settlementDate, not lastTradeDate, is the expiry. The quarterly ES options settle AM and stop trading the afternoon before, so TWS shows 17 December for something that expires on the 18th, while a weekly settling the same morning shows the 18th. Both are returned; pair positions on settlementDate.

  • Missing greeks are listed, not dropped. A contract IB never published model greeks for appears under missing with the reason. The usual causes are no market data subscription for that instrument, or a strike too illiquid for IB to imply a volatility. Note that delayed data does carry model greeks, so a missing row is rarely explained by the market data type alone.

get_vol_surfaceA

IB's implied volatility surface for an underlying: a grid of (expiry, strike) with impliedVol, delta, optPrice, undPrice and daysToExpiry.

This is the input that makes local repricing deterministic. With IB's own volatilities in hand, a constant-volatility scenario needs no proprietary model — only Black-76 arithmetic on top of numbers IB published.

Do not collapse the surface to one number. Volatility has a term structure: ES at 139 days can sit near 15% at the money while the front month prints 12%. Using the front month for a longer tenor understates a long-dated position badly. Read the tenor you need, and interpolate between tenors in total variance if you must.

daysToExpiry counts to the settlement date. For AM-settled expiries that is one day past the last trading date TWS shows.

Strikes with no published volatility come back under missing with the reason rather than being silently absent — a surface missing its left wing looks identical to one that has none.

Check which instrument you got. A bare root is ambiguous: ES is the E-mini S&P 500 future and Eversource Energy on NYSE, and with no sec_type the stock wins. The resolved contract comes back under underlying, any collision is listed in warnings, and a surface on the wrong instrument looks perfectly reasonable until you notice the strikes are two orders of magnitude off.

stress_portfolioA

Reprice the whole portfolio — options, equities, futures — across underlying shocks and return the P&L curve and its trough.

The trough is the primary output: the worst point of the curve and the shock at which it sits. troughRefined interpolates between grid points for where the minimum actually falls, and is labelled as interpolated.

The model, returned with every result in assumptions:

  • all underlyings are shocked by the same percentage at once, which is Risk Navigator's own default assumption. Equity positions can be scaled with betas; options and futures move one-for-one with their underlying.

  • options are repriced with Black-76 on the shocked forward using IB's implied volatility. Equity options are carried from spot using IB's pvDividend, so both kinds go through one pricer.

  • P&L is model-price-now against model-price-shocked, so the curve is exactly zero at zero shock by construction. The gap between the local model and IB's own price is reported per position as modelVsMarket instead of being folded into the curve. Expect it to be small out of the money and a couple of percent in the money, where IB prices the early exercise that Black-76 has no room for — so the curve understates losses slightly once options go deep in the money.

  • bonds are held flat unless bond_rate_shift_bp is set; anything this server does not model is held flat and named in warnings.

Check reconciled before quoting anything. At zero shock the portfolio is rebuilt from its positions and compared against NetLiquidation; a residual over 1% returns reconciled: false with the residual attached. A curve that does not reconcile is missing something, and the number it gives for the trough is missing it too.

Risk Navigator's own volatility shock model is not published. sticky_strike is the approximation that corresponds to its default curve, not a reproduction of it — expect the shape to match and the last few percent not to.

stress_whatifA

The same stress run, with hypothetical legs added — three curves and three troughs: the portfolio as it stands, the portfolio plus the legs, and the difference.

This is what replaces reading Risk Navigator's What-If by hand: "if I add N puts at strike K expiring E, where does the trough move to?"

Read all three troughs. The trough of the difference is not the difference of the troughs — adding protection moves where the worst point sits as well as how deep it is, and comparing only the depths hides the move. A structure that lifts the bottom by very little may still have pushed it from −8% out to −15%, which is the part that matters.

Hypothetical options are priced off IB's current model greeks for those exact contracts, so both curves start from the same volatilities and the difference is the structure alone. A leg that cannot be resolved or priced is reported in legProblems and left out of the second curve; the comparison then covers only the legs that did resolve, and says so.

Nothing is sent to IB's order path here — this is pure local repricing. For what the structure costs in margin, use whatif_order.

whatif_orderA

IB's own margin impact for a hypothetical structure. Nothing reaches the market.

Each order carries whatIf=True, which IB evaluates in its margin engine and discards: it is never routed, never acknowledged as live, never appears in the order book. The tool is still gated behind IBKR_ENABLE_WHATIF=true because it is the only thing in this server that touches the order path at all; with the gate closed it sends nothing and returns blocked: true.

Returns initMargin, maintMargin and equityWithLoan before/after/change, plus commission and any warningText, in two views:

  • perLeg — each leg evaluated on its own.

  • cumulative — legs 1..k as a combo, for every k, so you can see where the offset appears.

Read the combined figure, not the sum of the legs. SPAN offsets the legs against each other and against what the account already holds, so the two differ — the difference is reported as offset.spanOffset. IB's what-if on arbitrary multi-leg combos is unreliable and will sometimes return nothing; that is reported per step and does not mean the structure is invalid.

If every call fails with no margin figures, check whether TWS has "Read-Only API" enabled — that setting blocks what-if orders too.

Prompts

Interactive templates invoked by user choice

NameDescription

No prompts

Resources

Contextual data attached and managed by the client

NameDescription

No resources

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/simoneb/ibkr-risk-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server