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.

92 lines (91 loc) 2.91 kB
import { logger } from './logger.js'; /** * Request context utilities class */ export class RequestContextService { /** * Private constructor to enforce singleton pattern */ constructor() { this.config = {}; logger.debug('RequestContext service initialized'); } /** * Get the singleton RequestContextService instance * @returns RequestContextService instance */ static getInstance() { if (!RequestContextService.instance) { RequestContextService.instance = new RequestContextService(); } return RequestContextService.instance; } /** * Configure service settings * @param config New configuration * @returns Updated configuration */ configure(config) { this.config = { ...this.config, ...config }; logger.debug('RequestContext configuration updated'); return { ...this.config }; } /** * Get current configuration * @returns Current configuration */ getConfig() { return { ...this.config }; } /** * Create a request context with unique ID and timestamp * @param additionalContext Additional context properties * @returns Request context object */ createRequestContext(additionalContext = {}) { const requestId = crypto.randomUUID(); const timestamp = new Date().toISOString(); return { requestId, timestamp, ...additionalContext }; } /** * Generate a secure random string * @param length Length of the string * @param chars Character set to use * @returns Random string */ generateSecureRandomString(length = 32, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { const randomValues = new Uint8Array(length); crypto.getRandomValues(randomValues); let result = ''; for (let i = 0; i < length; i++) { result += chars[randomValues[i] % chars.length]; } return result; } } // Create and export singleton instance export const requestContextService = RequestContextService.getInstance(); // Export convenience functions that delegate to the singleton instance export const configureContext = (config) => { return requestContextService.configure(config); }; export const createRequestContext = (additionalContext = {}) => { return requestContextService.createRequestContext(additionalContext); }; export const generateSecureRandomString = (length = 32, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') => { return requestContextService.generateSecureRandomString(length, chars); }; // Export default utilities export default { requestContextService, configureContext, createRequestContext, generateSecureRandomString };