check_connection
Verify MongoDB database connectivity and health status to ensure the connection is active and operational.
Instructions
Check if MongoDB connection is active and healthy
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.js:266-315 (handler)The handler function for the 'check_connection' tool. It checks the MongoDB connection status, performs a ping and retrieves server status with timeouts, and returns a text content response indicating connection health or errors.async checkConnection() { const timeout = (ms) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation timed out')), ms) ); try { if (!this.mongoClient || !this.db || !this.isConnected) { return { content: [ { type: "text", text: `MongoDB not available\nTarget URI: ${MONGODB_URI}\nTarget Database: ${DATABASE_NAME}\nStatus: Disconnected - MCP server is running without database connection`, }, ], }; } // Apply 10-second timeout to ping and serverStatus operations await Promise.race([ this.db.admin().ping(), timeout(10000) ]); const serverStatus = await Promise.race([ this.db.admin().serverStatus(), timeout(10000) ]); return { content: [ { type: "text", text: `MongoDB connection is healthy\nConnected to: ${MONGODB_URI}\nDatabase: ${DATABASE_NAME}\nServer version: ${serverStatus.version}\nUptime: ${serverStatus.uptime} seconds`, }, ], }; } catch (error) { this.isConnected = false; this.mongoClient = null; this.db = null; return { content: [ { type: "text", text: `MongoDB connection lost: ${error.message}\nTarget URI: ${MONGODB_URI}\nTarget Database: ${DATABASE_NAME}\nStatus: MCP server continues running without database connection`, }, ], }; } }
- server.js:121-129 (registration)Registration of the 'check_connection' tool in the ListTools response, including its name, description, and empty input schema.{ name: "check_connection", description: "Check if MongoDB connection is active and healthy", inputSchema: { type: "object", properties: {}, required: [], }, },
- server.js:124-128 (schema)Input schema for the 'check_connection' tool, which requires no parameters (empty object).inputSchema: { type: "object", properties: {}, required: [], },
- server.js:147-149 (handler)Dispatch case in the generic CallToolRequestHandler that routes 'check_connection' calls to the specific handler method.case "check_connection": return await this.checkConnection();