Skip to content

Commit 8aa2f34

Browse files
committed
Refactor PID extraction to helper and fix busyInfo position in busy case
1 parent 2c2865c commit 8aa2f34

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

PowerShell.MCP.Proxy/Tools/PowerShellTools.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ private static string TruncatePipeline(string pipeline, int maxLength = 30)
182182
return normalized[..(maxLength - 3)] + "...";
183183
}
184184

185+
private static string GetPidString(string? pipeName)
186+
{
187+
if (pipeName == null) return "unknown";
188+
var pid = ConsoleSessionManager.GetPidFromPipeName(pipeName);
189+
return pid?.ToString() ?? "unknown";
190+
}
191+
185192
[McpServerTool]
186193
[Description("Retrieves the current location and all available drives (providers) from the PowerShell session. Returns current_location and other_drive_locations array. Call this when you need to understand the current PowerShell context, as users may change location during the session. When executing multiple invoke_expression commands in succession, calling once at the beginning is sufficient.")]
187194
public static async Task<string> GetCurrentLocation(
@@ -304,7 +311,7 @@ public static async Task<string> InvokeExpression(
304311
var (completedOutputs, busyStatusInfo) = await CollectAllCachedOutputsAsync(powerShellService, newPipeName, cancellationToken);
305312

306313
// Extract PID from pipe name (format: PowerShell.MCP.Communication.{PID})
307-
var pid = newPipeName?.Split('.').LastOrDefault() ?? "unknown";
314+
var pid = GetPidString(newPipeName);
308315

309316
// Build response: busy status first + message + startResult + completedOutputs
310317
var response = new StringBuilder();
@@ -335,7 +342,7 @@ public static async Task<string> InvokeExpression(
335342
var (completedOutputs, busyStatusInfo) = await CollectAllCachedOutputsAsync(powerShellService, readyPipeName, cancellationToken);
336343

337344
// Extract PID from pipe name (format: PowerShell.MCP.Communication.{PID})
338-
var pid = readyPipeName.Split('.').LastOrDefault() ?? "unknown";
345+
var pid = GetPidString(readyPipeName);
339346

340347
// Build response: busy status first + closedConsoleInfo + message + locationResult + completedOutputs
341348
var response = new StringBuilder();
@@ -393,11 +400,15 @@ public static async Task<string> InvokeExpression(
393400
var newPipeName = sessionManager.ActivePipeName;
394401
var (completedOutputs, busyInfo) = await CollectAllCachedOutputsAsync(powerShellService, newPipeName, cancellationToken);
395402

396-
var newPid = newPipeName?.Split('.').LastOrDefault() ?? "unknown";
403+
var newPid = GetPidString(newPipeName);
397404

398405
var busyResponse = new StringBuilder();
399-
// Busy status at the top
406+
// Busy status at the top (current pipe first, then other pipes)
400407
busyResponse.AppendLine(FormatBusyStatus(jsonResponse));
408+
if (busyInfo.Length > 0)
409+
{
410+
busyResponse.Append(busyInfo);
411+
}
401412
busyResponse.AppendLine();
402413
busyResponse.AppendLine($"Started new console PID#{newPid} with PowerShell.MCP module imported. Pipeline NOT executed - verify location and re-execute.");
403414
busyResponse.AppendLine();
@@ -411,11 +422,6 @@ public static async Task<string> InvokeExpression(
411422
busyResponse.AppendLine();
412423
busyResponse.Append(completedOutputs);
413424
}
414-
if (busyInfo.Length > 0)
415-
{
416-
busyResponse.AppendLine();
417-
busyResponse.Append(busyInfo);
418-
}
419425
return busyResponse.ToString();
420426
}
421427
break;

0 commit comments

Comments
 (0)