main.ts•1.96 kB
import { pino } from "pino";
import { initializeDatabase } from "./database.js";
import { startWhatsAppConnection, type WhatsAppSocket } from "./whatsapp.js";
import { startMcpServer } from "./mcp.js";
const waLogger = pino(
{
level: process.env.LOG_LEVEL || "info",
timestamp: pino.stdTimeFunctions.isoTime,
},
pino.destination("./wa-logs.txt")
);
const mcpLogger = pino(
{
level: process.env.LOG_LEVEL || "info",
timestamp: pino.stdTimeFunctions.isoTime,
},
pino.destination("./mcp-logs.txt")
);
async function main() {
mcpLogger.info("Starting WhatsApp MCP Server...");
let whatsappSocket: WhatsAppSocket | null = null;
try {
mcpLogger.info("Initializing database...");
initializeDatabase();
mcpLogger.info("Database initialized successfully.");
mcpLogger.info("Attempting to connect to WhatsApp...");
whatsappSocket = await startWhatsAppConnection(waLogger);
mcpLogger.info("WhatsApp connection process initiated.");
} catch (error: any) {
mcpLogger.fatal(
{ err: error },
"Failed during initialization or WhatsApp connection attempt"
);
process.exit(1);
}
try {
mcpLogger.info("Starting MCP server...");
await startMcpServer(whatsappSocket, mcpLogger, waLogger);
mcpLogger.info("MCP Server started and listening.");
} catch (error: any) {
mcpLogger.fatal({ err: error }, "Failed to start MCP server");
process.exit(1);
}
mcpLogger.info("Application setup complete. Running...");
}
async function shutdown(signal: string) {
mcpLogger.info(`Received ${signal}. Shutting down gracefully...`);
waLogger.flush();
mcpLogger.flush();
process.exit(0);
}
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
main().catch((error) => {
mcpLogger.fatal({ err: error }, "Unhandled error during application startup");
waLogger.flush();
mcpLogger.flush();
process.exit(1);
});