UNPKG

@thorium-dev-group/x402-mcp-extension

Version:
105 lines 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerHandler = registerHandler; exports.getAllRegisteredHandlers = getAllRegisteredHandlers; exports.clearGlobalRegistry = clearGlobalRegistry; exports.getRegistryStats = getRegistryStats; exports.updateHandler = updateHandler; exports.discoverDecoratedHandlers = discoverDecoratedHandlers; const GLOBAL_REGISTRY_KEY = '__x402_mcp_globalHandlerRegistry__'; function getGlobalObject() { if (typeof globalThis !== 'undefined') return globalThis; if (typeof global !== 'undefined') return global; if (typeof globalThis.window !== 'undefined') return globalThis.window; throw new Error('Unable to determine global object for registry'); } function getGlobalRegistry() { const globalObj = getGlobalObject(); if (!globalObj[GLOBAL_REGISTRY_KEY]) { globalObj[GLOBAL_REGISTRY_KEY] = new Map(); } return globalObj[GLOBAL_REGISTRY_KEY]; } const globalHandlerRegistry = getGlobalRegistry(); const logger = { debug: (message, ...args) => { if (process.env.NODE_ENV === 'development') { console.log(`[Registry] ${message}`, ...args); } } }; function getHandlerKey(target, propertyKey) { const ctorName = target.constructor?.name || target.name || 'anonymous'; return `${ctorName}.${String(propertyKey)}`; } function registerHandler(handler) { const key = getHandlerKey(handler.target, handler.propertyKey); logger.debug('Registering handler', key); const existing = globalHandlerRegistry.get(key); if (existing) { globalHandlerRegistry.set(key, { ...existing, ...handler }); logger.debug('Updated existing handler', key); } else { globalHandlerRegistry.set(key, handler); logger.debug('Added new handler', key); } } function getAllRegisteredHandlers() { const handlers = Array.from(globalHandlerRegistry.values()); logger.debug('Getting all registered handlers', handlers.length); for (const handler of handlers) { logger.debug('Handler details', getHandlerKey(handler.target, handler.propertyKey)); } return handlers; } function clearGlobalRegistry() { globalHandlerRegistry.clear(); } function getRegistryStats() { return { size: globalHandlerRegistry.size, keys: Array.from(globalHandlerRegistry.keys()) }; } function updateHandler(target, propertyKey, update) { const key = getHandlerKey(target, propertyKey); const existing = globalHandlerRegistry.get(key); if (existing) { Object.assign(existing, update); globalHandlerRegistry.set(key, existing); } } function discoverDecoratedHandlers(modules) { const handlers = []; for (const module of modules) { if (typeof module === 'function' && module.prototype) { const prototype = module.prototype; const methods = Object.getOwnPropertyNames(prototype); for (const methodName of methods) { if (methodName === 'constructor') continue; const method = prototype[methodName]; if (typeof method === 'function') { const hasToolOptions = Reflect.hasMetadata('x402_mcp_protocol:mcp_tool', method); const hasPromptOptions = Reflect.hasMetadata('x402_mcp_protocol:mcp_prompt', method); const hasResourceOptions = Reflect.hasMetadata('x402_mcp_protocol:mcp_resource', method); if (hasToolOptions || hasPromptOptions || hasResourceOptions) { handlers.push({ target: prototype, propertyKey: methodName, toolOptions: hasToolOptions ? Reflect.getMetadata('x402_mcp_protocol:mcp_tool', method) : undefined, promptOptions: hasPromptOptions ? Reflect.getMetadata('x402_mcp_protocol:mcp_prompt', method) : undefined, resourceOptions: hasResourceOptions ? Reflect.getMetadata('x402_mcp_protocol:mcp_resource', method) : undefined, }); } } } } } return handlers; } //# sourceMappingURL=registry.js.map