@rockship/apollo-io-mcp
Version:
A powerful Model Context Protocol (MCP) server implementation for seamless Apollo.io API integration, enabling AI assistants to interact with Apollo.io data
93 lines ⢠3.6 kB
JavaScript
import cors from "cors";
import dotenv from "dotenv";
import express from "express";
import path from "node:path";
// Load environment variables
dotenv.config();
/**
* Get server configuration from environment variables
*/
export function getServerConfig(portEnvVar, defaultPort) {
return {
port: parseInt(process.env[portEnvVar] || defaultPort, 10),
host: process.env.HTTP_HOST || "localhost",
environment: process.env.NODE_ENV || "development",
};
}
export function setupExpressApp(app) {
app.use(express.json());
app.use(cors({
origin: "*", // Configure appropriately for production
exposedHeaders: ["Mcp-Session-Id"],
allowedHeaders: ["Content-Type", "mcp-session-id"],
}));
// Serve static files from public directory
const publicPath = path.join(process.cwd(), "public");
app.use(express.static(publicPath));
// Specifically handle favicon.ico requests
app.get("/favicon.ico", (req, res) => {
res.sendFile(path.join(publicPath, "favicon.ico"));
});
// Serve index.html at root path
app.get("/", (req, res) => {
res.sendFile(path.join(publicPath, "index.html"));
});
}
export function setupHealthCheck(app, transports) {
app.get("/health", (req, res) => {
const isCombined = transports?.streamable || transports?.sse;
const stats = {
activeConnections: isCombined
? Object.keys(transports.streamable).length +
Object.keys(transports.sse).length
: Object.keys(transports).length,
};
res.json({
status: "healthy",
timestamp: new Date().toISOString(),
...stats,
});
});
}
export function setupListenServer(app, serverName, config) {
return app.listen(config.port, config.host, () => {
console.log(`š ${serverName} server started successfully`);
console.log(`š Server running at http://${config.host}:${config.port}`);
console.log(`š Environment: ${config.environment}`);
console.log(`š Health check: http://${config.host}:${config.port}/health`);
console.log(`ā” SSE endpoint: http://${config.host}:${config.port}/sse`);
console.log(`ā” HTTP Stream endpoint: http://${config.host}:${config.port}/mcp`);
});
}
export function setupGracefulShutdown(server, serverName) {
if (!server) {
console.error("ā Server is not initialized");
return;
}
const gracefulShutdown = (signal) => {
console.log(`\nš Received ${signal}. Starting graceful shutdown...`);
server?.close(() => {
console.log(`ā
${serverName} server closed successfully`);
process.exit(0);
});
// Force shutdown after 10 seconds
setTimeout(() => {
console.log("ā ļø Forcing shutdown after timeout");
process.exit(1);
}, 10000);
};
// Handle shutdown signals
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
// Handle uncaught exceptions
process.on("uncaughtException", (error) => {
console.error("š„ Uncaught Exception:", error);
gracefulShutdown("UNCAUGHT_EXCEPTION");
});
// Handle unhandled promise rejections
process.on("unhandledRejection", (reason, promise) => {
console.error("š„ Unhandled Rejection at:", promise, "reason:", reason);
gracefulShutdown("UNHANDLED_REJECTION");
});
}
//# sourceMappingURL=server-utils.js.map