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.8 KB
Raw Blame
import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'

import { waitFor } from '@testing-library/react'

import { preflightLogic } from 'lib/logic/preflightLogic'
import { teamLogic } from 'scenes/teamLogic'

import _preflight from '~/mocks/fixtures/_preflight.json'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import type { TeamType } from '~/types'

import { WebMCPToolError } from '../webmcpToolError'
import { webmcpLogic } from './webmcpLogic'

interface RegisteredTool {
    name: string
    inputSchema?: Record<string, unknown>
    execute: (input: Record<string, unknown>) => Promise<string>
}

describe('webmcpLogic', () => {
    let registerTool: jest.Mock
    let postedCommands: string[]

    beforeEach(() => {
        registerTool = jest.fn().mockResolvedValue(undefined)
        postedCommands = []
        Object.defineProperty(document, 'modelContext', { value: { registerTool }, configurable: true })

        useMocks({
            get: {
                '/_preflight': () => [200, { ..._preflight, webmcp_available: true }],
            },
            post: {
                '/api/projects/:team/webmcp/exec/': async ({ request }) => {
                    const { command } = (await request.json()) as { command: string }
                    postedCommands.push(command)
                    return command === 'call broken-tool {}'
                        ? [200, { content: [{ type: 'text', text: 'Tool failed' }], is_error: true }]
                        : [200, { content: [{ type: 'text', text: 'insight-get' }], is_error: false }]
                },
            },
        })
    })

    afterEach(() => {
        webmcpLogic.unmount()
        delete (document as Document & { modelContext?: unknown }).modelContext
    })

    it('registers the exec tool and forwards each call to the proxy', async () => {
        initKeaTests()
        webmcpLogic.mount()

        await waitFor(() => expect(registerTool).toHaveBeenCalled())
        const tool: RegisteredTool = registerTool.mock.calls[0][0]

        expect(tool.name).toEqual('exec')
        const schema = JSON.stringify(tool.inputSchema)
        expect(schema).toContain(`${window.location.origin}/project/${MOCK_DEFAULT_TEAM.id}/`)
        expect(schema).not.toContain('__POSTHOG_')

        await expect(tool.execute({ command: 'search insights' })).resolves.toEqual('insight-get')
        const failedCall = tool.execute({ command: 'call broken-tool {}' })
        await expect(failedCall).rejects.toThrow('Tool failed')
        await expect(failedCall).rejects.toBeInstanceOf(WebMCPToolError)
        expect(postedCommands).toEqual(['search insights', 'call broken-tool {}'])
    })

    it('retries registration on the next team load after a failed attempt', async () => {
        registerTool.mockRejectedValueOnce(new Error('Registration failed'))
        initKeaTests()
        webmcpLogic.mount()
        await waitFor(() => expect(registerTool).toHaveBeenCalledTimes(1))

        teamLogic.actions.loadCurrentTeamSuccess(MOCK_DEFAULT_TEAM)

        await waitFor(() => expect(registerTool).toHaveBeenCalledTimes(2))
    })

    it('registers the tool once the team loads after mount', async () => {
        initKeaTests(true, null as unknown as TeamType)
        webmcpLogic.mount()
        expect(registerTool).not.toHaveBeenCalled()

        teamLogic.actions.loadCurrentTeamSuccess(MOCK_DEFAULT_TEAM)

        await waitFor(() => expect(registerTool).toHaveBeenCalledTimes(1))
    })

    it('does not register the tool when the instance has no MCP server', async () => {
        useMocks({ get: { '/_preflight': () => [200, { ..._preflight, webmcp_available: false }] } })
        initKeaTests()
        webmcpLogic.mount()

        await waitFor(() => expect(preflightLogic.values.preflight).not.toBeNull())

        expect(registerTool).not.toHaveBeenCalled()
    })
})