UNPKG

@sigyl-dev/cli

Version:

Official Sigyl CLI for installing and managing MCP packages. Zero-config installation for public packages, secure API-based authentication.

189 lines • 8.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.openMCPInspector = openMCPInspector; exports.launchMCPInspector = launchMCPInspector; exports.getPlaygroundDir = getPlaygroundDir; const chalk_1 = __importDefault(require("chalk")); const node_child_process_1 = require("node:child_process"); const logger_1 = require("../logger"); const path_1 = __importDefault(require("path")); const node_fs_1 = require("node:fs"); const http_1 = __importDefault(require("http")); const serve_handler_1 = __importDefault(require("serve-handler")); async function openMCPInspector(mcpPort, customUrl) { try { // Default MCP Inspector URL - this would be the public MCP Inspector const inspectorUrl = customUrl || "https://mcp-inspector.com"; // Construct URL with MCP server connection info const connectionUrl = `${inspectorUrl}?server=localhost:${mcpPort}`; (0, logger_1.verboseLog)(`Opening MCP Inspector: ${connectionUrl}`); console.log(chalk_1.default.blue(`šŸ” MCP Inspector: ${chalk_1.default.underline(connectionUrl)}`)); // Open browser on different platforms const platform = process.platform; let command; let args; switch (platform) { case 'darwin': command = 'open'; args = [connectionUrl]; break; case 'win32': command = 'start'; args = ['', connectionUrl]; break; default: command = 'xdg-open'; args = [connectionUrl]; break; } (0, node_child_process_1.spawn)(command, args, { detached: true, stdio: 'ignore' }); } catch (error) { console.log(chalk_1.default.yellow("āš ļø Could not auto-open MCP Inspector")); console.log(chalk_1.default.gray(`Please visit: https://mcp-inspector.com?server=localhost:${mcpPort}`)); (0, logger_1.verboseLog)(`Error: ${error}`); } } async function launchMCPInspector(options = {}) { const { serverEntry = path_1.default.resolve(process.cwd(), "template-mcp/server.ts"), serverArgs = [], serverPort = 8080, playgroundDir = getPlaygroundDir(), playgroundPort = 3001, autoBuildPlayground = false, inspectorMode = process.env.SIGYL_INSPECTOR_MODE || 'local', } = options; console.log("\x1b[36m\n================ MCP Inspector ================\x1b[0m"); // --- MCP Server --- const isHttpServer = serverEntry.includes('server.js') || serverEntry.includes('server.ts'); let serverProcess = null; let staticServer = null; let inspectorOpened = false; let serverStarted = false; let playgroundStarted = false; function openInspectorUI(url) { if (inspectorOpened) return; inspectorOpened = true; console.log(`\n🌐 Opening MCP Inspector UI: ${url}`); const platform = process.platform; let command; let args; switch (platform) { case 'darwin': command = 'open'; args = [url]; break; case 'win32': command = 'start'; args = ['', url]; break; default: command = 'xdg-open'; args = [url]; break; } (0, node_child_process_1.spawn)(command, args, { detached: true, stdio: 'ignore' }); } function cleanup() { if (serverProcess) serverProcess.kill('SIGTERM'); if (staticServer) staticServer.close(); process.exit(0); } process.on('SIGINT', cleanup); process.on('SIGTERM', cleanup); // --- Playground Build Check --- const playgroundDist = path_1.default.join(playgroundDir, 'dist'); if (!(0, node_fs_1.existsSync)(playgroundDist)) { if (autoBuildPlayground) { console.log("[INFO] Playground build missing. Attempting to build..."); try { // Try to build using npm or pnpm const buildCmd = (0, node_fs_1.existsSync)(path_1.default.join(playgroundDir, 'package.json')) ? 'npm' : null; if (buildCmd) { const buildProc = (0, node_child_process_1.spawn)(buildCmd, ['run', 'build'], { cwd: playgroundDir, stdio: 'inherit' }); await new Promise((resolve, reject) => { buildProc.on('close', (code) => code === 0 ? resolve(true) : reject(new Error('Playground build failed'))); }); } else { throw new Error('No package.json found in playground directory. Cannot build.'); } } catch (e) { console.error("\x1b[31m[ERROR]\x1b[0m Failed to build playground:", e); process.exit(1); } } else { console.error("\x1b[31m[ERROR]\x1b[0m Playground build output (dist) is missing. Please run 'npm run build' in the playground directory:"); console.error(` cd ${playgroundDir} && npm run build`); process.exit(1); } } // --- Start MCP Server --- if (isHttpServer) { console.log(`Starting HTTP MCP Server on port ${serverPort}...`); serverProcess = (0, node_child_process_1.spawn)("npx", ["tsx", serverEntry, ...serverArgs], { stdio: ["inherit", "pipe", "pipe"], env: { ...process.env, PORT: String(serverPort) } }); serverProcess.stdout?.on("data", (data) => { const str = data.toString(); process.stdout.write(str); if (str.includes("MCP Server listening") && !serverStarted) { serverStarted = true; maybeOpenInspector(); } }); serverProcess.stderr?.on("data", (data) => { process.stderr.write(data.toString()); }); serverProcess.on("error", (err) => { console.error("Failed to start MCP Server:", err); }); serverProcess.on("close", (code) => { if (code !== 0) { console.error(`Server exited with code ${code}`); } else { console.log("\nšŸ‘‹ MCP Server stopped"); } }); } // --- Start Playground Static Server --- staticServer = http_1.default.createServer((req, res) => { return (0, serve_handler_1.default)(req, res, { public: playgroundDist }); }); staticServer.listen(playgroundPort, () => { playgroundStarted = true; console.log(`\nšŸŽ‰ Playground UI is running at http://localhost:${playgroundPort}`); maybeOpenInspector(); }); staticServer.on('error', (err) => { console.error(`\x1b[31m[ERROR]\x1b[0m Failed to start playground static server on port ${playgroundPort}:`, err); cleanup(); }); // --- Wait for both servers before opening browser --- function maybeOpenInspector() { if (serverStarted && playgroundStarted && !inspectorOpened) { console.log(`\n1. Your MCP server is running at http://localhost:${serverPort}/mcp`); console.log(`2. The Inspector UI will open in your browser at http://localhost:${playgroundPort}`); console.log("3. Connect using the UI to inspect your MCP server."); openInspectorUI(`http://localhost:${playgroundPort}`); } } } // Utility to resolve playground directory inside the CLI package function getPlaygroundDir() { // Check both possible locations: production (dist) and development (src) const candidates = [ path_1.default.resolve(__dirname, '../playground'), // sibling to dist/ or src/ path_1.default.resolve(__dirname, '../../playground'), // for nested build structures ]; for (const playgroundDir of candidates) { if ((0, node_fs_1.existsSync)(playgroundDir)) { console.log('[INFO] Using playground directory:', playgroundDir); return playgroundDir; } } console.error("\x1b[31m[ERROR]\x1b[0m Playground directory is missing from the CLI package. Tried:", candidates.join(', ')); console.error("This is a bug: the playground must be bundled with the CLI. Please reinstall or contact the maintainer."); process.exit(1); } //# sourceMappingURL=inspector.js.map