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
This commit is contained in:
77
src/index.ts
77
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}`);
|
||||
});
|
||||
// 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');
|
||||
}
|
||||
Reference in New Issue
Block a user