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): count auth failures separately in mcp_init_total (#72304)

Georgiy Tarasov7a1d3e8da1ca
diff --git a/services/mcp/src/hono/dispatcher.ts b/services/mcp/src/hono/dispatcher.ts
index 6b733b3ffe41..3015549e40ae 100644
--- a/services/mcp/src/hono/dispatcher.ts
+++ b/services/mcp/src/hono/dispatcher.ts
@@ -20,6 +20,7 @@ import type {
import GUIDELINES from '@shared/guidelines.md'
import { randomUUID } from 'node:crypto'
+import { mapErrorToAuthResponse } from '@/lib/auth-errors'
import { MCP_SERVER_NAME, MCP_SERVER_VERSION } from '@/lib/constants'
import type { RequestProperties } from '@/lib/request-properties'
@@ -149,9 +150,12 @@ class McpDispatcher {
} catch (error) {
// A failed resolution still fails the handshake for the client, so count
// it in mcp_init_total — otherwise MCPServerHighInitErrorRate only sees
- // failures past this point. The rethrow surfaces as a 500 upstream.
+ // failures past this point. Auth failures (expired tokens, missing scopes)
+ // are client-side and dominate resolution errors, so they get their own
+ // status to keep the alert on `status="error"` meaningful. The rethrow
+ // surfaces as a 401/403/500 upstream via handleCatchError.
if (hasInit) {
- initTotal.inc({ status: 'error' })
+ initTotal.inc({ status: mapErrorToAuthResponse(error) ? 'auth_error' : 'error' })
}
throw error
}
@@ -234,7 +238,7 @@ class McpDispatcher {
...(instructions ? { instructions } : {}),
}
} catch (error) {
- initTotal.inc({ status: 'error' })
+ initTotal.inc({ status: mapErrorToAuthResponse(error) ? 'auth_error' : 'error' })
throw error
}
}
diff --git a/services/mcp/tests/hono/dispatcher-init-errors.test.ts b/services/mcp/tests/hono/dispatcher-init-errors.test.ts
index 1a9773babddd..aeefc6d5beff 100644
--- a/services/mcp/tests/hono/dispatcher-init-errors.test.ts
+++ b/services/mcp/tests/hono/dispatcher-init-errors.test.ts
@@ -135,6 +135,20 @@ describe('McpDispatcher init error accounting', () => {
expect(mockInitTotalInc).toHaveBeenCalledWith({ status: 'error' })
})
+ it.each([
+ ['invalid API key', 'Failed to get user: INVALID_API_KEY'],
+ ['inactive OAuth token', 'Failed to get user: INACTIVE_OAUTH_TOKEN'],
+ ['missing scope', "Failed to get user: Missing PostHog API scope: 'user:read'"],
+ ])('counts a %s resolution failure as auth_error, not error', async (_label, message) => {
+ mockResolveState.mockRejectedValueOnce(new Error(message))
+
+ const dispatcher = new McpDispatcher({} as any, createMockRedis())
+
+ await expect(dispatcher.handleRequest(makeRequest('initialize'), makeProps())).rejects.toThrow()
+ expect(mockInitTotalInc).toHaveBeenCalledTimes(1)
+ expect(mockInitTotalInc).toHaveBeenCalledWith({ status: 'auth_error' })
+ })
+
it('does not count non-initialize requests when state resolution fails', async () => {
mockResolveState.mockRejectedValueOnce(new Error('django auth is down'))