/ src / cli.ts
cli.ts
 1  #!/usr/bin/env node
 2  
 3  // CRITICAL: Detect MCP mode and suppress console output BEFORE any imports
 4  // MCP servers use stdin/stdout for JSON-RPC via stdio transport
 5  // Detection: stdin/stdout are NOT TTY (piped/redirected) OR explicit MCP flags are present.
 6  // Note: We exclude help/version flags to allow CLI usage even when piped.
 7  const isMCP =
 8    ((!process.stdin.isTTY || !process.stdout.isTTY) ||
 9      process.argv.some(
10        (arg) =>
11          arg.includes("mcp") || arg.includes("stdio") || arg.includes("inspector")
12      )) &&
13    !process.argv.some((arg) => arg === "--help" || arg === "-h" || arg === "--version" || arg === "-v");
14  
15  if (isMCP) {
16    // Suppress all console methods (but NOT stdout/stderr streams - MCP SDK needs those)
17    const noop = () => {};
18    console.log = noop;
19    console.error = noop;
20    console.warn = noop;
21    console.info = noop;
22    console.debug = noop;
23    // NOTE: Do NOT redirect process.stdout.write or process.stderr.write
24    // as the MCP SDK uses those for JSON-RPC protocol communication
25  }
26  
27  import { runServer } from "./server/index.js";
28  
29  // Run the server and handle any fatal errors
30  runServer().catch((error) => {
31    // Only show errors when not running under MCP (to avoid protocol corruption)
32    if (!isMCP) {
33      // Restore console.error temporarily for fatal errors
34      const originalError = console.error;
35      if (originalError.toString() !== "() => {}") {
36        originalError("Fatal error running server:", error);
37      }
38    }
39    process.exit(1);
40  });