Skip to main content

Add your description here

Project description

Chuk Virtual Shell ๐Ÿš

A powerful virtual shell environment with MCP (Model Context Protocol) integration, perfect for AI agents and sandboxed execution environments.

Tests Coverage Python License

๐ŸŒŸ Overview

Chuk Virtual Shell provides a complete POSIX-like virtual shell environment designed specifically for AI agents and automation:

  • ๐Ÿค– MCP Server Integration: Full Model Context Protocol support for AI agents like Claude, Cline, and Aider
  • ๐Ÿ”„ Persistent Sessions: Stateful command execution with maintained context across interactions
  • ๐Ÿ”’ User Isolation: Complete session and task isolation between users
  • ๐Ÿ“ Virtual Filesystem: Pluggable storage providers (memory, SQLite, S3)
  • ๐ŸŽฏ 50+ Built-in Commands: Comprehensive Unix-like command set
  • โšก Advanced I/O: Full pipeline, redirection, and here-doc support
  • ๐Ÿ–๏ธ Pre-configured Sandboxes: Ready-to-use secure environments
  • ๐Ÿ”Œ Extensible Architecture: Easy to add custom commands and storage providers

๐Ÿš€ Quick Start

Installation

# Install with uv (recommended)
uv pip install chuk-virtual-shell

# Or with pip
pip install chuk-virtual-shell

# For MCP server functionality, install optional dependency (Unix/macOS only)
uv pip install chuk-virtual-shell[mcp-server]
# Or with pip
pip install chuk-virtual-shell[mcp-server]

# Note: MCP server requires Unix-like OS (Linux/macOS) due to uvloop dependency
# Windows users can use WSL or run the shell without MCP server features

Basic Usage

# Start interactive shell
uv run chuk-virtual-shell

# Use a pre-configured sandbox
uv run chuk-virtual-shell --sandbox ai_sandbox

# Start MCP server for AI agents
uv run python -m chuk_virtual_shell.mcp_server

MCP Integration for AI Agents

# See examples/mcp_client_demo.py for complete example
from examples.mcp_client_demo import SimpleMCPClient

client = SimpleMCPClient()
await client.start_server()

# Create isolated session
result = await client.call_tool("bash", {"command": "pwd"})
session_id = result["session_id"]

# Commands share state within session
await client.call_tool("bash", {
    "command": "export PROJECT=MyApp && mkdir -p /project/src",
    "session_id": session_id
})

# State persists across commands
result = await client.call_tool("bash", {
    "command": "echo $PROJECT && ls /project",
    "session_id": session_id
})
# Output: MyApp\nsrc

Try the Interactive Demo

# Run the complete MCP demonstration
uv run examples/mcp_client_demo.py

# Expected output shows:
# โœ… User isolation and session management
# โœ… State persistence across commands  
# โœ… Background task execution
# โœ… Multiple concurrent sessions
# โœ… Complex multi-step workflows

๐Ÿ“š Key Features

๐Ÿค– MCP Server Capabilities

Full Model Context Protocol implementation with user isolation:

# Available MCP tools:
- bash           # Execute shell commands with session persistence
- whoami         # Get user context and session info
- list_sessions  # List all active sessions for current user
- get_session_state  # Get session details (pwd, env, lifetime)
- destroy_session    # Clean up sessions
- get_task_output    # Get background task results
- cancel_task        # Cancel running background tasks

User Isolation Features:

  • Each user gets isolated sessions and tasks
  • Sessions maintain state (PWD, env vars, files) between commands
  • Background task execution with streaming output
  • Automatic session cleanup on disconnect
  • Per-user resource limits and quotas

Advanced Shell Features via MCP:

  • Full stderr redirection (2>, 2>>, 2>&1)
  • Combined output redirection (&>, &>>)
  • Complex pipelines and command chaining
  • Quoted filename support with spaces
  • All 50+ built-in shell commands available

๐Ÿ”„ Session Management

Stateful sessions that maintain context - essential for AI workflows:

from chuk_virtual_shell.session import ShellSessionManager
from chuk_virtual_shell.shell_interpreter import ShellInterpreter

# Create session manager
manager = ShellSessionManager(shell_factory=lambda: ShellInterpreter())

# Create persistent session
session_id = await manager.create_session()

# Commands share state
await manager.run_command(session_id, "cd /project")
await manager.run_command(session_id, "export API_KEY=secret")
await manager.run_command(session_id, "echo 'data' > file.txt")

# State persists
result = await manager.run_command(session_id, "pwd && echo $API_KEY && cat file.txt")
# Output: /project\nsecret\ndata

๐Ÿ“‹ Advanced I/O Redirection

Comprehensive redirection support (see docs/features/redirection.md):

# Output redirection
echo "Hello" > output.txt
echo "World" >> output.txt

# Input redirection  
sort < unsorted.txt > sorted.txt

# Pipelines
cat data.txt | grep "pattern" | sort | uniq > results.txt

# Here-documents (in scripts)
cat << EOF > config.yaml
server: localhost
port: 8080
EOF

# Advanced redirection
command 2> errors.txt          # Stderr redirection
command 2>&1                    # Merge stderr to stdout
command &> all_output.txt       # Combined output
command 2>> errors.txt         # Append stderr
command &>> all.txt            # Append combined output

๐ŸŽญ Quoting and Escaping

Full quoting semantics (see docs/features/quoting.md):

# Single quotes - literal
echo 'Hello $USER'              # Output: Hello $USER

# Double quotes - with expansion
echo "Hello $USER"              # Output: Hello alice

# Backslash escaping
echo "Price: \$100"             # Output: Price: $100
echo file\ with\ spaces.txt     # Output: file with spaces.txt

# Mixed quoting
echo "It's"' a nice day'        # Output: It's a nice day

