survey-mcp-server
Version:
Survey management server handling survey creation, response collection, analysis, and reporting with database access for data management
69 lines • 2.42 kB
JavaScript
import { MongoDBClient } from "../utils/mongodb.js";
import { logger } from "../utils/logger.js";
import { ObjectId } from "mongodb";
// Define the available resources
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"
}
];
// Helper function to parse URI
function parseResourceURI(uri) {
const url = new URL(uri);
return {
scheme: url.protocol.slice(0, -1), // Remove the trailing ':'
authority: url.hostname,
path: url.pathname
};
}
// Resource handler function
export async function handleReadResource(uri) {
try {
const parsedURI = parseResourceURI(uri);
const resourceType = parsedURI.authority; // e.g., 'details'
const identifier = parsedURI.path.substring(1); // Remove leading '/', e.g., 'user_id'
if (parsedURI.scheme === "user" && resourceType === "details") {
const userDetails = await getUserDetails(identifier);
return JSON.stringify(userDetails, null, 2);
}
else {
throw new Error(`Resource not found for uri: ${uri}`);
}
}
catch (error) {
logger.error(`Error reading resource ${uri}:`, error);
throw error;
}
}
// Get user details from MongoDB
async function getUserDetails(userId) {
try {
const mongoClient = MongoDBClient.getInstance();
const collection = await mongoClient.getCollection("users", "default");
const query = { _id: new ObjectId(userId) };
logger.info(`Query: ${JSON.stringify(query)}`);
const projection = {
_id: 0,
firstName: 1,
lastName: 1,
email: 1,
phone: 1
};
logger.info(`Projection: ${JSON.stringify(projection)}`);
const result = await collection.findOne(query, { projection });
logger.info(`Result: ${JSON.stringify(result)}`);
if (result === null) {
return { error: "User not found" };
}
// Ensure the result is a plain JavaScript object
return result ? { ...result } : {};
}
catch (error) {
logger.error(`Error fetching user details for user ID ${userId}:`, error);
return { error: String(error) };
}
}
//# sourceMappingURL=index.js.map