powershell-mcp 0.1.2

Hybrid vector + BM25 RAG over the official PowerShell documentation (MicrosoftDocs/PowerShell-Docs), served as an MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

function Step-ValidateEnvironment {
    Write-Step "Validating Environment"
    
    try {
        # Check required files
        $requiredFiles = @("Cargo.toml")
        foreach ($file in $requiredFiles) {
            $filePath = Join-Path $ProjectRoot $file
            if (-not (Test-Path $filePath)) {
                throw "Required file not found: $file"
            }
            Write-Host "[OK] Found: $file" -ForegroundColor Green
        }
        
        # Check directory structure
        $requiredDirs = @("src", "tests")
        foreach ($dir in $requiredDirs) {
            $dirPath = Join-Path $ProjectRoot $dir
            if (-not (Test-Path $dirPath)) {
                Write-Warning "Recommended directory not found: $dir"
            }
            else {
                Write-Host "[OK] Found: $dir/" -ForegroundColor Green
            }
        }
    }
    catch {
        Write-Error "Environment validation failed: $_"
        throw
    }
    finally {
        Write-EndStep
    }
}

function Step-Clean {
    Write-Step "Cleaning Build Artifacts"
    
    Push-Location $ProjectRoot
    try {
        # Clean specific targets, but protect the 'build' folder
        $cleanTargets = @(
            "target",
            "dist",
            ".pytest_cache",
            "*.egg-info",
            "__pycache__",
            "Cargo.lock"
        )
        
        foreach ($target in $cleanTargets) {
            Get-ChildItem -Path $target -Recurse -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "[OK] Cleaned: $target" -ForegroundColor Gray
        }
        
        # Clean specific files in build directory but preserve important files
        $buildDir = Join-Path $ProjectRoot "build"
        if (Test-Path $buildDir) {
            Write-Host "Cleaning build directory (preserving scripts and requirements)..." -ForegroundColor Gray
            
            # Files to preserve in build directory
            $preservePatterns = @(
                "*.ps1",           # PowerShell scripts
                "*.bat",           # Batch files
                "*.cmd",           # Command files
                "*.sh",            # Shell scripts
                "*requirements*.txt", # Requirements files
                "*.toml",          # Config files
                "*.yml",           # YAML files
                "*.yaml",          # YAML files
                "*.json",          # JSON config files
                "*.md",            # Documentation
                "*.txt"            # Text files (including requirements)
            )
            
            # Get all files in build directory
            $allFiles = Get-ChildItem -Path $buildDir -Recurse -Force -File -ErrorAction SilentlyContinue
            
            # Filter out files to preserve
            $filesToDelete = $allFiles | Where-Object {
                $file = $_
                $shouldPreserve = $false
                
                foreach ($pattern in $preservePatterns) {
                    if ($file.Name -like $pattern) {
                        $shouldPreserve = $true
                        break
                    }
                }
                
                return -not $shouldPreserve
            }
            
            # Delete only the files we don't want to preserve
            foreach ($file in $filesToDelete) {
                try {
                    Remove-Item $file.FullName -Force -ErrorAction SilentlyContinue
                    Write-Host "  Removed: $($file.Name)" -ForegroundColor DarkGray
                }
                catch {
                    Write-Warning "Could not remove: $($file.Name)"
                }
            }
            
            # Show preserved files
            $preservedFiles = Get-ChildItem -Path $buildDir -Recurse -Force -File -ErrorAction SilentlyContinue
            if ($preservedFiles.Count -gt 0) {
                Write-Host "  Preserved files:" -ForegroundColor Cyan
                foreach ($file in $preservedFiles) {
                    Write-Host "    - $($file.Name)" -ForegroundColor Green
                }
            }
            
            Write-Host "[OK] Cleaned build directory (preserved important files)" -ForegroundColor Gray
        }
        
        # Clean Cargo specifically
        if (Test-Command "cargo") {
            Write-Host "Running cargo clean..." -ForegroundColor Gray
            & cargo clean
        }
        
        Write-Host "[OK] Clean completed" -ForegroundColor Green
        
    }
    catch {
        Write-Error "Clean failed: $_"
        throw
    }
    finally {
        Pop-Location
        Write-EndStep
    }
}

