Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ A shell script execution server that allows AI models to execute shell scripts a

**Configuration:**
- `SHELL_CMD` - Environment variable to set the shell command to use (default: `sh -c`). Can include arguments, e.g., `bash -x` or `zsh`
- `SHELL_TIMEOUT_DISABLED` - Set to `true` to disable the timeout completely (default: timeout is 30 seconds)
- `SHELL_TIMEOUT` - Environment variable to set the timeout in seconds (default: 30 seconds)
- `SHELL_WORKING_DIR` - Environment variable to set the working directory for script execution (default: current directory)

**Input Format:**
Expand All @@ -546,14 +548,16 @@ A shell script execution server that allows AI models to execute shell scripts a
"exit_code": 0,
"success": true,
"error": ""
}
```

**Docker Image:**
```bash
docker run -e SHELL_CMD=bash ghcr.io/mudler/mcps/shell:latest
```

With timeout disabled:
```bash
docker run -e SHELL_CMD=bash -e SHELL_TIMEOUT_DISABLED=true ghcr.io/mudler/mcps/shell:latest
```

With custom working directory:
```bash
docker run -e SHELL_CMD=bash -e SHELL_WORKING_DIR=/workspace ghcr.io/mudler/mcps/shell:latest
Expand Down
2 changes: 2 additions & 0 deletions shell/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ RUN mkdir -p /tmp/shell-sessions
# Set environment variables
ENV SHELL_CMD="bash -c"
ENV SHELL_WORKING_DIR="/root"
ENV SHELL_TIMEOUT="30"
# Set SHELL_TIMEOUT_DISABLED=true to disable timeout completely
ENV TOOL_NAME=execute_command

# Run the MCP server
Expand Down
34 changes: 27 additions & 7 deletions shell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func getWorkingDirectory() string {
return os.Getenv("SHELL_WORKING_DIR")
}

// getTimeoutDisabled returns whether timeout is disabled via SHELL_TIMEOUT_DISABLED env var
func getTimeoutDisabled() bool {
timeoutDisabled := os.Getenv("SHELL_TIMEOUT_DISABLED")
return strings.ToLower(timeoutDisabled) == "true"
}

// getTimeout returns the default timeout from SHELL_TIMEOUT env var,
// or 30 seconds if not set or invalid
func getTimeout() int {
Expand All @@ -65,14 +71,28 @@ func ExecuteCommand(ctx context.Context, req *mcp.CallToolRequest, input Execute
ExecuteCommandOutput,
error,
) {
// Set default timeout if not provided
timeout := input.Timeout
if timeout <= 0 {
timeout = getTimeout()
// Determine timeout based on whether it's disabled
var timeout int
if getTimeoutDisabled() {
// Timeout is disabled - use 0 to indicate no timeout
timeout = 0
} else {
// Set default timeout if not provided
timeout = input.Timeout
if timeout <= 0 {
timeout = getTimeout()
}
}

// Create a context with timeout
cmdCtx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
// Create a context with timeout (or no timeout if disabled)
var cmdCtx context.Context
var cancel context.CancelFunc
if timeout > 0 {
cmdCtx, cancel = context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
} else {
cmdCtx = ctx
cancel = func() {}
}
defer cancel()

// Get shell command from environment variable (default: "sh -c")
Expand Down Expand Up @@ -181,7 +201,7 @@ func main() {
// Add tool for executing shell scripts
mcp.AddTool(server, &mcp.Tool{
Name: configurableName,
Description: "Execute a shell script and return the output, exit code, and any errors. The shell command can be configured via SHELL_CMD environment variable (default: 'sh -c'). The working directory can be set via SHELL_WORKING_DIR environment variable. The default timeout can be configured via SHELL_TIMEOUT environment variable (default: 30 seconds). An initialization script can be run before server startup via SHELL_INIT_SCRIPT environment variable.",
Description: "Execute a shell script and return the output, exit code, and any errors. The shell command can be configured via SHELL_CMD environment variable (default: 'sh -c'). The working directory can be set via SHELL_WORKING_DIR environment variable. The default timeout can be configured via SHELL_TIMEOUT environment variable (default: 30 seconds). Timeout can be disabled by setting SHELL_TIMEOUT_DISABLED=true. An initialization script can be run before server startup via SHELL_INIT_SCRIPT environment variable.",
}, ExecuteCommand)

// Run the server
Expand Down
Loading