From 18775f9e1ef66d6b3e7706c04e542052d4d04bd2 Mon Sep 17 00:00:00 2001 From: zhangzikuan Date: Sun, 1 Feb 2026 18:10:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=8F=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=9A=84=20daemon=20=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增功能: - 支持通过命令行参数 --timeout/-t 设置超时(秒) - 支持通过环境变量 MCPS_DAEMON_TIMEOUT 设置超时(秒) - 支持通过配置文件 daemonTimeout 字段设置超时(毫秒) - 默认超时从 5 秒调整为 20 秒 优先级:命令行参数 > 环境变量 > 配置文件 > 默认值 使用示例: mcps start --timeout 40 MCPS_DAEMON_TIMEOUT=25 mcps start 配置文件: { "daemonTimeout": 30000 } Co-Authored-By: Claude Sonnet 4.5 --- src/commands/daemon.ts | 14 ++++++++++++-- src/core/config.ts | 18 ++++++++++++++++++ src/core/daemon-client.ts | 18 +++++++++++------- src/types/config.ts | 1 + 4 files changed, 42 insertions(+), 9 deletions(-) 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; From fcdbd94e034154d715c6e054953eac9c665f8187 Mon Sep 17 00:00:00 2001 From: zhangzikuan Date: Sun, 1 Feb 2026 18:10:51 +0800 Subject: [PATCH 2/3] chore: bump version to 1.0.32 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7ff1440..9ad56db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@maplezzk/mcps", - "version": "1.0.29", + "version": "1.0.32", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@maplezzk/mcps", - "version": "1.0.29", + "version": "1.0.32", "license": "ISC", "dependencies": { "@modelcontextprotocol/sdk": "^1.25.3", diff --git a/package.json b/package.json index 09169fd..952ebd2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@maplezzk/mcps", - "version": "1.0.31", + "version": "1.0.32", "description": "A CLI to manage and use MCP servers", "publishConfig": { "access": "public" From 44df7a5cbb25ea40d22841a9e3458fb9adad6934 Mon Sep 17 00:00:00 2001 From: zhangzikuan Date: Sun, 1 Feb 2026 18:12:20 +0800 Subject: [PATCH 3/3] chore: bump version to 1.1.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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"