#!/usr/bin/env node
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { MCPServerController } from "../controllers/MCPServerController.js";
class SpecVerseCleanMCPServer {
  controller;
  config;
  constructor(config) {
    this.config = config;
    this.controller = new MCPServerController(config);
    this.setupEventHandlers();
  }
  setupEventHandlers() {
    const eventEmitter = this.controller.getEventEmitter();
    if (this.config.logging && this.config.mode !== "local") {
      eventEmitter.on("server-started", (event) => {
        const serverEvent = event;
        console.error(`Clean MCP Server started in ${serverEvent.mode} mode (PID: ${serverEvent.pid})`);
        if (serverEvent.port) {
          console.error(`Server listening on port ${serverEvent.port}`);
        }
      });
      eventEmitter.on("tool-called", (event) => {
        const toolEvent = event;
        console.error(`Tool executed: ${toolEvent.toolName} (${toolEvent.executionTime}ms)`);
      });
      eventEmitter.on("resource-requested", (event) => {
        const resourceEvent = event;
        console.error(`Resource accessed: ${resourceEvent.uri}`);
      });
      eventEmitter.on("error-occurred", (event) => {
        const errorEvent = event;
        console.error(`Error in ${errorEvent.operation}: ${errorEvent.error}`);
        if (errorEvent.context) {
          console.error("Context:", JSON.stringify(errorEvent.context, null, 2));
        }
      });
    }
  }
  async start() {
    await this.controller.initialize();
    if (this.config.mode === "local") {
      const transport = new StdioServerTransport(process.stdin, process.stdout);
      try {
        await this.controller.getServer().connect(transport);
      } catch (error) {
        console.error("MCP Server connection failed:", error);
        process.exit(1);
      }
      const gracefulShutdown = () => {
        process.exit(0);
      };
      process.on("SIGINT", gracefulShutdown);
      process.on("SIGTERM", gracefulShutdown);
      process.stdin.resume();
    } else if (this.config.mode === "remote") {
      if (this.config.logging) {
        console.error("Starting Clean MCP Server in remote mode...");
      }
      const { createServer } = await import("http");
      const server = createServer(async (req, res) => {
        if (req.url === "/health") {
          const metrics = this.controller.getMetrics();
          res.writeHead(200, { "Content-Type": "application/json" });
          res.end(JSON.stringify({
            status: "healthy",
            mode: "remote",
            metrics
          }));
        } else if (req.url === "/mcp/resources") {
          try {
            const resources = await this.controller.listResources();
            res.writeHead(200, { "Content-Type": "application/json" });
            res.end(JSON.stringify({
              resources,
              count: resources.length,
              mode: "remote"
            }));
          } catch (error) {
            res.writeHead(500, { "Content-Type": "application/json" });
            res.end(JSON.stringify({
              error: error instanceof Error ? error.message : String(error)
            }));
          }
        } else if (req.url?.startsWith("/mcp/resource/")) {
          try {
            const uri = decodeURIComponent(req.url.replace("/mcp/resource/", ""));
            const resource = await this.controller.readResource(uri);
            res.writeHead(200, { "Content-Type": "application/json" });
            res.end(JSON.stringify(resource));
          } catch (error) {
            res.writeHead(500, { "Content-Type": "application/json" });
            res.end(JSON.stringify({
              error: error instanceof Error ? error.message : String(error)
            }));
          }
        } else if (req.url === "/mcp") {
          res.writeHead(200, { "Content-Type": "application/json" });
          res.end(JSON.stringify({
            message: "MCP over HTTP partially implemented",
            mode: "remote",
            endpoints: [
              "/mcp/resources - List all resources",
              "/mcp/resource/{uri} - Read specific resource"
            ]
          }));
        } else {
          res.writeHead(404);
          res.end("Not found");
        }
      });
      const port = this.config.port || 3e3;
      server.listen(port, () => {
        if (this.config.logging) {
          console.error(`Clean MCP Server listening on port ${port}`);
          console.error("Health check: http://localhost:" + port + "/health");
        }
      });
      const gracefulShutdown = () => {
        if (this.config.logging) {
          console.error("Clean MCP Server shutting down...");
        }
        server.close(() => {
          process.exit(0);
        });
      };
      process.on("SIGINT", gracefulShutdown);
      process.on("SIGTERM", gracefulShutdown);
    } else {
      throw new Error(`Unsupported mode: ${this.config.mode}`);
    }
  }
  getController() {
    return this.controller;
  }
}
async function main() {
  process.on("uncaughtException", (error) => {
    console.error("Uncaught exception in Clean MCP server:", error);
    process.exit(1);
  });
  process.on("unhandledRejection", (reason, promise) => {
    console.error("Unhandled rejection in Clean MCP server:", reason);
    process.exit(1);
  });
  const args = process.argv.slice(2);
  const mode = args.includes("--mode") ? args[args.indexOf("--mode") + 1] : "local";
  const port = args.includes("--port") ? parseInt(args[args.indexOf("--port") + 1]) : 3e3;
  const config = {
    mode,
    port,
    logging: !args.includes("--silent"),
    resources_path: args.includes("--resources-path") ? args[args.indexOf("--resources-path") + 1] : void 0,
    features: {
      orchestrator: args.includes("--enable-orchestrator")
    }
  };
  if (config.logging && config.mode !== "local") {
    console.error("Starting Clean SpecVerse MCP Server with config:", JSON.stringify(config, null, 2));
  }
  try {
    const server = new SpecVerseCleanMCPServer(config);
    await server.start();
  } catch (error) {
    console.error("Failed to start Clean MCP Server:", error);
    process.exit(1);
  }
}
const condition1 = import.meta.url === `file://${process.argv[1]}`;
const condition2 = process.argv[1]?.includes("mcp-server.js");
const condition3 = process.argv[0]?.includes("specverse-mcp");
const condition4 = process.argv[1]?.includes("specverse-mcp");
const isMainModule = condition1 || condition2 || condition3 || condition4;
if (isMainModule) {
  main().catch((error) => {
    console.error("Fatal error in Clean MCP Server:", error);
    process.exit(1);
  });
}
export {
  SpecVerseCleanMCPServer,
  main
};
