Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Public/Start-McpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ function Start-McpServer {
[string[]]$Tools
)

# Get parent process ID to monitor for disconnection
$currentProcess = Get-Process -Id $PID
if ($null -eq $currentProcess.Parent) {
Write-Log -LogEntry @{ Level = 'Error'; Message = "Current process has no parent process. Shutting down MCP server..." }
return
}
$parentProcessId = $currentProcess.Parent.Id
Write-Log -LogEntry @{ Level = 'Info'; Message = "MCP Server started with PID: $PID, Parent PID: $parentProcessId" }

# Check if the tools are provided
if (-not $Tools) {
Write-Log -LogEntry @{ Level = 'Error'; Message = "No tools provided to Start-McpServer" }
Expand All @@ -13,6 +22,20 @@ function Start-McpServer {

Write-Log -LogEntry @{ Level = 'Info'; Message = "Starting MCP Server" }
while ($true) {

# Check if parent process is still running otherwise exit
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable $parentProcessId is not defined anywhere in the function. It's not included in the function parameters, and there's no evidence of it being set in the module scope. This code will fail at runtime because PowerShell will not be able to resolve this variable. Consider adding $parentProcessId as a parameter to the function, or use the automatic variable $PID with a mechanism to track the parent process ID.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops :) Sorry, seems I forgot to insert from the beginning 🤦‍♂️ Will fix it now

    # Get parent process ID to monitor for disconnection
    $currentProcess = Get-Process -Id $PID
    $parentProcessId = $currentProcess.Parent.Id
    Write-Log -LogEntry @{ Level = 'Info'; Message = "MCP Server started with PID: $PID, Parent PID: $parentProcessId" }

if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
Comment on lines +29 to +32
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check is unreachable code. When Get-Process -ErrorAction Stop succeeds, it will always return a process object, never null. If the process doesn't exist, it throws an exception which is caught by the catch block. This condition at line 20-23 will never execute. The catch block already handles the case when the parent process is not found.

Suggested change
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}

Copilot uses AI. Check for mistakes.
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
Comment on lines 15 to +37
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if the parent process is running on every iteration of the while loop could impact performance, especially when processing many requests. Consider checking the parent process less frequently, such as every N iterations or using a timer-based approach, to reduce the overhead while still catching orphaned processes in a reasonable timeframe.

Suggested change
while ($true) {
# Check if parent process is still running otherwise exit
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
# Control how often we check whether the parent process is still running
$parentCheckInterval = 100
$iterationCount = 0
while ($true) {
$iterationCount++
# Check if parent process is still running otherwise exit (only every N iterations)
if ($iterationCount % $parentCheckInterval -eq 0) {
try {
$parentProcess = Get-Process -Id $parentProcessId -ErrorAction Stop
if ($null -eq $parentProcess) {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process has exited. Shutting down MCP server..." }
exit
}
}
catch {
Write-Log -LogEntry @{ Level = 'Info'; Message = "Parent process not found. Shutting down MCP server..." }
exit
}
}

Copilot uses AI. Check for mistakes.

$inputLine = [Console]::In.ReadLine()
if ([string]::IsNullOrEmpty($inputLine)) { continue }
try {
Expand Down