UNPKG

navigation-equipment-manuals-mcp-server

Version:

Navigation Equipment Manuals MCP Server for create, update, delete, and search navigation equipment manuals

82 lines 2.24 kB
import { MongoClient } from 'mongodb'; import { logger } from './api.js'; let client; let db; /** * Connect to MongoDB using the provided URI */ export async function connectToMongoDB(uri) { try { logger.log('Attempting to connect to MongoDB...'); client = new MongoClient(uri); await client.connect(); // Get database name from URI or use default const dbName = extractDbNameFromUri(uri) || 'navigation_manuals'; db = client.db(dbName); // Test the connection await db.admin().ping(); logger.log(`Successfully connected to MongoDB database: ${dbName}`); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown MongoDB connection error'; logger.log(`Failed to connect to MongoDB: ${errorMessage}`); throw new Error(`MongoDB connection failed: ${errorMessage}`); } } /** * Get the database instance */ export function getDb() { if (!db) { throw new Error('Database not initialized. Call connectToMongoDB first.'); } return db; } /** * Get the MongoDB client instance */ export function getClient() { if (!client) { throw new Error('MongoDB client not initialized. Call connectToMongoDB first.'); } return client; } /** * Close the MongoDB connection */ export async function closeMongoDB() { if (client) { await client.close(); logger.log('MongoDB connection closed'); } } /** * Extract database name from MongoDB URI */ function extractDbNameFromUri(uri) { try { // Match pattern like mongodb://.../{dbname} or mongodb+srv://.../{dbname} const match = uri.match(/\/([^/?]+)(\?|$)/); return match ? match[1] : null; } catch (error) { logger.log(`Error extracting database name from URI: ${error}`); return null; } } /** * Check if MongoDB connection is healthy */ export async function checkMongoDBHealth() { try { if (!db) return false; await db.admin().ping(); return true; } catch (error) { logger.log(`MongoDB health check failed: ${error}`); return false; } } //# sourceMappingURL=mongodb.js.map