safe-commander-mcp
Version:
A secure MCP server for executing whitelisted development commands with comprehensive security controls and resource limits
65 lines (64 loc) • 2.12 kB
JavaScript
;
/**
* Rate limiting and tracking for command execution
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkRateLimit = checkRateLimit;
exports.trackCommandStart = trackCommandStart;
exports.trackCommandEnd = trackCommandEnd;
exports.getRunningCommandsCount = getRunningCommandsCount;
exports.getRunningCommands = getRunningCommands;
// Rate limiting and tracking storage
const commandCooldowns = new Map();
const runningCommands = new Set();
/**
* Rate limiting to prevent command spam
*
* @param command - The command to check rate limiting for
* @param maxConcurrentCommands - Maximum number of concurrent commands allowed
* @throws Error if rate limit is exceeded or too many concurrent commands
*/
function checkRateLimit(command, maxConcurrentCommands) {
const now = Date.now();
const lastExecution = commandCooldowns.get(command);
if (lastExecution && (now - lastExecution) < 1000) {
throw new Error('Rate limit exceeded. Please wait at least 1 second before executing the same command again');
}
if (runningCommands.size >= maxConcurrentCommands) {
throw new Error(`Maximum concurrent commands (${maxConcurrentCommands}) reached. Please wait for existing commands to complete`);
}
}
/**
* Track the start of a command execution
*
* @param commandId - Unique identifier for the command
* @param command - The command being executed
*/
function trackCommandStart(commandId, command) {
runningCommands.add(commandId);
commandCooldowns.set(command, Date.now());
}
/**
* Track the end of a command execution
*
* @param commandId - Unique identifier for the command
*/
function trackCommandEnd(commandId) {
runningCommands.delete(commandId);
}
/**
* Get the current number of running commands
*
* @returns Number of currently running commands
*/
function getRunningCommandsCount() {
return runningCommands.size;
}
/**
* Get the set of running command IDs (for shutdown handling)
*
* @returns Set of running command IDs
*/
function getRunningCommands() {
return new Set(runningCommands);
}