mcp-activity-tracker
Version:
MCP server for automatic worklog creation based on computer activity monitoring
116 lines (93 loc) ⢠3.05 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
function findBestPython() {
return new Promise((resolve) => {
const pythonVersions = ['python3.11', 'python3.10', 'python3.9', 'python3'];
function tryNext(index) {
if (index >= pythonVersions.length) {
resolve(null);
return;
}
const python = spawn(pythonVersions[index], ['--version']);
python.on('close', (code) => {
if (code === 0) {
resolve(pythonVersions[index]);
} else {
tryNext(index + 1);
}
});
python.on('error', () => {
tryNext(index + 1);
});
}
tryNext(0);
});
}
function installPythonDeps(pythonCmd) {
return new Promise((resolve, reject) => {
const requirementsPath = path.join(__dirname, '..', 'requirements.txt');
console.log('š¦ Installing Python dependencies...');
const pip = spawn(pythonCmd, ['-m', 'pip', 'install', '-r', requirementsPath], {
stdio: 'inherit'
});
pip.on('close', (code) => {
if (code === 0) {
console.log('ā
Python dependencies installed successfully');
resolve();
} else {
reject(new Error(`pip install failed with code ${code}`));
}
});
});
}
async function main() {
console.log('š Starting MCP Activity Tracker...');
// Find best Python version
const pythonCmd = await findBestPython();
if (!pythonCmd) {
console.error('ā Python 3.9+ is required but not found');
console.error('Please install Python 3.9 or higher and try again');
process.exit(1);
}
console.log(`ā
Found Python: ${pythonCmd}`);
// Install Python dependencies
try {
await installPythonDeps(pythonCmd);
} catch (error) {
console.error('ā Failed to install Python dependencies:', error.message);
console.error('Please run: pip install fastmcp psutil pynput plyer browser-history watchdog');
process.exit(1);
}
// Path to the Python script
const scriptPath = path.join(__dirname, '..', 'src', 'mcp_activity_tracker.py');
if (!fs.existsSync(scriptPath)) {
console.error('ā MCP server script not found:', scriptPath);
process.exit(1);
}
console.log('š” Starting MCP Activity Tracker server...');
// Start the Python MCP server
const mcpServer = spawn(pythonCmd, [scriptPath], {
stdio: 'inherit'
});
mcpServer.on('close', (code) => {
if (code !== 0) {
console.error(`ā MCP server exited with code ${code}`);
process.exit(code);
}
});
mcpServer.on('error', (err) => {
console.error('ā Failed to start MCP server:', err.message);
process.exit(1);
});
// Handle process termination
process.on('SIGINT', () => {
console.log('\nš Shutting down MCP Activity Tracker...');
mcpServer.kill('SIGINT');
});
process.on('SIGTERM', () => {
mcpServer.kill('SIGTERM');
});
}
main().catch(console.error);