|
| 1 | +#Requires -Version 5.1 |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | + Ponte MCP para conectar a CLI jurisprudencias.ps1 ao ecossistema OpenCode. |
| 6 | +.DESCRIPTION |
| 7 | + Expoe funcoes da API Jurisprudencias como comandos via stdin/stdout JSON |
| 8 | + no formato MCP (Model Context Protocol). |
| 9 | + Uso: Get-Content .\request.json | .\mcp-bridge.ps1 |
| 10 | +#> |
| 11 | + |
| 12 | +# carrega CLI |
| 13 | +. "$PSScriptRoot\..\scripts\jurisprudencias.ps1" -ErrorAction Stop |
| 14 | + |
| 15 | +function Write-McpResponse { |
| 16 | + param([object] $Data) |
| 17 | + $body = [PSCustomObject]@{ jsonrpc = "2.0"; result = $Data; id = 1 } |
| 18 | + $body | ConvertTo-Json -Depth 10 -Compress |
| 19 | +} |
| 20 | + |
| 21 | +function Write-McpError { |
| 22 | + param([string] $Message, [int] $Code = -1) |
| 23 | + $err = [PSCustomObject]@{ |
| 24 | + jsonrpc = "2.0" |
| 25 | + error = [PSCustomObject]@{ code = $Code; message = $Message } |
| 26 | + id = 1 |
| 27 | + } |
| 28 | + $err | ConvertTo-Json -Depth 5 -Compress |
| 29 | +} |
| 30 | + |
| 31 | +try { |
| 32 | + $inputJson = Get-Content -Raw -Encoding UTF8 -LiteralPath $env:MCP_REQUEST_PATH |
| 33 | + $req = $inputJson | ConvertFrom-Json |
| 34 | + |
| 35 | + $method = $req.method |
| 36 | + $params = $req.parameters |
| 37 | + |
| 38 | + if (-not $method) { throw "Metodo nao especificado" } |
| 39 | + |
| 40 | + switch ($method) { |
| 41 | + "jur.search" { |
| 42 | + $r = Search-JurDecisions @params |
| 43 | + if (-not $r) { Write-McpResponse @() } |
| 44 | + else { Write-McpResponse @($r) } |
| 45 | + } |
| 46 | + "jur.lookup" { |
| 47 | + $r = Get-JurDecision @params |
| 48 | + if (-not $r) { Write-McpResponse $null } |
| 49 | + else { Write-McpResponse $r } |
| 50 | + } |
| 51 | + "jur.courts" { |
| 52 | + $r = Get-JurCourts |
| 53 | + Write-McpResponse @($r) |
| 54 | + } |
| 55 | + "jur.cache_status" { |
| 56 | + $r = Get-JurCacheStatus *>&1 | Out-String |
| 57 | + Write-McpResponse @{ status = $r.Trim() } |
| 58 | + } |
| 59 | + "jur.cache_clear" { |
| 60 | + Clear-JurCache |
| 61 | + Write-McpResponse @{ cleared = $true } |
| 62 | + } |
| 63 | + "jur.cache_search" { |
| 64 | + $r = Search-JurCache @params |
| 65 | + Write-McpResponse @(if ($r) { $r } else { @() }) |
| 66 | + } |
| 67 | + "jur.export" { |
| 68 | + $path = if ($params.OutputPath) { $params.OutputPath } else { ".\jur_offline.html" } |
| 69 | + Export-JurDocs -OutputPath $path |
| 70 | + Write-McpResponse @{ path = $path } |
| 71 | + } |
| 72 | + default { |
| 73 | + Write-McpError "Metodo desconhecido: $method" -Code -32601 |
| 74 | + } |
| 75 | + } |
| 76 | +} catch { |
| 77 | + Write-McpError $_.Exception.Message -Code -32603 |
| 78 | +} |
0 commit comments