function Step-Build {
    Write-Step "Building Rust Extension"
    
    Push-Location $ProjectRoot
    try {
        # Set build environment
        if ($Native) {
            $env:RUSTFLAGS = "-C target-cpu=native"
            Write-Host "  Using native CPU optimizations" -ForegroundColor Cyan
        }
        else {
            $env:RUSTFLAGS = ""
        }
        $env:RUST_BACKTRACE = if ($Verbose) { "full" } else { "1" }
        
        # Verify maturin is available
        if (-not (Test-Path $MaturinExe)) {
            throw "Maturin not found: $MaturinExe"
        }
        
        $maturinVersion = & $MaturinExe --version
        Write-Host "[OK] Using: $maturinVersion" -ForegroundColor Green
        
        Write-Host "[OK] Environment configured" -ForegroundColor Green
        Write-Host "  RUSTFLAGS: $env:RUSTFLAGS" -ForegroundColor Gray
        Write-Host "  RUST_BACKTRACE: $env:RUST_BACKTRACE" -ForegroundColor Gray
        
    }
    catch {
        Write-Error "Build setup failed: $_"
        throw
    }
    finally {
        Pop-Location
        Write-EndStep
    }
}

function Step-CreateWheel {
    Write-Step "Creating Python Wheel"
    
    Push-Location $ProjectRoot
    try {
        # Build arguments
        $buildArgs = @("build")
        
        if ($Configuration -eq "Release") {
            $buildArgs += "--release"
        }
        
        $buildArgs += "--strip"  # Strip debug symbols for smaller wheels
        
        if ($Verbose) {
            $buildArgs += "--verbose"
        }
        
        Write-Host "Building wheel with: maturin $($buildArgs -join ' ')" -ForegroundColor Gray
        
        $buildStart = Get-Date
        & $MaturinExe @buildArgs
        $buildDuration = (Get-Date) - $buildStart
        
        if ($LASTEXITCODE -ne 0) {
            throw "Maturin build failed with exit code: $LASTEXITCODE"
        }
        
        Write-Host "[OK] Wheel built successfully in $($buildDuration.TotalSeconds.ToString('F1'))s" -ForegroundColor Green
        
        # List created wheels
        if (Test-Path $WheelsDir) {
            $wheels = Get-ChildItem -Path "$WheelsDir\*.whl"
            Write-Host "Created wheels:" -ForegroundColor Cyan
            foreach ($wheel in $wheels) {
                $size = [math]::Round($wheel.Length / 1MB, 2)
                Write-Host "  - $($wheel.Name) ($size MB)" -ForegroundColor Gray
            }
        }
        
    }
    catch {
        Write-Error "Wheel creation failed: $_"
        throw
    }
    finally {
        Pop-Location
        Write-EndStep
    }
}

function Step-InstallWheel {
    Write-Step "Installing RCLIENT Wheel"
    
    Push-Location $ProjectRoot
    try {
        # Find the latest wheel
        if (-not (Test-Path $WheelsDir)) {
            throw "Wheels directory not found: $WheelsDir"
        }
        
        $wheels = Get-ChildItem -Path "$WheelsDir\*.whl" | Sort-Object LastWriteTime -Descending
        if ($wheels.Count -eq 0) {
            throw "No wheel files found in $WheelsDir"
        }
        
        $latestWheel = $wheels[0]
        Write-Host "Installing wheel: $($latestWheel.Name)" -ForegroundColor Cyan
        
        # Install the wheel with force-reinstall (this will handle existing installations automatically)
        Write-Host "Installing/reinstalling wheel: $($latestWheel.Name)" -ForegroundColor Gray
        & $PythonExe -m pip install $latestWheel.FullName --force-reinstall --no-deps --quiet
        if ($LASTEXITCODE -ne 0) {
            throw "Failed to install wheel: $($latestWheel.Name)"
        }
        
        # Verify installation
        $testScript = @"
try:
    import rclient
    version = getattr(rclient, '__version__', 'unknown')
    location = getattr(rclient, '__file__', 'unknown')
    print('[OK] RCLIENT {0} installed successfully'.format(version))
    print('Location: {0}'.format(location))
except ImportError as e:
    print('[ERROR] Failed to import RCLIENT: {0}'.format(e))
    exit(1)
"@
        
        $result = & $PythonExe -c $testScript
        if ($LASTEXITCODE -ne 0) {
            throw "RCLIENT installation verification failed"
        }
        
        Write-Host $result -ForegroundColor Green
        
    }
    catch {
        Write-Error "Wheel installation failed: $_"
        throw
    }
    finally {
        Pop-Location
        Write-EndStep
    }
}

