UNPKG

ntfy-mcp-server

Version:

An MCP (Model Context Protocol) server designed to interact with the ntfy push notification service. It enables LLMs and AI agents to send notifications to your devices with extensive customization options.

64 lines (63 loc) 2.85 kB
import { BaseErrorCode, McpError } from "../../types-global/errors.js"; import { ErrorHandler } from "../../utils/errorHandler.js"; import { logger } from "../../utils/logger.js"; /** * Helper for consistent registration pattern with proper error handling * @param server MCP server instance * @param options Registration options * @param registerFn Function that performs the actual registration * @returns Promise resolving when registration is complete */ export async function registerComponent(server, options, registerFn) { // Create a component-specific logger const componentLogger = logger.createChildLogger({ module: `${options.type}Registration`, componentName: options.name, ...options.loggerContext }); componentLogger.info(`Registering ${options.type}: ${options.name}`); // Use ErrorHandler.tryCatch for consistent error handling return await ErrorHandler.tryCatch(async () => { // Call the registration function await registerFn(server, componentLogger); componentLogger.info(`${options.type} registered successfully: ${options.name}`); }, { operation: `registering ${options.type}`, // Provide context for better error tracking context: { componentType: options.type, componentName: options.name }, // Use a specific error code for registration failures errorCode: BaseErrorCode.INTERNAL_ERROR, // Custom error mapper for clearer error messages errorMapper: (error) => new McpError(error instanceof McpError ? error.code : BaseErrorCode.INTERNAL_ERROR, `Failed to register ${options.type} '${options.name}': ${error instanceof Error ? error.message : 'Unknown error'}`, { componentType: options.type, componentName: options.name }), // Registration errors are considered critical critical: true }); } /** * Register a tool with the MCP server using a consistent pattern * @param server MCP server instance * @param options Tool registration options * @param handlerFn Function that sets up the tool handler * @returns Promise resolving when registration is complete */ export async function registerTool(server, options, handlerFn) { return registerComponent(server, { ...options, type: 'tool' }, handlerFn); } /** * Register a resource with the MCP server using a consistent pattern * @param server MCP server instance * @param options Resource registration options * @param handlerFn Function that sets up the resource handler * @returns Promise resolving when registration is complete */ export async function registerResource(server, options, handlerFn) { return registerComponent(server, { ...options, type: 'resource' }, handlerFn); } export default { registerComponent, registerTool, registerResource };