Skip to content

Commit 1f4c3b4

Browse files
Add the home dir command to the preprocessor
1 parent a4f3f52 commit 1f4c3b4

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

mcp_nexus/Utilities/CommandPreprocessor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public string PreprocessCommand(string command)
113113
}
114114
}
115115

116+
// Handle !homedir (set home directory for extensions and configuration) - ensure directory exists
117+
else if (result.StartsWith("!homedir", StringComparison.OrdinalIgnoreCase))
118+
{
119+
var match = Regex.Match(result, @"^!homedir\s+(.+)$", RegexOptions.IgnoreCase);
120+
if (match.Success)
121+
{
122+
var path = match.Groups[1].Value.Trim().Trim('"').Trim('\'');
123+
EnsureDirectoryExists(path);
124+
}
125+
}
126+
116127
return result;
117128
}
118129

mcp_nexus/mcp_nexus.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
88
<WarningsAsErrors />
9-
<Version>1.0.6.83</Version>
10-
<AssemblyVersion>1.0.6.83</AssemblyVersion>
11-
<FileVersion>1.0.6.83</FileVersion>
9+
<Version>1.0.6.84</Version>
10+
<AssemblyVersion>1.0.6.84</AssemblyVersion>
11+
<FileVersion>1.0.6.84</FileVersion>
1212
<Product>MCP Nexus</Product>
1313
<Description>Model Context Protocol Server for Windows Debugging Tools</Description>
1414
<Company>CapulusCodeNinja</Company>

mcp_nexus_tests/Utilities/CommandPreprocessorTests.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,92 @@ public void PreprocessCommand_WithExistingDirectory_DoesNotFail()
201201
}
202202
}
203203

204+
[Theory]
205+
[InlineData("!homedir C:\\my\\home\\dir", "!homedir C:\\my\\home\\dir")]
206+
[InlineData("!homedir \"C:\\my\\home\\dir\"", "!homedir \"C:\\my\\home\\dir\"")]
207+
[InlineData("!homedir /mnt/c/my/home/dir", "!homedir C:\\my\\home\\dir")]
208+
[InlineData("!HOMEDIR C:\\test", "!HOMEDIR C:\\test")]
209+
public void PreprocessCommand_Homedir_ConvertsWslPaths(string input, string expected)
210+
{
211+
// Arrange
212+
string result = string.Empty;
213+
214+
try
215+
{
216+
// Act
217+
result = m_CommandPreprocessor.PreprocessCommand(input);
218+
219+
// Assert
220+
Assert.Equal(expected, result);
221+
}
222+
finally
223+
{
224+
// Cleanup - extract path from result for cleanup
225+
CleanupHomedirPath(result);
226+
}
227+
}
228+
229+
[Fact]
230+
public void PreprocessCommand_Homedir_CreatesDirectoryIfNotExists()
231+
{
232+
// Arrange
233+
var tempDir = Path.Combine(Path.GetTempPath(), "test_homedir_" + Guid.NewGuid().ToString("N")[..8]);
234+
var input = $"!homedir {tempDir}";
235+
236+
try
237+
{
238+
// Ensure directory doesn't exist initially
239+
if (Directory.Exists(tempDir))
240+
{
241+
Directory.Delete(tempDir, true);
242+
}
243+
244+
// Act
245+
var result = m_CommandPreprocessor.PreprocessCommand(input);
246+
247+
// Assert
248+
Assert.Equal($"!homedir {tempDir}", result);
249+
Assert.True(Directory.Exists(tempDir), "Directory should be created automatically");
250+
}
251+
finally
252+
{
253+
// Cleanup
254+
if (Directory.Exists(tempDir))
255+
{
256+
Directory.Delete(tempDir, true);
257+
}
258+
}
259+
}
260+
261+
[Fact]
262+
public void PreprocessCommand_Homedir_WithExistingDirectory_DoesNotFail()
263+
{
264+
// Arrange
265+
var tempDir = Path.Combine(Path.GetTempPath(), "test_homedir_existing_" + Guid.NewGuid().ToString("N")[..8]);
266+
var input = $"!homedir \"{tempDir}\"";
267+
268+
try
269+
{
270+
// Create directory first
271+
Directory.CreateDirectory(tempDir);
272+
273+
// Act
274+
var result = m_CommandPreprocessor.PreprocessCommand(input);
275+
276+
// Assert
277+
Assert.Equal($"!homedir \"{tempDir}\"", result);
278+
Assert.True(Directory.Exists(tempDir), "Directory should still exist");
279+
}
280+
finally
281+
{
282+
// Cleanup
283+
if (Directory.Exists(tempDir))
284+
{
285+
Directory.Delete(tempDir, true);
286+
}
287+
}
288+
}
289+
204290
/// <summary>
205291
/// Best-effort cleanup for any directories that may be created by CommandPreprocessor during tests.
206292
/// Parses .srcpath arguments and removes non-srv, non-UNC directories.
@@ -234,5 +320,35 @@ internal static void CleanupCreatedDirectoriesFromSrcpath(string command)
234320
}
235321
}
236322
}
323+
324+
/// <summary>
325+
/// Best-effort cleanup for any directories that may be created by CommandPreprocessor during !homedir tests.
326+
/// Parses !homedir argument and removes the directory.
327+
/// </summary>
328+
internal static void CleanupHomedirPath(string command)
329+
{
330+
if (string.IsNullOrWhiteSpace(command)) return;
331+
if (!command.StartsWith("!homedir", StringComparison.OrdinalIgnoreCase)) return;
332+
333+
// Extract argument after !homedir
334+
var firstSpace = command.IndexOf(' ');
335+
if (firstSpace < 0 || firstSpace + 1 >= command.Length) return;
336+
var path = command[(firstSpace + 1)..].Trim().Trim('"').Trim('\'');
337+
338+
// Skip UNC paths
339+
if (path.StartsWith("\\\\")) return;
340+
341+
try
342+
{
343+
if (Directory.Exists(path))
344+
{
345+
Directory.Delete(path, true);
346+
}
347+
}
348+
catch
349+
{
350+
// Ignore cleanup errors in tests
351+
}
352+
}
237353
}
238354
}

0 commit comments

Comments
 (0)