Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 12 additions & 2 deletions src/commands/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ function isPortInUse(port: number): Promise<boolean> {
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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -226,6 +234,7 @@ export const registerDaemonCommand = (program: Command) => {
program.command('start')
.description('Start the daemon')
.option('-p, --port <number>', 'Daemon port', String(DAEMON_PORT))
.option('-t, --timeout <seconds>', 'Startup timeout in seconds (default: 30)')
.option('-v, --verbose', 'Show detailed logs')
.action((options) => startAction(options));

Expand Down Expand Up @@ -253,6 +262,7 @@ export const registerDaemonCommand = (program: Command) => {

daemonCmd.command('start', { isDefault: true, hidden: true })
.description('Start the daemon (default)')
.option('-t, --timeout <seconds>', 'Startup timeout in seconds (default: 30)')
.action((options) => startAction(options));

daemonCmd.command('stop')
Expand Down
18 changes: 18 additions & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
18 changes: 11 additions & 7 deletions src/core/daemon-client.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
Expand All @@ -12,9 +13,12 @@ export class DaemonClient {
}
}

static async startDaemon(): Promise<void> {
static async startDaemon(timeout?: number): Promise<void> {
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'], {
Expand All @@ -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<void> {
static async ensureDaemon(timeout?: number): Promise<void> {
if (await this.isRunning()) {
return;
}
await this.startDaemon();
await this.startDaemon(timeout);
}

static async executeTool(serverName: string, toolName: string, args: any) {
Expand Down
1 change: 1 addition & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof StdioServerConfigSchema>;
Expand Down