defect-inspection-tools-mcp-server
Version:
Defect inspection tools server handling defect detection, analysis, and reporting with AI/ML capabilities for quality control
85 lines • 3.04 kB
JavaScript
import { logger } from "../utils/logger.js";
import { getMongoClient } from "../utils/mongodb.js";
import { config } from "../utils/config.js";
import { ObjectId } from "mongodb";
export class ResourceHandler {
constructor(server) {
this.server = server;
}
// List of available resources
getResourceList() {
return [
{
uri: "user://details/<user_id>",
name: "User Details",
description: "Details about the user based on the given user id",
mimeType: "application/json"
}
];
}
async handleReadResource(uri) {
try {
logger.info("Reading resource", { uri });
const parsedUri = new URL(uri);
const resourceType = parsedUri.hostname; // e.g., 'details'
const identifier = parsedUri.pathname.substring(1); // remove leading '/'
if (parsedUri.protocol === "user:" && resourceType === "details") {
const userDetails = await this.getUserDetails(identifier);
return {
contents: [
{
uri: uri,
text: JSON.stringify(userDetails, null, 2),
mimeType: "application/json"
}
]
};
}
else {
throw new Error(`Resource not found for uri: ${uri}`);
}
}
catch (error) {
logger.error(`Error reading resource ${uri}:`, error);
return {
contents: [
{
uri: uri,
text: JSON.stringify({ error: `Resource not found for uri: ${uri}` }, null, 2),
mimeType: "application/json"
}
]
};
}
}
async getUserDetails(userId) {
try {
logger.info("Getting user details", { userId });
// Validate ObjectId format
if (!ObjectId.isValid(userId)) {
return { error: "Invalid user ID format" };
}
const client = await getMongoClient(config.mongoUri);
const db = client.db(config.dbName);
const collection = db.collection("users");
const query = { "_id": new ObjectId(userId) };
const projection = {
"_id": 0,
"firstName": 1,
"lastName": 1,
"email": 1,
"phone": 1
};
const result = await collection.findOne(query, { projection });
if (!result) {
return { error: "User not found" };
}
return result;
}
catch (error) {
logger.error("Error getting user details:", error);
return { error: `Failed to get user details: ${error}` };
}
}
}
//# sourceMappingURL=index.js.map