@gabrielmaialva33/mcp-filesystem
Version:
MCP server for secure filesystem access
62 lines • 2.39 kB
JavaScript
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { logger } from '../../logger/index.js';
import { metrics } from '../../metrics/index.js';
const execPromise = promisify(exec);
class CommandExecutor {
async executeCommand(command, workingDir, timeout = 10000) {
const endMetric = metrics.startOperation('bash_execute');
try {
await logger.debug(`Executing Bash command: ${command}`, { workingDir, timeout });
const options = {
cwd: workingDir || process.cwd(),
timeout,
maxBuffer: 1024 * 1024 * 10,
shell: '/bin/bash',
};
try {
const { stdout, stderr } = await execPromise(command, options);
await logger.debug(`Command executed successfully: ${command}`, {
stdoutLength: stdout.length,
stderrLength: stderr.length,
});
endMetric();
return {
stdout,
stderr,
exitCode: 0,
};
}
catch (error) {
const stderr = error.stderr || '';
const stdout = error.stdout || '';
const exitCode = error.code || 1;
await logger.warn(`Command execution failed: ${command}`, {
exitCode,
stderr: stderr.substring(0, 500) + (stderr.length > 500 ? '...' : ''),
});
endMetric();
return {
stdout,
stderr,
exitCode,
};
}
}
catch (error) {
metrics.recordError('bash_execute');
throw error;
}
}
async executePipedCommand(commands, workingDir, timeout = 30000) {
const pipedCommand = commands.join(' | ');
return this.executeCommand(pipedCommand, workingDir, timeout);
}
async executeCommandWithStream(command, workingDir) {
const { stdout } = await this.executeCommand(command, workingDir);
return stdout.split('\n').filter((line) => line.trim() !== '');
}
}
export const commandExecutor = new CommandExecutor();
export default CommandExecutor;
//# sourceMappingURL=command_executor.js.map