Module: McpDiff::Core::Rules
- Defined in:
- lib/mcp_diff/core/rules.rb
Overview
The v1 rule catalog (24 rules), faithful port of packages/core/src/rules/*. NOTE: the TS project uses one-folder-per-rule as the contributor funnel; the Ruby port keeps them in one file for the parity milestone. Splitting into per-rule files is a follow-up (see ruby/README.md).
Constant Summary collapse
- ALL =
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
build- CATALOG =
ALL.map { |r| { "id" => r.id, "class" => r.severity, "description" => r.description } }.freeze
Class Method Summary collapse
-
.build ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- .keys_of(value) ⇒ Object
- .schema_findings(rule, change, schema_key) ⇒ Object
Class Method Details
.build ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/mcp_diff/core/rules.rb', line 25 def build [ # ───────────────── breaking ───────────────── Rule.new(id: "tool-removed", severity: "breaking", description: "A tool that existed in the lockfile is no longer exposed by the server.") do |c| next nil unless c[:kind] == "tool-removed" finding(c[:name], "tool was removed") end, Rule.new(id: "tool-param-required-added", severity: "breaking", description: "A tool input parameter is now required (newly required existing param, or new required param). Calls that previously validated are now rejected.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "required-added" f << finding(c[:name], sc[:existed_before] ? %(param "#{sc[:param]}" is now required) : %(new required param "#{sc[:param]}")) end f.empty? ? nil : f end, Rule.new(id: "tool-param-removed", severity: "breaking", description: "A tool input parameter was removed. Callers still sending it may be rejected (additionalProperties) or silently ignored.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| f << finding(c[:name], %(param "#{sc[:param]}" was removed)) if sc[:kind] == "param-removed" end f.empty? ? nil : f end, Rule.new(id: "tool-param-type-narrowed", severity: "breaking", description: "A tool input parameter accepts fewer types or values than before (type set shrank, or an enum constraint appeared). Inputs that used to validate are now rejected.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| case sc[:kind] when "type-narrowed" f << finding(c[:name], %(param "#{sc[:param]}" type narrowed (#{sc[:before]} → #{sc[:after]}))) when "enum-constraint-added" f << finding(c[:name], %(param "#{sc[:param]}" is now restricted to an enum)) end end f.empty? ? nil : f end, Rule.new(id: "tool-enum-value-removed", severity: "breaking", description: "An enum value was removed from a tool input parameter. Calls sending the removed value are now rejected.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "enum-value-removed" f << finding(c[:name], %(param "#{sc[:param]}": enum value(s) removed: #{SchemaDiff.format_values(sc[:values])})) end f.empty? ? nil : f end, Rule.new(id: "tool-input-schema-incompatible", severity: "breaking", description: "Catch-all: the tool's input schema changed in a way mcp-diff cannot prove compatible (nested or unrecognized JSON-Schema constructs). Fails loud rather than silently passing (D9).") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| case sc[:kind] when "param-changed-opaque" f << finding(c[:name], %(param "#{sc[:param]}" schema changed — cannot prove compatibility)) when "schema-changed-opaque" f << finding(c[:name], "input schema changed — cannot prove compatibility") end end f.empty? ? nil : f end, Rule.new(id: "tool-output-schema-incompatible", severity: "breaking", description: "The tool's output schema no longer keeps its promise: a field was removed, made optional, widened, or changed unrecognizably. Outputs may no longer validate against what consumers expect (covariant, D9).") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["outputSchema"], c[:after]["outputSchema"]).each do |sc| msg = case sc[:kind] when "param-removed" then %(output field "#{sc[:param]}" was removed) when "required-removed" then %(output field "#{sc[:param]}" is no longer guaranteed) when "type-widened" then %(output field "#{sc[:param]}" type widened (#{sc[:before]} → #{sc[:after]})) when "enum-value-added" then %(output field "#{sc[:param]}": new enum value(s) old consumers don't expect: #{SchemaDiff.format_values(sc[:values])}) when "enum-constraint-removed" then %(output field "#{sc[:param]}" is no longer enum-constrained) when "param-changed-opaque" then %(output field "#{sc[:param]}" schema changed — cannot prove compatibility) when "schema-changed-opaque" then "output schema changed — cannot prove compatibility" end f << finding(c[:name], msg) if msg end f.empty? ? nil : f end, Rule.new(id: "capability-removed", severity: "breaking", description: "A server capability (tools, resources, prompts, logging, …) is no longer advertised. Clients relying on it will fail.") do |c| next nil unless c[:kind] == "capability-removed" finding(c[:capability], "capability was removed") end, Rule.new(id: "resource-removed", severity: "breaking", description: "A resource that existed in the lockfile is no longer exposed (matched by uri).") do |c| next nil unless c[:kind] == "resource-removed" finding(c[:uri], "resource was removed") end, Rule.new(id: "prompt-removed", severity: "breaking", description: "A prompt that existed in the lockfile is no longer exposed (matched by name).") do |c| next nil unless c[:kind] == "prompt-removed" finding(c[:name], "prompt was removed") end, # ───────────────── behavior ───────────────── Rule.new(id: "tool-description-changed", severity: "behavior", description: "A tool's description text changed. For MCP the description is the prompt surface steering the LLM — a change can alter when and how agents call the tool.") do |c| next nil unless c[:kind] == "tool-modified" next nil if c[:before]["description"] == c[:after]["description"] finding(c[:name], "description text changed (LLM prompt surface)") end, Rule.new(id: "tool-param-description-changed", severity: "behavior", description: "A tool input parameter's description changed. Param descriptions are read by the LLM when filling arguments — a change can alter what agents pass.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "description-changed" f << finding(c[:name], %(param "#{sc[:param]}" description changed (LLM prompt surface))) end f.empty? ? nil : f end, Rule.new(id: "tool-annotation-changed", severity: "behavior", description: "Tool annotations changed (readOnlyHint, destructiveHint, idempotentHint, …). Clients use these to decide confirmation prompts and parallelism — a change alters agent behavior.") do |c| next nil unless c[:kind] == "tool-modified" next nil if Canonical.deep_equal(c[:before]["annotations"], c[:after]["annotations"]) before = Rules.keys_of(c[:before]["annotations"]) after = Rules.keys_of(c[:after]["annotations"]) changed = (before.keys | after.keys).reject { |k| Canonical.deep_equal(before[k], after[k]) }.sort finding(c[:name], "annotations changed: #{changed.join(', ')}") end, Rule.new(id: "server-instructions-changed", severity: "behavior", description: "The server's instructions text changed. Instructions are injected into the client's system context — the LLM prompt surface for the whole server.") do |c| next nil unless c[:kind] == "instructions-changed" detail = c[:before].nil? ? "added" : (c[:after].nil? ? "removed" : "changed") finding("server", "instructions #{detail} (LLM prompt surface)") end, Rule.new(id: "default-value-changed", severity: "behavior", description: "A tool input parameter's default value changed. Calls that omit the param now behave differently.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "default-changed" f << finding(c[:name], %(param "#{sc[:param]}" default value changed)) end f.empty? ? nil : f end, # ───────────────── compat ───────────────── Rule.new(id: "tool-added", severity: "compat", description: "A new tool is exposed by the server.") do |c| next nil unless c[:kind] == "tool-added" finding(c[:name], "new tool") end, Rule.new(id: "tool-param-optional-added", severity: "compat", description: "A new optional input parameter was added. Existing calls keep working.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "param-added" && !sc[:required] f << finding(c[:name], %(new optional param "#{sc[:param]}")) end f.empty? ? nil : f end, Rule.new(id: "tool-enum-value-added", severity: "compat", description: "An enum value was added to a tool input parameter. Existing calls keep working; new inputs are accepted.") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["inputSchema"], c[:after]["inputSchema"]).each do |sc| next unless sc[:kind] == "enum-value-added" f << finding(c[:name], %(param "#{sc[:param]}": enum value(s) added: #{SchemaDiff.format_values(sc[:values])})) end f.empty? ? nil : f end, Rule.new(id: "tool-output-field-added", severity: "compat", description: "The tool's output schema gained a field. Old consumers ignore fields they don't know (covariant, D9).") do |c| next nil unless c[:kind] == "tool-modified" f = [] SchemaDiff.diff_object_schema(c[:before]["outputSchema"], c[:after]["outputSchema"]).each do |sc| next unless sc[:kind] == "param-added" f << finding(c[:name], %(output schema gained field "#{sc[:param]}")) end f.empty? ? nil : f end, Rule.new(id: "resource-added", severity: "compat", description: "A new resource is exposed by the server.") do |c| next nil unless c[:kind] == "resource-added" finding(c[:uri], "new resource") end, Rule.new(id: "prompt-added", severity: "compat", description: "A new prompt is exposed by the server.") do |c| next nil unless c[:kind] == "prompt-added" finding(c[:name], "new prompt") end, Rule.new(id: "capability-added", severity: "compat", description: "The server advertises a new capability.") do |c| next nil unless c[:kind] == "capability-added" finding(c[:capability], "new capability") end, # ───────────────── info ───────────────── Rule.new(id: "server-version-changed", severity: "info", description: "The server's reported version changed. Informational — not a contract change by itself.") do |c| next nil unless c[:kind] == "server-info-changed" before = (c[:before] || {})["version"] || "(none)" after = (c[:after] || {})["version"] || "(none)" next nil if before == after finding("server", "version #{before} → #{after}") end, Rule.new(id: "title-changed", severity: "info", description: "A display title changed (server, tool, resource, or prompt). Titles are for humans, not the model — informational.") do |c| case c[:kind] when "tool-modified", "prompt-modified" c[:before]["title"] != c[:after]["title"] ? finding(c[:name], "title changed") : nil when "resource-modified" c[:before]["title"] != c[:after]["title"] ? finding(c[:uri], "title changed") : nil when "server-info-changed" (c[:before] || {})["title"] != (c[:after] || {})["title"] ? finding("server", "title changed") : nil end end ] end |
.keys_of(value) ⇒ Object
16 17 18 |
# File 'lib/mcp_diff/core/rules.rb', line 16 def keys_of(value) value.is_a?(Hash) ? value : {} end |
.schema_findings(rule, change, schema_key) ⇒ Object
20 21 22 |
# File 'lib/mcp_diff/core/rules.rb', line 20 def schema_findings(rule, change, schema_key) SchemaDiff.diff_object_schema(change[:before][schema_key], change[:after][schema_key]) end |