diff --git a/package-lock.json b/package-lock.json index 94c136e..c20dd0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@maplezzk/mcps", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@maplezzk/mcps", - "version": "1.1.1", + "version": "1.1.2", "license": "ISC", "dependencies": { "@modelcontextprotocol/sdk": "^1.25.3", diff --git a/package.json b/package.json index 888a1f6..1b28284 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@maplezzk/mcps", - "version": "1.1.1", + "version": "1.1.2", "description": "A CLI to manage and use MCP servers", "publishConfig": { "access": "public" diff --git a/src/commands/daemon.ts b/src/commands/daemon.ts index e3148c0..f62f2b1 100644 --- a/src/commands/daemon.ts +++ b/src/commands/daemon.ts @@ -34,6 +34,14 @@ function isPortInUse(port: number): Promise { const startAction = async (options: any) => { const port = parseInt(options.port || process.env.MCPS_PORT || DAEMON_PORT); + // Get timeout from option, env, or config (in seconds) + let timeout = 30; // Default 30 seconds for daemon start + if (options.timeout) { + timeout = parseInt(options.timeout, 10); + } else if (process.env.MCPS_DAEMON_TIMEOUT) { + timeout = parseInt(process.env.MCPS_DAEMON_TIMEOUT, 10); + } + // Check if port is in use (more reliable than HTTP check) const portInUse = await isPortInUse(port); if (portInUse) { @@ -98,8 +106,8 @@ const startAction = async (options: any) => { // Wait briefly to ensure it started (optional but good UX) // We can poll status for a second const start = Date.now(); - // Increased timeout to allow for connection initialization - while (Date.now() - start < 30000) { + // Use timeout from option/env (convert to ms) + while (Date.now() - start < timeout * 1000) { // If child reported port conflict, check if daemon is actually running if (childFailed) { const stillRunning = await isPortInUse(port); @@ -226,6 +234,7 @@ export const registerDaemonCommand = (program: Command) => { program.command('start') .description('Start the daemon') .option('-p, --port ', 'Daemon port', String(DAEMON_PORT)) + .option('-t, --timeout ', 'Startup timeout in seconds (default: 30)') .option('-v, --verbose', 'Show detailed logs') .action((options) => startAction(options)); @@ -253,6 +262,7 @@ export const registerDaemonCommand = (program: Command) => { daemonCmd.command('start', { isDefault: true, hidden: true }) .description('Start the daemon (default)') + .option('-t, --timeout ', 'Startup timeout in seconds (default: 30)') .action((options) => startAction(options)); daemonCmd.command('stop') diff --git a/src/core/config.ts b/src/core/config.ts index 67fdf65..468e7f0 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -112,6 +112,24 @@ export class ConfigManager { config.mcpServers[name] = result.data; this.saveConfig(config); } + + getDaemonTimeout(): number { + // Priority: environment variable > config file > default + const envTimeout = process.env.MCPS_DAEMON_TIMEOUT; + if (envTimeout) { + const parsed = parseInt(envTimeout, 10); + if (!isNaN(parsed) && parsed > 0) { + return parsed * 1000; // Convert seconds to milliseconds + } + } + + const config = this.loadConfig(); + if (config.daemonTimeout) { + return config.daemonTimeout; + } + + return 20000; // Default 20 seconds + } } export const configManager = new ConfigManager(); diff --git a/src/core/daemon-client.ts b/src/core/daemon-client.ts index 6cc0958..98b5f35 100644 --- a/src/core/daemon-client.ts +++ b/src/core/daemon-client.ts @@ -1,6 +1,7 @@ import { spawn } from 'child_process'; import chalk from 'chalk'; import { DAEMON_BASE_URL } from './constants.js'; +import { configManager } from './config.js'; export class DaemonClient { static async isRunning(): Promise { @@ -12,9 +13,12 @@ export class DaemonClient { } } - static async startDaemon(): Promise { + static async startDaemon(timeout?: number): Promise { console.log(chalk.gray('Starting background daemon...')); - + + // Get timeout from parameter or config + const daemonTimeout = timeout || configManager.getDaemonTimeout(); + // Use process.argv[1] which points to the CLI entry point // detached: true allows the child to keep running after parent exits const subprocess = spawn(process.execPath, [process.argv[1], 'daemon', 'start'], { @@ -27,21 +31,21 @@ export class DaemonClient { // Wait for daemon to be ready const start = Date.now(); - while (Date.now() - start < 5000) { // 5s timeout + while (Date.now() - start < daemonTimeout) { if (await this.isRunning()) { return; } await new Promise(resolve => setTimeout(resolve, 200)); } - - throw new Error('Daemon failed to start within timeout'); + + throw new Error(`Daemon failed to start within ${daemonTimeout / 1000}s timeout`); } - static async ensureDaemon(): Promise { + static async ensureDaemon(timeout?: number): Promise { if (await this.isRunning()) { return; } - await this.startDaemon(); + await this.startDaemon(timeout); } static async executeTool(serverName: string, toolName: string, args: any) { diff --git a/src/types/config.ts b/src/types/config.ts index 5da1530..846345c 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -26,6 +26,7 @@ export const ServerConfigSchema = z.union([ // Standard MCP config format (mcpServers) export const ConfigSchema = z.object({ mcpServers: z.record(z.string(), ServerConfigSchema), + daemonTimeout: z.number().optional(), }); export type StdioServerConfig = z.infer;