UNPKG

voyage-and-consumption-mcp-server

Version:

Voyage and consumption management server handling vessel position tracking, ETA monitoring, fuel consumption, lube oil consumption, fresh water production and weather data

306 lines 14.8 kB
// get_me_cylinder_oil_consumption_and_rob // get_vessel_mecc_history // meclo_historical_data // get_vessel_aecc_history import { logger } from "../../utils/index.js"; import { DatabaseManager } from "../../utils/index.js"; import { getDataLink, getArtifact } from "../../utils/index.js"; import { isValidImoForCompany } from "../../utils/imoUtils.js"; export class LubricantOilToolHandler { constructor() { this.databaseManager = new DatabaseManager(); } // async getMeCylinderOilConsumptionAndRob(args: { imo: string; session_id?: string }): Promise<ToolResponse> { // const { imo, session_id = "testing" } = args; // return fetchQADetailsAndCreateResponse( // imo, // 37, // "get_me_cylinder_oil_consumption_and_rob", // "ME cylinder oil consumption and ROB", // session_id // ); // } async handleGetVesselMeccHistory(args) { const { imo, start_date: startDate, end_date: endDate } = args; const toolName = "get_vessel_mecc_history"; if (!imo) { throw new Error("imo is required. Only the following properties are allowed: imo, start_date, end_date. Please check the tool schema for get_vessel_mecc_history for the allowed properties."); } // Validate IMO for company const companyName = process.env.COMPANY_NAME || ''; const finalDbName = process.env.GROUP_DETAILS_DB_NAME || process.env.FLEET_DISTRIBUTION_DB_NAME || ''; const finalMongoUri = process.env.GROUP_DETAILS_MONGO_URI || process.env.FLEET_DISTRIBUTION_MONGO_URI || ''; if (companyName && finalDbName && finalMongoUri && companyName !== "Synergy") { const isValid = await isValidImoForCompany(imo, companyName, finalDbName, finalMongoUri); if (!isValid) { return [{ type: "text", text: `Access denied: IMO ${imo} is not authorized for company ${companyName}`, title: "Access Denied" }]; } } logger.info("handleGetVesselMeccHistory called", args); try { // Parse IMO to number const parsedImo = parseInt(imo); // Build query with correct field names const query = { "IMO No": parsedImo }; // Add date filters if provided if (startDate || endDate) { query["Report Date"] = {}; if (startDate) { const start = new Date(startDate); query["Report Date"]["$gte"] = start; } if (endDate) { const end = new Date(endDate); query["Report Date"]["$lte"] = end; } } // Projection to get relevant MECC fields const projection = { "_id": 0, "Report Date": 1, "Vessel Name": 1, "data.Steaming time (HRS)": 1, "data.MECC consumption (LTRS)": 1, "data.ROB MECC": 1 }; // Initialize database and get collection await this.databaseManager.initializeDatabase(process.env.MONGODB_ETL_DEV_DATA_DB_NAME || process.env.mongodbEtlDevDataDbName || "", process.env.MONGODB_ETL_DEV_DATA_URI || process.env.mongodbEtlDevDataUri || ""); const db = this.databaseManager.getDb(); const collection = db.collection('common_consumption_log_api'); const documents = await collection.find(query, { projection }).toArray(); // Handle no data found case if (!documents || documents.length === 0) { const dateRange = startDate && endDate ? ` between ${startDate} and ${endDate}` : startDate ? ` from ${startDate}` : endDate ? ` until ${endDate}` : ''; return [{ type: "text", text: `No MECC consumption data found for vessel with IMO ${parsedImo}${dateRange}` }]; } // Get data link using syia-mcp-utils const dataLink = await getDataLink(documents); // Create artifact data using syia-mcp-utils const artifactData = await getArtifact(toolName, dataLink); // Return main data and artifact return [ { type: "text", text: JSON.stringify(documents, null, 2), title: `MECC Consumption History for IMO ${parsedImo}`, format: "json" }, { type: "text", text: JSON.stringify(artifactData, null, 2), title: `Artifact: ${toolName}`, format: "json" } ]; } catch (error) { logger.error(`Error fetching MECC history for IMO ${imo}:`, error); throw new Error(`Error fetching MECC history: ${error instanceof Error ? error.message : String(error)}`); } finally { await this.databaseManager.closeDatabase(); } } async handleMecloHistoricalData(args) { const { imo, start_date: startDate, end_date: endDate } = args; const toolName = "meclo_historical_data"; if (!imo) { throw new Error("imo is required. Only the following properties are allowed: imo, start_date, end_date. Please check the tool schema for meclo_historical_data for the allowed properties."); } // Validate IMO for company const companyName = process.env.COMPANY_NAME || ''; const finalDbName = process.env.GROUP_DETAILS_DB_NAME || process.env.FLEET_DISTRIBUTION_DB_NAME || ''; const finalMongoUri = process.env.GROUP_DETAILS_MONGO_URI || process.env.FLEET_DISTRIBUTION_MONGO_URI || ''; if (companyName && finalDbName && finalMongoUri && companyName !== "Synergy") { const isValid = await isValidImoForCompany(imo, companyName, finalDbName, finalMongoUri); if (!isValid) { return [{ type: "text", text: `Access denied: IMO ${imo} is not authorized for company ${companyName}`, title: "Access Denied" }]; } } logger.info("handleMecloHistoricalData called", args); try { // Parse IMO to number const parsedImo = parseInt(imo); // Build query with correct field names const query = { "IMO No": parsedImo }; // Add date filters if provided if (startDate || endDate) { query["Report Date"] = {}; if (startDate) { const start = new Date(startDate); query["Report Date"]["$gte"] = start; } if (endDate) { const end = new Date(endDate); query["Report Date"]["$lte"] = end; } } // Projection to get relevant ME cylinder oil fields const projection = { "_id": 0, "Report Date": 1, "Vessel Name": 1, "data.Steaming time (HRS)": 1, "data.ME cyl LS 40 BN ROB consumption (LTRS)": 1, "data.ME cyl 70/100 BN ROB/Cap consumption (LTRS)": 1, "data.MECYL 40BN consumption (MT)": 1, "data.MECYL 20/25 BN consumption (MT)": 1, "data.MECYL 70BN consumption (MT)": 1, "data.MECYL 100BN consumption (MT)": 1, "data.ROBMECYLCalculated": 1, "data.ROBMECYLLSCalculated": 1, "data.ROB MECYL 20/25 BN": 1, "data.ROB MECYL 40BN": 1, "data.ROB MECYL 70BN": 1, "data.ROB MECYL 100BN": 1 }; // Initialize database and get collection await this.databaseManager.initializeDatabase(process.env.MONGODB_ETL_DEV_DATA_DB_NAME || process.env.mongodbEtlDevDataDbName || "", process.env.MONGODB_ETL_DEV_DATA_URI || process.env.mongodbEtlDevDataUri || ""); const db = this.databaseManager.getDb(); const collection = db.collection('common_consumption_log_api'); const documents = await collection.find(query, { projection }).toArray(); // Handle no data found case if (!documents || documents.length === 0) { const dateRange = startDate && endDate ? ` between ${startDate} and ${endDate}` : startDate ? ` from ${startDate}` : endDate ? ` until ${endDate}` : ''; return [{ type: "text", text: `No ME cylinder oil consumption data found for vessel with IMO ${parsedImo}${dateRange}` }]; } // Get data link using syia-mcp-utils const dataLink = await getDataLink(documents); // Create artifact data using syia-mcp-utils const artifactData = await getArtifact(toolName, dataLink); // Return main data and artifact return [ { type: "text", text: JSON.stringify(documents, null, 2), title: `ME Cylinder Oil Consumption History for IMO ${parsedImo}`, format: "json" }, { type: "text", text: JSON.stringify(artifactData, null, 2), title: `Artifact: ${toolName}`, format: "json" } ]; } catch (error) { logger.error(`Error fetching ME cylinder oil history for IMO ${imo}:`, error); throw new Error(`Error fetching ME cylinder oil history: ${error instanceof Error ? error.message : String(error)}`); } finally { await this.databaseManager.closeDatabase(); } } async handleGetVesselAeccHistory(args) { const { imo, start_date: startDate, end_date: endDate } = args; const toolName = "get_vessel_aecc_history"; if (!imo) { throw new Error("imo is required. Only the following properties are allowed: imo, start_date, end_date. Please check the tool schema for get_vessel_aecc_history for the allowed properties."); } // Validate IMO for company const companyName = process.env.COMPANY_NAME || ''; const finalDbName = process.env.GROUP_DETAILS_DB_NAME || process.env.FLEET_DISTRIBUTION_DB_NAME || ''; const finalMongoUri = process.env.GROUP_DETAILS_MONGO_URI || process.env.FLEET_DISTRIBUTION_MONGO_URI || ''; if (companyName && finalDbName && finalMongoUri && companyName !== "Synergy") { const isValid = await isValidImoForCompany(imo, companyName, finalDbName, finalMongoUri); if (!isValid) { return [{ type: "text", text: `Access denied: IMO ${imo} is not authorized for company ${companyName}`, title: "Access Denied" }]; } } logger.info("handleGetVesselAeccHistory called", args); try { // Parse IMO to number const parsedImo = parseInt(imo); // Build query with correct field names const query = { "IMO No": parsedImo }; // Add date filters if provided if (startDate || endDate) { query["Report Date"] = {}; if (startDate) { const start = new Date(startDate); query["Report Date"]["$gte"] = start; } if (endDate) { const end = new Date(endDate); query["Report Date"]["$lte"] = end; } } // Projection to get relevant AECC and MECC fields const projection = { "_id": 0, "Report Date": 1, "Vessel Name": 1, "data.Steaming time (HRS)": 1, "data.AECC consumption (LTRS)": 1, "data.MECC consumption (LTRS)": 1, "data.ROB AECC": 1, "data.ROB MECC": 1 }; // Initialize database and get collection await this.databaseManager.initializeDatabase(process.env.MONGODB_ETL_DEV_DATA_DB_NAME || process.env.mongodbEtlDevDataDbName || "", process.env.MONGODB_ETL_DEV_DATA_URI || process.env.mongodbEtlDevDataUri || ""); const db = this.databaseManager.getDb(); const collection = db.collection('common_consumption_log_api'); const documents = await collection.find(query, { projection }).toArray(); // Handle no data found case if (!documents || documents.length === 0) { const dateRange = startDate && endDate ? ` between ${startDate} and ${endDate}` : startDate ? ` from ${startDate}` : endDate ? ` until ${endDate}` : ''; return [{ type: "text", text: `No AECC/MECC consumption data found for vessel with IMO ${parsedImo}${dateRange}` }]; } // Get data link using syia-mcp-utils const dataLink = await getDataLink(documents); // Create artifact data using syia-mcp-utils const artifactData = await getArtifact(toolName, dataLink); // Return main data and artifact return [ { type: "text", text: JSON.stringify(documents, null, 2), title: `AECC/MECC Consumption History for IMO ${parsedImo}`, format: "json" }, { type: "text", text: JSON.stringify(artifactData, null, 2), title: `Artifact: ${toolName}`, format: "json" } ]; } catch (error) { logger.error(`Error fetching AECC/MECC history for IMO ${imo}:`, error); throw new Error(`Error fetching AECC/MECC history: ${error instanceof Error ? error.message : String(error)}`); } finally { await this.databaseManager.closeDatabase(); } } } //# sourceMappingURL=lubricantOilTools.js.map