From 4e89e5458cccfba8143a9894f024412bb1566b0e Mon Sep 17 00:00:00 2001 From: jango-blockchained Date: Mon, 3 Feb 2025 16:46:22 +0100 Subject: [PATCH] feat: add Claude mode support with conditional Express server initialization - Modify index.ts to conditionally start Express server based on PROCESSOR_TYPE - Add logic to skip server initialization when in Claude mode - Update file comments to reflect new conditional server startup behavior - Log informative message when running in Claude mode --- src/index.ts | 77 ++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/index.ts b/src/index.ts index ee126f6..eb5976c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,8 @@ * Home Assistant MCP (Master Control Program) * Main application entry point * - * This file initializes the Express server and sets up all necessary - * middleware and routes for the application. + * This file initializes the Express server and sets up necessary + * middleware and routes for the application when not in Claude mode. * * @module index */ @@ -21,48 +21,53 @@ import { initLogRotation } from './utils/log-rotation.js'; logger.info('Starting Home Assistant MCP...'); logger.info('Initializing Home Assistant connection...'); -/** - * Initialize Express application with security middleware - * and route handlers - */ -const app = express(); - // Initialize log rotation initLogRotation(); -// Apply logging middleware first to catch all requests -app.use(requestLogger); - -// Apply security middleware -app.use(securityHeaders); -app.use(rateLimiter); -app.use(express.json()); -app.use(validateRequest); -app.use(sanitizeInput); - /** * Initialize LiteMCP instance * This provides the core MCP functionality */ const server = new LiteMCP('home-assistant', APP_CONFIG.VERSION); -/** - * Mount API routes under /api - * All API endpoints are prefixed with /api - */ -app.use('/api', apiRoutes); +// Only start Express server when not in Claude mode +if (process.env.PROCESSOR_TYPE !== 'claude') { + /** + * Initialize Express application with security middleware + * and route handlers + */ + const app = express(); -/** - * Apply error handling middleware - * This should be the last middleware in the chain - */ -app.use(errorLogger); -app.use(errorHandler); + // Apply logging middleware first to catch all requests + app.use(requestLogger); -/** - * Start the server and listen for incoming connections - * The port is configured in the environment variables - */ -app.listen(APP_CONFIG.PORT, () => { - logger.info(`Server is running on port ${APP_CONFIG.PORT}`); -}); \ No newline at end of file + // Apply security middleware + app.use(securityHeaders); + app.use(rateLimiter); + app.use(express.json()); + app.use(validateRequest); + app.use(sanitizeInput); + + /** + * Mount API routes under /api + * All API endpoints are prefixed with /api + */ + app.use('/api', apiRoutes); + + /** + * Apply error handling middleware + * This should be the last middleware in the chain + */ + app.use(errorLogger); + app.use(errorHandler); + + /** + * Start the server and listen for incoming connections + * The port is configured in the environment variables + */ + app.listen(APP_CONFIG.PORT, () => { + logger.info(`Server is running on port ${APP_CONFIG.PORT}`); + }); +} else { + logger.info('Running in Claude mode - Express server disabled'); +} \ No newline at end of file