Array parameter `items` field not stored during MCP tool parsing, causing OpenAI schema validation failures
Description
Array parameter items field not stored during MCP tool parsing, causing OpenAI schema validation failures
Bug Description
When ruby_llm-mcp parses MCP tool definitions from servers, the items field for array parameters is extracted from the JSON-RPC response but never stored in the Parameter object. This causes OpenAI's function calling API to reject the tools with schema validation errors.
Impact
- OpenAI users affected: Tools with array parameters fail to register with OpenAI
- Error message:
Invalid schema for function 'tool_name': array schema missing items - Workaround required: Monkey patching to preserve the field
- Claude users may not notice: Anthropic's API appears more forgiving about incomplete schemas
Root Cause
In lib/ruby_llm/mcp/tool.rb, the process_parameter method extracts the items field but doesn't assign it to the Parameter object:
def process_parameter(param_name, param_data)
param = Parameter.new
param.type = param_data["type"] # ✅ Stored
param.description = param_data["description"] # ✅ Stored
items = param_data["items"] # 🔍 Extracted from MCP response
# 🐛 BUG: Never assigned to param!
# Missing: param.items = items
param # Returns with @items = nil
end
Reproduction Steps
-
Setup MCP server with array parameters that have
itemsdefined:# MCP server tool definition input_schema( properties: { filters: { type: "array", items: { # This gets lost! type: "object", properties: { field: { type: "string" }, operator: { type: "string" }, value: { type: "string" } } } } } ) -
Connect via ruby_llm-mcp client:
client = RubyLLM::MCP::Client.new(url: "http://localhost:8080/api/mcp") client.connect tools = client.tools # items field is nil here -
Use with RubyLLM + OpenAI:
RubyLLM.configure do |config| config.openai_api_key = ENV["OPENAI_API_KEY"] end # Try to register tools with OpenAI agent = SwarmSDK.build do agent :assistant do model "gpt-4o" provider :openai mcp_server(:my_server, url: "http://localhost:8080/api/mcp") end end -
Result: OpenAI rejects the schema:
OpenAI::InvalidRequestError: Invalid schema for function 'my_tool': array schema missing items (Property: tools.functions.my_tool.parameters.properties.filters)
Expected Behavior
Parameter objects should preserve all schema fields from the MCP response, including:
items(for arrays)properties(for objects)enum(for enumerations)required(for object field requirements)minimum/maximum(for numbers)pattern(for strings)- etc.
Actual Behavior
Only type and description are reliably stored. Other fields are lost during parsing.
Evidence from MCP Wire Traffic
The MCP server does send the complete schema. Here's what the HTTP response looks like:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [{
"name": "my_tool",
"inputSchema": {
"properties": {
"filters": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": {"type": "string"},
"operator": {"type": "string"},
"value": {"type": "string"}
}
}
}
}
}
}]
}
}
The items field is present in the response! But after process_parameter runs, param.items is nil.
Related Issues
This appears to be the root cause of #28:
- Issue #28 reported:
@items["type"]crashes withNoMethodError - Fix for #28: Added safe navigation (
&.) to prevent crash - But: Didn't investigate why
@itemsis nil in the first place - This issue: Explains why - the field is never stored during parsing!
Also related to the pattern seen in #17, where schema fields were assumed to exist without proper handling.
Environment
ruby_llm-mcp: 0.6.1 (latest)ruby_llm: 1.8.2 (latest)- Ruby: 3.4.4
- Provider: OpenAI (gpt-4o)
- MCP Server: Escalator (Rails-based MCP server with 9 tools)
Proposed Solution
Update process_parameter to store all relevant schema fields:
def process_parameter(param_name, param_data)
param = Parameter.new
param.type = param_data["type"]
param.description = param_data["description"]
# Store items field for arrays
if param_data["items"]
param.items = process_nested_schema(param_data["items"])
end
# Store properties field for objects
if param_data["properties"]
param.properties = process_nested_properties(param_data["properties"])
end
# Store enum values
if param_data["enum"]
param.enum = param_data["enum"]
end
# Store required fields (when it's an array, not a boolean)
if param_data["required"].is_a?(Array)
param.required = param_data["required"]
end
# Store validation constraints
param.minimum = param_data["minimum"] if param_data["minimum"]
param.maximum = param_data["maximum"] if param_data["maximum"]
param.minItems = param_data["minItems"] if param_data["minItems"]
param.maxItems = param_data["maxItems"] if param_data["maxItems"]
param.pattern = param_data["pattern"] if param_data["pattern"]
param
end
private
def process_nested_schema(schema_data)
return schema_data unless schema_data.is_a?(Hash)
# Recursively process nested schemas
processed = schema_data.dup
if processed["properties"].is_a?(Hash)
processed["properties"] = processed["properties"].transform_values do |prop|
process_nested_schema(prop)
end
end
if processed["items"]
processed["items"] = process_nested_schema(processed["items"])
end
processed
end
Workaround
Until this is fixed, users can monkey patch the gem:
# lib/ruby_llm_mcp_parameter_patch.rb
module RubyLLMMCPParameterPatch
def process_parameter(param_name, param_data)
param = super(param_name, param_data)
# Manually store the items field that was dropped
if param_data["items"]
param.items = param_data["items"]
end
param
end
end
RubyLLM::MCP::Tool.prepend(RubyLLMMCPParameterPatch)
However, users also need to patch ruby_llm itself to serialize the items field (see additional context below).
Additional Context
This bug requires a two-layer fix:
- Fix ruby_llm-mcp (this issue): Store the items field during parsing
- Fix ruby_llm (separate issue): Serialize the items field to OpenAI
The second issue is in ruby_llm's OpenAI provider (lib/ruby_llm/providers/openai.rb), which only serializes type and description:
def param_schema(param)
{
type: param.type,
description: param.description
# 🐛 Missing: items, properties, enum, etc.
}
end
I can open a separate issue for ruby_llm if helpful.
Test Case
I have a complete test case with:
- Working MCP server (Escalator with 9 tools)
- Reproduction script showing the failure
- Working monkey patches that fix both layers
- Verification that all tools work with the patches applied
Happy to share if useful for debugging/testing.
Why This Matters
MCP is gaining adoption, and many users will want to use OpenAI (not just Claude). This bug blocks entire categories of tools (anything with array parameters) from working with OpenAI.
Given that JSON Schema arrays require the items field to be valid, this is a critical compatibility issue for OpenAI users.
Thank you for maintaining this gem! Let me know if I can provide any additional information or testing.
Labels to Suggest
bugschemaopenaiparameters
Additional Files to Attach
lib/ruby_llm_mcp_parameter_patch.rb- Working monkey patchtest_patch.rb- Reproduction scriptROOT_CAUSE_ANALYSIS.md- Complete technical investigation
Notes
- Keep tone respectful and collaborative
- Acknowledge maintainer's work on similar issues (#17, #28)
- Offer to help with testing/PR if needed
- Provide complete reproduction steps so issue is actionable
- Include working solution to make fix easier
Updated Issue Comment for GitHub Issue #70
Update: More Accurate Analysis After Reviewing Source Code
I've reviewed the actual source code and need to clarify my initial report. The issue is more nuanced than I first described.
What I Got Wrong
My original report stated that the items field is "not stored" during parameter processing. This is partially incorrect.
Looking at the actual code in lib/ruby_llm/mcp/tool.rb lines 135-136:
if param.type == :array
items = param_data["items"] # Line 135: Extracts items
param.items = items # Line 136: ✅ DOES store it!
The items field IS being stored! So my initial analysis was incomplete.
The ACTUAL Bugs
After reviewing the source, there are actually three separate issues:
Bug #1: Missing Nil Checks (Lines 137, 140) 🐛
Location: lib/ruby_llm/mcp/tool.rb line 137
if param.type == :array
items = param_data["items"]
param.items = items
if items.key?("properties") # 🐛 Crashes if items is nil
param.properties = create_parameters(items)
end
if items.key?("enum") # 🐛 Crashes if items is nil (line 140)
param.enum = items["enum"]
end
end
Problem: When items is nil, calling items.key?() raises NoMethodError.
This explains Issue #28! That issue fixed the crash in item_type but didn't fix the root cause here.
Fix:
if items && items.key?("properties") # ✅ Add nil check
param.properties = create_parameters(items)
end
if items && items.key?("enum") # ✅ Add nil check
param.enum = items["enum"]
end
Bug #2: Items Stored as Raw Hash (Line 136) 🐛
Location: lib/ruby_llm/mcp/tool.rb line 136
param.items = items # Stores raw hash from JSON response
Problem: The items value is stored as-is (raw hash), but for deeply nested schemas, it should be recursively processed.
Example:
# Input from MCP server:
{
"type": "array",
"items": {
"type": "object",
"properties": {
"field": {"type": "string"},
"operator": {"type": "string"}
}
}
}
# Current: Items stored as raw hash
param.items = {"type" => "object", "properties" => {...}}
# Better: Recursively process nested schemas
# (though raw hash might be acceptable if serialization handles it)
This might be acceptable depending on how the downstream code (RubyLLM) handles it.
Bug #3: RubyLLM's OpenAI Provider Doesn't Serialize Items 🐛
Location: Different gem (ruby_llm gem, not this repo)
Even though param.items is stored, when RubyLLM serializes the parameters for OpenAI's function calling API, it only includes type and description, dropping the items field.
This is why OpenAI rejects the schema even though the field exists in the Parameter object.
I can open a separate issue in the ruby_llm repository if that would be helpful.
Why This Causes OpenAI Validation Failures
The bug chain:
- ✅ ruby_llm-mcp stores items (line 136)
- 🐛 BUT crashes on nil checks (lines 137, 140) for some schemas
- ✅ Items field exists in Parameter object
- 🐛 BUT ruby_llm doesn't serialize items to OpenAI
- ❌ OpenAI rejects incomplete schema
Proposed Fixes
Minimal Fix (ruby_llm-mcp only)
Add nil checks to prevent crashes:
File: lib/ruby_llm/mcp/tool.rb
Line 137: https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L137
# Before:
if items.key?("properties")
param.properties = create_parameters(items)
end
# After:
if items && items.key?("properties")
param.properties = create_parameters(items)
end
Line 140: https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L140
# Before:
if items.key?("enum")
param.enum = items["enum"]
end
# After:
if items && items.key?("enum")
param.enum = items["enum"]
end
Complete Fix (requires both gems)
- Fix ruby_llm-mcp: Add nil checks (above)
- Fix ruby_llm: Update OpenAI provider to serialize
items,properties,enum, etc.
The second fix is in a different repository (crmne/ruby_llm).
My Workaround
I'm currently using two monkey patches:
- One for ruby_llm-mcp (ensures items handling doesn't crash)
- One for ruby_llm (ensures items is serialized to OpenAI)
Both patches are working successfully with all 9 tools from our Escalator MCP server.
Apologies for Confusion
Sorry for the initial inaccurate report! After reviewing the actual source code, I now understand that:
- The
itemsfield IS stored (line 136) ✅ - But nil checks are missing (lines 137, 140) 🐛
- And the main OpenAI issue is in the ruby_llm gem's serialization 🐛
The root cause is more complex than I initially described. I hope this clarification is helpful!
Additional Context: Links to Source Code
- process_parameter method: https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L126-L150
- Parameter class with items accessor: https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/parameter.rb#L6
- Line 136 (items assignment): https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L136
- Line 137 (missing nil check): https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L137
- Line 140 (missing nil check): https://github.com/patvice/ruby_llm-mcp/blob/main/lib/ruby_llm/mcp/tool.rb#L140
I should have read this issue before.
Here's a comment I left on a different issue explaining my findings about this bug
Hey @aminpaks, do you have patch for the issue already made? Happy to get a contribution from this, if not let me know.
Hey @aminpaks! Our tool params implementation changed significantly between versions 0.6 and 0.7 of the gem. We now pass the params information directly from the MCP server into RubyLLM. This became possible when RubyLLM added full support for structured params through the RubyLLM::Schema dependency getting packed in by default.
That being said, this issue should no longer occur in RubyLLM 1.9 and RubyLLM::MCP 0.7, so I’m going to close this out. If you run into any other issues, please open a new one.
Thanks again for taking the time to investigate and report this.