|
2 | 2 | .SYNOPSIS |
3 | 3 | Validates MCP appsettings instance configuration. |
4 | 4 | #> |
5 | | -[CmdletBinding()] |
6 | | -param( |
7 | | - [string]$ConfigPath = "src/McpServer.Support.Mcp/appsettings.json" |
8 | | -) |
9 | | - |
10 | | -$ErrorActionPreference = "Stop" |
11 | | - |
12 | | -if (-not (Test-Path $ConfigPath)) { |
13 | | - throw "Config file not found: $ConfigPath" |
14 | | -} |
15 | | - |
16 | | -$json = Get-Content -Raw -Path $ConfigPath | ConvertFrom-Json |
17 | | -if (-not $json.Mcp) { |
18 | | - throw "Missing 'Mcp' section." |
19 | | -} |
20 | | - |
21 | | -$instances = $json.Mcp.Instances |
22 | | -if (-not $instances) { |
23 | | - Write-Host "No Mcp:Instances configured. Validation passed." |
24 | | - exit 0 |
25 | | -} |
26 | | - |
27 | | -$ports = @{} |
28 | | -$instances.PSObject.Properties | ForEach-Object { |
29 | | - $name = $_.Name |
30 | | - $instance = $_.Value |
31 | | - |
32 | | - if (-not $instance.RepoRoot) { |
33 | | - throw "Instance '$name' missing RepoRoot." |
| 5 | +[CmdletBinding()] |
| 6 | +param( |
| 7 | + [string]$ConfigPath = "" |
| 8 | +) |
| 9 | + |
| 10 | +$ErrorActionPreference = "Stop" |
| 11 | +$yamlKeyPattern = '[A-Za-z0-9_][A-Za-z0-9_\-]*' |
| 12 | + |
| 13 | +function ConvertFrom-YamlScalar { |
| 14 | + <# |
| 15 | + .SYNOPSIS |
| 16 | + Normalizes a simple YAML scalar value for validation. |
| 17 | +
|
| 18 | + .DESCRIPTION |
| 19 | + Trims surrounding whitespace and removes matching single- or double-quote delimiters |
| 20 | + when both ends use the same quote character. Mismatched quote pairs are left unchanged |
| 21 | + so malformed values are not silently rewritten during validation. |
| 22 | + #> |
| 23 | + param( |
| 24 | + [string]$Value |
| 25 | + ) |
| 26 | + |
| 27 | + $trimmed = $Value.Trim() |
| 28 | + if ($trimmed.Length -ge 2 -and ( |
| 29 | + ($trimmed[0] -eq "'" -and $trimmed[$trimmed.Length - 1] -eq "'") -or |
| 30 | + ($trimmed[0] -eq '"' -and $trimmed[$trimmed.Length - 1] -eq '"'))) { |
| 31 | + return $trimmed.Substring(1, $trimmed.Length - 2) |
| 32 | + } |
| 33 | + |
| 34 | + return $trimmed |
| 35 | +} |
| 36 | + |
| 37 | +function Get-McpInstancesFromYaml { |
| 38 | + <# |
| 39 | + .SYNOPSIS |
| 40 | + Extracts the Mcp:Instances block from the repository YAML settings file. |
| 41 | +
|
| 42 | + .DESCRIPTION |
| 43 | + Parses the checked-in appsettings YAML using the repository's current indentation pattern |
| 44 | + so the validation script can run in CI without depending on an external YAML module. |
| 45 | + The return value includes a HasMcp flag and an ordered dictionary of instance settings |
| 46 | + containing RepoRoot, Port, and TodoStorage fields needed by this validator. |
| 47 | + #> |
| 48 | + param( |
| 49 | + [string]$Path |
| 50 | + ) |
| 51 | + |
| 52 | + $lines = Get-Content -Path $Path |
| 53 | + $hasMcp = $false |
| 54 | + $instances = [ordered]@{} |
| 55 | + $inInstances = $false |
| 56 | + $currentInstance = $null |
| 57 | + $inTodoStorage = $false |
| 58 | + |
| 59 | + foreach ($rawLine in $lines) { |
| 60 | + $line = $rawLine.TrimEnd() |
| 61 | + if ([string]::IsNullOrWhiteSpace($line) -or $line.TrimStart().StartsWith('#')) { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + if ($line -match '^Mcp:\s*$') { |
| 66 | + $hasMcp = $true |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if (-not $hasMcp) { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if ($line -match '^ Instances:\s*$') { |
| 75 | + $inInstances = $true |
| 76 | + $currentInstance = $null |
| 77 | + $inTodoStorage = $false |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + if (-not $inInstances) { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + if ($line -match "^ ${yamlKeyPattern}:\s*$") { |
| 86 | + # A sibling key under Mcp means the Instances block has ended. |
| 87 | + break |
| 88 | + } |
| 89 | + |
| 90 | + if ($line -match "^ (${yamlKeyPattern}):\s*$") { |
| 91 | + $currentInstance = $Matches[1] |
| 92 | + $instances[$currentInstance] = [ordered]@{ |
| 93 | + RepoRoot = $null |
| 94 | + Port = $null |
| 95 | + TodoStorage = [ordered]@{ |
| 96 | + Provider = $null |
| 97 | + SqliteDataSource = $null |
| 98 | + } |
| 99 | + } |
| 100 | + $inTodoStorage = $false |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + if ($null -eq $currentInstance) { |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + if ($line -match '^ TodoStorage:\s*$') { |
| 109 | + $inTodoStorage = $true |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + if ($line -match '^ (RepoRoot|Port):\s*') { |
| 114 | + $inTodoStorage = $false |
| 115 | + } |
| 116 | + |
| 117 | + if ($line -match '^ RepoRoot:\s*(.+)$') { |
| 118 | + $instances[$currentInstance].RepoRoot = ConvertFrom-YamlScalar $Matches[1] |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if ($line -match '^ Port:\s*(.+)$') { |
| 123 | + $instances[$currentInstance].Port = ConvertFrom-YamlScalar $Matches[1] |
| 124 | + continue |
| 125 | + } |
| 126 | + |
| 127 | + if ($inTodoStorage -and $line -match '^ Provider:\s*(.+)$') { |
| 128 | + $instances[$currentInstance].TodoStorage.Provider = ConvertFrom-YamlScalar $Matches[1] |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + if ($inTodoStorage -and $line -match '^ SqliteDataSource:\s*(.+)$') { |
| 133 | + $instances[$currentInstance].TodoStorage.SqliteDataSource = ConvertFrom-YamlScalar $Matches[1] |
| 134 | + continue |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return @{ |
| 139 | + HasMcp = $hasMcp |
| 140 | + Instances = $instances |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function ConvertTo-McpInstanceMap { |
| 145 | + <# |
| 146 | + .SYNOPSIS |
| 147 | + Normalizes parsed instance settings into a consistent ordered dictionary. |
| 148 | +
|
| 149 | + .DESCRIPTION |
| 150 | + Converts either JSON-derived PSCustomObject instances or the YAML parser output into |
| 151 | + the same RepoRoot/Port/TodoStorage shape so the validation logic can iterate a single |
| 152 | + data structure regardless of the source file format. |
| 153 | + #> |
| 154 | + param( |
| 155 | + [object]$Instances |
| 156 | + ) |
| 157 | + |
| 158 | + $instanceMap = [ordered]@{} |
| 159 | + if ($null -eq $Instances) { |
| 160 | + return $instanceMap |
| 161 | + } |
| 162 | + |
| 163 | + $entries = if ($Instances -is [System.Collections.IDictionary]) { |
| 164 | + $Instances.GetEnumerator() | Sort-Object Name |
| 165 | + } |
| 166 | + else { |
| 167 | + $Instances.PSObject.Properties | ForEach-Object { |
| 168 | + [pscustomobject]@{ |
| 169 | + Name = $_.Name |
| 170 | + Value = $_.Value |
| 171 | + } |
| 172 | + } | Sort-Object Name |
| 173 | + } |
| 174 | + |
| 175 | + foreach ($entry in $entries) { |
| 176 | + $instanceMap[$entry.Name] = [ordered]@{ |
| 177 | + RepoRoot = $entry.Value.RepoRoot |
| 178 | + Port = $entry.Value.Port |
| 179 | + TodoStorage = [ordered]@{ |
| 180 | + Provider = $entry.Value.TodoStorage.Provider |
| 181 | + SqliteDataSource = $entry.Value.TodoStorage.SqliteDataSource |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return $instanceMap |
| 187 | +} |
| 188 | + |
| 189 | +if ([string]::IsNullOrWhiteSpace($ConfigPath)) { |
| 190 | + $candidatePaths = @( |
| 191 | + "src/McpServer.Support.Mcp/appsettings.yaml", |
| 192 | + "src/McpServer.Support.Mcp/appsettings.yml", |
| 193 | + "src/McpServer.Support.Mcp/appsettings.json" |
| 194 | + ) |
| 195 | + $ConfigPath = $candidatePaths | Where-Object { Test-Path $_ } | Select-Object -First 1 |
| 196 | +} |
| 197 | + |
| 198 | +if (-not (Test-Path $ConfigPath)) { |
| 199 | + throw "Config file not found: $ConfigPath" |
| 200 | +} |
| 201 | + |
| 202 | +$extension = [System.IO.Path]::GetExtension($ConfigPath) |
| 203 | +$config = switch ($extension.ToLowerInvariant()) { |
| 204 | + ".yaml" { Get-McpInstancesFromYaml -Path $ConfigPath } |
| 205 | + ".yml" { Get-McpInstancesFromYaml -Path $ConfigPath } |
| 206 | + ".json" { |
| 207 | + $json = Get-Content -Raw -Path $ConfigPath | ConvertFrom-Json |
| 208 | + @{ |
| 209 | + HasMcp = $null -ne $json.Mcp |
| 210 | + Instances = ConvertTo-McpInstanceMap -Instances $json.Mcp.Instances |
| 211 | + } |
| 212 | + } |
| 213 | + default { throw "Unsupported config format '$extension' for '$ConfigPath'." } |
| 214 | +} |
| 215 | + |
| 216 | +if (-not $config.HasMcp) { |
| 217 | + throw "Missing 'Mcp' section." |
| 218 | +} |
| 219 | + |
| 220 | +$instances = $config.Instances |
| 221 | +if (-not $instances) { |
| 222 | + Write-Host "No Mcp:Instances configured. Validation passed." |
| 223 | + exit 0 |
| 224 | +} |
| 225 | + |
| 226 | +$instanceEntries = $instances.GetEnumerator() | Sort-Object Name |
| 227 | + |
| 228 | +$ports = @{} |
| 229 | +$instanceEntries | ForEach-Object { |
| 230 | + $name = $_.Name |
| 231 | + $instance = $_.Value |
| 232 | + |
| 233 | + if (-not $instance.RepoRoot) { |
| 234 | + throw "Instance '$name' missing RepoRoot." |
34 | 235 | } |
35 | 236 | $resolvedRoot = [System.IO.Path]::GetFullPath([string]$instance.RepoRoot) |
36 | 237 | if (-not (Test-Path -Path $resolvedRoot -PathType Container)) { |
@@ -58,14 +259,14 @@ $instances.PSObject.Properties | ForEach-Object { |
58 | 259 |
|
59 | 260 | if ($provider -eq "sqlite") { |
60 | 261 | $sqliteDataSource = "" |
61 | | - if ($instance.TodoStorage -and $instance.TodoStorage.SqliteDataSource) { |
62 | | - $sqliteDataSource = [string]$instance.TodoStorage.SqliteDataSource |
63 | | - } |
64 | | - if ([string]::IsNullOrWhiteSpace($sqliteDataSource)) { |
65 | | - throw "Instance '$name' provider sqlite requires TodoStorage.SqliteDataSource." |
66 | | - } |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -$instanceCount = @($instances.PSObject.Properties).Count |
71 | | -Write-Host "MCP config validation passed for $instanceCount instances." |
| 262 | + if ($instance.TodoStorage -and $instance.TodoStorage.SqliteDataSource) { |
| 263 | + $sqliteDataSource = [string]$instance.TodoStorage.SqliteDataSource |
| 264 | + } |
| 265 | + if ([string]::IsNullOrWhiteSpace($sqliteDataSource)) { |
| 266 | + throw "Instance '$name' provider sqlite requires TodoStorage.SqliteDataSource." |
| 267 | + } |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +$instanceCount = @($instanceEntries).Count |
| 272 | +Write-Host "MCP config validation passed for $instanceCount instances." |
0 commit comments