Fix nil handling in item_type method and add parameter specs#37
Fix nil handling in item_type method and add parameter specs#37patvice merged 2 commits intopatvice:mainfrom MadBomber:nil_item_type
Conversation
This commit updates the `item_type` method to safely handle cases where the `@items` variable may be nil, preventing potential exceptions. The change utilizes the safe navigation operator `&.` to ensure the method returns nil instead of raising an error when `@items` is not properly initialized. Additionally, this commit introduces a set of RSpec tests for the `RubyLLM::MCP::Parameter` class. These tests cover various cases for the `item_type` method, ensuring it behaves correctly regarding the presence or absence of the `type` key in the `@items` hash as well as validating the class initialization. This enhancement improves code robustness and test coverage. In Model Context Protocol (MCP), having a nil value for the item type in a Parameter object has specific implications: MCP Parameter Schema Context In MCP, parameters are defined using JSON Schema, and the items property is used specifically for array-type parameters to define the schema of array elements. What nil item_type means: 1. Non-array parameter - If @Items["type"] is nil, it most likely means this parameter is not an array type. The items property is only relevant for parameters with "type": "array". 2. Missing or incomplete schema - It could indicate: - The parameter schema doesn't include an items definition - The parameter is a primitive type (string, number, boolean, etc.) rather than an array - The schema is malformed or incomplete 3. Expected behavior - For non-array parameters, item_type returning nil is actually correct behavior since there's no array element type to describe. Example MCP Parameter Schemas: ```json // Array parameter (would have item_type) { "type": "array", "items": { "type": "string" // This would make item_type return :string } } // Non-array parameter (item_type should be nil) { "type": "string" // No "items" property, so item_type is nil } ``` The nil guard is correct because: - It prevents crashes when checking item_type on non-array parameters - It follows the MCP specification where items is only meaningful for array types - It allows the code to gracefully handle both array and non-array parameters The nil guard makes your Parameter class more robust when dealing with the variety of parameter types that MCP supports.
lib/ruby_llm/mcp/parameter.rb
Outdated
|
|
||
| def item_type | ||
| @items["type"].to_sym | ||
| @items&.[]("type")&.to_sym |
There was a problem hiding this comment.
Possible to use dig on this, that's the pattern that used in other locations in the codebase
@items&.dig("type")&.to_sym
spec/ruby_llm/mcp/parameter_spec.rb
Outdated
|
|
||
| RSpec.describe RubyLLM::MCP::Parameter do | ||
| describe "#item_type" do | ||
| context "when @items is nil" do |
There was a problem hiding this comment.
These context block was just on it statments - can you collapse them down or bucket some it statements together
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe RubyLLM::MCP::Parameter do | ||
| describe "#item_type" do |
There was a problem hiding this comment.
Looking at your specs, we are missing the case that kicked off the issue in the first place. An array type without a item type attached. Possible to add that case?
|
The premise of this PR makes sense, and I added a few comments for feedback. You do have a failing spec that would need to be corrected, otherwise should be good to merge with some corrections and passing specs. |
|
The test suite as many failures I think because it has some manual steps that must be performed w/r/t to either the vcr or live server setups. |
|
Really! VCR specs are only used when we have tests that are communicated to an LLM service. We do use a MCP server that is ran in Maybe just focus on fixing |
|
If you are having trouble - I'm happy to go in an fix up the spec on my machine, let me know |
|
Hey @MadBomber - had a moment this morning and when ahead and got your PR passing. Will end up putting all of this into a v0.5 release which I should be getting out today. Thanks again for the contribution |
This commit updates the
item_typemethod to safely handle cases where the@itemsvariable may be nil, preventing potential exceptions. The change utilizes the safe navigation operator&.to ensure the method returns nil instead of raising an error when@itemsis not properly initialized.Additionally, this commit introduces a set of RSpec tests for the
RubyLLM::MCP::Parameterclass. These tests cover various cases for theitem_typemethod, ensuring it behaves correctly regarding the presence or absence of thetypekey in the@itemshash as well as validating the class initialization. This enhancement improves code robustness and test coverage.In Model Context Protocol (MCP), having a nil value for the item type in a Parameter object has specific implications:
MCP Parameter Schema Context
In MCP, parameters are defined using JSON Schema, and the items property is used specifically for array-type parameters to define the schema of array elements.
What nil item_type means:
Example MCP Parameter Schemas:
The nil guard is correct because:
The nil guard makes your Parameter class more robust when dealing with the variety of parameter types that MCP supports.