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.

4.3 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path, selectors } from 'kea'
import { combineUrl, router } from 'kea-router'

import { urls } from 'scenes/urls'

import { sceneLogic } from '~/scenes/sceneLogic'

import { mcpAnalyticsFiltersLogic } from './mcpAnalyticsFiltersLogic'
import { type MCPDashboardStage, mcpAnalyticsOnboardingLogic } from './mcpAnalyticsOnboardingLogic'

export type MCPAnalyticsTab =
    | 'activity'
    | 'dashboard'
    | 'sessions'
    | 'tool-quality'
    | 'intent-clustering'
    | 'missing-capabilities'
    | 'notifications'

export const TAB_DESCRIPTIONS: Record<MCPAnalyticsTab, string> = {
    activity: 'Live feed of tool calls and what agents are trying to do with your MCP server.',
    dashboard: 'Tool call volume, error rates, and latency across your MCP server.',
    sessions: 'Sessions where users interacted with your MCP tools.',
    'tool-quality': 'Understand how reliably your MCP tools support user workflows.',
    'intent-clustering':
        'Cluster semantically similar user intents and see which tools each cluster routes to. Highlights inconsistent routing.',
    'missing-capabilities': 'What agents asked your server for and could not get, in their own words, newest first.',
    notifications:
        'Recurring summaries of what agents are doing, plus instant alerts for the things you need to know about right away.',
}

const SCENE_KEY_TO_TAB: Record<string, MCPAnalyticsTab> = {
    mcpAnalyticsActivity: 'activity',
    mcpAnalyticsDashboard: 'dashboard',
    mcpAnalyticsSessions: 'sessions',
    mcpAnalyticsToolQuality: 'tool-quality',
    mcpAnalyticsIntentClustering: 'intent-clustering',
    mcpAnalyticsMissingCapabilities: 'missing-capabilities',
    mcpAnalyticsNotifications: 'notifications',
}

function resolveAutomaticLanding(activeTab: MCPAnalyticsTab, dashboardStage: MCPDashboardStage | null): void {
    const { landing, ...restParams } = router.values.searchParams
    if (landing !== 'auto' || activeTab !== 'dashboard' || dashboardStage === null) {
        return
    }

    const target = dashboardStage === 'activity' ? urls.mcpAnalyticsActivity() : urls.mcpAnalyticsDashboard()
    router.actions.replace(combineUrl(target, restParams).url)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpAnalyticsSceneLogicValues {
    dashboardStage: MCPDashboardStage | null // mcpAnalyticsOnboardingLogic
    sceneKey: string | null // sceneLogic
    activeTab: MCPAnalyticsTab
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpAnalyticsSceneLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        activeTab: (sceneKey: string | null) => MCPAnalyticsTab
    }
}

export type mcpAnalyticsSceneLogicType = MakeLogicType<
    mcpAnalyticsSceneLogicValues,
    {},
    Record<string, any>,
    mcpAnalyticsSceneLogicMeta
>

export const mcpAnalyticsSceneLogic = kea<mcpAnalyticsSceneLogicType>([
    path(['products', 'mcp_analytics', 'frontend', 'mcpAnalyticsSceneLogic']),
    connect(() => ({
        values: [sceneLogic, ['sceneKey'], mcpAnalyticsOnboardingLogic, ['dashboardStage']],
        logic: [mcpAnalyticsFiltersLogic],
    })),
    selectors({
        activeTab: [
            (s) => [s.sceneKey],
            (sceneKey: string): MCPAnalyticsTab => SCENE_KEY_TO_TAB[sceneKey] ?? 'dashboard',
        ],
    }),
    listeners(({ values }) => ({
        // Never-set-up projects stay in-scene: the scene's `emptyState` (SceneExport)
        // renders the setup screen right here until data arrives, so there's no
        // redirect into the app-wide onboarding flow.
        [mcpAnalyticsOnboardingLogic.actionTypes.loadSignalsSuccess]: () => {
            // Volume decides the default landing tab. Only resolves when the manifest's
            // bare-URL redirect tagged the navigation with `landing=auto` — a deep link
            // to /dashboard never re-routes, and an explicit tab click is never overridden.
            resolveAutomaticLanding(values.activeTab, values.dashboardStage)
        },
    })),
    // The setup-status gate can load signals before the scene logic mounts. Resolve
    // from the current value too so the one-time success action cannot be missed.
    afterMount(({ values }) => resolveAutomaticLanding(values.activeTab, values.dashboardStage)),
])