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:
57
src/index.ts
57
src/index.ts
@@ -2,8 +2,8 @@
|
|||||||
* Home Assistant MCP (Master Control Program)
|
* Home Assistant MCP (Master Control Program)
|
||||||
* Main application entry point
|
* Main application entry point
|
||||||
*
|
*
|
||||||
* This file initializes the Express server and sets up all necessary
|
* This file initializes the Express server and sets up necessary
|
||||||
* middleware and routes for the application.
|
* middleware and routes for the application when not in Claude mode.
|
||||||
*
|
*
|
||||||
* @module index
|
* @module index
|
||||||
*/
|
*/
|
||||||
@@ -21,48 +21,53 @@ import { initLogRotation } from './utils/log-rotation.js';
|
|||||||
logger.info('Starting Home Assistant MCP...');
|
logger.info('Starting Home Assistant MCP...');
|
||||||
logger.info('Initializing Home Assistant connection...');
|
logger.info('Initializing Home Assistant connection...');
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize Express application with security middleware
|
|
||||||
* and route handlers
|
|
||||||
*/
|
|
||||||
const app = express();
|
|
||||||
|
|
||||||
// Initialize log rotation
|
// Initialize log rotation
|
||||||
initLogRotation();
|
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
|
* Initialize LiteMCP instance
|
||||||
* This provides the core MCP functionality
|
* This provides the core MCP functionality
|
||||||
*/
|
*/
|
||||||
const server = new LiteMCP('home-assistant', APP_CONFIG.VERSION);
|
const server = new LiteMCP('home-assistant', APP_CONFIG.VERSION);
|
||||||
|
|
||||||
/**
|
// 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 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);
|
||||||
|
|
||||||
|
/**
|
||||||
* Mount API routes under /api
|
* Mount API routes under /api
|
||||||
* All API endpoints are prefixed with /api
|
* All API endpoints are prefixed with /api
|
||||||
*/
|
*/
|
||||||
app.use('/api', apiRoutes);
|
app.use('/api', apiRoutes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply error handling middleware
|
* Apply error handling middleware
|
||||||
* This should be the last middleware in the chain
|
* This should be the last middleware in the chain
|
||||||
*/
|
*/
|
||||||
app.use(errorLogger);
|
app.use(errorLogger);
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the server and listen for incoming connections
|
* Start the server and listen for incoming connections
|
||||||
* The port is configured in the environment variables
|
* The port is configured in the environment variables
|
||||||
*/
|
*/
|
||||||
app.listen(APP_CONFIG.PORT, () => {
|
app.listen(APP_CONFIG.PORT, () => {
|
||||||
logger.info(`Server is running on port ${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