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
118 lines • 5.48 kB
JavaScript
// get_fresh_water_status
// get_vessel_fresh_water_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 FreshWaterToolHandler {
constructor() {
this.databaseManager = new DatabaseManager();
}
// async getFreshWaterStatus(args: { imo: string; session_id?: string }): Promise<ToolResponse> {
// const { imo, session_id = "testing" } = args;
// return fetchQADetailsAndCreateResponse(
// imo,
// 41,
// "get_fresh_water_status",
// "fresh water status",
// session_id,
// config.vesselInfoDbName,
// config.vesselInfoMongoUri
// );
// }
async handleGetVesselFreshWaterHistory(args) {
const { imo, start_date: startDate, end_date: endDate } = args;
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_fresh_water_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"
}];
}
}
const toolName = "get_vessel_fresh_water_history";
logger.info("handleGetVesselFreshWaterHistory 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 fresh water fields
const projection = {
"_id": 0,
"Report Date": 1,
"Vessel Name": 1,
"Event": 1,
"EventType": 1,
"data.Steaming time (HRS)": 1,
"data.FW production (MT)": 1,
"data.FW consumption (MT)": 1,
"data.ROB FW": 1,
"data.freshWaterConsumption": 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 fresh water 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: `Fresh Water 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 fresh water history for IMO ${imo}:`, error);
throw new Error(`Error fetching fresh water history: ${error instanceof Error ? error.message : String(error)}`);
}
finally {
await this.databaseManager.closeDatabase();
}
}
}
//# sourceMappingURL=freshWaterTools.js.map