web-parser-mcp
Version:
🚀 MCP SERVER FIXED v3.7.9! Resolved import errors, middleware conflicts, type hints - NOW WORKING PERFECTLY!
107 lines (87 loc) • 3.13 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
function findPython() {
// Try different Python command variations
const pythonCommands = ['python3', 'python', 'py'];
for (const cmd of pythonCommands) {
try {
const result = require('child_process').execSync(`${cmd} --version`, { stdio: 'pipe' });
if (result.toString().includes('Python 3.')) {
return cmd;
}
} catch (e) {
// Command not found, try next
}
}
console.error('Error: Python 3.x not found. Please install Python 3.x and ensure it\'s in your PATH.');
process.exit(1);
}
function checkUvInstalled() {
try {
require('child_process').execSync('uv --version', { stdio: 'pipe' });
return true;
} catch (e) {
return false;
}
}
function main() {
// Find the main.py file relative to this script
const scriptDir = path.dirname(__filename);
const projectDir = path.dirname(scriptDir);
const mainPyPath = path.join(projectDir, 'main.py');
if (!fs.existsSync(mainPyPath)) {
console.error(`Error: main.py not found at ${mainPyPath}`);
process.exit(1);
}
// Check if uv is available for better dependency management
const hasUv = checkUvInstalled();
let pythonCmd;
let args;
if (hasUv) {
// Use uv to run the script with proper dependencies
pythonCmd = 'uv';
args = ['run', mainPyPath];
} else {
// Fallback to regular Python
pythonCmd = findPython();
args = [mainPyPath];
console.warn('Warning: uv not found. Using system Python. Consider installing uv for better dependency management:');
console.warn(' pip install uv');
console.warn(' or visit: https://docs.astral.sh/uv/getting-started/installation/');
}
// Start the MCP server
console.log('Starting Web Parser MCP Server...');
console.log(`Using command: ${pythonCmd} ${args.join(' ')}`);
const child = spawn(pythonCmd, args, {
cwd: projectDir,
stdio: 'inherit',
shell: process.platform === 'win32'
});
child.on('error', (err) => {
console.error('Failed to start server:', err.message);
process.exit(1);
});
child.on('exit', (code, signal) => {
if (signal) {
console.log(`Server terminated by signal: ${signal}`);
} else {
console.log(`Server exited with code: ${code}`);
}
process.exit(code || 0);
});
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('Shutting down server...');
child.kill('SIGINT');
});
process.on('SIGTERM', () => {
console.log('Shutting down server...');
child.kill('SIGTERM');
});
}
if (require.main === module) {
main();
}
module.exports = { main };