Skip to content

fix: initialize gateway brokerage session with browser cookies#43

Merged
NitayRabi merged 1 commit into
code-rabi:masterfrom
infinitnet:fix/gateway-cookie-brokerage-session
May 12, 2026
Merged

fix: initialize gateway brokerage session with browser cookies#43
NitayRabi merged 1 commit into
code-rabi:masterfrom
infinitnet:fix/gateway-cookie-brokerage-session

Conversation

@infinitnet
Copy link
Copy Markdown
Contributor

This PR makes Client Portal Gateway authentication completion more reliable after browser/headless login.

It addresses a case where the Gateway page reports Client login succeeds, but REST endpoints such as /iserver/auth/status can remain authenticated: false unless the REST brokerage session is initialized using the browser-established Gateway cookies and the full brokerage-session sequence.

Changes:

  • Capture Gateway/browser cookies after headless login and pass them to IBClient.
  • Add IBClient.setSessionCookies(...) and attach the captured cookies to raw Gateway REST requests.
  • Initialize the brokerage session using a safer two-pass flow:
    • no-cookie primer pass when browser cookies exist,
    • browser-cookie pass for the final authenticated REST session.
  • Run the Gateway brokerage-session sequence with:
    • /sso/validate,
    • /iserver/auth/status to derive MAC / hardware_info,
    • form-encoded /iserver/auth/ssodh/init,
    • /iserver/reauthenticate,
    • /tickle,
    • /portfolio/accounts,
    • bounded status polling until /iserver/auth/status becomes authenticated.
  • Treat Client login succeeds as browser-login success only; do not return auth success until the REST brokerage session is actually initialized.
  • Use initializeBrokerageSession() from browser-auth polling when available, with a backwards-compatible fallback to the previous checkAuthenticationStatus() + reauthenticate() path.
  • Pass IB_PAPER_TRADING into headless auth config so the paper/live selection is preserved.
  • Add a POST-to-GET fallback for /tickle session maintenance.

Why

In some Gateway sessions, successful browser login does not immediately make the Client Portal REST session usable. The UI can show Client login succeeds while REST still returns authenticated: false.

In that state, simply calling /iserver/reauthenticate or checking /iserver/auth/status is not always sufficient. The working sequence needs the Gateway/browser cookie context and the brokerage-session initialization endpoints to be exercised before the MCP server declares authentication complete.

This prevents false-positive authentication success where downstream account/positions/orders tools still fail because the REST brokerage session was not fully established.

Copy link
Copy Markdown
Contributor

@NitayRabi NitayRabi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! This improves the reliability of the REST session initialization significantly. Merging now.

@NitayRabi NitayRabi merged commit 959de17 into code-rabi:master May 12, 2026
2 checks passed
@NitayRabi
Copy link
Copy Markdown
Contributor

Thanks for the contribution! This improves the reliability of the REST session initialization significantly. Merging now.

@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 1.21.9 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants