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.

2.2 KB
Raw Blame
#!/usr/bin/env tsx
import { build } from 'esbuild'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { pathToFileURL } from 'node:url'

import { copyInstructions } from './copy-instructions'
import { honoEsbuildOptions } from './hono-esbuild-config'

const MCP_ROOT = path.resolve(__dirname, '..')
const REPO_ROOT = path.resolve(MCP_ROOT, '../..')
const ENTRY = path.resolve(__dirname, 'lib/render-webmcp-exec-tool.ts')
const OUTPUT_TS = path.resolve(REPO_ROOT, 'products/webmcp/frontend/execTool.generated.ts')

async function main(): Promise<void> {
    // tsx cannot load the `.md` template imports, so bundle the renderer the way the server build does.
    copyInstructions()
    const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'webmcp-exec-tool-'))
    const bundle = path.join(tmpDir, 'render.mjs')
    try {
        await build({
            ...honoEsbuildOptions(),
            entryPoints: [ENTRY],
            outfile: bundle,
            sourcemap: false,
            absWorkingDir: MCP_ROOT,
            logLevel: 'error',
        })
        const renderer = (await import(pathToFileURL(bundle).href)) as typeof import('./lib/render-webmcp-exec-tool')

        const output =
            '// AUTO-GENERATED by services/mcp/scripts/generate-webmcp-exec-tool.ts.\n' +
            '// Regenerate with `hogli build:openapi`. Do not edit.\n' +
            '\n' +
            `export const APP_ORIGIN_PLACEHOLDER = ${JSON.stringify(renderer.APP_ORIGIN_PLACEHOLDER)}\n` +
            `export const PROJECT_ID_PLACEHOLDER = ${JSON.stringify(renderer.PROJECT_ID_PLACEHOLDER)}\n` +
            '\n' +
            'export const WEBMCP_EXEC_TOOL: {\n' +
            '    name: string\n' +
            '    description: string\n' +
            '    inputSchema: Record<string, unknown>\n' +
            `} = ${JSON.stringify(renderer.renderWebMcpExecTool(), null, 4)}\n`

        fs.writeFileSync(OUTPUT_TS, output)
        process.stdout.write(`Wrote ${path.relative(REPO_ROOT, OUTPUT_TS)}\n`)
    } finally {
        fs.rmSync(tmpDir, { recursive: true, force: true })
    }
}

void main().catch((error: unknown) => {
    process.stderr.write(`${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`)
    process.exit(1)
})