#!/usr/bin/env node import path from "path"; import fs from "fs-extra"; import chalk from "chalk"; import { Command } from "commander"; import inquirer from "inquirer"; import ora from "ora"; import { fileURLToPath } from "url"; import { checkNodeVersion } from "./utils/check-node.js"; import { createProject } from "./helpers/create.js"; import { isFolderEmpty } from "./utils/is-folder-empty.js"; import { downloadAndExtractExample, listExamples } from "./helpers/examples.js"; import { applyCloudflareSettings } from "./helpers/cloudflare.js"; import { readProjectName, reportScaffold } from "./helpers/telemetry.js"; checkNodeVersion(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const packageJson = JSON.parse( fs.readFileSync(path.join(__dirname, "../package.json"), "utf8") ); const program = new Command() .name("create-xmcp-app") .description("Create a new xmcp application") .version( packageJson.version, "-v, --version", "Output the current version of create-xmcp-app." ) .argument("[directory]") .usage("[directory] [options]") .option("--example ", "Download and extract an example/template") .option("--list-examples", "List all available examples/templates") .helpOption("-h, --help", "Display help message.") .option("-y, --yes", "Skip confirmation prompt", false) .option("--use-npm", "Use npm as package manager (default: use npm)") .option("--use-yarn", "Use yarn as package manager") .option("--use-pnpm", "Use pnpm as package manager") .option("--use-bun", "Use bun as package manager") .option("--skip-install", "Skip installing dependencies", false) .option("--http", "Enable HTTP transport", false) .option("--stdio", "Enable STDIO transport", false) .option("--cloudflare, --cf", "Initialize for Cloudflare Workers", false) .option("--ui", "Initialize with MCP App template (non-tailwind)", false) .option("--ui-kit", "Initialize with @xmcp-dev/ui starter files", false) .option("--tailwind, --tw", "Use Tailwind CSS (only with MCP App)", false) .action(async (projectDir, options) => { const cloudflareFlag = options.cloudflare || process.argv.includes("--cloudflare") || process.argv.includes("--cf"); const skipInstallFlag = options.skipInstall || process.argv.includes("--skip-install"); console.log(chalk.bold(`\ncreate-xmcp-app@${packageJson.version}`)); if (options.listExamples) { await listExamples(); process.exit(0); } if (options.example) { const targetPath = projectDir ? path.resolve(process.cwd(), projectDir) : process.cwd(); const targetName = path.basename(targetPath); if (fs.existsSync(targetPath)) { const stats = fs.statSync(targetPath); if (!stats.isDirectory()) { console.error( chalk.red(`Error: ${targetName} exists but is not a directory.`) ); process.exit(1); } if (!isFolderEmpty(targetPath, targetName)) { console.error(chalk.red(`The directory ${targetPath} is not empty.`)); process.exit(1); } } else { fs.ensureDirSync(targetPath); } const exampleStartTime = Date.now(); await downloadAndExtractExample(targetPath, options.example); if (cloudflareFlag) { applyCloudflareSettings(targetPath); } await reportScaffold({ command: "create-xmcp-app", version: packageJson.version, projectName: readProjectName(targetPath), template: "example", features: cloudflareFlag ? ["cloudflare"] : [], duration: Date.now() - exampleStartTime, }); console.log(); if (cloudflareFlag) { console.log( `${chalk.bgHex("#F38020").white(" Cloudflare ")} ${chalk.dim( "bootstrapped for Workers deploy." )}` ); console.log(); } console.log("Next steps:"); if (projectDir) { console.log(` cd ${chalk.cyan(projectDir)}`); } console.log(` ${chalk.cyan("pnpm install")}`); console.log(` ${chalk.cyan("pnpm run dev")}`); console.log(); console.log("To learn more about xmcp:"); console.log(` - Read the documentation at https://xmcp.dev/docs\n`); process.exit(0); } // If project directory wasn't specified, ask for it if (!projectDir) { const answers = await inquirer.prompt([ { type: "input", name: "projectDir", message: "What is your project named?", default: "my-xmcp-app", }, ]); projectDir = answers.projectDir; } // Normalize project directory const resolvedProjectPath = path.resolve(process.cwd(), projectDir); const projectName = path.basename(resolvedProjectPath); // Check if directory exists if (fs.existsSync(resolvedProjectPath)) { const stats = fs.statSync(resolvedProjectPath); if (!stats.isDirectory()) { console.error( chalk.red(`Error: ${projectName} exists but is not a directory.`) ); process.exit(1); } // Check if directory is empty if (!isFolderEmpty(resolvedProjectPath, projectName)) { console.error( chalk.red(`The directory ${resolvedProjectPath} is not empty.`) ); process.exit(1); } } let packageManager = "npm"; let skipInstall = skipInstallFlag; let transports = ["http"]; let selectedPaths = ["tools", "prompts", "resources"]; let template = "typescript"; let templateChoice = "default"; let tailwind = false; let uiKit = Boolean(options.uiKit); if (options.http || options.stdio) { transports = []; if (options.http) transports.push("http"); if (options.stdio) transports.push("stdio"); } if (uiKit) { template = "mcp-apps"; templateChoice = "mcp-app"; transports = ["http"]; selectedPaths = ["tools", "resources"]; tailwind = true; } else if (options.ui) { template = "mcp-apps"; templateChoice = "mcp-app"; transports = ["http"]; selectedPaths = ["tools"]; tailwind = false; } if (!options.yes) { if (!options.ui && !uiKit) { const templateAnswers = await inquirer.prompt([ { type: "list", name: "template", message: "Select a template:", choices: [ { name: "Default (Standard MCP server)", value: "default", }, { name: "MCP App (React widgets for ext-apps)", value: "mcp-app", }, { name: "MCP App with UI kit (components and JSON renderer)", value: "ui-kit", }, ], default: "default", }, ]); uiKit = templateAnswers.template === "ui-kit"; templateChoice = uiKit ? "mcp-app" : templateAnswers.template; if (templateChoice === "mcp-app") { template = "mcp-apps"; transports = ["http"]; selectedPaths = uiKit ? ["tools", "resources"] : ["tools"]; tailwind = uiKit; } } if ( templateChoice === "mcp-app" && !options.tailwind && !options.ui && !uiKit ) { const tailwindAnswers = await inquirer.prompt([ { type: "confirm", name: "tailwind", message: "Would you like to use Tailwind CSS?", default: true, }, ]); tailwind = tailwindAnswers.tailwind; } if (options.useYarn) packageManager = "yarn"; if (options.usePnpm) packageManager = "pnpm"; if (options.useBun) packageManager = "bun"; if ( !options.useYarn && !options.usePnpm && !options.useBun && !options.useNpm ) { const pmAnswers = await inquirer.prompt([ { type: "list", name: "packageManager", message: "Select a package manager:", choices: [ { name: "npm", value: "npm" }, { name: "yarn", value: "yarn" }, { name: "pnpm", value: "pnpm" }, { name: "bun", value: "bun" }, ], default: "npm", }, ]); packageManager = pmAnswers.packageManager; } if (templateChoice === "default" && !options.http && !options.stdio) { const transportAnswers = await inquirer.prompt([ { type: "list", name: "transport", message: "Select the transport you want to use:", choices: [ { name: "HTTP (runs on a server)", value: "http", }, { name: "STDIO (runs on the user's machine)", value: "stdio", }, ], default: "http", }, ]); transports = [transportAnswers.transport]; } // Path selection checklist (only for none template) if (templateChoice === "default") { const pathAnswers = await inquirer.prompt([ { type: "checkbox", name: "paths", message: "Select components to initialize:", choices: [ { name: "Tools", value: "tools", checked: true, }, { name: "Prompts", value: "prompts", checked: true, }, { name: "Resources", value: "resources", checked: true, }, ], }, ]); selectedPaths = pathAnswers.paths; } console.log(); console.log( `Creating a new xmcp app in ${chalk.green(resolvedProjectPath)}.\n` ); const { confirmed } = await inquirer.prompt([ { type: "confirm", name: "confirmed", message: "Ok to continue?", default: true, }, ]); if (!confirmed) { console.log(chalk.yellow("Aborting installation.")); process.exit(0); } } else { // Use command-line options when --yes is provided if (options.useYarn) packageManager = "yarn"; if (options.usePnpm) packageManager = "pnpm"; if (options.useBun) packageManager = "bun"; // Use all paths by default in non-interactive mode (unless template is set) if (templateChoice === "default") { selectedPaths = ["tools", "prompts", "resources"]; } // Default to Tailwind for MCP app template in non-interactive mode if (templateChoice === "mcp-app" && !options.ui && !uiKit) { tailwind = true; } } if (options.uiKit && options.ui) { console.log( chalk.yellow( "Using --ui-kit starter and ignoring --ui because --ui-kit already scaffolds an MCP App." ) ); } else if (options.ui && options.tailwind) { console.log( chalk.yellow( "Ignoring --tailwind because --ui scaffolds the non-tailwind MCP App template." ) ); } else if (options.tailwind && templateChoice !== "mcp-app") { console.log( chalk.yellow( "Ignoring --tailwind because it only applies to the MCP App template." ) ); } const scaffoldStartTime = Date.now(); const getScaffoldReport = () => ({ command: "create-xmcp-app" as const, version: packageJson.version, projectName, packageManager, template, features: [ ...transports, ...(cloudflareFlag ? ["cloudflare"] : []), ...(tailwind ? ["tailwind"] : []), ...(uiKit ? ["ui-kit"] : []), ...(skipInstall ? ["skip-install"] : []), ], duration: Date.now() - scaffoldStartTime, }); const spinner = ora("Creating your xmcp app...").start(); try { createProject({ projectPath: resolvedProjectPath, projectName, packageManager, transports: transports, packageVersion: packageJson.version, skipInstall, paths: selectedPaths, template, tailwind, uiKit, cloudflare: cloudflareFlag, }); spinner.succeed(chalk.green("Your xmcp app is ready")); console.log(); if (cloudflareFlag) { console.log( `${chalk.bgHex("#F38020").white(" Cloudflare ")} ${chalk.dim( "bootstrapped for Workers deploy." )}` ); console.log(); } console.log("Next steps:"); if (resolvedProjectPath !== process.cwd()) { console.log(` cd ${chalk.cyan(projectDir)}`); } if (packageManager === "yarn") { skipInstall && console.log(` ${chalk.cyan("yarn install")}`); console.log(` ${chalk.cyan("yarn dev")}`); } else if (packageManager === "pnpm") { skipInstall && console.log(` ${chalk.cyan("pnpm install")}`); console.log(` ${chalk.cyan("pnpm dev")}`); } else if (packageManager === "bun") { skipInstall && console.log(` ${chalk.cyan("bun install")}`); console.log(` ${chalk.cyan("bun dev")}`); } else { skipInstall && console.log(` ${chalk.cyan("npm install")}`); console.log(` ${chalk.cyan("npm run dev")}`); } console.log(); console.log("To learn more about xmcp:"); console.log(` - Read the documentation at https://xmcp.dev/docs\n`); await reportScaffold(getScaffoldReport()); } catch (error) { spinner.fail(chalk.red("Failed to create the project.")); console.error(error); await reportScaffold({ ...getScaffoldReport(), error }); process.exit(1); } }); program.parse(process.argv);