index.tsโข3.39 kB
#!/usr/bin/env node
/**
 * Demo MCP Server - Main Entry Point
 * 
 * A comprehensive boilerplate MCP server built with TypeScript using 
 * Domain-Driven Design patterns and dynamic component loading.
 */
import { MyMCPServer } from "./server/index.js";
import type { MCPServerConfig } from "./types/index.js";
/**
 * Server configuration
 */
const serverConfig: MCPServerConfig = {
  name: "demo-mcp-server",
  version: "1.0.0",
  capabilities: {
    tools: true,
    resources: true,
    prompts: true,
    logging: true
  }
};
/**
 * Initialize and start the MCP server
 */
async function main(): Promise<void> {
  console.log("๐ Starting Demo MCP Server...");
  console.log(`๐ Server: ${serverConfig.name} v${serverConfig.version}`);
  let server: MyMCPServer | null = null;
  try {
    // Create server instance
    server = new MyMCPServer(serverConfig);
    // Initialize the server and register all components
    console.log("๐ง Initializing server...");
    await server.initialize();
    // Connect to transport
    console.log("๐ Connecting to transport...");
    await server.connect();
    // Log server status
    const status = server.getStatus();
    console.log("โ
 Server started successfully!");
    console.log(`๐ Status: ${JSON.stringify({
      initialized: status.initialized,
      connected: status.connected,
      loadedModules: status.loadedModules.length
    }, null, 2)}`);
    console.log("๐ก Demo MCP Server is running on stdio");
    console.log("๐ฏ Ready to handle MCP requests...");
    // Setup graceful shutdown
    setupGracefulShutdown(server);
  } catch (error) {
    console.error("โ Fatal error starting server:", error);
    
    if (server) {
      try {
        await server.shutdown();
      } catch (shutdownError) {
        console.error("โ Error during emergency shutdown:", shutdownError);
      }
    }
    
    process.exit(1);
  }
}
/**
 * Setup graceful shutdown handlers
 */
function setupGracefulShutdown(server: MyMCPServer): void {
  const shutdown = async (signal: string) => {
    console.log(`\n๐ Received ${signal}, shutting down gracefully...`);
    
    try {
      await server.shutdown();
      console.log("โ
 Server shutdown completed");
      process.exit(0);
    } catch (error) {
      console.error("โ Error during shutdown:", error);
      process.exit(1);
    }
  };
  // Handle various shutdown signals
  process.on("SIGINT", () => shutdown("SIGINT"));
  process.on("SIGTERM", () => shutdown("SIGTERM"));
  process.on("SIGQUIT", () => shutdown("SIGQUIT"));
  // Handle uncaught exceptions
  process.on("uncaughtException", async (error) => {
    console.error("โ Uncaught exception:", error);
    try {
      await server.shutdown();
    } catch (shutdownError) {
      console.error("โ Error during emergency shutdown:", shutdownError);
    }
    process.exit(1);
  });
  // Handle unhandled promise rejections
  process.on("unhandledRejection", async (reason, promise) => {
    console.error("โ Unhandled rejection at:", promise, "reason:", reason);
    try {
      await server.shutdown();
    } catch (shutdownError) {
      console.error("โ Error during emergency shutdown:", shutdownError);
    }
    process.exit(1);
  });
}
/**
 * Start the server
 */
main().catch((error) => {
  console.error("โ Fatal error in main():", error);
  process.exit(1);
});