codex - 💡(How to fix) Fix Codex does not auto-refresh routed MCP OAuth tokens even when a refresh token is stored [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#17265Fetched 2026-04-10 03:43:13
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
2
Timeline (top)
labeled ×4commented ×1unlabeled ×1

Codex persists a refresh_token for a routed MCP server in ~/.codex/.credentials.json, but it does not automatically refresh the access token when the token expires.

As a result, once the access token expires:

  • MCP tool calls start failing with auth errors
  • automations and later runs break
  • the only recovery is manual CLI login and browser reauth

This defeats the purpose of storing a refresh token.

Error Message

{"success": false, "error": "invalid_grant", "msg": "Authorization required"}

Root Cause

Codex persists a refresh_token for a routed MCP server in ~/.codex/.credentials.json, but it does not automatically refresh the access token when the token expires.

As a result, once the access token expires:

  • MCP tool calls start failing with auth errors
  • automations and later runs break
  • the only recovery is manual CLI login and browser reauth

This defeats the purpose of storing a refresh token.

RAW_BUFFERClick to expand / collapse

What issue are you seeing?

Summary

Codex persists a refresh_token for a routed MCP server in ~/.codex/.credentials.json, but it does not automatically refresh the access token when the token expires.

As a result, once the access token expires:

  • MCP tool calls start failing with auth errors
  • automations and later runs break
  • the only recovery is manual CLI login and browser reauth

This defeats the purpose of storing a refresh token.

Environment

  • Product: Codex desktop / Codex MCP client. MacOS 26.3.1 (a)
  • Date observed: April 9, 2026

Expected behavior

If Codex has a valid stored refresh_token for the MCP server, it should automatically refresh the expired access token and continue making authenticated MCP calls without requiring manual CLI login.

This is especially important for:

  • automations
  • recurring background runs
  • later thread/tool invocations after some idle time

Actual behavior

When the access token expires, Codex starts returning auth failures instead of using the stored refresh token to recover automatically.

Observed failure:

{"success": false, "error": "invalid_grant", "msg": "Authorization required"}


### What steps can reproduce the bug?

- Auth to MCP, observe the credentials and refresh token stored in credentials.json
- Wait for the token expiry, access the MCP again (prior to refresh token expiry), it will fail on auth and not automatically refresh as it should
- Use the CLI to authenticate manually, it will then work again

### What is the expected behavior?

Within the refresh token expiry window re-auth should be automatic

### Additional information

_No response_

extent analysis

TL;DR

Implement automatic refresh token logic to obtain a new access token when the existing one expires, using the stored refresh_token in ~/.codex/.credentials.json.

Guidance

  • Review the authentication flow in Codex to identify where the refresh token logic should be implemented to automatically obtain a new access token when the existing one expires.
  • Verify that the refresh_token stored in ~/.codex/.credentials.json is valid and not expired, and that it is being used correctly in the authentication flow.
  • Consider implementing a periodic check for access token expiration, and use the refresh_token to obtain a new access token before it is needed for MCP calls.
  • Investigate the error handling for the invalid_grant error, to ensure that it is properly handled and that the refresh token logic is triggered when necessary.

Example

No specific code example can be provided without more information about the Codex authentication implementation, but a general example of refresh token usage might involve sending a request to the token endpoint with the refresh_token and other required parameters, such as client ID and client secret.

Notes

The implementation details of the refresh token logic may vary depending on the specific requirements and constraints of the Codex application and the MCP server. It is also important to ensure that the refresh token is properly validated and handled to prevent security vulnerabilities.

Recommendation

Apply a workaround by implementing automatic refresh token logic, as this will allow Codex to continue making authenticated MCP calls without requiring manual CLI login, which is especially important for automations and recurring background runs.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

If Codex has a valid stored refresh_token for the MCP server, it should automatically refresh the expired access token and continue making authenticated MCP calls without requiring manual CLI login.

This is especially important for:

  • automations
  • recurring background runs
  • later thread/tool invocations after some idle time

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING