UNPKG

navigation-equipment-manuals-mcp-server

Version:

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

142 lines 4.03 kB
import fs from 'fs'; import path from 'path'; import os from 'os'; /** * Simple logger utility for debugging and tracking operations */ export const logger = { /** * Log a message to both console and file */ log: (message) => { const timestamp = new Date().toISOString(); const logMessage = `${timestamp} - ${message}`; // Log to console (stderr to avoid interfering with MCP protocol on stdout) console.error(logMessage); // Also log to file for debugging try { const logFile = path.join(os.tmpdir(), 'navigation-equipment-manuals-debug.log'); fs.appendFileSync(logFile, logMessage + '\n'); } catch (error) { // Silently fail if we can't write to the log file } }, /** * Log an error message */ error: (message, error) => { const errorMessage = error ? `${message}: ${error.message}` : message; logger.log(`ERROR - ${errorMessage}`); if (error && error.stack) { logger.log(`STACK - ${error.stack}`); } }, /** * Log a warning message */ warn: (message) => { logger.log(`WARN - ${message}`); }, /** * Log an info message */ info: (message) => { logger.log(`INFO - ${message}`); }, /** * Log a debug message (only in debug mode) */ debug: (message) => { if (process.env.DEBUG === 'true' || process.argv.includes('--debug')) { logger.log(`DEBUG - ${message}`); } } }; /** * Utility function to validate MongoDB ObjectId format */ export function isValidObjectId(id) { return /^[0-9a-fA-F]{24}$/.test(id); } /** * Utility function to sanitize search queries */ export function sanitizeSearchQuery(query) { if (!query || typeof query !== 'string') { return ''; } // Remove potentially dangerous characters and limit length return query .replace(/[<>'"&]/g, '') // Remove HTML/XML dangerous chars .replace(/[{}[\]]/g, '') // Remove JSON dangerous chars .substring(0, 1000) // Limit length .trim(); } /** * Utility function to validate equipment ID */ export function validateEquipmentId(id) { if (!id) return false; if (typeof id === 'string') { return id.trim().length > 0; } if (typeof id === 'number') { return Number.isInteger(id) && id > 0; } return false; } /** * Utility function to normalize manual type */ export function normalizeManualType(type) { if (!type || typeof type !== 'string') { return 'general'; } const normalized = type.toLowerCase().trim(); const validTypes = ['operation', 'maintenance', 'installation', 'service', 'troubleshooting', 'manual', 'general']; if (validTypes.includes(normalized)) { return normalized; } return 'general'; } /** * Utility function to normalize equipment category */ export function normalizeCategory(category) { if (!category || typeof category !== 'string') { return 'general'; } const normalized = category.toLowerCase().trim(); const validCategories = ['radar', 'gps', 'compass', 'echosounder', 'ais', 'vhf', 'ecdis', 'autopilot', 'general']; if (validCategories.includes(normalized)) { return normalized; } return 'general'; } /** * Utility function to format results for MCP response */ export function formatMcpResponse(data, message) { return { content: [ { type: "text", text: message || JSON.stringify(data, null, 2) } ] }; } /** * Utility function to handle pagination parameters */ export function getPaginationParams(limit, offset) { const normalizedLimit = Math.min(Math.max(limit || 20, 1), 100); const normalizedOffset = Math.max(offset || 0, 0); return { limit: normalizedLimit, offset: normalizedOffset }; } //# sourceMappingURL=api.js.map