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.5 KB
Raw Blame
/**
 * MCP definition discovery and schema path resolution.
 *
 * Shared by generate-orval-schemas.mjs and generate-tools.ts.
 * MCP-specific: knows about services/mcp/definitions/ and products/*\/mcp/.
 */
import fs from 'node:fs'
import path from 'node:path'

export const modulesWithStrictBodies = new Set(['email_templates'])

/**
 * Resolve the OpenAPI schema path, respecting OPENAPI_SCHEMA_PATH env override.
 *
 * @param {string} repoRoot - absolute path to the repository root
 * @returns {string} absolute path to the OpenAPI JSON schema file
 */
export function resolveSchemaPath(repoRoot) {
    const defaultPath = path.resolve(repoRoot, 'frontend', 'tmp', 'openapi.json')
    return process.env.OPENAPI_SCHEMA_PATH ? path.resolve(repoRoot, process.env.OPENAPI_SCHEMA_PATH) : defaultPath
}

/**
 * Discover all MCP YAML definition files.
 *
 * Scans:
 * - services/mcp/definitions/*.yaml — core MCP tool configs
 * - products/*\/mcp/*.yaml — per-product tool configs
 *
 * @param {object} opts
 * @param {string} opts.definitionsDir - absolute path to services/mcp/definitions/
 * @param {string} opts.productsDir - absolute path to the products/ directory
 * @returns {{ moduleName: string, filePath: string }[]}
 */
export function discoverDefinitions({ definitionsDir, productsDir }) {
    const sources = []

    if (fs.existsSync(definitionsDir)) {
        for (const file of fs.readdirSync(definitionsDir)) {
            if (!file.endsWith('.yaml') && !file.endsWith('.yml')) {
                continue
            }
            sources.push({
                moduleName: file.replace(/\.ya?ml$/, ''),
                filePath: path.join(definitionsDir, file),
            })
        }
    }

    if (fs.existsSync(productsDir)) {
        for (const product of fs.readdirSync(productsDir, { withFileTypes: true })) {
            if (!product.isDirectory() || product.name.startsWith('_')) {
                continue
            }
            const mcpDir = path.join(productsDir, product.name, 'mcp')
            if (!fs.existsSync(mcpDir)) {
                continue
            }
            for (const file of fs.readdirSync(mcpDir)) {
                if (!file.endsWith('.yaml') && !file.endsWith('.yml')) {
                    continue
                }
                const moduleName =
                    file === 'tools.yaml' || file === 'tools.yml' ? product.name : file.replace(/\.ya?ml$/, '')
                sources.push({
                    moduleName,
                    filePath: path.join(mcpDir, file),
                })
            }
        }
    }

    return sources.sort((a, b) => a.moduleName.localeCompare(b.moduleName))
}

/**
 * Returns true if the parsed YAML has the shape of a query wrapper config
 * (has `wrappers` key instead of `tools`).
 */
export function isQueryWrappersConfig(parsed) {
    return typeof parsed === 'object' && parsed !== null && 'wrappers' in parsed && !('tools' in parsed)
}

/**
 * Returns true if the parsed YAML has the shape of a category config
 * (has `tools` key instead of `wrappers`).
 */
export function isToolsConfig(parsed) {
    return typeof parsed === 'object' && parsed !== null && 'tools' in parsed
}

/**
 * Orval's export name for an operation: `messaging_templates_create` becomes `MessagingTemplatesCreate`.
 * @param {string} operationId
 * @returns {string}
 */
export function operationIdToPascal(operationId) {
    return operationId
        .split(/[_.]/)
        .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
        .join('')
}