PowerShell.MCP.psm1
|
# PowerShell.MCP Module Script # Provides automatic cleanup when Remove-Module is executed # On Linux/macOS, PSReadLine interferes with timer events # Remove it to ensure MCP polling works correctly if (-not $IsWindows) { Remove-Module PSReadLine -ErrorAction SilentlyContinue } # Set OnRemove script block to execute cleanup automatically $ExecutionContext.SessionState.Module.OnRemove = { try { #Write-Host "[PowerShell.MCP] Module removal detected, starting cleanup..." -ForegroundColor Yellow # Load and execute MCPCleanup.ps1 from embedded resources $assembly = [System.Reflection.Assembly]::GetAssembly([PowerShell.MCP.MCPModuleInitializer]) $resourceName = "PowerShell.MCP.Resources.MCPCleanup.ps1" $stream = $assembly.GetManifestResourceStream($resourceName) if ($stream) { $reader = New-Object System.IO.StreamReader($stream) $cleanupScript = $reader.ReadToEnd() $reader.Close() $stream.Close() # Execute cleanup script Invoke-Expression $cleanupScript #Write-Host "[PowerShell.MCP] OnRemove cleanup completed" -ForegroundColor Green } else { #Write-Warning "[PowerShell.MCP] MCPCleanup.ps1 resource not found" } } catch { #Write-Warning "[PowerShell.MCP] Error during module removal cleanup: $($_.Exception.Message)" } } #Write-Host "[PowerShell.MCP] Module loaded with OnRemove cleanup support" -ForegroundColor Green <# .SYNOPSIS Gets the path to the PowerShell.MCP.Proxy executable for the current platform. .DESCRIPTION Returns the full path to the platform-specific PowerShell.MCP.Proxy executable. Use this path in your MCP client configuration. .PARAMETER Escape If specified, escapes backslashes for use in JSON configuration files. .EXAMPLE Get-MCPProxyPath Returns: C:\Program Files\PowerShell\7\Modules\PowerShell.MCP\bin\win-x64\PowerShell.MCP.Proxy.exe .EXAMPLE Get-MCPProxyPath -Escape Returns: C:\\Program Files\\PowerShell\\7\\Modules\\PowerShell.MCP\\bin\\win-x64\\PowerShell.MCP.Proxy.exe .OUTPUTS System.String #> function Get-MCPProxyPath { [CmdletBinding()] [OutputType([string])] param( [switch]$Escape ) $moduleBase = $PSScriptRoot $binFolder = Join-Path $moduleBase 'bin' # Determine RID based on OS and architecture $rid = if ($IsWindows) { switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) { 'X64' { 'win-x64' } 'Arm64' { 'win-arm64' } default { 'win-x64' } } } elseif ($IsMacOS) { switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) { 'X64' { 'osx-x64' } 'Arm64' { 'osx-arm64' } default { 'osx-x64' } } } elseif ($IsLinux) { switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture) { 'X64' { 'linux-x64' } 'Arm64' { 'linux-arm64' } 'Arm' { 'linux-arm' } default { 'linux-x64' } } } else { throw "Unsupported operating system" } # Determine executable name $exeName = if ($IsWindows) { 'PowerShell.MCP.Proxy.exe' } else { 'PowerShell.MCP.Proxy' } $proxyPath = Join-Path $binFolder $rid $exeName if (-not (Test-Path $proxyPath)) { throw "PowerShell.MCP.Proxy not found at: $proxyPath. Please ensure the module is properly installed for your platform ($rid)." } if ($Escape) { return $proxyPath.Replace('\', '\\') } return $proxyPath } Export-ModuleMember -Function Get-MCPProxyPath |