function Step-RunTests {
    Write-Step "Running Test Suite"
    
    Push-Location $ProjectRoot
    try {
        # Clean up existing pytest temp directories in system temp
        $userTemp = "$env:LOCALAPPDATA\Temp"
        $pytestTempPattern = "pytest-of-*"
        Get-ChildItem -Path $userTemp -Filter $pytestTempPattern -Directory -ErrorAction SilentlyContinue | 
        ForEach-Object { 
            try { 
                Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
                Write-Host "Cleaned up system temp: $($_.Name)" -ForegroundColor Gray
            }
            catch { 
                Write-Warning "Could not clean system temp: $($_.Name)"
            }
        }

        $testsDir = "tests"
        if (-not (Test-Path $testsDir)) {
            Write-Warning "Tests directory not found: $testsDir"
            Add-TestResult -TestName "Test Discovery" -Status "skipped" -Message "No tests directory found"
            return
        }
        
        # Discover test files
        $testFiles = Get-ChildItem -Path $testsDir -Filter "test_*.py" -Recurse
        Write-Host "Discovered $($testFiles.Count) test files:" -ForegroundColor Cyan
        foreach ($file in $testFiles) {
            Write-Host "  - $($file.Name)" -ForegroundColor Gray
        }
        
        if ($testFiles.Count -eq 0) {
            Write-Warning "No test files found"
            Add-TestResult -TestName "Test Discovery" -Status "skipped" -Message "No test files found"
            return
        }
        
        # Set custom temp directory for pytest (pytest will create it automatically)
        $customTempDir = Join-Path $ProjectRoot "pytest-temp"
        
        # Prepare pytest arguments
        $pytestArgs = @(
            $testsDir,
            "-v",
            "--tb=short",
            "--strict-markers",
            "--basetemp=$customTempDir"  # pytest creates this automatically
        )
        
        # Add JSON report if pytest-json-report is available
        try {
            & $PythonExe -c "import pytest_jsonreport" 2>$null
            if ($LASTEXITCODE -eq 0) {
                $pytestArgs += "--json-report"
                $pytestArgs += "--json-report-file=$OutputPath\test-report.json"
            }
        }
        catch {}
        
        # Add HTML report if pytest-html is available
        try {
            & $PythonExe -c "import pytest_html" 2>$null
            if ($LASTEXITCODE -eq 0) {
                $pytestArgs += "--html=$OutputPath\test-report.html"
                $pytestArgs += "--self-contained-html"
            }
        }
        catch {}
        
        if ($Coverage) {
            $pytestArgs += @(
                "--cov=rclient",
                "--cov-report=html:$OutputPath\coverage-html",
                "--cov-report=xml:$OutputPath\coverage.xml",
                "--cov-report=term-missing"
            )
        }
        
        if ($FailFast) {
            $pytestArgs += "-x"  # Stop on first failure
        }
        
        # Disable parallel execution to avoid temp directory permission issues
        # Comment out xdist to run tests sequentially
        # try {
        #     & $PythonExe -c "import xdist" 2>$null
        #     if ($LASTEXITCODE -eq 0) {
        #         $pytestArgs += "-n"
        #         $pytestArgs += "auto"  # Use all available cores
        #     }
        # } catch {}
        
        Write-Host "Running pytest with: $($pytestArgs -join ' ')" -ForegroundColor Gray
        
        $testStart = Get-Date
        & $PythonExe -m pytest @pytestArgs
        $testExitCode = $LASTEXITCODE
        $testDuration = (Get-Date) - $testStart
        
        Write-Host "[OK] Tests completed in $($testDuration.TotalSeconds.ToString('F1'))s" -ForegroundColor Green
        
        # Parse test results if JSON report exists
        $testReportPath = Join-Path $OutputPath "test-report.json"
        if (Test-Path $testReportPath) {
            try {
                $testReport = Get-Content $testReportPath | ConvertFrom-Json
                # Fix: Handle null/empty test result values properly
                $Global:TestResults.Total = if ($testReport.summary.total) { $testReport.summary.total } else { 0 }
                $Global:TestResults.Passed = if ($testReport.summary.passed) { $testReport.summary.passed } else { 0 }
                $Global:TestResults.Failed = if ($testReport.summary.failed) { $testReport.summary.failed } else { 0 }
                $Global:TestResults.Skipped = if ($testReport.summary.skipped) { $testReport.summary.skipped } else { 0 }
                
                Write-Host "Test Results:" -ForegroundColor Cyan
                Write-Host "  Total: $($Global:TestResults.Total)" -ForegroundColor White
                Write-Host "  Passed: $($Global:TestResults.Passed)" -ForegroundColor Green
                Write-Host "  Failed: $($Global:TestResults.Failed)" -ForegroundColor $(if ($Global:TestResults.Failed -eq 0) { 'Green' } else { 'Red' })
                Write-Host "  Skipped: $($Global:TestResults.Skipped)" -ForegroundColor Yellow
                
            }
            catch {
                Write-Warning "Could not parse test report: $_"
            }
        }
        
        # Clean up custom temp directory after tests
        if (Test-Path $customTempDir) {
            Remove-Item $customTempDir -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "Cleaned up pytest temp directory" -ForegroundColor Gray
        }
        
        # Handle test results
        if ($testExitCode -eq 0) {
            Write-Host "[OK] All tests passed!" -ForegroundColor Green
        }
        elseif ($testExitCode -eq 5) {
            Write-Warning "No tests were collected"
        }
        else {
            Write-Error "Tests failed with exit code: $testExitCode"
            if ($FailFast) {
                throw "Tests failed and fail-fast is enabled"
            }
        }
        
    }
    catch {
        Write-Error "Test execution failed: $_"
        throw
    }
    finally {
        Pop-Location
        Write-EndStep
    }
}

