Skip to content

Commit 4b1ae3d

Browse files
adwsinghrhernandez35
authored andcommitted
Modify only a single shell config and not all of them
1 parent 59fe467 commit 4b1ae3d

File tree

1 file changed

+59
-38
lines changed
  • mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli

1 file changed

+59
-38
lines changed

mcp/mcp-cli-api/src/main/java/software/amazon/smithy/java/mcp/cli/ConfigUtils.java

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.LinkedHashMap;
2222
import java.util.List;
23+
import java.util.Map;
2324
import java.util.Optional;
2425
import java.util.ServiceLoader;
2526
import java.util.Set;
@@ -63,6 +64,18 @@ private ConfigUtils() {}
6364
private static final Path SHIMS_DIR = CONFIG_DIR.resolve("mcp-servers");
6465
private static final Path CONFIG_PATH = CONFIG_DIR.resolve("config.json");
6566

67+
private static final Map<String, String> SHELL_TO_CONFIG = Map.of(
68+
"zsh",
69+
".zshrc",
70+
"bash",
71+
".bashrc",
72+
"sh",
73+
".profile",
74+
"dash",
75+
".profile",
76+
"ksh",
77+
".profile");
78+
6679
private static final DefaultConfigProvider DEFAULT_CONFIG_PROVIDER;
6780

6881
static {
@@ -410,46 +423,54 @@ public static void ensureMcpServersDirInPath() {
410423
private static boolean tryAddToShellConfigs(String shimsDirStr) {
411424
var pathExport = "export PATH=\"" + shimsDirStr + ":$PATH\"";
412425
var comment = "# Added by smithy-mcp";
413-
var configFiles = List.of(".zshrc", ".bashrc", ".profile", ".bash_profile");
414-
415-
String addedTo = null;
416-
417-
for (var configFile : configFiles) {
418-
var configPath = resolveFromHomeDir(configFile);
419-
420-
if (Files.exists(configPath) && Files.isWritable(configPath)) {
421-
try {
422-
// Check if already present
423-
var lines = new ArrayList<>(Files.readAllLines(configPath, StandardCharsets.UTF_8));
424-
boolean alreadyPresent = lines.stream()
425-
.anyMatch(line -> line.contains(shimsDirStr)
426-
&& line.contains("PATH")
427-
&& !(line.trim().startsWith("#")));
428-
429-
if (!alreadyPresent) {
430-
// Add the export statement
431-
lines.add("");
432-
lines.add(comment);
433-
lines.add(pathExport);
434-
435-
Files.write(configPath,
436-
lines,
437-
StandardCharsets.UTF_8,
438-
StandardOpenOption.WRITE,
439-
StandardOpenOption.TRUNCATE_EXISTING);
440-
441-
System.out.println("Added mcp-servers directory to PATH in " + configFile);
442-
addedTo = configFile;
443-
}
444-
} catch (IOException e) {
445-
// Continue to next config file if this one fails
446-
}
447-
}
426+
427+
// Get the current shell from SHELL environment variable
428+
String shellPath = System.getenv("SHELL");
429+
String configFile = null;
430+
431+
if (shellPath != null) {
432+
// Extract shell name from path (e.g., "/bin/zsh" -> "zsh")
433+
String shellName = shellPath.substring(shellPath.lastIndexOf('/') + 1);
434+
configFile = SHELL_TO_CONFIG.get(shellName);
448435
}
449436

450-
if (addedTo != null) {
451-
System.out.println("Please restart your shell or run 'source ~/" + configFiles + "' to reload your PATH");
452-
return true;
437+
// If we don't have a mapping for the shell or SHELL is not set, fall back to original behavior
438+
if (configFile == null) {
439+
return false;
440+
}
441+
442+
var configPath = resolveFromHomeDir(configFile);
443+
444+
if (Files.exists(configPath) && Files.isWritable(configPath)) {
445+
try {
446+
// Check if already present
447+
var lines = new ArrayList<>(Files.readAllLines(configPath, StandardCharsets.UTF_8));
448+
boolean alreadyPresent = lines.stream()
449+
.anyMatch(line -> line.contains(shimsDirStr)
450+
&& line.contains("PATH")
451+
&& !(line.trim().startsWith("#")));
452+
453+
if (!alreadyPresent) {
454+
// Add the export statement
455+
lines.add("");
456+
lines.add(comment);
457+
lines.add(pathExport);
458+
459+
Files.write(configPath,
460+
lines,
461+
StandardCharsets.UTF_8,
462+
StandardOpenOption.WRITE,
463+
StandardOpenOption.TRUNCATE_EXISTING);
464+
465+
System.out.println("Added mcp-servers directory to PATH in " + configFile);
466+
System.out.println(
467+
"Please restart your shell or run 'source ~/" + configFile + "' to reload your PATH");
468+
return true;
469+
}
470+
return true; // Already present, so consider it successful
471+
} catch (IOException e) {
472+
// Fall through to return false
473+
}
453474
}
454475
return false;
455476
}

0 commit comments

Comments
 (0)