refactor: move drivers directory into mcontainer package

- Relocate goose driver to mcontainer/drivers/
- Update ConfigManager to dynamically scan for driver YAML files
- Add support for mc-driver.yaml instead of mai-driver.yaml
- Update Driver model to support init commands and other YAML fields
- Auto-discover drivers at runtime instead of hardcoding them
- Update documentation to reflect new directory structure
This commit is contained in:
2025-03-11 19:37:11 -06:00
parent 6f08e2b274
commit 307eee4fce
10 changed files with 92 additions and 102 deletions

View File

@@ -8,35 +8,10 @@ DEFAULT_CONFIG_DIR = Path.home() / ".config" / "mc"
DEFAULT_CONFIG_FILE = DEFAULT_CONFIG_DIR / "config.yaml"
DEFAULT_DRIVERS_DIR = Path.home() / ".config" / "mc" / "drivers"
PROJECT_ROOT = Path(__file__).parent.parent
BUILTIN_DRIVERS_DIR = PROJECT_ROOT / "drivers"
BUILTIN_DRIVERS_DIR = Path(__file__).parent / "drivers" # mcontainer/drivers
# Default built-in driver configurations
DEFAULT_DRIVERS = {
"goose": Driver(
name="goose",
description="Goose with MCP servers",
version="1.0.0",
maintainer="team@monadical.com",
image="monadical/mc-goose:latest",
ports=[8000, 22],
),
"aider": Driver(
name="aider",
description="Aider coding assistant",
version="1.0.0",
maintainer="team@monadical.com",
image="monadical/mc-aider:latest",
ports=[22],
),
"claude-code": Driver(
name="claude-code",
description="Claude Code environment",
version="1.0.0",
maintainer="team@monadical.com",
image="monadical/mc-claude-code:latest",
ports=[22],
),
}
# Dynamically loaded from drivers directory at runtime
DEFAULT_DRIVERS = {}
class ConfigManager:
@@ -46,6 +21,10 @@ class ConfigManager:
self.drivers_dir = DEFAULT_DRIVERS_DIR
self.config = self._load_or_create_config()
# Always load package drivers on initialization
# These are separate from the user config
self.builtin_drivers = self._load_package_drivers()
def _load_or_create_config(self) -> Config:
"""Load existing config or create a new one with defaults"""
if self.config_path.exists():
@@ -80,18 +59,12 @@ class ConfigManager:
self.config_dir.mkdir(parents=True, exist_ok=True)
self.drivers_dir.mkdir(parents=True, exist_ok=True)
# Load built-in drivers from directories
builtin_drivers = self.load_builtin_drivers()
# Merge with default drivers, with directory drivers taking precedence
drivers = {**DEFAULT_DRIVERS, **builtin_drivers}
# Initial config without drivers
config = Config(
docker={
"socket": "/var/run/docker.sock",
"network": "mc-network",
},
drivers=drivers,
defaults={
"driver": "goose",
},
@@ -115,12 +88,23 @@ class ConfigManager:
yaml.dump(config_dict, f)
def get_driver(self, name: str) -> Optional[Driver]:
"""Get a driver by name"""
"""Get a driver by name, checking builtin drivers first, then user-configured ones"""
# Check builtin drivers first (package drivers take precedence)
if name in self.builtin_drivers:
return self.builtin_drivers[name]
# If not found, check user-configured drivers
return self.config.drivers.get(name)
def list_drivers(self) -> Dict[str, Driver]:
"""List all available drivers"""
return self.config.drivers
"""List all available drivers (both builtin and user-configured)"""
# Start with user config drivers
all_drivers = dict(self.config.drivers)
# Add builtin drivers, overriding any user drivers with the same name
# This ensures that package-provided drivers always take precedence
all_drivers.update(self.builtin_drivers)
return all_drivers
def add_session(self, session_id: str, session_data: dict) -> None:
"""Add a session to the config"""
@@ -140,12 +124,12 @@ class ConfigManager:
def load_driver_from_dir(self, driver_dir: Path) -> Optional[Driver]:
"""Load a driver configuration from a directory"""
yaml_path = (
driver_dir / "mai-driver.yaml"
) # Keep this name for backward compatibility
# Try with mc-driver.yaml first (new format), then mai-driver.yaml (legacy)
yaml_path = driver_dir / "mc-driver.yaml"
if not yaml_path.exists():
return None
yaml_path = driver_dir / "mai-driver.yaml" # Backward compatibility
if not yaml_path.exists():
return None
try:
with open(yaml_path, "r") as f:
@@ -159,28 +143,33 @@ class ConfigManager:
print(f"Driver config {yaml_path} missing required fields")
return None
# Create driver object
driver = Driver(
name=driver_data["name"],
description=driver_data["description"],
version=driver_data["version"],
maintainer=driver_data["maintainer"],
image=f"monadical/mc-{driver_data['name']}:latest",
ports=driver_data.get("ports", []),
)
# Use Driver.model_validate to handle all fields from YAML
# This will map all fields according to the Driver model structure
try:
# Ensure image field is set if not in YAML
if "image" not in driver_data:
driver_data["image"] = f"monadical/mc-{driver_data['name']}:latest"
driver = Driver.model_validate(driver_data)
return driver
except Exception as validation_error:
print(
f"Error validating driver data from {yaml_path}: {validation_error}"
)
return None
return driver
except Exception as e:
print(f"Error loading driver from {yaml_path}: {e}")
return None
def load_builtin_drivers(self) -> Dict[str, Driver]:
"""Load all built-in drivers from the drivers directory"""
def _load_package_drivers(self) -> Dict[str, Driver]:
"""Load all package drivers from the mcontainer/drivers directory"""
drivers = {}
if not BUILTIN_DRIVERS_DIR.exists():
return drivers
# Search for mc-driver.yaml files in each subdirectory
for driver_dir in BUILTIN_DRIVERS_DIR.iterdir():
if driver_dir.is_dir():
driver = self.load_driver_from_dir(driver_dir)
@@ -191,10 +180,10 @@ class ConfigManager:
def get_driver_path(self, driver_name: str) -> Optional[Path]:
"""Get the directory path for a driver"""
# Check built-in drivers first
builtin_path = BUILTIN_DRIVERS_DIR / driver_name
if builtin_path.exists() and builtin_path.is_dir():
return builtin_path
# Check package drivers first (these are the bundled ones)
package_path = BUILTIN_DRIVERS_DIR / driver_name
if package_path.exists() and package_path.is_dir():
return package_path
# Then check user drivers
user_path = self.drivers_dir / driver_name

View File

@@ -225,25 +225,35 @@ class ContainerManager:
env_vars["MC_CONFIG_DIR"] = "/mc-config"
env_vars["MC_DRIVER_CONFIG_DIR"] = f"/mc-config/{driver_name}"
# Create driver-specific config directories
# Create driver-specific config directories and set up direct volume mounts
if driver.persistent_configs:
print("Setting up persistent configuration directories:")
for config in driver.persistent_configs:
# Get target directory path on host
target_dir = project_config_path / config.target.lstrip(
target_dir = project_config_path / config.target.removeprefix(
"/mc-config/"
)
# Create directory if it's a directory type config
if config.type == "directory":
dir_existed = target_dir.exists()
target_dir.mkdir(parents=True, exist_ok=True)
print(f" - Created directory: {target_dir}")
if not dir_existed:
print(f" - Created directory: {target_dir}")
# For files, make sure parent directory exists
elif config.type == "file":
target_dir.parent.mkdir(parents=True, exist_ok=True)
# File will be created by the container if needed
# Mount persistent config directly to container path
session_volumes[str(target_dir)] = {
"bind": config.source,
"mode": "rw",
}
print(
f" - Created direct volume mount: {target_dir} -> {config.source}"
)
# Create container
container = self.client.containers.create(
image=driver.image,

View File

@@ -0,0 +1,47 @@
FROM python:3.12-slim
LABEL maintainer="team@monadical.com"
LABEL description="Goose with MCP servers"
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
openssh-server \
bash \
curl \
&& rm -rf /var/lib/apt/lists/*
# Set up SSH server
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
# Create app directory
WORKDIR /app
# Install python dependencies
# This is done before copying scripts for better cache management
RUN pip install --no-cache-dir goose-ai langfuse
# Copy initialization scripts
COPY mc-init.sh /mc-init.sh
COPY entrypoint.sh /entrypoint.sh
COPY mc-driver.yaml /mc-driver.yaml
COPY init-status.sh /init-status.sh
# Make scripts executable
RUN chmod +x /mc-init.sh /entrypoint.sh /init-status.sh
# Set up initialization status check on login
RUN echo '[ -x /init-status.sh ] && /init-status.sh' >> /etc/bash.bashrc
# Set up environment
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# Expose ports
EXPOSE 8000 22
# Set entrypoint
ENTRYPOINT ["/entrypoint.sh"]

View File

@@ -0,0 +1,41 @@
# Goose Driver for MC
This driver provides a containerized environment for running [Goose](https://goose.ai).
## Features
- Pre-configured environment for Goose AI
- Self-hosted instance integration
- SSH access
- Git repository integration
- Langfuse logging support
## Environment Variables
| Variable | Description | Required |
|----------|-------------|----------|
| `LANGFUSE_INIT_PROJECT_PUBLIC_KEY` | Langfuse public key | No |
| `LANGFUSE_INIT_PROJECT_SECRET_KEY` | Langfuse secret key | No |
| `LANGFUSE_URL` | Langfuse API URL | No |
| `MC_PROJECT_URL` | Project repository URL | No |
| `MC_GIT_SSH_KEY` | SSH key for Git authentication | No |
| `MC_GIT_TOKEN` | Token for Git authentication | No |
## Build
To build this driver:
```bash
cd drivers/goose
docker build -t monadical/mc-goose:latest .
```
## Usage
```bash
# Create a new session with this driver
mc session create --driver goose
# Create with project repository
mc session create --driver goose --project github.com/username/repo
```

View File

@@ -0,0 +1,17 @@
#!/bin/bash
# Entrypoint script for Goose driver
# Run the standard initialization script
/mc-init.sh
# Start SSH server in the background
/usr/sbin/sshd
# Print welcome message
echo "==============================================="
echo "Goose driver container started"
echo "SSH server running on port 22"
echo "==============================================="
# Keep container running
exec tail -f /dev/null

View File

@@ -0,0 +1,65 @@
#!/bin/bash
# Script to check and display initialization status
# Function to display initialization logs
show_init_logs() {
if [ -f "/init.log" ]; then
echo "Displaying initialization logs:"
echo "----------------------------------------"
cat /init.log
echo "----------------------------------------"
else
echo "No initialization logs found."
fi
}
# Function to follow logs until initialization completes
follow_init_logs() {
if [ ! -f "/init.log" ]; then
echo "No initialization logs found."
return
fi
echo "Initialization is still in progress. Showing logs:"
echo "----------------------------------------"
tail -f /init.log &
tail_pid=$!
# Check every second if initialization has completed
while true; do
if [ -f "/init.status" ] && grep -q "INIT_COMPLETE=true" "/init.status"; then
kill $tail_pid 2>/dev/null
echo "----------------------------------------"
echo "Initialization completed."
break
fi
sleep 1
done
}
# Check if we're in an interactive shell
if [ -t 0 ]; then
INTERACTIVE=true
else
INTERACTIVE=false
fi
# Check initialization status
if [ -f "/init.status" ]; then
if grep -q "INIT_COMPLETE=true" "/init.status"; then
echo "MC initialization has completed."
# No longer prompt to show logs when initialization is complete
else
echo "MC initialization is still in progress."
follow_init_logs
fi
else
echo "Cannot determine initialization status."
# Ask if user wants to see logs if they exist (only in interactive mode)
if [ -f "/init.log" ] && [ "$INTERACTIVE" = true ]; then
read -p "Do you want to see initialization logs? (y/n): " show_logs
if [[ "$show_logs" =~ ^[Yy] ]]; then
show_init_logs
fi
fi
fi

View File

@@ -0,0 +1,63 @@
name: goose
description: Goose AI environment
version: 1.0.0
maintainer: team@monadical.com
image: monadical/mc-goose:latest
init:
pre_command: /mc-init.sh
command: /entrypoint.sh
environment:
- name: LANGFUSE_INIT_PROJECT_PUBLIC_KEY
description: Langfuse public key
required: false
sensitive: true
- name: LANGFUSE_INIT_PROJECT_SECRET_KEY
description: Langfuse secret key
required: false
sensitive: true
- name: LANGFUSE_URL
description: Langfuse API URL
required: false
default: https://cloud.langfuse.com
# Project environment variables
- name: MC_PROJECT_URL
description: Project repository URL
required: false
- name: MC_PROJECT_TYPE
description: Project repository type (git, svn, etc.)
required: false
default: git
- name: MC_GIT_SSH_KEY
description: SSH key for Git authentication
required: false
sensitive: true
- name: MC_GIT_TOKEN
description: Token for Git authentication
required: false
sensitive: true
ports:
- 8000 # Main application
- 22 # SSH server
volumes:
- mountPath: /app
description: Application directory
persistent_configs:
- source: "/app/.goose"
target: "/mc-config/goose-app"
type: "directory"
description: "Goose memory"
- source: "/root/.config/goose"
target: "/mc-config/goose-config"
type: "directory"
description: "Goose configuration"

View File

@@ -0,0 +1,61 @@
#!/bin/bash
# Standardized initialization script for MC drivers
# Redirect all output to both stdout and the log file
exec > >(tee -a /init.log) 2>&1
# Mark initialization as started
echo "=== MC Initialization started at $(date) ==="
echo "INIT_COMPLETE=false" > /init.status
# Project initialization
if [ -n "$MC_PROJECT_URL" ]; then
echo "Initializing project: $MC_PROJECT_URL"
# Set up SSH key if provided
if [ -n "$MC_GIT_SSH_KEY" ]; then
mkdir -p ~/.ssh
echo "$MC_GIT_SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null
ssh-keyscan gitlab.com >> ~/.ssh/known_hosts 2>/dev/null
ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null
fi
# Set up token if provided
if [ -n "$MC_GIT_TOKEN" ]; then
git config --global credential.helper store
echo "https://$MC_GIT_TOKEN:x-oauth-basic@github.com" > ~/.git-credentials
fi
# Clone repository
git clone $MC_PROJECT_URL /app
cd /app
# Run project-specific initialization if present
if [ -f "/app/.mc/init.sh" ]; then
bash /app/.mc/init.sh
fi
# Persistent configs are now directly mounted as volumes
# No need to create symlinks anymore
if [ -n "$MC_CONFIG_DIR" ] && [ -d "$MC_CONFIG_DIR" ]; then
echo "Using persistent configuration volumes (direct mounts)"
fi
fi
# Goose uses self-hosted instance, no API key required
# Set up Langfuse logging if credentials are provided
if [ -n "$LANGFUSE_INIT_PROJECT_SECRET_KEY" ] && [ -n "$LANGFUSE_INIT_PROJECT_PUBLIC_KEY" ]; then
echo "Setting up Langfuse logging"
export LANGFUSE_INIT_PROJECT_SECRET_KEY="$LANGFUSE_INIT_PROJECT_SECRET_KEY"
export LANGFUSE_INIT_PROJECT_PUBLIC_KEY="$LANGFUSE_INIT_PROJECT_PUBLIC_KEY"
export LANGFUSE_URL="${LANGFUSE_URL:-https://cloud.langfuse.com}"
fi
echo "MC driver initialization complete"
# Mark initialization as complete
echo "=== MC Initialization completed at $(date) ==="
echo "INIT_COMPLETE=true" > /init.status

View File

@@ -25,15 +25,26 @@ class PersistentConfig(BaseModel):
description: str = ""
class VolumeMount(BaseModel):
mountPath: str
description: str = ""
class DriverInit(BaseModel):
pre_command: Optional[str] = None
command: str
class Driver(BaseModel):
name: str
description: str
version: str
maintainer: str
image: str
init: Optional[DriverInit] = None
environment: List[DriverEnvironmentVariable] = []
ports: List[int] = []
volumes: List[Dict[str, str]] = []
volumes: List[VolumeMount] = []
persistent_configs: List[PersistentConfig] = []