codex icon indicating copy to clipboard operation
codex copied to clipboard

Unable to authenticate with MCP server when running in remote environment

Open WhammyLeaf opened this issue 10 months ago • 6 comments

What version of Codex is running?

0.79.0

What subscription do you have?

Own API key

Which model were you using?

gpt-5.1

What platform is your computer?

Linux 5.15.0-305.176.4.el8uek.x86_64 x86_64 x86_64

What issue are you seeing?

We are running the codex CLI tool in a remote codeserver/WebIDE environment. It is impossible to access OAuth protected MCP servers in this environment due to Codex using localhost as callback URI: the localhost will point to the machine that the user is on, not the machine that codex is running on.

What steps can reproduce the bug?

  1. Setup a codeserver/WebIDE environment.
  2. Define config for an OAuth protected MCP server in config.toml
  3. try to authenticate with codex mcp login [NAME]
  4. new tab opens in browser window
  5. but OH NO: "This site can’t be reached"

What is the expected behavior?

New tab in browser should indicate successful authentication and when going back to codeserver tab, the MCP server authentication process in the terminal should be completed.

Additional information

No response

WhammyLeaf avatar Jan 07 '26 15:01 WhammyLeaf

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #8827
  • #8576
  • #6766

Powered by Codex Action

github-actions[bot] avatar Jan 07 '26 15:01 github-actions[bot]

I guess a similar solution as what was used to support https://github.com/openai/codex/issues/2798 could be applied here. This would also be very useful for running in other headless environments, such as containers.

janEbert avatar May 11 '26 14:05 janEbert

@janEbert @eric-oai What exactly is the solution? Actually, the current functionality almost supports authenticating with oauth protected mcp servers in a webide. There is just one technicality which needs to be handled. I explain it here: https://github.com/openai/codex/issues/24058

WhammyLeaf avatar May 22 '26 10:05 WhammyLeaf

Hey, quoting from the linked issue, because it summarizes the solution well:

Add “Hosted Redirect + Copy-Paste Code” Flow (PKCE)

  • CLI launches OAuth with PKCE and redirect_uri pointing to an OpenAI-hosted page (e.g., https://console.openai.com/cli/callback).
  • After the user signs in (in any browser), the hosted page displays a short one-time code (e.g., ABCD-EFGH-IJKL) tied to the CLI session via state.
  • The CLI prompts: Paste the authentication code: and exchanges it (with code_verifier) for tokens.
  • The CLI never depends on a local HTTP server.

As another option, Claude Code, for example, also redirects to a local server, but you can just paste the redirect URL into Claude Code to finish authentication. So even if you can't reach the local exchange server the URL points to, as long as you got correctly redirected to the URL (again, from an external auth provider), you can still authenticate.

janEbert avatar May 27 '26 09:05 janEbert

Confirmed this still occurs with Codex CLI 0.153.4 using Linear's remote MCP server over SSH.

Reproduction:

  1. SSH into a Linux VM.
  2. Run codex mcp add linear --url https://mcp.linear.app/mcp (or codex mcp login linear).
  3. Open the authorization URL in a browser on the local machine and approve access.
  4. Linear redirects the browser to a loopback URL such as http://127.0.0.1:<port>/callback/<path>?code=...&state=....
  5. The browser cannot reach that address because 127.0.0.1 is the local machine while the callback listener belongs to Codex on the VM. The CLI offers no prompt or option to paste the resulting callback URL, so it waits indefinitely.

As a workaround, sending that complete callback URL to the listener from inside the VM with curl immediately returns Authentication complete and codex mcp list then reports Linear as enabled with OAuth authentication. This confirms the authorization flow and callback handler work; the missing piece for SSH/headless use is a CLI fallback that accepts the callback URL (or authorization code) directly.

A Paste the callback URL: fallback after printing the browser URL would solve this without requiring users to configure SSH port forwarding.

electronicmax avatar Sep 05 '26 11:09 electronicmax

this is also happening with the 0.154.0, why is this so hard to fix? add a backup option to just paste the callback url like claude code does

rotarur avatar Sep 14 '26 11:09 rotarur