@goparrot/franchise-mcp-server
Version:
MCP Server for Franchise API
40 lines (39 loc) • 1.77 kB
JavaScript
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function validateServiceExists(serviceName, methodsMap) {
if (!methodsMap[serviceName]) {
const availableServices = JSON.stringify(Object.keys(methodsMap), null, 2);
throw new Error(`Invalid service: ${serviceName}. Available services: ${availableServices}`);
}
}
function validateMethodExists(serviceName, method, methodsMap) {
if (!methodsMap[serviceName][method]) {
const availableMethods = JSON.stringify(Object.keys(methodsMap[serviceName]), null, 2);
throw new Error(`Invalid method ${method} for service ${serviceName}. Available methods: ${availableMethods}`);
}
}
function validateWriteAccess(serviceName, method, methodInfo, disallowWrites) {
if (disallowWrites && methodInfo?.isWrite) {
throw new Error(`Write operations are not allowed in this environment. ` +
`Attempted operation: ${serviceName}.${method}`);
}
}
function validateHandlerExists(serviceName, method, handlersMap) {
if (!handlersMap[serviceName]?.[method]) {
throw new Error(`No handler found for ${serviceName}.${method}`);
}
}
export function handleTool(params, methodsMap, handlersMap, disallowWrites) {
const { service, method } = params;
const serviceName = capitalizeFirst(service);
validateServiceExists(serviceName, methodsMap);
validateMethodExists(serviceName, method, methodsMap);
validateWriteAccess(serviceName, method, methodsMap[serviceName][method], disallowWrites);
validateHandlerExists(serviceName, method, handlersMap);
return {
serviceName,
methodInfo: methodsMap[serviceName][method],
handler: handlersMap[serviceName][method],
};
}