tryaii-mcp-server
Version:
TryAII MCP Server - 15+ AI models with comparison, cost tracking, and collective intelligence
237 lines • 8.31 kB
JavaScript
import { Router } from 'express';
import { httpClient } from '../services/httpClient.js';
import { sessionManager } from '../services/sessionManager.js';
import { databaseService } from '../services/database.js';
import { logger } from '../utils/logger.js';
import { config } from '../utils/config.js';
const router = Router();
// We'll need to access the MultiTransportServer instance - this will be set by the main server
let multiTransportServer = null;
export const setMultiTransportServer = (server) => {
multiTransportServer = server;
};
/**
* GET /health
* Basic health check endpoint
*/
router.get('/', (req, res) => {
const health = {
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
version: process.env.npm_package_version || '1.0.0',
modes: config.server.modes
};
res.json(health);
});
/**
* GET /health/detailed
* Detailed health check with all service statuses
*/
router.get('/detailed', async (req, res) => {
try {
const startTime = Date.now();
// Check HTTP client health
const httpHealthy = httpClient.isClientHealthy();
const httpStatus = httpClient.getStatus();
const httpMetrics = httpClient.getMetrics();
// Check session manager health
const sessionManagerHealthy = sessionManager.isRunning();
const sessionMetrics = sessionManager.getMetrics();
// Check database health
const databaseHealthy = databaseService.isHealthy();
const databaseInfo = databaseService.getConnectionInfo();
// Check transport health
const transportHealthy = multiTransportServer ? multiTransportServer.isHealthy() : true;
const transportStatus = multiTransportServer ? multiTransportServer.getStatus() : null;
// Overall health status
const isHealthy = httpHealthy && sessionManagerHealthy && databaseHealthy && transportHealthy;
const healthData = {
status: isHealthy ? 'healthy' : 'unhealthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
version: process.env.npm_package_version || '1.0.0',
environment: config.server.nodeEnv,
modes: config.server.modes,
services: {
httpClient: {
healthy: httpHealthy,
status: httpStatus,
metrics: httpMetrics
},
sessionManager: {
healthy: sessionManagerHealthy,
metrics: sessionMetrics
},
database: {
healthy: databaseHealthy,
connection: databaseInfo
},
...(transportStatus && { transports: transportStatus })
},
system: {
nodeVersion: process.version,
platform: process.platform,
arch: process.arch,
memory: process.memoryUsage(),
pid: process.pid
},
responseTime: Date.now() - startTime
};
const statusCode = isHealthy ? 200 : 503;
res.status(statusCode).json(healthData);
}
catch (error) {
logger.error('Health check failed', { error });
res.status(503).json({
status: 'unhealthy',
timestamp: new Date().toISOString(),
error: 'Health check failed',
details: error instanceof Error ? error.message : String(error)
});
}
});
/**
* GET /health/transports
* Transport-specific health check for dual-mode server
*/
// router.get('/transports', (req: Request, res: Response) => {
// try {
// if (!multiTransportServer) {
// return res.status(503).json({
// healthy: false,
// error: 'Transport server not available',
// timestamp: new Date().toISOString()
// });
// }
// const isHealthy = multiTransportServer.isHealthy();
// const status = multiTransportServer.getStatus();
// return res.status(isHealthy ? 200 : 503).json({
// healthy: isHealthy,
// status,
// timestamp: new Date().toISOString()
// });
// } catch (error) {
// logger.error('Transport health check failed', { error });
// return res.status(503).json({
// healthy: false,
// error: 'Transport health check failed',
// details: error instanceof Error ? error.message : String(error),
// timestamp: new Date().toISOString()
// });
// }
// });
/**
* GET /health/mcp-connections
* MCP protocol specific connection health
*/
// router.get('/mcp-connections', (req: Request, res: Response) => {
// try {
// if (!multiTransportServer) {
// return res.status(503).json({
// healthy: false,
// error: 'Transport server not available',
// timestamp: new Date().toISOString()
// });
// }
// const status = multiTransportServer.getStatus();
// const mcpTransportStatus = status?.transports?.mcp;
// if (!mcpTransportStatus) {
// return res.status(404).json({
// healthy: false,
// error: 'MCP transport not enabled',
// timestamp: new Date().toISOString()
// });
// }
// return res.json({
// healthy: mcpTransportStatus.healthy,
// connectionCount: mcpTransportStatus.connections || 0,
// activeConnections: mcpTransportStatus.activeConnections || [],
// timestamp: new Date().toISOString()
// });
// } catch (error) {
// logger.error('MCP connections health check failed', { error });
// return res.status(503).json({
// healthy: false,
// error: 'MCP connections health check failed',
// details: error instanceof Error ? error.message : String(error),
// timestamp: new Date().toISOString()
// });
// }
// });
/**
* GET /health/mcp
* MCP service specific health check
*/
router.get('/http-client', async (req, res) => {
try {
const isHealthy = await httpClient.healthCheck();
const status = httpClient.getStatus();
const metrics = httpClient.getMetrics();
res.status(isHealthy ? 200 : 503).json({
healthy: isHealthy,
status,
metrics,
timestamp: new Date().toISOString()
});
}
catch (error) {
logger.error('HTTP client health check failed', { error });
res.status(503).json({
healthy: false,
error: 'HTTP client health check failed',
details: error instanceof Error ? error.message : String(error),
timestamp: new Date().toISOString()
});
}
});
/**
* GET /health/database
* Database specific health check
*/
router.get('/database', (req, res) => {
try {
const isHealthy = databaseService.isHealthy();
const connectionInfo = databaseService.getConnectionInfo();
res.status(isHealthy ? 200 : 503).json({
healthy: isHealthy,
connection: connectionInfo,
timestamp: new Date().toISOString()
});
}
catch (error) {
logger.error('Database health check failed', { error });
res.status(503).json({
healthy: false,
error: 'Database health check failed',
details: error instanceof Error ? error.message : String(error),
timestamp: new Date().toISOString()
});
}
});
/**
* GET /health/sessions
* Session manager health check
*/
router.get('/sessions', (req, res) => {
try {
const isHealthy = sessionManager.isRunning();
const metrics = sessionManager.getMetrics();
res.status(isHealthy ? 200 : 503).json({
healthy: isHealthy,
metrics,
timestamp: new Date().toISOString()
});
}
catch (error) {
logger.error('Session manager health check failed', { error });
res.status(503).json({
healthy: false,
error: 'Session manager health check failed',
details: error instanceof Error ? error.message : String(error),
timestamp: new Date().toISOString()
});
}
});
export default router;
//# sourceMappingURL=health.js.map