UNPKG

crew-management-mcp-server

Version:

Crew management server handling crew records, certifications, scheduling, payroll, and vessel assignments with ERP access for data extraction

140 lines 4.48 kB
/** * Resources for Crew Management MCP Server * Converted from Python to TypeScript */ import { MongoClient } from "mongodb"; import { config } from "../utils/config.js"; import { logger } from "../utils/logger.js"; // MongoDB client setup (matching the Python implementation) const client = new MongoClient(config.mongoUri); export const resourceList = [ { uri: "user://details/<user_id>", name: "User Details", description: "Details about the user based on the given user id", mimeType: "application/json" } ]; export async function readResource(uri) { try { const url = new URL(uri); const resourceType = url.hostname; const identifier = url.pathname.substring(1); // Remove leading slash if (url.protocol === "imo:") { if (resourceType === "overview") { return JSON.stringify(await getOverview(identifier), null, 2); } else if (resourceType === "pms_summary") { return JSON.stringify(await getSummary(identifier), null, 2); } else if (resourceType === "status") { return JSON.stringify(await getStatus(identifier), null, 2); } } else if (url.protocol === "user:" && resourceType === "details") { return JSON.stringify(await getUserDetails(identifier), null, 2); } return `Resource not found for uri: ${uri}`; } catch (error) { logger.error(`Error reading resource ${uri}:`, error); return JSON.stringify({ error: `Failed to read resource: ${error}` }, null, 2); } } async function getOverview(imo) { try { await client.connect(); const db = client.db(config.mongoDbName); const collection = db.collection("vesselinfos"); const query = { imo: parseInt(imo), questionNo: 114 }; const projection = { _id: 0, answer: 1, vesselName: 1 }; const result = await collection.findOne(query, { projection }); return result; } catch (error) { logger.error(`Error getting overview for IMO ${imo}:`, error); return { error: String(error) }; } } async function getSummary(imo) { try { await client.connect(); const db = client.db(config.mongoDbName); const collection = db.collection("vesselinfos"); const query = { imo: parseInt(imo), questionNo: 116 }; const projection = { _id: 0, answer: 1, vesselName: 1 }; const result = await collection.findOne(query, { projection }); return result; } catch (error) { logger.error(`Error getting summary for IMO ${imo}:`, error); return { error: String(error) }; } } async function getStatus(imo) { try { await client.connect(); const db = client.db(config.mongoDbName); const collection = db.collection("vesselinfos"); const query = { imo: parseInt(imo), questionNo: 136 }; const projection = { _id: 0, answer: 1, vesselName: 1 }; const result = await collection.findOne(query, { projection }); return result; } catch (error) { logger.error(`Error getting status for IMO ${imo}:`, error); return { error: String(error) }; } } async function getUserDetails(userId) { try { await client.connect(); const db = client.db(config.mongoDbName); const collection = db.collection("users"); // Convert string to ObjectId if it's a valid ObjectId format let query; try { const { ObjectId } = await import("mongodb"); query = { _id: new ObjectId(userId) }; } catch { // If ObjectId conversion fails, try with string query = { _id: userId }; } const projection = { _id: 0, firstName: 1, lastName: 1, email: 1, phone: 1 }; const result = await collection.findOne(query, { projection }); return result; } catch (error) { logger.error(`Error getting user details for ID ${userId}:`, error); return { error: String(error) }; } } //# sourceMappingURL=index.js.map