tryaii-mcp-server
Version:
TryAII MCP Server - 15+ AI models with comparison, cost tracking, and collective intelligence
138 lines • 5.66 kB
JavaScript
import mongoose from 'mongoose';
import { EventEmitter } from 'events';
import { logger } from '../utils/logger.js';
import { config } from '../utils/config.js';
export class DatabaseService extends EventEmitter {
static instance;
mainConnection = null;
mcpConnection = null;
isMainConnected = false;
isMcpConnected = false;
lastHealthCheck = null;
constructor() {
super();
}
static getInstance() {
if (!DatabaseService.instance) {
DatabaseService.instance = new DatabaseService();
}
return DatabaseService.instance;
}
async connect() {
try {
// Connect to main database (existing)
logger.info('Connecting to main database...', { uri: this.sanitizeUri(config.database.mainUri) });
this.mainConnection = await mongoose.createConnection(config.database.mainUri, config.database.options);
this.isMainConnected = true;
logger.info('Successfully connected to main database', {
dbName: this.mainConnection.name,
host: this.mainConnection.host
});
// For MCP database, reuse main connection if URIs are the same
if (config.database.mcpUri === config.database.mainUri) {
logger.info('Using main database connection for MCP (same URI)');
this.mcpConnection = this.mainConnection;
this.isMcpConnected = true;
logger.info('✅ Database connections initialized (main + MCP)');
}
else {
try {
// Connect to separate MCP database
logger.info('Connecting to MCP database...', { uri: this.sanitizeUri(config.database.mcpUri) });
this.mcpConnection = await mongoose.createConnection(config.database.mcpUri, config.database.options);
this.isMcpConnected = true;
logger.info('Successfully connected to MCP database');
}
catch (mcpError) {
logger.warn('Failed to connect to separate MCP database, using main database', { error: mcpError });
this.mcpConnection = this.mainConnection;
this.isMcpConnected = true;
}
}
this.lastHealthCheck = new Date();
this.emit('connected');
// Set up connection event handlers for main connection
this.mainConnection.on('error', (error) => {
logger.error('Main database connection error', error);
this.isMainConnected = false;
this.emit('error', error);
});
this.mainConnection.on('disconnected', () => {
logger.warn('Main database disconnected');
this.isMainConnected = false;
this.emit('disconnected');
});
// Set up connection event handlers for MCP connection (only if different from main)
if (this.mcpConnection !== this.mainConnection) {
this.mcpConnection.on('error', (error) => {
logger.error('MCP database connection error', error);
this.isMcpConnected = false;
this.emit('error', error);
});
this.mcpConnection.on('disconnected', () => {
logger.warn('MCP database disconnected');
this.isMcpConnected = false;
this.emit('disconnected');
});
}
}
catch (error) {
this.isMainConnected = false;
this.isMcpConnected = false;
logger.error('Failed to connect to databases', { error });
throw error;
}
}
async disconnect() {
if (this.mainConnection) {
await this.mainConnection.close();
this.isMainConnected = false;
}
if (this.mcpConnection && this.mcpConnection !== this.mainConnection) {
await this.mcpConnection.close();
}
this.isMcpConnected = false;
this.lastHealthCheck = null;
logger.info('Disconnected from all databases');
}
isHealthy() {
return this.isMainConnected && this.isMcpConnected;
}
getMainConnection() {
if (!this.mainConnection || !this.isMainConnected) {
throw new Error('Main database not connected');
}
return this.mainConnection;
}
getMcpConnection() {
if (!this.mcpConnection || !this.isMcpConnected) {
throw new Error('MCP database not connected');
}
return this.mcpConnection;
}
getConnectionInfo() {
return {
main: {
isConnected: this.isMainConnected,
readyState: this.mainConnection?.readyState,
host: this.mainConnection?.host,
port: this.mainConnection?.port,
name: this.mainConnection?.name
},
mcp: {
isConnected: this.isMcpConnected,
readyState: this.mcpConnection?.readyState,
host: this.mcpConnection?.host,
port: this.mcpConnection?.port,
name: this.mcpConnection?.name
}
};
}
sanitizeUri(uri) {
// Remove credentials from URI for logging
return uri.replace(/\/\/([^:]+):([^@]+)@/, '//***:***@');
}
}
// Create singleton instance
export const databaseService = DatabaseService.getInstance();
//# sourceMappingURL=database.js.map