Skip to content

MCP bearer-token env var appears configured even when missing from active process #30125

Open

Description

@alamorre

What version of Codex CLI is running?

codex-cli 0.142.0

What platform is your computer?

macOS arm64 / Codex Desktop-bundled CLI

What issue are you seeing?

For a Streamable HTTP MCP server configured with bearer_token_env_var, codex mcp list and codex mcp get can make the server look bearer-authenticated even when the referenced environment variable is not visible to the active Codex process. The actual MCP request then reaches the server without an Authorization header, so protected tools are hidden or startup fails.

This is confusing because the status path says the server has bearer auth configured, while the runtime path cannot send it. codex doctor does notice an MCP warning, but the more direct MCP commands do not surface the missing env var or tell the user that the active Desktop/thread/process needs a restart after the launcher environment is changed.

No token values are included in this report.

What steps can reproduce the bug?

  1. Configure a remote Streamable HTTP MCP server with a bearer-token env var:

    [mcp_servers.example]
    url = "https://example.com/mcp"
    bearer_token_env_var = "EXAMPLE_MCP_TOKEN"
  2. Launch Codex Desktop or Codex CLI from an environment where EXAMPLE_MCP_TOKEN is not present.

  3. Run codex mcp list or codex mcp get example.

  4. Attempt to use the MCP server from the active Codex session.

Observed behavior

In a real repro with a Breakdown MCP server:

$ codex mcp get breakdown
breakdown
  enabled: true
  transport: streamable_http
  url: https://www.breakdown.sh/api/mcp
  bearer_token_env_var: BREAKDOWN_API_TOKEN
  http_headers: -
  env_http_headers: -

$ printenv BREAKDOWN_API_TOKEN >/dev/null && echo set || echo missing
missing

$ codex mcp list
Name       Url                               Bearer Token Env Var  Status   Auth
breakdown  https://www.breakdown.sh/api/mcp  BREAKDOWN_API_TOKEN   enabled  Bearer token

The server-side MCP diagnostic reports that the request arrived without a bearer token:

{
  "ok": false,
  "state": "missing_token",
  "summary": "Breakdown MCP is reachable, but this request did not include a bearer token.",
  "error": { "code": "unauthorized", "message": "Missing bearer token" }
}

codex doctor --summary does catch a generic warning:

[!!] mcp  MCP configuration has optional issues - Set the missing MCP env vars or disable the affected server.

But the direct codex mcp list/get flow still reads as if bearer auth is ready, and the active MCP tool surface remains unusable until the process/thread is restarted with the env var available.

Expected behavior

Codex should distinguish these states in the MCP status path:

  • bearer-token env var is configured
  • bearer-token env var is present in the current process environment
  • bearer-token env var is missing, so no Authorization header will be sent

Concretely, one or more of these would help:

  • codex mcp list/get should show a warning/status such as missing env var EXAMPLE_MCP_TOKEN instead of only Auth: Bearer token.
  • codex doctor should make the missing server/env-var actionable from the default output, while still redacting secret values.
  • The active Desktop/thread MCP startup error should say that the configured bearer-token env var is missing from the process environment and that restarting Codex is required after changing GUI/launcher env vars.
  • If Codex supports live MCP client/session refresh, changing MCP auth config or logging in should invalidate/recreate the active MCP client; otherwise the UI should explicitly say a new process/thread is required.

Additional information

This may overlap with other MCP auth/session-state reports, but this issue is specifically about bearer_token_env_var being configured while absent from the active process environment. Related issues I found while checking for duplicates:

