From 69e4c340bbd8183cabe925d4db2df642f66d4fe9 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 11 Sep 2025 09:37:23 +0000 Subject: [PATCH] Fix MCP cleanup trap scoping issue causing exit errors This fixes bash "unbound variable" errors that occur when ClaudeBox exits with MCP servers configured. The issue was caused by EXIT traps not having access to local variables from the function where they're defined. Changes: - Simplified cleanup_mcp_files() to only use mcp_temp_files array - Added user_mcp_file to mcp_temp_files array for proper cleanup - Added parameter expansion safety (${mcp_temp_files[@]:-}) - Removed direct variable access in cleanup function The cleanup now relies entirely on the mcp_temp_files array which is accessible to the EXIT trap, rather than trying to access function- scoped local variables that are out of scope when the trap executes. Fixes the "user_mcp_file: unbound variable" error that occurred on every ClaudeBox exit when MCP servers were configured. --- lib/docker.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/docker.sh b/lib/docker.sh index 3e3fb50..327d983 100755 --- a/lib/docker.sh +++ b/lib/docker.sh @@ -296,17 +296,11 @@ run_claudebox_container() { # Set up cleanup trap for temporary MCP config files cleanup_mcp_files() { local file - for file in "${mcp_temp_files[@]}"; do + for file in "${mcp_temp_files[@]:-}"; do if [[ -f "$file" ]]; then rm -f "$file" fi done - if [[ -n "$user_mcp_file" ]] && [[ -f "$user_mcp_file" ]]; then - rm -f "$user_mcp_file" - fi - if [[ -n "$project_mcp_file" ]] && [[ -f "$project_mcp_file" ]]; then - rm -f "$project_mcp_file" - fi } trap cleanup_mcp_files EXIT @@ -315,6 +309,7 @@ run_claudebox_container() { user_mcp_file=$(create_mcp_config_file "$HOME/.claude.json" "") if [[ -n "$user_mcp_file" ]]; then + mcp_temp_files+=("$user_mcp_file") local user_count=$(jq '.mcpServers | length' "$user_mcp_file" 2>/dev/null || echo "0") if [[ "$user_count" -gt 0 ]]; then if [[ "$VERBOSE" == "true" ]]; then @@ -358,6 +353,7 @@ run_claudebox_container() { local project_count=$(jq '.mcpServers | length' "$temp_project_file" 2>/dev/null || echo "0") if [[ "$project_count" -gt 0 ]]; then project_mcp_file="$temp_project_file" + # Note: temp_project_file already added to mcp_temp_files above if [[ "$VERBOSE" == "true" ]]; then printf "Found %s project MCP servers\n" "$project_count" >&2 fi