# Bash Execution Debugging Improvements
## Current Issues
When executing bash commands through the MCP server, errors are not always well-reported, making it difficult to understand why a command failed. The current implementation in `execute_bash` provides basic error reporting but lacks:
1. Detailed error classification
2. Contextual execution information
3. User-friendly error messages with recovery suggestions
## Implementation Plan
### Phase 1: Enhanced Error Reporting
#### 1.1 Improve Error Classification
Modify the `execute_bash` function to classify errors into specific types:
- **Command Not Found**: When the shell cannot locate the executable
- **Permission Denied**: When the user lacks permissions to execute the command
- **Path Issues**: When working with non-existent directories
- **Execution Failure**: When a command returns a non-zero exit code
- **Shell Issues**: When the shell itself encounters problems
#### 1.2 Context-Rich Error Messages
Include relevant contextual information in error messages:
- The exact command being executed
- The working directory at the time of execution
- The exit code and relevant error output
- Parsed command components (for complex commands)
### Phase 2: Contextual Debugging Information
#### 2.1 Debug Logging System
Add a comprehensive logging system that can be enabled with a debug flag:
- Log command parsing steps
- Log directory resolution
- Log environment state
- Log command execution process
#### 2.2 Execution Context Capture
Create a new struct to capture the complete execution context:
```rust
struct CommandExecutionContext {
command: String,
working_directory: PathBuf,
is_compound_command: bool,
command_parts: Vec<String>,
environment_variables: HashMap<String, String>,
timestamp: SystemTime,
}
```
This context will be captured before execution and included in error reports.
### Phase 3: Error Recovery Suggestions
For common errors, provide specific suggestions to help users recover:
- For "command not found" - suggest installation commands
- For permission issues - suggest chmod commands
- For path issues - verify path validity
## Implementation Details
### Modified execute_bash Function
The core implementation change will focus on the execute_bash function in src/main.rs:
1. Add a debug logging flag
2. Create an execution context capture mechanism
3. Enhance error reporting with structured information
4. Implement error classification
5. Include recovery suggestions in error messages
### Example Error Output
Before:
```
Failed to execute command 'ls /nonexistent': No such file or directory
```
After:
```
Command Execution Failed:
- Command: 'ls /nonexistent'
- Working Directory: /Users/alex/github/corrode-mcp
- Error Type: Path Not Found
- Exit Code: 2
- Error Details: No such file or directory
- Suggestion: Verify the path exists before accessing it
```
## Expected Benefits
1. **Faster debugging**: Users will immediately understand why a command failed
2. **Better error recovery**: Suggestions will help users fix issues quickly
3. **Improved visibility**: Complete context will make tracking down issues easier
4. **More reliable execution**: Better error handling will prevent cascading failures
## Next Steps
After implementing these improvements, we will:
1. Add unit tests for the new error handling
2. Document the enhanced error reporting in the README
3. Consider adding a config option to control verbosity levels