- MCP server cu stdio transport pentru performanță maximă
- Tool-uri pentru file operations, HTTP requests, system commands
- Suport NATS pentru comunicare inter-module
- Configurare nginx cu API key auth și SSL
- Arhitectură modulară și extensibilă
🤖 Generated with Claude Code
4.5 KiB
4.5 KiB
System Command Tool
Execute system commands with comprehensive security controls and output handling.
Overview
The SystemCommandTool allows controlled execution of whitelisted system commands with proper security boundaries, timeout handling, and output management.
Input Schema
{
command: string; // Command to execute (must be whitelisted)
args?: string[]; // Command arguments
cwd?: string; // Working directory
env?: Record<string, string>; // Environment variables
timeout?: number; // Timeout in ms (100-300000, default: 30000)
stdin?: string; // Input to send to stdin
}
Output Schema
{
stdout: string; // Standard output
stderr: string; // Standard error output
exitCode: number; // Process exit code
duration: number; // Execution time in ms
}
Whitelisted Commands
Only the following commands are allowed for security:
ls- List directory contentscat- Display file contentsgrep- Search text patternsfind- Find files and directoriesecho- Print textpwd- Print working directorywhoami- Show current userdate- Show current date/timeenv- Show environment variableswhich- Find command locationwc- Word/line/character counthead- Show first linestail- Show last linessort- Sort linesuniq- Remove duplicate linescurl- HTTP requestsping- Network connectivity testdig- DNS lookupps- Process listdf- Disk usagedu- Directory sizeuptime- System uptime
Security Features
Command Validation
- Only whitelisted commands can be executed
- Shell operators (
|,&,;,>,<, etc.) are blocked in arguments - Prevents command injection attacks
Resource Limits
- Configurable timeout (max 5 minutes)
- Output size limited to 1MB per stream
- Process is killed if limits exceeded
Permission Control
- Requires
system:execpermission - Inherits process environment with modifications
Example Usage
Basic Command
const result = await mcp.callTool('system_command', {
command: 'ls',
args: ['-la', '/tmp']
});
console.log(result.stdout);
With Working Directory
const result = await mcp.callTool('system_command', {
command: 'grep',
args: ['-r', 'TODO', '.'],
cwd: '/home/user/project'
});
With Environment Variables
const result = await mcp.callTool('system_command', {
command: 'echo',
args: ['$MY_VAR'],
env: {
MY_VAR: 'Hello from environment!'
}
});
With Timeout
const result = await mcp.callTool('system_command', {
command: 'find',
args: ['/', '-name', '*.log'],
timeout: 5000 // 5 seconds
});
With stdin Input
const result = await mcp.callTool('system_command', {
command: 'grep',
args: ['error'],
stdin: 'line 1\nerror on line 2\nline 3'
});
Error Handling
Command Not Allowed
// This will throw an error
await mcp.callTool('system_command', {
command: 'rm' // Not in whitelist
});
// Error: Command not allowed: rm
Shell Injection Prevention
// This will throw an error
await mcp.callTool('system_command', {
command: 'ls',
args: ['; rm -rf /'] // Shell operators blocked
});
// Error: Shell characters not allowed in arguments
Timeout Handling
try {
await mcp.callTool('system_command', {
command: 'find',
args: ['/'],
timeout: 1000 // 1 second
});
} catch (error) {
// Error: Command timed out after 1000ms
}
Non-Zero Exit Codes
Non-zero exit codes don't throw errors but are returned in the result:
const result = await mcp.callTool('system_command', {
command: 'grep',
args: ['nonexistent', 'file.txt']
});
// result.exitCode will be 1 or 2
// result.stderr will contain the error message
Best Practices
- Use specific commands instead of shell scripts
- Set appropriate timeouts for long-running commands
- Check exit codes to handle command failures
- Limit output size by using head/tail when appropriate
- Avoid user input in command arguments without validation
- Use working directory instead of cd commands
- Monitor stderr for warnings and errors
Limitations
- No shell features (pipes, redirects, wildcards)
- No interactive commands
- No sudo or privileged operations
- Output limited to 1MB per stream
- Maximum timeout of 5 minutes