Skip to main content

How to Connect OpenAI Codex to NinjaTrader 8 with CrossTrade MCP

The OpenAI Codex CLI is OpenAI's terminal coding agent. It runs GPT models with native tool calling and supports remote HTTP MCP servers via a TOML config. Important: remote MCP support requires Codex CLI 0.134.0 (May 26, 2026) or newer. Older versions are stdio-only.

OAuth scope: mcp:tradeSingle-vendor host

Prerequisites

RequirementDetail
CrossTrade subscriptionElite
CrossTrade Add-Onv1.13.0 or higher
NinjaTrader 8Running, broker connected
AccountSim101 for first runs
OAuth scopemcp:trade
Install commandcurl -fsSL https://chatgpt.com/codex/install.sh | sh
Config file~/.codex/config.toml
CrossTrade MCP server
https://app.crosstrade.io/v1/api/mcp
Transport: streamable-http. Auth: OAuth 2.1 + PKCE. Request scope mcp:trade. Read plus write. Place/cancel orders, deploy strategies, compile NinjaScript.

Why Codex for NinjaScript

Codex is to GPT what Claude Code is to Claude: a terminal coding agent that edits local files and calls tools in the same session. If you prefer GPT models (strict tool-call adherence, fast iteration on errors), Codex is the right harness.

Step 1: Install Codex (need 0.134.0 or newer for remote MCP)

# macOS / Linux (recommended)
curl -fsSL https://chatgpt.com/codex/install.sh | sh

# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

# Or via npm (requires Node.js 22+)
npm install -g @openai/codex

# Or Homebrew
brew install --cask codex

Verify: codex --version (must be 0.134.0+ for remote HTTP MCP).

Version cliff

Remote Streamable HTTP MCP support landed in Codex CLI 0.134.0 (May 26, 2026). Earlier versions can only register stdio MCP servers and will silently ignore url = "..." in config.toml. Upgrade before configuring.

Step 2: Sign in to OpenAI

codex login

Codex uses your ChatGPT Plus/Pro/Business/Edu/Enterprise subscription, OR an OpenAI API key (export OPENAI_API_KEY=...). The ChatGPT free tier is not supported via the sign-in flow; the API-key path is the fallback.

Step 3: Register CrossTrade via the CLI

The easiest way:

codex mcp add crosstrade --url https://app.crosstrade.io/v1/api/mcp

For bearer-token auth (CrossTrade's OAuth issues a token you can paste):

codex mcp add crosstrade \
--url https://app.crosstrade.io/v1/api/mcp \
--bearer-token-env-var CROSSTRADE_TOKEN

Then export CROSSTRADE_TOKEN=... in your shell rc.

Step 3 (alternative): Edit ~/.codex/config.toml directly

[mcp_servers.crosstrade]
url = "https://app.crosstrade.io/v1/api/mcp"
bearer_token_env_var = "CROSSTRADE_TOKEN"
startup_timeout_sec = 10.0
tool_timeout_sec = 60.0
No transport field

Codex infers transport from which field is set: url → Streamable HTTP, command → stdio. There is no transport = "http" key — adding one is ignored.

Step 4: OAuth (for servers that prefer browser flow)

If CrossTrade's MCP server advertises OAuth metadata, you can do:

codex mcp login crosstrade --scopes mcp:trade

This opens a browser, completes OAuth on a local callback port, and stores the token automatically. If your IdP needs a fixed redirect URI, pin the port via mcp_oauth_callback_port in config.toml.

OAuth login only works for Streamable HTTP servers, not stdio.

Step 5: Verify

codex mcp list
codex

In the Codex prompt:

List the CrossTrade MCP tools. Then call GetMcpCapabilities and McpSelfTest.
Report add-on version, NT8 version, and which accounts are connected.

Step 6: NinjaScript compile loop

The pattern from Claude Code transfers:

  1. GetNinjaScriptHelp on every type and method you plan to use.
  2. Draft into a file in the workspace.
  3. CompileNinjaScript(in_memory: true).
  4. On failure, LookupNinjaScriptSymbol and repair.
  5. WriteNinjaScriptFile after confirmation.
  6. RunStrategyBacktest on Sim101 with realistic commission and slippage.

See Vibe Code a NinjaScript Strategy for the full sequence and prompts.

Managing the MCP connection

CommandWhat it does
codex mcp listList configured servers
codex mcp get crosstradeShow one server's config
codex mcp login crosstradeRun OAuth flow
codex mcp logout crosstradeRevoke token
codex mcp remove crosstradeDelete from config

Codex-specific tips

  • GPT-5 reasoning produces tighter NinjaScript than non-reasoning variants, at higher latency. Use for hard problems, swap to GPT-4.1 for routine iterations.
  • Approval mode is per-server (default_tools_approval_mode = "prompt" in the TOML table) so every write call waits for confirmation — strongly recommended for mcp:trade.

FAQ

Codex vs Claude Code: which is better for NinjaScript?

Both work. Claude generally writes cleaner C# on the first attempt; GPT generally fixes errors faster on the second. Try both on the same spec and decide.

Do I need ChatGPT Plus or an API key?

Either works. ChatGPT Plus / Pro / Business / Edu / Enterprise plans work via codex login. Or set OPENAI_API_KEY for the API path.

Can I pair Codex with non-OpenAI models?

Codex is GPT-locked by design. If you want a multi-model harness, use OpenCode, Cline, or Continue and route OpenAI through them.

OAuth handshake

The first tool call surfaces an authorization URL. Open it in a browser, approve mcp:trade, and return to the harness. The access token is stored by the harness (keychain, config file, or memory depending on the client).

See CrossTrade MCP OAuth for the full flow, and 403/408 troubleshooting if the handshake fails.

Verify the connection

Smoke-test before doing anything stateful:

Call GetMcpCapabilities and McpSelfTest. Then ListAccounts and GetConnections.
Report add-on version, NT8 version, and which accounts are linked.

If you authorized mcp:trade, also confirm Sim101 is the default account before any order placement.

Funded-account safety
The mcp:trade scope grants order placement. The scope does not protect a funded account; your prompt and account selection do. Default to Sim101 during setup. For funded accounts, verify the firm's automation policy first.

Troubleshooting

SymptomLikely causeFix
403 insufficient_scopeToken authorized at mcp:read but tool requires mcp:tradeReauthorize the connection and select mcp:trade
OAuth callback failsSystem browser blocked the redirect or popupCopy the auth URL manually, complete it, paste the code back
Tools list is empty after connectServer registered but session did not refreshRestart the harness or trigger a tool list refresh
Add-on offline errorNT8 not running, or add-on not loadedConfirm NT8 is open and the CrossTrade add-on (v1.13.0+) is installed
url = "..." in config.toml does nothingCodex CLI below 0.134.0 supports stdio MCP onlyUpgrade with npm install -g @openai/codex@latest (verify codex --version >= 0.134.0)
Set transport = "http" but Codex ignores itThere is no transport field; Codex infers transport from url vs commandRemove transport; presence of url selects Streamable HTTP automatically
Project-scope .codex/config.toml MCP servers ignoredProject not yet trustedRun codex in the project once and accept the trust prompt; project-scoped MCP servers load only in trusted projects
Authorization header contains literal env var namebearer_token_env_var takes a name (no leading $), not the token itselfSet bearer_token_env_var = "CROSSTRADE_TOKEN" and export the env var in your shell rc
codex mcp login fails with "OAuth not supported"Trying to OAuth a stdio servercodex mcp login only works for Streamable HTTP servers (those defined with url, not command)