function Step-CollectArtifacts {
    Write-Step "Collecting Artifacts"
    
    try {
        # Copy wheels
        if (Test-Path $WheelsDir) {
            Get-ChildItem -Path "$WheelsDir\*.whl" | ForEach-Object {
                Copy-Item -Path $_.FullName -Destination $ArtifactsPath -Force
                Write-Host "[OK] Collected: $($_.Name)" -ForegroundColor Green
            }
        }
        
        # Copy test reports
        $reports = @("test-report.html", "test-report.json", "coverage.xml")
        foreach ($report in $reports) {
            $reportPath = Join-Path $OutputPath $report
            if (Test-Path $reportPath) {
                Copy-Item -Path $reportPath -Destination $ArtifactsPath -Force
                Write-Host "[OK] Collected: $report" -ForegroundColor Green
            }
        }
        
        # Copy coverage HTML report
        $coverageHtml = Join-Path $OutputPath "coverage-html"
        if (Test-Path $coverageHtml) {
            Copy-Item -Path $coverageHtml -Destination $ArtifactsPath -Recurse -Force
            Write-Host "[OK] Collected: coverage-html/" -ForegroundColor Green
        }
        
        # Create build manifest
        $manifest = @{
            BuildTime     = $Global:BuildStartTime.ToString("yyyy-MM-dd HH:mm:ss")
            Configuration = $Configuration
            Platform      = $Platform
            PythonVersion = $PythonVersion
            TestResults   = $Global:TestResults
            Artifacts     = Get-ChildItem -Path $ArtifactsPath -File | ForEach-Object { $_.Name }
        }
        
        $manifest | ConvertTo-Json -Depth 5 | Out-File (Join-Path $ArtifactsPath "build-manifest.json")
        Write-Host "[OK] Build manifest created" -ForegroundColor Green
        
    }
    catch {
        Write-Error "Artifact collection failed: $_"
        throw
    }
    finally {
        Write-EndStep
    }
}

function Step-GenerateReport {
    Write-Step "Generating Build Report"
    
    try {
        $duration = (Get-Date) - $Global:BuildStartTime
        $durationStr = "{0:hh\:mm\:ss}" -f $duration
        
        $buildStatus = if ($Global:TestResults.Failed -eq 0) { "BUILD SUCCESSFUL" } else { "BUILD FAILED" }
        
        $reportParams = @{
            BuildTime     = $Global:BuildStartTime.ToString("yyyy-MM-dd HH:mm:ss")
            Duration      = $durationStr
            Configuration = $Configuration
            Platform      = $Platform
            TestResults   = $Global:TestResults
            BuildStatus   = $buildStatus
            Artifacts     = Get-ChildItem -Path $ArtifactsPath -File | ForEach-Object { $_.Name }
        }
        
        $reportContent = @"
# RCLIENT Build Report

**Build Time:** $($reportParams.BuildTime)  
**Duration:** $($reportParams.Duration)  
**Configuration:** $($reportParams.Configuration)  
**Platform:** $($reportParams.Platform)  

## Test Results
- **Total Tests:** $($reportParams.TestResults.Total)
- **Passed:** $($reportParams.TestResults.Passed)
- **Failed:** $($reportParams.TestResults.Failed)
- **Skipped:** $($reportParams.TestResults.Skipped)

## Build Status
$($reportParams.BuildStatus)

## Artifacts
$($reportParams.Artifacts -join "`n- ")

---
*Generated by RCLIENT CI/CD Pipeline (PowerShell Edition)*
"@
        
        $reportPath = Join-Path $ArtifactsPath "BUILD-REPORT.md"
        $reportContent | Out-File $reportPath
        
        Write-Host ""
        Write-Host $reportContent -ForegroundColor Cyan
        Write-Host ""
        
    }
    catch {
        Write-Error "Report generation failed: $_"
        throw
    }
    finally {
        Write-EndStep
    }
}