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): treat a 403 from oauth introspection as a rejected token (#114791)

posthog[bot]aa9f36fa4d7c
diff --git a/services/mcp/src/lib/StateManager.ts b/services/mcp/src/lib/StateManager.ts
index 0828a0160b08..a7e8108b608d 100644
--- a/services/mcp/src/lib/StateManager.ts
+++ b/services/mcp/src/lib/StateManager.ts
@@ -111,12 +111,17 @@ export class StateManager {
const introspectionResult = await this._api.oauth().introspect({ token: this._api.config.apiToken })
if (!introspectionResult.success) {
- // Only a 401 rejects the token. Any other failure means the check did not run, and a
- // rejection would make the client discard a token that is still valid.
- if (introspectionResult.error.message.includes(ErrorCode.INVALID_API_KEY)) {
+ // Only a 401 or 403 rejects the token. Self-introspection returns 403 when the token
+ // no longer exists, for example after a refresh rotated it. Any other failure means
+ // the check did not run, and a rejection would make the client discard a valid token.
+ const { error } = introspectionResult
+ if (
+ error.message.includes(ErrorCode.INVALID_API_KEY) ||
+ (error instanceof PostHogApiError && error.status === 403)
+ ) {
throw new Error(ErrorCode.INVALID_API_KEY)
}
- throw wrapError('Failed to introspect OAuth token', introspectionResult.error)
+ throw wrapError('Failed to introspect OAuth token', error)
}
if (!introspectionResult.data.active) {
diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.ts
index c0a11a229162..823008ade292 100644
--- a/services/mcp/tests/unit/StateManager.test.ts
+++ b/services/mcp/tests/unit/StateManager.test.ts
@@ -221,17 +221,39 @@ describe('StateManager', () => {
})
it.each([
- { label: 'a 401', message: 'INVALID_API_KEY: token revoked', rejected: true },
- { label: 'an outage', message: 'Network error: fetch failed', rejected: false },
+ { label: 'a 401', error: new Error('INVALID_API_KEY: token revoked'), rejected: true },
+ {
+ label: 'a 403 for an unknown token',
+ error: new PostHogApiError({
+ status: 403,
+ statusText: 'Forbidden',
+ body: '',
+ url: 'https://us.posthog.com/oauth/introspect',
+ method: 'POST',
+ }),
+ rejected: true,
+ },
+ {
+ label: 'a 503',
+ error: new PostHogApiError({
+ status: 503,
+ statusText: 'Service Unavailable',
+ body: '',
+ url: 'https://us.posthog.com/oauth/introspect',
+ method: 'POST',
+ }),
+ rejected: false,
+ },
+ { label: 'an outage', error: new Error('Network error: fetch failed'), rejected: false },
])(
- 'reports an introspection failure from $label as a rejection only for a 401',
- async ({ message, rejected }) => {
+ 'reports an introspection failure from $label as a rejection only when the token is refused',
+ async ({ error: introspectError, rejected }) => {
const api = {
config: { apiToken: 'pha_test' },
apiKeys: () => ({
current: async () => ({ success: false, error: { message: 'not a personal key' } }),
}),
- oauth: () => ({ introspect: async () => ({ success: false, error: new Error(message) }) }),
+ oauth: () => ({ introspect: async () => ({ success: false, error: introspectError }) }),
} as unknown as ApiClient
const error = await new StateManager(cache, api).getApiKey().catch((e: Error) => e)