fix(auth): auto-refresh on PostHog 401/403 auth failures#2186
Merged
Conversation
PostHog's OAuth access tokens can go dead before the client's local
`accessTokenExpiresAt` expires (the previous access token is invalidated
when a new one is minted via refresh). When that happens, callers see
HTTP 401/403 with bodies the proxy and renderer fetcher weren't
matching, so neither auto-recovered.
- MCP proxy: retry-with-refresh on HTTP 401 in addition to the existing
JSON-RPC body sentinels. Extend the sentinel set to match the literal
strings the servers actually return (`Invalid API key` from
mcp.posthog.com via Cloudflare, `Authentication failed` from the
us.posthog.com installation proxy).
- Renderer fetcher: retry-with-refresh on HTTP 403 when the body is
`{type: "authentication_error", code: "authentication_failed"}` (the
shape PostHog's Django API returns for invalid bearer tokens), in
addition to the existing 401 retry.
Generated-By: PostHog Code
Task-Id: 83ede0c1-3f83-4fe3-a4bd-69cf379be315
joshsny
approved these changes
May 18, 2026
tatoalo
approved these changes
May 18, 2026
Contributor
|
- Drop bare HTTP 401 trigger from MCP proxy retry — authenticatedFetch already retries on 401/403, so the second refresh was redundant and risked churning tokens under the server's in-place access-token rotation behavior. - Gate the new substring matches (`Invalid API key`, `Authentication failed`) on `status >= 400` to avoid spurious refreshes when a 200 JSON-RPC tool result legitimately mentions those phrases. - Add tests covering the renderer fetcher's 403-with-authentication_failed retry path and adjacent negative cases. Generated-By: PostHog Code Task-Id: 83ede0c1-3f83-4fe3-a4bd-69cf379be315
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MCP tool discovery and renderer task creation were surfacing 401/403
authentication_failedto users because the existing refresh-and-retry paths didn't match the real error shapes (Invalid API key,Authentication failed, 403 withauthentication_errorbody).Extend the MCP proxy to retry-with-refresh on HTTP 401 plus the literal server strings, and the renderer fetcher to retry on 403 with
authentication_failedin addition to 401.Created with PostHog Code