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.

3.3 KB
Raw Blame
import { MakeLogicType, actions, kea, listeners, path, reducers } from 'kea'
import posthog from 'posthog-js'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import { eventDefinitionsList } from 'products/event_definitions/frontend/generated/api'

import { MCP_ANALYTICS_NUDGE_TOAST_ID, MCPAnalyticsNudgeToast } from './MCPAnalyticsNudgeToast'

export type MCPAnalyticsNudgeSurface = 'insight' | 'dashboard'

export function titleMentionsMCP(title: string | null | undefined): boolean {
    return !!title && /mcp/i.test(title)
}

/** Shows the MCP analytics nudge if the project has no `$mcp_tool_call` events yet. Fire and forget. */
export function tryShowMCPAnalyticsNudge(surface: MCPAnalyticsNudgeSurface): void {
    try {
        if (!mcpAnalyticsNudgeLogic.isMounted()) {
            mcpAnalyticsNudgeLogic.mount()
        }
        mcpAnalyticsNudgeLogic.actions.maybeShowNudge(surface)
    } catch (error) {
        console.warn('[mcpAnalyticsNudge] dispatch failed', { surface, error })
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpAnalyticsNudgeLogicValues {
    handledTeamIds: number[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpAnalyticsNudgeLogicActions {
    markTeamHandled: (teamId: number) => {
        teamId: number
    }
    maybeShowNudge: (surface: MCPAnalyticsNudgeSurface) => {
        surface: MCPAnalyticsNudgeSurface
    }
}

export type mcpAnalyticsNudgeLogicType = MakeLogicType<mcpAnalyticsNudgeLogicValues, mcpAnalyticsNudgeLogicActions>

export const mcpAnalyticsNudgeLogic = kea<mcpAnalyticsNudgeLogicType>([
    // pinned: the path sets the localStorage key of handledTeamIds
    path(['products', 'mcp_analytics', 'frontend', 'nudge', 'mcpAnalyticsNudgeLogic']),
    actions({
        maybeShowNudge: (surface: MCPAnalyticsNudgeSurface) => ({ surface }),
        markTeamHandled: (teamId: number) => ({ teamId }),
    }),
    reducers({
        handledTeamIds: [
            [] as number[],
            { persist: true },
            {
                markTeamHandled: (state, { teamId }) => (state.includes(teamId) ? state : [...state, teamId]),
            },
        ],
    }),
    listeners(({ values, actions }) => ({
        maybeShowNudge: async ({ surface }) => {
            const teamId = teamLogic.values.currentTeamId
            if (!teamId || values.handledTeamIds.includes(teamId)) {
                return
            }
            // Claim the team before the request, so two quick saves cannot both show the toast.
            actions.markTeamHandled(teamId)

            try {
                const response = await eventDefinitionsList(String(teamId), { names: ['$mcp_tool_call'], limit: 1 })
                if (response.results.length > 0 || teamLogic.values.currentTeamId !== teamId) {
                    return
                }
            } catch {
                return
            }

            lemonToast.info(<MCPAnalyticsNudgeToast surface={surface} />, {
                toastId: MCP_ANALYTICS_NUDGE_TOAST_ID,
                autoClose: false,
                icon: false, // the body carries its own MCP icon
            })
            posthog.capture('mcp analytics nudge shown', { surface })
        },
    })),
])