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.5 KB
Raw Blame
import { MakeLogicType, actions, kea, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { actionToUrl, urlToAction } from 'kea-router'

import { projectLogic } from 'scenes/projectLogic'
import { urls } from 'scenes/urls'

import { mcpRegistryServersDiscoverRetrieve } from './generated/api'
import { MCPDiscoverCandidateApi, MCPDiscoverResponseApi } from './generated/api.schemas'

/** Enough candidates to compare, few enough to read without scrolling past the fold. */
const RESULT_LIMIT = 10

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpRegistryLogicValues {
    candidates: MCPDiscoverCandidateApi[]
    intent: string
    response: MCPDiscoverResponseApi | null
    responseLoading: boolean
    searchError: string | null
    searchedIntent: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpRegistryLogicActions {
    search: (_: void) => void
    searchFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    searchSuccess: (
        response: MCPDiscoverResponseApi | null,
        payload?: void
    ) => {
        response: MCPDiscoverResponseApi | null
        payload?: void
    }
    setIntent: (intent: string) => {
        intent: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpRegistryLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        candidates: (response: MCPDiscoverResponseApi | null) => MCPDiscoverCandidateApi[]
        searchedIntent: (response: MCPDiscoverResponseApi | null) => string
    }
}

export type mcpRegistryLogicType = MakeLogicType<
    mcpRegistryLogicValues,
    mcpRegistryLogicActions,
    Record<string, any>,
    mcpRegistryLogicMeta
>

export const mcpRegistryLogic = kea<mcpRegistryLogicType>([
    path(['products', 'mcp_registry', 'frontend', 'mcpRegistryLogic']),
    actions({
        setIntent: (intent: string) => ({ intent }),
    }),
    reducers({
        intent: ['' as string, { setIntent: (_, { intent }) => intent }],
        // kea-loaders only keeps response/responseLoading. The scene needs the failure itself.
        searchError: [
            null as string | null,
            {
                search: () => null,
                searchSuccess: () => null,
                searchFailure: (_, { error }) => String(error),
            },
        ],
    }),
    loaders(({ values }) => ({
        response: [
            null as MCPDiscoverResponseApi | null,
            {
                search: async (_: void, breakpoint) => {
                    const intent = values.intent.trim()
                    if (!intent) {
                        return null
                    }
                    // The whole page reads one endpoint. Keeping it that way is what lets this
                    // move to a public origin later without dragging project plumbing along.
                    const projectId = String(projectLogic.findMounted()?.values.currentProjectId)
                    const response = await mcpRegistryServersDiscoverRetrieve(projectId, {
                        intent,
                        limit: RESULT_LIMIT,
                    })
                    breakpoint()
                    return response
                },
            },
        ],
    })),
    selectors({
        candidates: [
            (s) => [s.response],
            (response: MCPDiscoverResponseApi | null): MCPDiscoverCandidateApi[] => response?.candidates ?? [],
        ],
        searchedIntent: [
            (s) => [s.response],
            (response: MCPDiscoverResponseApi | null): string => response?.intent ?? '',
        ],
    }),
    actionToUrl(({ values }) => ({
        // A search someone can paste to a colleague, rather than one that lives in local state.
        search: () => [urls.mcpRegistry(), values.intent ? { q: values.intent } : {}],
    })),
    urlToAction(({ actions, values }) => ({
        [urls.mcpRegistry()]: (_, searchParams) => {
            const query = String(searchParams.q ?? '')
            // Searching pushes the query into the URL, which lands back here. Bail on that echo,
            // or the second pass fires the same request again before the first has resolved.
            if (query === values.intent) {
                return
            }
            actions.setIntent(query)
            if (query) {
                actions.search()
            }
        },
    })),
])