#!/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);
});