Skip to content

Commit 462d0e7

Browse files
fix: filter out empty strings in server arguments to prevent parseArgs error
When running 'npx .' without any arguments, empty strings were being passed as positional arguments to the server, causing Node.js parseArgs to fail with 'Unexpected argument' error. This fix filters out empty strings from the arguments array before passing them to spawnPromise. Also added a test to verify the web client can start without any arguments, preventing regression of this issue.
1 parent cf44b30 commit 462d0e7

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

cli/scripts/cli-tests.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,111 @@ async function runErrorTest(testName, ...args) {
299299
}
300300
}
301301

302+
// Function to run a web client startup test
303+
async function runWebClientTest(testName, ...args) {
304+
const outputFile = path.join(
305+
OUTPUT_DIR,
306+
`${testName.replace(/\//g, "_")}.log`,
307+
);
308+
309+
console.log(`\n${colors.YELLOW}Testing: ${testName}${colors.NC}`);
310+
TOTAL_TESTS++;
311+
312+
// Run the command and capture output
313+
console.log(
314+
`${colors.BLUE}Command: node ${BUILD_DIR}/cli.js ${args.join(" ")}${colors.NC}`,
315+
);
316+
317+
try {
318+
// Create a write stream for the output file
319+
const outputStream = fs.createWriteStream(outputFile);
320+
321+
// Spawn the process
322+
return new Promise((resolve) => {
323+
const child = spawn("node", [path.join(BUILD_DIR, "cli.js"), ...args], {
324+
stdio: ["ignore", "pipe", "pipe"],
325+
env: {
326+
...process.env,
327+
MCP_AUTO_OPEN_ENABLED: "false", // Prevent browser from opening
328+
SERVER_PORT: "9999", // Use a different port for testing
329+
CLIENT_PORT: "9998", // Use a different port for testing
330+
},
331+
});
332+
333+
let output = "";
334+
let serverStarted = false;
335+
336+
child.stdout.on("data", (data) => {
337+
output += data.toString();
338+
outputStream.write(data);
339+
340+
// Check if server started successfully
341+
if (output.includes("MCP Inspector is up and running")) {
342+
serverStarted = true;
343+
// Kill the process after successful startup
344+
child.kill();
345+
}
346+
});
347+
348+
child.stderr.on("data", (data) => {
349+
output += data.toString();
350+
outputStream.write(data);
351+
});
352+
353+
const timeout = setTimeout(() => {
354+
console.log(`${colors.YELLOW}Test timed out: ${testName}${colors.NC}`);
355+
child.kill();
356+
}, 5000); // 5 second timeout for web client startup
357+
358+
child.on("close", (code) => {
359+
clearTimeout(timeout);
360+
outputStream.end();
361+
362+
if (serverStarted) {
363+
console.log(`${colors.GREEN}✓ Test passed: ${testName}${colors.NC}`);
364+
console.log(`${colors.BLUE}Server started successfully${colors.NC}`);
365+
PASSED_TESTS++;
366+
resolve(true);
367+
} else {
368+
console.log(`${colors.RED}✗ Test failed: ${testName}${colors.NC}`);
369+
console.log(`${colors.RED}Error output:${colors.NC}`);
370+
console.log(
371+
output
372+
.split("\n")
373+
.map((line) => ` ${line}`)
374+
.join("\n"),
375+
);
376+
FAILED_TESTS++;
377+
378+
// Stop after any error is encountered
379+
console.log(
380+
`${colors.YELLOW}Stopping tests due to error. Please validate and fix before continuing.${colors.NC}`,
381+
);
382+
process.exit(1);
383+
}
384+
});
385+
});
386+
} catch (error) {
387+
console.error(
388+
`${colors.RED}Error running test: ${error.message}${colors.NC}`,
389+
);
390+
FAILED_TESTS++;
391+
process.exit(1);
392+
}
393+
}
394+
302395
// Run all tests
303396
async function runTests() {
397+
console.log(
398+
`\n${colors.YELLOW}=== Running Basic Tests ===${colors.NC}`,
399+
);
400+
401+
// Test 0: Basic web client mode without any arguments (regression test for npx .)
402+
await runWebClientTest(
403+
"web_client_no_args"
404+
// No arguments - should start web client without error
405+
);
406+
304407
console.log(
305408
`\n${colors.YELLOW}=== Running Basic CLI Mode Tests ===${colors.NC}`,
306409
);

client/bin/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async function startProdServer(serverOptions) {
9595
mcpServerArgs && mcpServerArgs.length > 0
9696
? `--args=${mcpServerArgs.join(" ")}`
9797
: "",
98-
],
98+
].filter(arg => arg !== ""),
9999
{
100100
env: {
101101
...process.env,

0 commit comments

Comments
 (0)