Skip to content

Commit aa0b34f

Browse files
committed
Add debug output to NamedPipeClient and fix test stderr reading
1 parent dfaf501 commit aa0b34f

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

.github/workflows/cross-platform-test.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,22 @@ jobs:
317317
Get-ChildItem /tmp -Filter "CoreFxPipe_*" -ErrorAction SilentlyContinue | ForEach-Object { Write-Host $_.FullName }
318318
}
319319
320+
# Kill process first, then read outputs
320321
if (-not $bgProcess.HasExited) {
321-
Write-Host "=== Available stdout ===" -ForegroundColor Yellow
322-
while ($bgProcess.StandardOutput.Peek() -ge 0) {
323-
Write-Host ([char]$bgProcess.StandardOutput.Read()) -NoNewline
324-
}
325-
Write-Host ""
326-
# Kill process to allow stderr task to complete
322+
Write-Host "Killing background process..."
327323
$bgProcess.Kill()
328324
$bgProcess.WaitForExit(5000)
329325
}
330326
327+
# Now read stdout (sync, process is dead)
328+
Write-Host "=== stdout ===" -ForegroundColor Yellow
329+
$stdoutTask = $bgProcess.StandardOutput.ReadToEndAsync()
330+
if ($stdoutTask.Wait(3000)) {
331+
Write-Host $stdoutTask.Result
332+
} else {
333+
Write-Host "(stdout read timeout)"
334+
}
335+
331336
# Wait for stderr task and display
332337
Write-Host "=== stderr (debug output) ===" -ForegroundColor Yellow
333338
if ($bgStderrTask.Wait(3000)) {

PowerShell.MCP.Proxy/Services/NamedPipeClient.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,50 @@ public class NamedPipeClient
99

1010
public async Task<string> SendRequestAsync(string arguments)
1111
{
12+
Console.Error.WriteLine($"[DEBUG] NamedPipeClient.SendRequestAsync called");
13+
Console.Error.WriteLine($"[DEBUG] Arguments length: {arguments.Length}");
14+
1215
try
1316
{
1417
if (!PowerShellProcessManager.IsPowerShellProcessRunning())
1518
{
19+
Console.Error.WriteLine("[DEBUG] PowerShell process not running");
1620
return $"The PowerShell 7 console is not running. Use start_powershell_console tool to start it first.";
1721
}
1822

23+
Console.Error.WriteLine($"[DEBUG] Creating NamedPipeClientStream for pipe: {PipeName}");
1924
using var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut);
2025

2126
// Attempt Named Pipe connection
2227
try
2328
{
29+
Console.Error.WriteLine("[DEBUG] Connecting to Named Pipe (3s timeout)...");
2430
await pipeClient.ConnectAsync(1000 * 3); // 3 second timeout
31+
Console.Error.WriteLine("[DEBUG] Connected to Named Pipe successfully");
2532
}
2633
catch (TimeoutException)
2734
{
35+
Console.Error.WriteLine("[DEBUG] Named Pipe connection timeout");
2836
return $"PowerShell 7 (pwsh) is running but PowerShell.MCP module is not imported.\r\n\r\nMANDATORY STEPS - DO NOT SKIP ANY:\r\n1. Explain the situation to the user\r\n2. Present exactly these two options:\r\n - Option 1: User imports PowerShell.MCP module manually in current console\r\n - Option 2: I will automatically start fresh console with PowerShell.MCP imported\r\n3. STOP and wait for explicit user response - DO NOT make any choice for the user\r\n4. ONLY if user explicitly chooses option 2, execute start_powershell_console\r\n5. If user chooses option 1, provide the command: Import-Module PowerShell.MCP\r\n6. DO NOT execute any PowerShell commands until user makes their choice\r\n\r\nCRITICAL: Never assume user preference or execute start_powershell_console without explicit user consent.";
2937
}
3038

3139
// Convert JSON message to UTF-8 bytes
3240
var messageBytes = Encoding.UTF8.GetBytes(arguments);
41+
Console.Error.WriteLine($"[DEBUG] Message bytes length: {messageBytes.Length}");
3342

3443
// Create 4-byte Little Endian message length
3544
var lengthBytes = BitConverter.GetBytes(messageBytes.Length);
45+
Console.Error.WriteLine($"[DEBUG] Sending length prefix: {messageBytes.Length}");
3646

3747
// Send message length prefix + JSON message body
3848
await pipeClient.WriteAsync(lengthBytes, 0, lengthBytes.Length);
3949
await pipeClient.WriteAsync(messageBytes, 0, messageBytes.Length);
50+
await pipeClient.FlushAsync();
51+
Console.Error.WriteLine("[DEBUG] Message sent, waiting for response...");
4052

4153
// Receive response: proper length prefix handling
4254
var response = await ReceiveMessageAsync(pipeClient);
55+
Console.Error.WriteLine($"[DEBUG] Response received, length: {response.Length}");
4356
return response;
4457
}
4558
catch (TimeoutException)
@@ -49,18 +62,22 @@ public async Task<string> SendRequestAsync(string arguments)
4962
}
5063
catch (Exception ex)
5164
{
52-
Console.Error.WriteLine($"[ERROR] Named Pipe communication failed: {ex.Message}");
65+
Console.Error.WriteLine($"[ERROR] Named Pipe communication failed: {ex.GetType().Name}: {ex.Message}");
66+
Console.Error.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
5367
return string.Empty;
5468
}
5569
}
5670

5771
private async Task<string> ReceiveMessageAsync(NamedPipeClientStream pipeClient)
5872
{
73+
Console.Error.WriteLine("[DEBUG] ReceiveMessageAsync: Reading length prefix...");
74+
5975
// 1. Read message length (4 bytes) reliably
6076
var lengthBuffer = new byte[4];
6177
await ReadExactAsync(pipeClient, lengthBuffer, 4);
6278

6379
var messageLength = BitConverter.ToInt32(lengthBuffer, 0);
80+
Console.Error.WriteLine($"[DEBUG] ReceiveMessageAsync: Message length = {messageLength}");
6481

6582
// 2. Validate message length
6683
if (messageLength < 0)
@@ -69,11 +86,14 @@ private async Task<string> ReceiveMessageAsync(NamedPipeClientStream pipeClient)
6986
}
7087

7188
// 3. Read message body reliably
89+
Console.Error.WriteLine($"[DEBUG] ReceiveMessageAsync: Reading message body...");
7290
var messageBuffer = new byte[messageLength];
7391
await ReadExactAsync(pipeClient, messageBuffer, messageLength);
7492

7593
// 4. UTF-8 decode
76-
return Encoding.UTF8.GetString(messageBuffer);
94+
var result = Encoding.UTF8.GetString(messageBuffer);
95+
Console.Error.WriteLine($"[DEBUG] ReceiveMessageAsync: Complete");
96+
return result;
7797
}
7898

7999
private async Task ReadExactAsync(NamedPipeClientStream pipeClient, byte[] buffer, int count)

0 commit comments

Comments
 (0)