Situatie
De regulă, un agent AI (Claude, Copilot) este cel care apelează unelte externe prin MCP. Această soluție inversează rolul: PowerShell devine el însuși un client MCP, capabil să apeleze servere MCP existente (de exemplu, un server MCP pentru automatizare browser) direct din propriile scripturi de administrare — fără să mai fie nevoie de un chat AI intermediar pentru task-uri repetitive.
Utilă pentru cazuri gen: verificarea automată a unui portal web intern, completarea unui formular, sau extragerea de date dintr-o aplicație fără API, toate orchestrate din PowerShell.
Backup
Soluția este un script de automatizare — nu modifică sisteme critice. Testează întâi pe un mediu non-producție.
Solutie
Pasi de urmat
Pasul 1 — Cerințe preliminare
# PowerShell 7+ este recomandat pentru suport JSON si procese imbunatatit
$PSVersionTable.PSVersion
# Node.js necesar pentru serverul MCP de automatizare browser
node --version
Pasul 2 — Instalare server MCP pentru automatizare browser
# Instaleaza serverul MCP Playwright (automatizare browser)
npm install -g @modelcontextprotocol/server-playwright
Pasul 3 — Clasă client MCP reutilizabilă în PowerShell
# ============================================================
# MCP-Client.ps1 - Client MCP minimal in PowerShell (stdio)
# ============================================================
class MCPClient {
[System.Diagnostics.Process]$Process
[int]$RequestId = 0
MCPClient([string]$Command, [string[]]$Arguments) {
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $Command
$psi.Arguments = $Arguments -join " "
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $true
$psi.UseShellExecute = $false
$this.Process = [System.Diagnostics.Process]::Start($psi)
}
[PSCustomObject] SendRequest([string]$Method, [hashtable]$Params) {
$this.RequestId++
$Request = @{
jsonrpc = "2.0"
id = $this.RequestId
method = $Method
params = $Params
} | ConvertTo-Json -Depth 5 -Compress
$this.Process.StandardInput.WriteLine($Request)
$this.Process.StandardInput.Flush()
$ResponseLine = $this.Process.StandardOutput.ReadLine()
return $ResponseLine | ConvertFrom-Json
}
[void] Close() {
$this.Process.StandardInput.Close()
$this.Process.WaitForExit(5000)
}
}
Pasul 4 — Script de utilizare: verificare automată portal web intern
# ============================================================
# Check-InternalPortal.ps1
# Verifica automat un portal intern folosind MCP + Playwright
# ============================================================
. .\MCP-Client.ps1
$Client = [MCPClient]::new("npx", @("@modelcontextprotocol/server-playwright"))
try {
# Initializare conexiune MCP
$InitResponse = $Client.SendRequest("initialize", @{
protocolVersion = "2024-11-05"
capabilities = @{}
})
Write-Host "[OK] Conectat la server MCP: $($InitResponse.result.serverInfo.name)" -ForegroundColor Green
# Navigare catre portalul intern
$NavResponse = $Client.SendRequest("tools/call", @{
name = "browser_navigate"
arguments = @{ url = "http://portal.intern.local/status" }
})
# Extragere continut pagina
$ContentResponse = $Client.SendRequest("tools/call", @{
name = "browser_get_text"
arguments = @{ selector = "#system-status" }
})
$Status = $ContentResponse.result.content
if ($Status -like "*OK*") {
Write-Host "[OK] Portal functional: $Status" -ForegroundColor Green
} else {
Write-Host "[ALERTA] Status neasteptat: $Status" -ForegroundColor Red
# Aici poti adauga trimitere email/notificare
}
}
finally {
$Client.Close()
}
Pasul 5 — Rulare și programare automată
# Testare manuala
.\Check-InternalPortal.ps1
# Programare zilnica cu Scheduled Task
$Action = New-ScheduledTaskAction -Execute "pwsh.exe" `
-Argument "-File C:\Scripts\Check-InternalPortal.ps1"
$Trigger = New-ScheduledTaskTrigger -Daily -At "09:00"
Register-ScheduledTask -TaskName "MCP_PortalCheck" `
-Action $Action -Trigger $Trigger -RunLevel Highest
Pasul 6 — Extindere: combinare cu un LLM local pentru interpretare rezultate
# Combina rezultatul MCP cu Foundry Local (din Solutia 1) pentru interpretare
$Prompt = "Interpreteaza acest status de portal si spune daca necesita interventie: $Status"
$Interpretare = Invoke-FoundryLocal -Prompt $Prompt
Write-Host "Interpretare AI: $Interpretare"
Leave A Comment?