import { wrapSafeHandler } from './safeHandlerWrapper.mjs'; /** * A no-op registered capability used as fallback when SDK registration fails. * * This prevents one broken capability from aborting the entire registration loop. * The capability will appear as "disabled" and cannot be enabled. */ const FAILED_REGISTERED_CAPABILITY = Object.freeze({ enabled: false, enable () {}, disable () {}, remove () {} }); /** * Creates a safe capability registration that protects Strapi core from user callback errors * at three levels: * * - Level 1: Catch factory invocation errors (createHandler throws) * - Level 2: Catch runtime execution errors (handler throws during invocation) * - Level 3: Catch MCP SDK registration errors (SDK rejects the registration) * * This prevents one broken capability from: * - Aborting the entire registration loop * - Crashing the MCP server * - Leaking unhandled errors to the user */ const createSafeCapabilityRegistration = (config)=>{ const { strapi, capabilityType, name, createHandler, createFallbackHandler, createErrorResult, registerWithSdk } = config; try { // Level 1: Safe factory invocation — catch errors from user's createHandler let rawHandler; try { rawHandler = createHandler(strapi); } catch (error) { const message = error instanceof Error ? error.message : String(error); strapi.log.error(`[MCP] ${capabilityType} "${name}" handler factory threw during initialization: ${message}`); // Substitute a fallback handler that always returns an error to the MCP client rawHandler = createFallbackHandler(message); } // Level 2: Safe runtime wrapping — catch errors from user's handler during execution const safeHandler = wrapSafeHandler(rawHandler, { strapi, capabilityType, name, createErrorResult }); return registerWithSdk(safeHandler); } catch (error) { // Level 3: Catch MCP SDK registration errors — prevent one broken capability from aborting all others const message = error instanceof Error ? error.message : String(error); strapi.log.error(`[MCP] Failed to register ${capabilityType.toLowerCase()} "${name}" with MCP server: ${message}`); return FAILED_REGISTERED_CAPABILITY; } }; export { FAILED_REGISTERED_CAPABILITY, createSafeCapabilityRegistration }; //# sourceMappingURL=createSafeCapabilityRegistration.mjs.map