Skip to content

Commit 5e7f497

Browse files
committed
feat: Add configurable npm executable path and UI setting
1 parent 1aaca03 commit 5e7f497

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

Editor/UnityBridge/McpUnityEditorWindow.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ private void DrawServerTab()
141141
settings.SaveSettings();
142142
}
143143

144+
EditorGUILayout.Space();
145+
146+
// NPM Executable Path
147+
string newNpmPath = EditorGUILayout.TextField(new GUIContent("NPM Executable Path", "Optional: Full path to the npm executable (e.g., /Users/user/.asdf/shims/npm or C:\\path\\to\\npm.cmd). If not set, 'npm' from the system PATH will be used."), settings.NpmExecutablePath);
148+
if (newNpmPath != settings.NpmExecutablePath)
149+
{
150+
settings.NpmExecutablePath = newNpmPath;
151+
settings.SaveSettings();
152+
}
153+
144154
EditorGUILayout.Space();
145155

146156
// Server control buttons

Editor/UnityBridge/McpUnitySettings.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ public class McpUnitySettings
1616
public const string ServerVersion = "1.0.0";
1717
public const string PackageName = "com.gamelovers.mcp-unity";
1818
public const int RequestTimeoutMinimum = 10;
19-
20-
private const string EnvUnityPort = "UNITY_PORT";
21-
private const string EnvUnityRequestTimeout = "UNITY_REQUEST_TIMEOUT";
19+
20+
// Paths
2221
private const string SettingsPath = "ProjectSettings/McpUnitySettings.json";
2322

2423
private static McpUnitySettings _instance;
@@ -35,6 +34,9 @@ public class McpUnitySettings
3534
[Tooltip("Whether to show info logs in the Unity console")]
3635
public bool EnableInfoLogs = true;
3736

37+
[Tooltip("Optional: Full path to the npm executable (e.g., /Users/user/.asdf/shims/npm or C:\\path\\to\\npm.cmd). If not set, 'npm' from the system PATH will be used.")]
38+
public string NpmExecutablePath = string.Empty;
39+
3840
/// <summary>
3941
/// Singleton instance of settings
4042
/// </summary>

Editor/Utils/McpUtils.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,26 +306,37 @@ private static string GetCursorConfigPath()
306306
/// <param name="workingDirectory">The working directory where the npm command should be executed.</param>
307307
public static void RunNpmCommand(string arguments, string workingDirectory)
308308
{
309-
string shellCommand = "/bin/bash";
310-
string shellArguments = $"-c \"npm {arguments}\"";
311-
312-
if (Application.platform == RuntimePlatform.WindowsEditor)
313-
{
314-
shellCommand = "cmd.exe";
315-
shellArguments = $"/c npm {arguments}";
316-
}
309+
string npmExecutable = McpUnitySettings.Instance.NpmExecutablePath;
310+
bool useCustomNpmPath = !string.IsNullOrWhiteSpace(npmExecutable);
317311

318312
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
319313
{
320-
FileName = shellCommand,
321-
Arguments = shellArguments,
322314
WorkingDirectory = workingDirectory,
323315
RedirectStandardOutput = true,
324316
RedirectStandardError = true,
325-
UseShellExecute = false,
317+
UseShellExecute = false, // Important for redirection and direct execution
326318
CreateNoWindow = true
327319
};
328320

321+
if (useCustomNpmPath)
322+
{
323+
// Use the custom path directly
324+
startInfo.FileName = npmExecutable;
325+
startInfo.Arguments = arguments;
326+
}
327+
else if (Application.platform == RuntimePlatform.WindowsEditor)
328+
{
329+
// Fallback to cmd.exe to find 'npm' in PATH
330+
startInfo.FileName = "cmd.exe";
331+
startInfo.Arguments = $"/c npm {arguments}";
332+
}
333+
else // macOS / Linux
334+
{
335+
// Fallback to /bin/bash to find 'npm' in PATH
336+
startInfo.FileName = "/bin/bash";
337+
startInfo.Arguments = $"-c \"npm {arguments}\"";
338+
}
339+
329340
try
330341
{
331342
using (var process = System.Diagnostics.Process.Start(startInfo))

0 commit comments

Comments
 (0)