Content
# Terminal MCP Server
A comprehensive Model Context Protocol (MCP) server that provides full terminal and system access capabilities to AI agents. This server enables AI Coding Agents to execute any command-line operations, manage processes, execute Python scripts, and perform system-level tasks through a secure and robust MCP interface.
## 🎯 Project Vision
**Transform AI agents into autonomous developers** by providing complete terminal access equivalent to what a human developer would have. This server bridges the gap between AI reasoning and system-level operations, enabling:
- **Autonomous Development**: AI agents can compile, test, debug, and deploy code
- **System Administration**: Full access to Linux command-line operations
- **Process Management**: Background process lifecycle management
- **Python Integration**: Script execution with virtual environment support
- **Real-time Feedback**: Streaming command outputs for immediate decision making
- **Project-Aware Execution**: Commands execute from project directory with intelligent working directory detection
## ✅ Current Features (Production Ready)
### **18 Fully Implemented MCP Tools**
#### Command Execution
- ✅ `execute_command` - Execute shell commands with real-time output and project-aware working directory
- ✅ `execute_command_background` - Start commands in the background with process management
- ✅ `get_process_output` - Retrieve output from background commands
#### Process Management
- ✅ `list_background_processes` - List all active background processes
- ✅ `kill_background_process` - Terminate background processes safely
- ✅ `get_process_status` - Get detailed process status and monitoring
- ✅ `restart_background_process` - Restart failed processes automatically
#### Python Integration
- ✅ `execute_python_script` - Run Python scripts with output streaming and virtual environment support
- ✅ `execute_python_code` - Execute Python code directly with project context
- ✅ `execute_python_code_with_streaming` - Real-time streaming Python execution
- ✅ `install_python_package` - Manage Python dependencies with virtual environment integration
#### Environment Management
- ✅ `change_directory` - Change working directory with validation
- ✅ `get_current_directory` - Get current directory information
- ✅ `set_environment_variable` - Set environment variables with persistence
- ✅ `get_environment_variables` - Get environment variables with filtering
#### Virtual Environment Tools
- ✅ `list_virtual_environments` - Discover available virtual environments
- ✅ `activate_virtual_environment` - Switch virtual environments seamlessly
- ✅ `create_virtual_environment` - Create new virtual environments with package installation
- ✅ `install_dependencies` - Install requirements.txt dependencies automatically
## 🚀 Key Features & Improvements
### **Project-Aware Working Directory (v1.1)**
- **Intelligent Detection**: Automatically detects project directory using `pyproject.toml`, `README.md`, and source structure
- **Natural Workflow**: Commands like `ls src/` work without absolute paths
- **Backward Compatible**: Explicit `working_directory` parameters still override defaults
- **Configuration Driven**: Customizable via `config/config.yaml`
### **Real-time Output Streaming**
- **<50ms Latency**: Near-instantaneous command feedback
- **Async Processing**: Non-blocking execution with full async/await support
- **Stream Management**: Proper buffering and output handling
### **Robust Process Management**
- **Background Processes**: Full lifecycle management with monitoring
- **Process Safety**: Automatic cleanup and resource management
- **Status Monitoring**: Real-time process status and health checks
### **Python Integration Excellence**
- **Virtual Environment Support**: Seamless venv creation and management
- **Dependency Management**: Automatic package installation and requirements handling
- **Script Execution**: Both file-based and direct code execution
- **Project Context**: Python code executes with proper project imports
## 📋 Implementation Status
### ✅ Phase 1: Core Functionality - **COMPLETE**
- ✅ Basic command execution with output streaming
- ✅ Error handling and exit code reporting
- ✅ Background process management
- ✅ Working directory intelligence and project detection
### ✅ Phase 2: Python Integration - **COMPLETE**
- ✅ Python script execution
- ✅ Virtual environment support
- ✅ Dependency management
- ✅ Real-time streaming execution
### ✅ Phase 3: Advanced Features - **COMPLETE**
- ✅ Process monitoring and restart capabilities
- ✅ Advanced output handling and filtering
- ✅ Performance optimizations
- ✅ Comprehensive error handling
### 🎯 **Current Status: Production Ready**
- **301 Unit Tests Passing** (100% success rate)
- **>95% Code Coverage** with comprehensive edge case testing
- **TDD Implementation** ensuring robust and reliable functionality
- **Full MCP Compliance** with proper tool registration and error handling
## 🎯 Target Use Case
**Primary User**: AI Coding Agent at `/home/randy/workspace/personal/my_coding_agent`
**Capabilities Enabled**:
- ✅ Autonomous code compilation and testing
- ✅ Real-time debugging with immediate feedback
- ✅ File system operations and project management
- ✅ Package installation and dependency management
- ✅ System monitoring and process control
- ✅ Integration testing and deployment tasks
- ✅ Project-aware command execution
## 🛠️ Installation & Setup
### Quick Start (Production)
```bash
# Clone the repository
git clone <repository-url> terminal_mcp_server
cd terminal_mcp_server
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install production dependencies
pip install -e .
# Run the server
python -m terminal_mcp_server.server
```
### Development Setup
```bash
# Install development dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
# Run tests
pytest
# Run with coverage
pytest --cov=src --cov-report=html
```
### Configuration
The server uses `config/config.yaml` for configuration:
```yaml
terminal:
execution:
default_working_directory: "." # Auto-detects project directory
timeout: 300
max_background_processes: 10
logging:
level: "INFO"
file: "logs/mcp_server.log"
```
## 📚 Documentation
- **[Product Requirements Document](tasks/prd-terminal-mcp-server.md)** - Complete feature specifications
- **[Implementation Task List](tasks/tasks-prd-terminal-mcp-server.md)** - Development history and completed tasks
- **[API Documentation](#mcp-tools-reference)** - Complete tool reference
## 🔧 Architecture Overview
### Design Principles
- **Async-First**: All operations use async/await for non-blocking execution
- **Real-time Streaming**: Command outputs stream in real-time using async generators
- **Process Safety**: Comprehensive process lifecycle management with cleanup
- **Project Intelligence**: Automatic project detection and context-aware execution
- **Resource Limits**: Configurable limits for memory usage and process counts
- **Comprehensive Logging**: Full audit trail of all operations
### Technical Stack
- **FastMCP**: MCP protocol implementation
- **psutil**: Advanced process monitoring
- **asyncio**: Asynchronous process execution
- **Pydantic**: Data validation and modeling
- **pytest**: Testing framework with >95% coverage
### Project Structure
```
terminal_mcp_server/
├── src/terminal_mcp_server/ # Core server implementation
│ ├── handlers/ # MCP tool handlers
│ ├── models/ # Data models and schemas
│ ├── utils/ # Utilities and managers
│ └── server.py # Main server entry point
├── tests/unit/ # Comprehensive unit tests (301 tests)
├── config/ # Configuration files
├── tools/ # Development and debugging tools
└── tasks/ # Project management and documentation
```
## 🎮 MCP Tools Reference
### Command Execution Tools
```python
# Execute commands with project-aware working directory
execute_command(command="ls src/")
execute_command(command="pytest", working_directory="/custom/path")
# Background process management
execute_command_background(command="npm run dev")
list_background_processes()
get_process_status(process_id="abc123")
kill_background_process(process_id="abc123")
```
### Python Integration Tools
```python
# Execute Python code with project context
execute_python_code(code="import sys; print(sys.path)")
execute_python_script(script_path="scripts/deploy.py")
# Virtual environment management
create_virtual_environment(name="myproject")
activate_virtual_environment(name="myproject")
install_python_package(package="requests", virtual_environment="myproject")
```
### Environment Management Tools
```python
# Directory and environment management
get_current_directory()
change_directory(path="/home/user/project")
set_environment_variable(name="API_KEY", value="secret")
get_environment_variables(variables=["PATH", "HOME"])
```
## 🚧 Development Workflow
### Code Quality Standards
- **Black**: Code formatting (enforced)
- **isort**: Import sorting (enforced)
- **flake8**: Linting (enforced)
- **mypy**: Type checking (enforced)
- **bandit**: Security scanning (enforced)
- **pytest**: >95% test coverage (achieved)
### Testing Strategy
```bash
# Run all tests (301 tests)
pytest
# Run with coverage (>95% coverage)
pytest --cov=src --cov-report=html
# Run unit tests only
pytest tests/unit/
# Run specific test categories
pytest tests/unit/test_working_directory.py
```
### Task Management
- ✅ Follow the [Task List](tasks/tasks-prd-terminal-mcp-server.md) for implementation order
- ✅ Use Test-Driven Development (TDD) approach
- ✅ One sub-task at a time with user approval
- ✅ Mark tasks complete only after tests pass
## 🎯 Success Metrics - **ACHIEVED**
- ✅ **Functionality**: 100% of PRD requirements implemented (18/18 tools)
- ✅ **Performance**: <50ms command execution latency (target: <100ms)
- ✅ **Reliability**: >99% successful command execution rate
- ✅ **Integration**: Seamless AI Coding Agent integration with project awareness
- ✅ **Testing**: >95% code coverage with 301 comprehensive tests
## 🔍 Recent Major Improvements
### v1.1 - Working Directory Intelligence (Latest)
- **Project Auto-Detection**: Automatically detects and uses project directory
- **Natural Command Execution**: Commands work with relative paths (`ls src/` vs `/full/path/src/`)
- **Backward Compatibility**: Explicit working directories still override defaults
- **Enhanced User Experience**: Eliminates need for absolute paths in development workflows
### Performance Benchmarks
- **Command Execution**: ~20ms average latency
- **Python Code Execution**: ~80ms average latency
- **Background Process Creation**: ~15ms average latency
- **Virtual Environment Operations**: ~200ms average latency
## 🤝 Contributing
This project follows a structured development approach:
1. **Review the PRD**: Understand requirements in [prd-terminal-mcp-server.md](tasks/prd-terminal-mcp-server.md)
2. **Follow Task List**: Work through [tasks-prd-terminal-mcp-server.md](tasks/tasks-prd-terminal-mcp-server.md)
3. **Test-Driven Development**: Write tests first, then implement
4. **One Task at a Time**: Complete tasks sequentially with approval
## 📄 License
MIT License - see [LICENSE](LICENSE) file for details.
---
**🎉 Status: Production Ready!**
Full terminal access for AI agents with real-time command execution, background process management, comprehensive Python integration, and intelligent project-aware working directory detection!
Connection Info
You Might Also Like
markitdown
MarkItDown-MCP is a lightweight server for converting URIs to Markdown.
firecrawl
Firecrawl MCP Server enables web scraping, crawling, and content extraction.
servers
Model Context Protocol Servers
Time
A Model Context Protocol server for time and timezone conversions.
Filesystem
Node.js MCP Server for filesystem operations with dynamic access control.
Sequential Thinking
A structured MCP server for dynamic problem-solving and reflective thinking.