๐Ÿ–๏ธ Pre-configured Sandboxes

Ready-to-use secure environments:

# config/ai_sandbox.yaml - Restricted AI agent environment
environment:
  HOME: /sandbox
  USER: ai
  PATH: /bin
  SANDBOX_MODE: restricted

filesystem:
  provider: memory
  
initialization:
  - mkdir -p /sandbox/workspace
  - echo "AI Sandbox Ready" > /sandbox/README.txt

Available sandboxes:

  • ai_sandbox - Restricted environment for AI code execution
  • default - Balanced development environment
  • readonly - Read-only exploration
  • e2b - E2B.dev compatible environment
  • tigris - Tigris Data S3-compatible storage

๐Ÿ“Š Feature Matrix

Feature Category Feature Status Notes
MCP Integration MCP Server โœ… Full protocol support
User Isolation โœ… Session & task isolation
Background Tasks โœ… Async execution with streaming
Session Persistence โœ… State maintained across commands
I/O Redirection Basic pipes (|) โœ… Multi-stage pipelines
Output redirect (>, >>) โœ… Write and append
Input redirect (<) โœ… Read from files
Stderr redirect (2>, 2>>) โœ… Full stderr redirection
Combined (2>&1, &>, &>>) โœ… Merge stdout/stderr
Here-docs (<<) โšก Works in script runner
Shell Operators Chaining (&&, ||, ;) โœ… Full conditional execution
Command substitution ($()) โœ… Both syntaxes supported
Variable expansion โœ… $VAR, ${VAR}
Glob patterns (*, ?) โœ… Full support
Control Flow if/then/else โœ… Conditional logic
for/while loops โœ… Full iteration support
case statements โœ… Pattern matching
Functions โŒ Planned
Commands File operations โœ… cp, mv, rm, mkdir, touch
Text processing โœ… grep, sed, awk, sort, uniq
File viewing โœ… cat, head, tail, more
System utilities โœ… find, which, tree, date

Legend:

  • โœ… Full Support: Complete implementation with tests
  • โšก Partial Support: Works with limitations
  • ๐Ÿšง In Development: Parser/infrastructure ready
  • โŒ Not Supported: Not yet implemented

๐Ÿ› ๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           MCP Client (AI Agent)                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚ MCP Protocol
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           MCP Server (chuk_virtual_shell)       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  User Isolation & Session Management     โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  Shell Interpreter & Command Executor    โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  Virtual Filesystem (Memory/SQLite/S3)   โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“– Documentation

๐Ÿงช Testing

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=chuk_virtual_shell

# Run specific test categories
uv run pytest tests/test_mcp_server.py
uv run pytest tests/test_quoting_comprehensive.py
uv run pytest tests/test_advanced_redirection.py

Current test status: 1411 tests passing (18 skipped)

๐Ÿค Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Development Setup

# Clone the repository
git clone https://github.com/chrishayuk/chuk-virtual-shell.git
cd chuk-virtual-shell

# Install with dev dependencies
uv pip install -e ".[dev]"

# Run tests
uv run pytest

# Run linting
uv run ruff check .

# Run type checking
uv run mypy chuk_virtual_shell

๐Ÿ”ง Troubleshooting

MCP Server Issues

Problem: ModuleNotFoundError: No module named 'chuk_mcp_server'

Solution: Install the MCP server optional dependency:

uv pip install chuk-virtual-shell[mcp-server]
# or with pip
pip install chuk-virtual-shell[mcp-server]

Problem: RuntimeError: uvloop does not support Windows at the moment

Solution: MCP server functionality requires Unix-like OS due to uvloop dependency:

  • Linux/macOS: Install normally with [mcp-server] extra
  • Windows: Use WSL (Windows Subsystem for Linux) or run without MCP features
  • Alternative: Use the shell directly without MCP server integration

Problem: MCP demo fails with JSON decode error

Solution: Ensure the MCP server dependency is installed and the server is accessible:

# Test MCP server directly
uv run python -m chuk_virtual_shell.mcp_server

# Run the interactive demo
uv run examples/mcp_client_demo.py

General Issues

Problem: Command not found errors

Solution: Ensure you're using the correct command syntax. Check available commands:

# In interactive mode
help

# Check specific command help
help ls

Problem: File permission errors

Solution: The virtual filesystem has simulated permissions. Use appropriate commands:

# Create directories with proper paths
mkdir -p /path/to/directory

# Check current working directory
pwd

๐Ÿ“„ License

MIT License - see LICENSE for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ฎ Contact


Ready to give your AI agents a powerful shell environment? Get started with uv pip install chuk-virtual-shell! ๐Ÿš€

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chuk_virtual_shell-0.1.7.tar.gz (270.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chuk_virtual_shell-0.1.7-py3-none-any.whl (177.2 kB view details)

Uploaded Python 3

File details

Details for the file chuk_virtual_shell-0.1.7.tar.gz.

File metadata

  • Download URL: chuk_virtual_shell-0.1.7.tar.gz
  • Upload date:
  • Size: 270.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for chuk_virtual_shell-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9a48250df00123c3beb2df45467489d0b0997870b03738a79f96be3949502211
MD5 82fab7929238d531cadd69181498ec5f
BLAKE2b-256 08fefcfbf642941e66d2bb4d3a7d83bca1c80b8ac0038b39f67964d109023416

See more details on using hashes here.

File details

Details for the file chuk_virtual_shell-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for chuk_virtual_shell-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f258ddadb9827594fdf5b2d64722d00f2e17a3964579a2747f6ad9ccc4fce42d
MD5 db5b06e40cb67d78d1961172b7d5fdff
BLAKE2b-256 10f06511d44ca6dc04efa987f4bca1f96cae38595d480752cfa2cf290d3ca671

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page