#!/usr/bin/env node
/**
 * Self-bootstrapping MCP server entry point.
 * Builds dist/index.js automatically if missing, then starts the server.
 * This means users don't need to run `pnpm build` before the MCP server works.
 */
const { existsSync } = require('fs');
const { execSync } = require('child_process');
const { join } = require('path');

const dist = join(__dirname, 'dist', 'index.js');

if (!existsSync(dist)) {
  process.stderr.write('[holoscript-mcp] dist not found — building...\n');
  try {
    execSync('pnpm build', { cwd: __dirname, stdio: 'inherit' });
  } catch {
    // pnpm may not be available; fall back to npx tsup
    execSync('npx tsup', { cwd: __dirname, stdio: 'inherit' });
  }
  process.stderr.write('[holoscript-mcp] build complete\n');
}

require(dist);
