mcp-context-forge icon indicating copy to clipboard operation
mcp-context-forge copied to clipboard

Auto-refresh silently does nothing for authorization_code OAuth servers

Open a-effort opened this issue 2 months ago • 3 comments

Summary Background auto-refresh runs with created_via="health_check", but the code that loads a stored OAuth token only runs when created_via == "manual_refresh" (mcpgateway/services/gateway_service.py:8193). Background refresh never loads a credential itself and reuses whatever the health check loaded (:6324).

Details: For the authorization_code grant, the health check looks up the token as the platform admin (:6146). The lookup works only if the admin has completed the OAuth flow for that server. It finds nothing if the admin has not, or if the only token is team-scoped, since that needs a request JWT.

With no credential, discovery returns empty lists without calling the server. A check correctly stops this from clearing the stored catalog, but AUTO_REFRESH_SERVERS then does nothing for these servers. The token miss (:6152) and the empty result (:8250) log only at debug, and the refresh still reports success, which also resets the failure backoff. An operator who turns the setting on will assume it covers every server.

Proposed fix Either load a service-level credential for background refresh on these servers, or report in the API and the new UI when a background refresh had no usable credential.

Scope: API and backend.

a-effort avatar Sep 28 '26 17:09 a-effort

hello @a-effort Ma'am, I'd like to work on this. Before writing code, I want to confirm the direction, since the two options in the description have different security implications.

What I see on main: check_health_of_gateways triggers the refresh with created_via="health_check" (around L5487), and _refresh_gateway_tools_resources_prompts only resolves the stored OAuth token when created_via == "manual_refresh" (L7283). So for an authorization_code gateway without a passthrough Authorization header, _initialize_gateway returns empty lists, the empty-result guard skips the update, and nothing is logged.

Proposal (option 2, "report"):

  1. In the auto-refresh branch, detect authorization_code gateways that have no passthrough Authorization, and skip the refresh call instead of making a request that can't succeed.
  2. Log it once per gateway at info (e.g. "auto-refresh skipped for : authorization_code servers require a manual refresh"), not on every cycle.
  3. Expose it on the gateway read schema (e.g. requires_manual_refresh: bool) so the API and the new UI can show it. I'd keep the UI change for a follow-up PR unless you'd prefer it here.
  4. Unit tests for the skip, the log-once behaviour, and the schema field.

I'm not proposing option 1 (refreshing in the background with a stored user token), because it would populate a shared catalog with one user's credentials. If that's the direction you want, I'm happy to take guidance on which credential the background path should use.

Does this approach work for you?

Saurav-Gupta-9741 avatar Oct 23 '26 05:10 Saurav-Gupta-9741

@Saurav-Gupta-9741 , thanks for jumping in to contribute! 🙌

Agreed this is the best approach: Option 2. Log it once per gateway at info (e.g. "auto-refresh skipped for : authorization_code servers require a manual refresh"), not on every cycle.

a-effort avatar Nov 16 '26 22:11 a-effort

Hi @a-effort Ma'am, #7327 is now merged, implementing option 2 as agreed here: auto-refresh skips authorization_code gateways with no stored user token and logs it once per gateway at info. The PR didn't link this issue for auto-close, so could you please close it when you have a moment? Thank you for the guidance on the direction!

Saurav-Gupta-9741 avatar Nov 20 '26 13:11 Saurav-Gupta-9741