|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe 'Tests for MCP server' { |
| 5 | + BeforeAll { |
| 6 | + $processStartInfo = [System.Diagnostics.ProcessStartInfo]::new() |
| 7 | + $processStartInfo.FileName = "dsc" |
| 8 | + $processStartInfo.Arguments = "--trace-format plaintext mcp" |
| 9 | + $processStartInfo.RedirectStandardError = $true |
| 10 | + $processStartInfo.RedirectStandardOutput = $true |
| 11 | + $processStartInfo.RedirectStandardInput = $true |
| 12 | + $mcp = [System.Diagnostics.Process]::Start($processStartInfo) |
| 13 | + |
| 14 | + function Send-McpRequest($request, [switch]$notify) { |
| 15 | + $request = $request | ConvertTo-Json -Compress -Depth 10 |
| 16 | + $mcp.StandardInput.WriteLine($request) |
| 17 | + $mcp.StandardInput.Flush() |
| 18 | + if (!$notify) { |
| 19 | + while ($mcp.StandardOutput.Peek() -eq -1) { |
| 20 | + Start-Sleep -Milliseconds 100 |
| 21 | + } |
| 22 | + $stdout = $mcp.StandardOutput.ReadLine() |
| 23 | + return ($stdout | ConvertFrom-Json -Depth 30) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + AfterAll { |
| 29 | + $mcp.StandardInput.Close() |
| 30 | + $mcp.WaitForExit() |
| 31 | + } |
| 32 | + |
| 33 | + It 'Initialization works' { |
| 34 | + $mcpRequest = @{ |
| 35 | + jsonrpc = "2.0" |
| 36 | + id = 1 |
| 37 | + method = "initialize" |
| 38 | + params = @{ |
| 39 | + protocolVersion = "2024-11-05" |
| 40 | + capabilities = @{ |
| 41 | + tools = @{} |
| 42 | + } |
| 43 | + clientInfo = @{ |
| 44 | + name = "Test Client" |
| 45 | + version = "1.0.0" |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $response = Send-McpRequest -request $mcpRequest |
| 51 | + |
| 52 | + $response.id | Should -Be 1 |
| 53 | + $response.result.capabilities.tools | Should -Not -Be $null |
| 54 | + $response.result.instructions | Should -Not -BeNullOrEmpty |
| 55 | + |
| 56 | + $notifyInitialized = @{ |
| 57 | + jsonrpc = "2.0" |
| 58 | + method = "notifications/initialized" |
| 59 | + } |
| 60 | + |
| 61 | + Send-McpRequest -request $notifyInitialized -notify |
| 62 | + } |
| 63 | + |
| 64 | + It 'Tools/List works' { |
| 65 | + $mcpRequest = @{ |
| 66 | + jsonrpc = "2.0" |
| 67 | + id = 2 |
| 68 | + method = "tools/list" |
| 69 | + params = @{} |
| 70 | + } |
| 71 | + |
| 72 | + $response = Send-McpRequest -request $mcpRequest |
| 73 | + |
| 74 | + $response.id | Should -Be 2 |
| 75 | + $response.result.tools.Count | Should -Be 1 |
| 76 | + $response.result.tools[0].name | Should -BeExactly 'list_dsc_resources' |
| 77 | + } |
| 78 | + |
| 79 | + It 'Calling list_dsc_resources works' { |
| 80 | + $mcpRequest = @{ |
| 81 | + jsonrpc = "2.0" |
| 82 | + id = 3 |
| 83 | + method = "tools/call" |
| 84 | + params = @{ |
| 85 | + name = "list_dsc_resources" |
| 86 | + arguments = @{} |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + $response = Send-McpRequest -request $mcpRequest |
| 91 | + $response.id | Should -Be 3 |
| 92 | + $resources = dsc resource list | ConvertFrom-Json -Depth 20 | Select-Object type, kind, description -Unique |
| 93 | + $response.result.structuredContent.resources.Count | Should -Be $resources.Count |
| 94 | + for ($i = 0; $i -lt $resources.Count; $i++) { |
| 95 | + ($response.result.structuredContent.resources[$i].psobject.properties | Measure-Object).Count | Should -Be 3 |
| 96 | + $response.result.structuredContent.resources[$i].type | Should -BeExactly $resources[$i].type -Because ($response.result.structuredContent | ConvertTo-Json -Depth 20 | Out-String) |
| 97 | + $response.result.structuredContent.resources[$i].kind | Should -BeExactly $resources[$i].kind -Because ($response.result.structuredContent | ConvertTo-Json -Depth 20 | Out-String) |
| 98 | + $response.result.structuredContent.resources[$i].description | Should -BeExactly $resources[$i].description -Because ($response.result.structuredContent | ConvertTo-Json -Depth 20 | Out-String) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments