Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp): stop reporting rejected keys on scope refresh (#119274)

posthog[bot]1fbf66029ff5
diff --git a/services/mcp/src/lib/StateManager.ts b/services/mcp/src/lib/StateManager.ts
index 5c664d067681..80d27de43c9a 100644
--- a/services/mcp/src/lib/StateManager.ts
+++ b/services/mcp/src/lib/StateManager.ts
@@ -1,6 +1,7 @@
import type { ApiClient, GroupType } from '@/api/client'
import type { Schemas } from '@/api/generated'
import { hasScope } from '@/lib/api'
+import { classifyAuthFailure } from '@/lib/auth-errors'
import { classifyAuthMethod } from '@/lib/auth-method'
import type { ScopedCache } from '@/lib/cache/ScopedCache'
import { StateLookupCache } from '@/lib/cache/StateLookupCache'
@@ -209,7 +210,11 @@ export class StateManager {
}
// A failed refresh must not end a live session. Every API call is authorized again
// server-side, so the last known scopes cannot grant access the key does not hold.
- this._reportException(error, 'api_key_refresh_failed')
+ // A rejected credential is an expected outcome, not a fault, so only report other failures.
+ const { reason } = classifyAuthFailure(error)
+ if (reason !== 'invalid_api_key' && reason !== 'inactive_oauth_token') {
+ this._reportException(error, 'api_key_refresh_failed')
+ }
await this._cache.set('apiKeyFetchedAt', Date.now()).catch(() => {})
return cached
}
diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.ts
index 574e7d69bea8..06dd6aa3211e 100644
--- a/services/mcp/tests/unit/StateManager.test.ts
+++ b/services/mcp/tests/unit/StateManager.test.ts
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { ApiClient } from '@/api/client'
import { MemoryCache } from '@/lib/cache/MemoryCache'
import { StateLookupCache } from '@/lib/cache/StateLookupCache'
-import { PostHogApiError, PostHogTransportError } from '@/lib/errors'
+import { ErrorCode, PostHogApiError, PostHogTransportError } from '@/lib/errors'
import { getPostHogClient } from '@/lib/posthog'
import { StateManager } from '@/lib/StateManager'
import type { ApiRedactedPersonalApiKey, ApiUser } from '@/schema/api'
@@ -349,19 +349,27 @@ describe('StateManager', () => {
}
)
- it('keeps the cached scopes when a refresh fails', async () => {
+ it.each([
+ { label: 'an outage', error: new Error('API unreachable'), reported: true },
+ { label: 'a rejected key', error: new Error(ErrorCode.INVALID_API_KEY), reported: false },
+ { label: 'an inactive token', error: new Error(ErrorCode.INACTIVE_OAUTH_TOKEN), reported: false },
+ ])('keeps the cached scopes when a refresh fails from $label', async ({ error, reported }) => {
vi.useFakeTimers()
try {
const current = vi
.fn()
.mockResolvedValueOnce({ success: true, data: mockApiKey })
- .mockRejectedValue(new Error('API unreachable'))
+ .mockRejectedValue(error)
const api = { config: { apiToken: 'phx_test' }, apiKeys: () => ({ current }) } as unknown as ApiClient
await new StateManager(cache, api).getApiKey()
vi.advanceTimersByTime(3 * 60 * 1000)
- expect((await new StateManager(cache, api).getApiKey()).scopes).toEqual(mockApiKey.scopes)
+ const stateManager = new StateManager(cache, api)
+ const reportSpy = vi.spyOn(stateManager as any, '_reportException').mockImplementation(() => {})
+
+ expect((await stateManager.getApiKey()).scopes).toEqual(mockApiKey.scopes)
+ expect(reportSpy).toHaveBeenCalledTimes(reported ? 1 : 0)
} finally {
vi.useRealTimers()
}