From b844d22a68216d03586b486ca305389e79543689 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Wed, 2 Dec 2026 13:18:27 +0000 Subject: [PATCH] #244 Update ISHRemoteMcpServer to dual-era MCP 2026-07-28 stateless and legacy 2024-11-05 - Add server/discover, resultType, ttlMs/cacheScope, -32022 version gate - tools/call: unknown tool -> -32602, execution errors -> isError true - serverInfo uses ISHRemoteMcpServer and the module version - Register-IshRemoteMcpTool: add title, strict schema for no-parameter tools --- ...Invoke-IshRemoteMcpHandleRequest.Tests.ps1 | 108 ++++++++++++++++ .../Invoke-IshRemoteMcpHandleRequest.ps1 | 115 +++++++++++++++--- .../Public/Register-IshRemoteMcpTool.ps1 | 11 +- 3 files changed, 215 insertions(+), 19 deletions(-) diff --git a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.Tests.ps1 b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.Tests.ps1 index d2853e9..941aeb7 100644 --- a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.Tests.ps1 +++ b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.Tests.ps1 @@ -167,6 +167,114 @@ Describe "Invoke-IshRemoteMcpHandleRequest" -Skip:($PSVersionTable.PSVersion.Maj It "Should return valid JSON" { { $result | ConvertFrom-Json } | Should -Not -Throw } + It "Should return protocol error -32602 for unknown tool" { + $json = $result | ConvertFrom-Json + $json.error.code | Should-Be -32602 + } + } + + Context "Method Tools/Call with tool execution error" { + BeforeAll { + Mock -ModuleName ISHRemote Write-IshRemoteLog { } + $request = [PSCustomObject]@{ + id = 7 + method = "tools/call" + params = @{ + name = "Get-Date" + arguments = @{ NonExistingParameter = "x" } + } + } + $result = Invoke-IshRemoteMcpHandleRequest -Request $request -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' + } + It "Should have isError true so the LLM can self-correct" { + $json = $result | ConvertFrom-Json + $json.result.isError | Should-Be $true + } + It "Should describe the error in content" { + $json = $result | ConvertFrom-Json + $json.result.content[0].text | Should-NotBeNull + } + } + + Context "Modern era (2026-07-28)" { + BeforeAll { + Mock -ModuleName ISHRemote Write-IshRemoteLog { } + function New-ModernRequest($id, $method, $params = @{}) { + $params['_meta'] = @{ 'io.modelcontextprotocol/protocolVersion' = '2026-07-28' } + [PSCustomObject]@{ id = $id; method = $method; params = [PSCustomObject]$params } | + ConvertTo-Json -Depth 10 | ConvertFrom-Json + } + $discover = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 10 'server/discover') -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '"test instructions"' | ConvertFrom-Json + $ping = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 11 'ping') -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + $list = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 12 'tools/list') -ToolsListJson '[{"name":"Get-IshFolder","title":"Get ISH Folder"}]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + $call = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 13 'tools/call' @{ name = 'Get-Date'; arguments = @{ Format = 'yyyy' } }) -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + $callError = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 14 'tools/call' @{ name = 'Get-Date'; arguments = @{ NonExistingParameter = 'x' } }) -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + $callUnknown = Invoke-IshRemoteMcpHandleRequest -Request (New-ModernRequest 15 'tools/call' @{ name = 'Non-ExistentCommand'; arguments = @{} }) -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + } + It "server/discover returns supportedVersions" { + $discover.result.supportedVersions | Should-Be "2026-07-28" + } + It "server/discover returns capabilities, caching hints and instructions" { + $discover.result.resultType | Should-Be "complete" + $discover.result.capabilities.tools | Should-NotBeNull + $discover.result.ttlMs | Should-NotBeNull + $discover.result.cacheScope | Should-Be "public" + $discover.result.instructions | Should-Be "test instructions" + } + It "server/discover returns serverInfo in _meta" { + $info = $discover.result._meta.'io.modelcontextprotocol/serverInfo' + $info.name | Should-Be "ISHRemoteMcpServer" + $info.version | Should-NotBeNull + } + It "ping returns resultType complete" { + $ping.result.resultType | Should-Be "complete" + } + It "tools/list returns resultType complete with caching hints" { + $list.result.resultType | Should-Be "complete" + $list.result.ttlMs | Should-NotBeNull + $list.result.cacheScope | Should-Be "public" + $list.result.tools[0].title | Should-Be "Get ISH Folder" + } + It "tools/call returns resultType complete and isError false" { + $call.result.resultType | Should-Be "complete" + $call.result.isError | Should-Be $false + } + It "tools/call execution error returns isError true" { + $callError.result.resultType | Should-Be "complete" + $callError.result.isError | Should-Be $true + } + It "tools/call unknown tool returns -32602" { + $callUnknown.error.code | Should-Be -32602 + } + } + + Context "Unsupported protocol version" { + BeforeAll { + Mock -ModuleName ISHRemote Write-IshRemoteLog { } + $request = '{"id":20,"method":"tools/list","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"1900-01-01"}}}' | ConvertFrom-Json + $json = Invoke-IshRemoteMcpHandleRequest -Request $request -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + } + It "Should return error code -32022" { + $json.error.code | Should-Be -32022 + } + It "Should list supported versions and the requested one" { + $json.error.data.supported | Should-Be "2026-07-28" + $json.error.data.requested | Should-Be "1900-01-01" + } + } + + Context "Legacy era regression (dual-era)" { + BeforeAll { + Mock -ModuleName ISHRemote Write-IshRemoteLog { } + $request = [PSCustomObject]@{ id = 21; method = "initialize"; params = @{} } + $json = Invoke-IshRemoteMcpHandleRequest -Request $request -ToolsListJson '[]' -ResourcesListJson '[]' -InstructionsJson '""' | ConvertFrom-Json + } + It "initialize still returns protocolVersion 2024-11-05" { + $json.result.protocolVersion | Should-Be "2024-11-05" + } + It "initialize returns ISHRemoteMcpServer serverInfo" { + $json.result.serverInfo.name | Should-Be "ISHRemoteMcpServer" + } } Context "Unknown Method" { diff --git a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.ps1 b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.ps1 index 5285a08..a12cbea 100644 --- a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.ps1 +++ b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Invoke-IshRemoteMcpHandleRequest.ps1 @@ -1,7 +1,9 @@ <# .DESCRIPTION - Processes incoming MCP requests including initialize, ping, tools/list, and tools/call methods. + Processes incoming MCP requests including initialize, server/discover, ping, tools/list, and tools/call methods. Returns formatted JSON-RPC 2.0 responses for each request type. + The server is dual-era: requests carrying _meta["io.modelcontextprotocol/protocolVersion"] are answered as + stateless MCP 2026-07-28, while the legacy initialize handshake keeps answering protocol version 2024-11-05. #> function Invoke-IshRemoteMcpHandleRequest { param( @@ -11,19 +13,63 @@ function Invoke-IshRemoteMcpHandleRequest { [string]$InstructionsJson ) - # Initialize Method + $modernVersion = "2026-07-28" + $supportedVersions = @($modernVersion) + $versionMetaKey = "io.modelcontextprotocol/protocolVersion" + $idJson = $Request.id | ConvertTo-Json -Depth 10 -Compress + + # Module version for serverInfo, falls back when the module is not loaded by name (e.g. dot-sourced) + $serverVersion = "0.0.0" + $ishRemoteModule = Get-Module -Name ISHRemote | Select-Object -First 1 + if ($ishRemoteModule) { $serverVersion = $ishRemoteModule.Version.ToString() } + $serverInfo = [ordered]@{ name = "ISHRemoteMcpServer"; version = $serverVersion } + + # Era detection: a per-request protocol version in _meta means modern, otherwise legacy (initialize handshake) + $requestedVersion = $null + if ($Request.method -ne "initialize" -and + $null -ne $Request.PSObject.Properties['params'] -and $null -ne $Request.params -and + $null -ne $Request.params.PSObject.Properties['_meta'] -and $null -ne $Request.params._meta -and + $null -ne $Request.params._meta.PSObject.Properties[$versionMetaKey]) { + $requestedVersion = [string]$Request.params._meta.$versionMetaKey + } + $isModern = $null -ne $requestedVersion + + if ($isModern -and $requestedVersion -notin $supportedVersions) { + $errorResponse = [ordered]@{ + jsonrpc = "2.0" + id = $Request.id + error = [ordered]@{ + code = -32022 + message = "Unsupported protocol version" + data = [ordered]@{ supported = @($supportedVersions); requested = $requestedVersion } + } + } + return ($errorResponse | ConvertTo-Json -Depth 10 -Compress) + } + + # Initialize Method (legacy era) if ($Request.method -eq "initialize") { - # Static response for simplicity, adjust serverInfo as needed - $response = '{"jsonrpc":"2.0","id":' + ($Request.id | ConvertTo-Json -Depth 10 -Compress) + ',"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{"listChanged":false}},"instructions":' + $instructionsJson + ',"serverInfo":{"name":"ISHRemote MCP Server","version":"0.3.0"}}}' + $serverInfoJson = $serverInfo | ConvertTo-Json -Compress + $response = '{"jsonrpc":"2.0","id":' + $idJson + ',"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{"listChanged":false}},"instructions":' + $InstructionsJson + ',"serverInfo":' + $serverInfoJson + '}}' + return $response + } + + # Server/Discover Method (modern era) + if ($Request.method -eq "server/discover") { + $serverInfoJson = $serverInfo | ConvertTo-Json -Compress + $supportedJson = ConvertTo-Json -InputObject @($supportedVersions) -Compress + $response = '{"jsonrpc":"2.0","id":' + $idJson + ',"result":{"resultType":"complete","supportedVersions":' + $supportedJson + ',"capabilities":{"tools":{"listChanged":false}},"_meta":{"io.modelcontextprotocol/serverInfo":' + $serverInfoJson + '},"instructions":' + $InstructionsJson + ',"ttlMs":3600000,"cacheScope":"public"}}' return $response } # Ping Method if ($Request.method -eq "ping") { - $pingResponse = @{ + $pingResult = [ordered]@{} + if ($isModern) { $pingResult.resultType = "complete" } + $pingResponse = [ordered]@{ jsonrpc = "2.0" id = $Request.id - result = @{} + result = $pingResult } $response = $pingResponse | ConvertTo-Json -Depth 10 -Compress @@ -33,7 +79,11 @@ function Invoke-IshRemoteMcpHandleRequest { # Tools/List Method if ($Request.method -eq "tools/list") { - $response = '{"jsonrpc":"2.0","id":' + ($Request.id | ConvertTo-Json -Depth 10 -Compress) + ',"result":{"tools":' + $ToolsListJson + '}}' + $modernFields = '' + if ($isModern) { $modernFields = '"resultType":"complete",' } + $cachingFields = '' + if ($isModern) { $cachingFields = ',"ttlMs":3600000,"cacheScope":"public"' } + $response = '{"jsonrpc":"2.0","id":' + $idJson + ',"result":{' + $modernFields + '"tools":' + $ToolsListJson + $cachingFields + '}}' # Use the Write-IshRemoteLog function correctly with a hashtable Write-IshRemoteLog -LogEntry @{ @@ -50,14 +100,42 @@ function Invoke-IshRemoteMcpHandleRequest { if ($Request.method -eq "tools/call") { $toolName = $Request.params.name $targetArgs = $Request.params.arguments | ConvertTo-Json -Depth 10 | ConvertFrom-Json -Depth 10 -AsHashtable + if ($null -eq $targetArgs) { $targetArgs = @{} } + + # Unknown tool is a protocol error, not a tool execution error + $toolCommand = $null + if (-not [string]::IsNullOrEmpty($toolName)) { + $toolCommand = Get-Command -Name $toolName -ErrorAction Ignore | Select-Object -First 1 + } + if ($null -eq $toolCommand) { + Write-IshRemoteLog -LogEntry @{ + Level = 'Error' + Message = "Unknown tool [$toolName]" + RequestId = $Request.id + Method = $Request.method + ToolName = $toolName + } + $errorResponse = [ordered]@{ + jsonrpc = "2.0" + id = $Request.id + error = [ordered]@{ code = -32602; message = "Unknown tool: $toolName" } + } + return ($errorResponse | ConvertTo-Json -Depth 10 -Compress) + } + $result = $null + $isError = $false try { + # Promote non-terminating errors, so the LLM gets them as actionable feedback + $ErrorActionPreference = 'Stop' $result = & $toolName @targetArgs } catch { + $isError = $true + $result = "Error invoking tool [$toolName]: $_" Write-IshRemoteLog -LogEntry @{ Level = 'Error' - Message = "Error invoking tool [$toolName]: $_" + Message = $result RequestId = $Request.id Method = $Request.method ToolName = $toolName @@ -78,25 +156,26 @@ function Invoke-IshRemoteMcpHandleRequest { # TODO if $result holds a cmdlet error like 'Find-IshUser: [-102001]...' or PowerShell errors like 'ParseError' # then add 'see docs://tools for more information' to instruct the LLM to download this McpResource for proper usage + $callResult = [ordered]@{} + if ($isModern) { $callResult.resultType = "complete" } + $callResult.content = @( + [ordered]@{ + type = "text" + text = $result | Out-String + } + ) + $callResult.isError = $isError $response = [ordered]@{ jsonrpc = "2.0" id = $Request.id - result = @{ - content = @( - [ordered]@{ - type = "text" - text = $result | Out-String - } - ) - isError = $false - } + result = $callResult } return ($response | ConvertTo-Json -Depth 10 -Compress) } # Unknown Method Error - $response = '{"jsonrpc":"2.0","id":' + ($Request.id | ConvertTo-Json -Depth 10 -Compress) + ',"error":{"code":-32601,"message":"Method not found"}}' + $response = '{"jsonrpc":"2.0","id":' + $idJson + ',"error":{"code":-32601,"message":"Method not found"}}' return $response } diff --git a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Register-IshRemoteMcpTool.ps1 b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Register-IshRemoteMcpTool.ps1 index 12757fa..be1cfb5 100644 --- a/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Register-IshRemoteMcpTool.ps1 +++ b/Source/ISHRemote/Trisoft.ISHRemote/Scripts/Public/Register-IshRemoteMcpTool.ps1 @@ -163,13 +163,22 @@ function Register-IshRemoteMcpTool { } # If verb is Delete, then destructiveHint=$true # If verb is Get, then idempotentHint=$true - # Omit title and openWorldHint to hold defaults + # Omit openWorldHint to hold defaults # if verb is Get, then readOnlyHint=$true + # Human-readable title for IDE/chat UIs, e.g. Get-IshFolder becomes 'Get ISH Folder' + $title = ($CommandInfo.Name -creplace '([a-z0-9])([A-Z])', '$1 $2') -replace '-', ' ' -creplace '\bIsh\b', 'ISH' + + if ($fullLoad -and $inputSchema.properties.Count -eq 0) { + # Tool without parameters, so reject any argument + $inputSchema = [ordered]@{ type = 'object'; additionalProperties = $false } + } + $results[$CommandInfo.Name] = [ordered]@{ # Keep using CommandInfo.Name as key for internal logic name = $CommandInfo.Name # Use original name for output + title = $title description = $description annotations = $annotations inputSchema = $inputSchema