Activity

  1. added
    bug
    CLI
    mcp
    auth
    config
    on Jun 25, 2026
  2. Nicolas0315 commented on Jun 26, 2026

    @Nicolas0315

    I pushed a proposed fix for this here:

    https://github.com/openai/codex/compare/main...Nicolas0315:codex:codex/mcp-missing-bearer-env-status?expand=1

    Summary:

    • Treat bearer_token_env_var as unavailable when the named variable is missing, empty, or invalid Unicode in the active Codex process.
    • Keep the wire/json auth status compatible by mapping that internal unavailable state to not_logged_in.
    • Surface the missing env var clearly in codex mcp list text output and add bearer_token_env_var_status to codex mcp get text output.
    • Prefer a missing-env explanation in MCP startup failure text instead of implying an OAuth login is missing.

    Verification:

    • just fmt
    • git diff --check
    • cargo test --manifest-path codex-rs\Cargo.toml -p codex-rmcp-client determine_auth_status_
    • cargo test --manifest-path codex-rs\Cargo.toml -p codex-mcp mcp_init_error_display_reports_missing_bearer_token_env_var
    • cargo test --manifest-path codex-rs\Cargo.toml -p codex-mcp mcp_startup_failure_reason_requires_existing_oauth_and_auth_failure
    • cargo test --manifest-path codex-rs\Cargo.toml -p codex-cli --test mcp_list list_and_get_report_missing_bearer_token_env_var -- --exact
    • cargo test --manifest-path codex-rs\Cargo.toml -p codex-cli --test mcp_list
    • cargo check --manifest-path codex-rs\Cargo.toml -p codex-cli -p codex-mcp -p codex-rmcp-client

    SSH verification across my available machines:

    • cyberagent-mac-m5max: fresh clone, patch apply, git diff --check, and the targeted rmcp-client/codex-mcp/codex-cli tests passed.
    • nicolas-macbook-pro: fresh clone, patch apply, and git diff --check passed; Rust toolchain is not installed there.
    • rtx5060ti: fresh clone, patch apply, and git diff --check passed; Rust builds are blocked by the host resolving link to GNU coreutils instead of the MSVC linker.
    • nicolas2025: fresh clone, patch apply, and git diff --check passed; cargo/rustc paths on that host are zero-byte stubs.
  3. Nicolas0315 commented on Jun 26, 2026

    @Nicolas0315

    Update: I rebased the branch onto current origin/main (5267e805f) and force-with-lease updated the fork branch to 7950b8a70.

    Additional verification after the rebase:

    • Local Windows: just fmt, git diff --check main...HEAD, the targeted rmcp-client/codex-mcp tests, full codex-cli --test mcp_list, and cargo check -p codex-cli -p codex-mcp -p codex-rmcp-client all passed.
    • SSH fresh-checkout diff checks passed on cyberagent-mac-m5max, nicolas-macbook-pro, rtx5060ti, and nicolas2025 against the rebased fork branch.
    • SSH on cyberagent-mac-m5max: targeted rmcp-client, codex-mcp, and codex-cli tests passed from a fresh checkout of 7950b8a70.
  4. isaac-ranger commented on Aug 12, 2026

    @isaac-ranger

    Linux Desktop reproduction, because apparently we are testing whether a desktop application can locate a desktop secret store without first completing a shell-environment scavenger hunt.

    Environment

    • ChatGPT/Codex App package: 26.803.81509-1 (x86_64)
    • Bundled Codex CLI: 0.147.0-alpha.6.6
    • Platform: Linux 7.1.5-201.fc44.x86_64 x86_64 unknown
    • Desktop secret service: GNOME Secret Service, available and unlocked
    • Transport: authenticated Streamable HTTP MCP on localhost

    Confirmed behavior

    1. codex mcp list --json reports the server as configured and enabled.
    2. The server configuration correctly names a bearer_token_env_var.
    3. The official /usr/lib/chatgpt/ChatGPT process and its bundled Codex child do not contain that variable when launched normally from the desktop entry.
    4. MCP initialize without the credential returns HTTP 401.
    5. The same initialization using the correct credential returns HTTP 200 and a valid MCP session.
    6. Because authentication failed, the server's tools are simply absent from the active task.

    The current user journey is admirably compact:

    configure server → enable server → omit credential → suppress useful authentication failure → erase tools → invite user to question whether the server ever existed

    Security through existential uncertainty is innovative, but it is not quite secret integration.

    Working local workaround

    I verified a workaround that:

    1. Stores the credential in GNOME Keyring.
    2. Uses a user-level desktop launcher to retrieve it at app startup.
    3. Exports it only to the ChatGPT/Codex parent process so MCP authentication can initialize.
    4. Adds a shell_environment_policy.filters exclusion so the credential is not propagated into task shells or captured by future shell snapshots.
    5. Leaves no credential bytes in the launcher, desktop file, command line, model context, or issue report.

    After that, keyring-authenticated MCP initialization returns HTTP 200. So the server, token, protocol, and Linux keyring are all functioning. The missing component is the desktop app doing the desktop-app part.

    A desktop application requiring users to invent a keyring-backed launch wrapper for an authentication mode the application already advertises is certainly one interpretation of “integrated.”

    What #30125 should fix

    The proposed status/error improvement here is necessary: configured bearer auth and usable bearer auth are not the same state, despite their shared enthusiasm for the word “bearer.”

    But it does not address the underlying Desktop UX gap tracked by:

    For Desktop, the complete behavior should be:

    • Let users bind a named MCP secret in Settings.
    • Store it in Keychain / Credential Manager / Secret Service.
    • Resolve it locally for the exact MCP server.
    • Never expose it to task shells, snapshots, prompts, logs, or unrelated subprocesses.
    • Display the actual 401 and missing-secret alias when resolution fails.
    • Refresh the MCP client after the binding changes, or clearly require an app restart.

    GNOME Secret Service was already running throughout this reproduction and, in a stirring demonstration of professional boundaries, the app never bothered it.

  5. TarjinderSingh commented on Aug 15, 2026

    @TarjinderSingh

    Additional macOS Desktop reproduction:

    • ChatGPT Desktop 26.810.50856, bundled Codex CLI 0.148.0-alpha.9
    • The configured bearer-token variable is available through launchctl getenv
    • A normally launched ChatGPT process does not contain that variable
    • MCP configuration nevertheless appears enabled and bearer-authenticated

    There is also a separate runtime problem: when the bundled CLI is launched directly from a shell where the token is explicitly present, its MCP tool call still fails, while standalone Codex 0.147.0 completes the identical call.

    That A/B result is documented separately here: #38794

    This supports treating Desktop credential resolution and the bundled-runtime MCP failure as two independent defects.

  6. karloows commented on Oct 6, 2026

    @karloows

    I’m seeing what appears to be a stale environment snapshot for a remote Streamable HTTP MCP server configured with bearer_token_env_var.

    The MCP configuration is:

    [mcp_servers.github]
    url = "https://api.githubcopilot.com/mcp/"
    bearer_token_env_var = "GITHUB_PAT_TOKEN"
    enabled = true

    GITHUB_PAT_TOKEN is a fine-grained PAT with the target private repository explicitly selected and read permissions for repository contents and metadata.

    From a terminal where direnv is active, the same token can read both repository metadata and a file:

    curl -sS -o /dev/null -w "%{http_code}\n" \
      -H "Authorization: Bearer $GITHUB_PAT_TOKEN" \
      https://api.github.com/repos/REDACTED/REDACTED
    
    # 200
    curl -sS -o /dev/null -w "%{http_code}\n" \
      -H "Authorization: Bearer $GITHUB_PAT_TOKEN" \
      https://api.github.com/repos/REDACTED/REDACTED/contents/README.md
    
    # 200

    In the active Codex session, the equivalent direct API request returns 404, and the GitHub MCP get_file_contents call also returns 404. The MCP authenticates as the expected GitHub user, so it is not completely unauthenticated; it appears to be using a different or stale PAT.

    I ran direnv reload and restarted/recreated the Codex session, but the behavior persisted. Another MCP client can read the same private repository successfully.

    I have redacted all organization, repository, account, and token identifiers. Is there a supported way to confirm which environment snapshot/Codex process supplied bearer_token_env_var to a remote MCP connection, or to force the active MCP client to refresh it?

  7. MdRaf1 commented on Oct 13, 2026

    @MdRaf1

    Confirmed on Linux with codex-cli 0.162.1: codex mcp list --json reports auth_status: "bearer_token" when bearer_token_env_var names an environment variable that is absent from the process. This reproduces with a CLI-only configuration override, without changing any configuration files or using a real credential.

    Reproduction:

    import json
    import os
    import subprocess
    
    probe_env = os.environ.copy()
    probe_env.pop("CODEX_MCP_REPRO_TOKEN", None)
    result = subprocess.run(
        [
            "codex",
            "-c",
            'mcp_servers.credential_probe={url="http://127.0.0.1:9/mcp",bearer_token_env_var="CODEX_MCP_REPRO_TOKEN"}',
            "mcp",
            "list",
            "--json",
        ],
        capture_output=True,
        text=True,
        env=probe_env,
        check=True,
    )
    probe = next(
        entry for entry in json.loads(result.stdout)
        if entry["name"] == "credential_probe"
    )
    print("Token variable present:", "CODEX_MCP_REPRO_TOKEN" in probe_env)
    print("Reported auth status:", probe["auth_status"])

    Observed output:

    Token variable present: False
    Reported auth status: bearer_token
    

    The loopback endpoint is deliberately unused. The point of this reproduction is local credential-status reporting; it does not establish that a credential is accepted by a server. Output is limited to the probe so other configured server details and credentials are not printed.

    Source analysis at 90f00bd89ed3263b4d5993849cccb795f4330449:

    • auth_status_before_discovery returns McpAuthState::BearerToken whenever bearer_token_env_var.is_some(), before resolving a credential value or checking alternative authorization sources.
    • run_list uses the computed authentication state for its JSON and table output. Configuring a variable name is therefore presented as bearer authentication even when that variable cannot supply a token.

    Expected behavior: make the missing credential explicit, naming the server and environment variable without printing its value. A configured authentication mechanism should be distinguished from an available credential. Any fix should preserve valid static headers, environment headers, credential helpers, and stored OAuth fallback behavior; changing token resolution or automatically sourcing shell startup files is unnecessary.

    Related local context: a token export below the non-interactive early-return guard in .bashrc was present in interactive Bash but absent in non-interactive Bash. That difference also reproduces in ordinary Bash without Codex. It explains the missing credential, while the misleading Codex status above is the separate diagnostic issue. An HTTP header helper successfully restored GitHub MCP initialization and an authenticated get_me call.

    This adds a reproduction on 0.162.1 and a source reference from current main to the existing report. No token values or private repository identifiers are included.

  8. creativeessenceglobal-wq commented on Oct 28, 2026

    @creativeessenceglobal-wq

    Codex CLI 0.164.0 — MCP bearer authentication fails despite successful direct handshake

    Environment

    • Windows 10 Pro
    • Codex CLI 0.164.0
    • Remote MCP server: Kapso (https://api.kapso.ai/mcp)
    • Authentication: bearer_token_env_var = "KAPSO_API_KEY"
    • Transport: Streamable HTTP

    Observed behavior

    Codex reports kapso: authentication required (0 tools). Logs show an authentication failure during the initial MCP initialize request.

    Independent verification

    Using the existing API key locally, without exposing its value:

    • Authenticated MCP initialize: HTTP 200
    • Protocol: 2025-11-25
    • Server: kapso-mcp v3.4.0
    • Authenticated tools/list: 192 tools discovered

    The launching PowerShell session reported KEY_PRESENT=True, yet Codex continued to report authentication required.

    Important limitation

    We cannot confirm whether Codex omitted the Authorization header, replaced it, or encountered another runtime authentication failure because the logs do not expose header presence.

    Additional finding

    Two direct read-only Kapso tools/call requests returned JSON-RPC -32603 (internal error). This may be a separate server-side issue and should not be conflated with Codex's initialization failure.

    Request

    Could maintainers confirm whether Codex's MCP runtime correctly resolves bearer_token_env_var and sends the bearer header during Streamable HTTP initialization, particularly when Codex Desktop or another persistent host process is involved?

    Related: #26760.

    No API keys or sensitive credentials are included.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIauthbugconfigmcp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions