Merge pull request #279 from koic/refactor__use_symbolize_names
Use `symbolize_names: true` in `JSON.parse` calls
This commit is contained in:
commit
eacf51ec94
2 changed files with 5 additions and 18 deletions
|
|
@ -176,7 +176,7 @@ module MCP
|
||||||
body = parse_request_body(body_string)
|
body = parse_request_body(body_string)
|
||||||
return body unless body.is_a?(Hash) # Error response
|
return body unless body.is_a?(Hash) # Error response
|
||||||
|
|
||||||
if body["method"] == "initialize"
|
if body[:method] == "initialize"
|
||||||
handle_initialization(body_string, body)
|
handle_initialization(body_string, body)
|
||||||
else
|
else
|
||||||
return missing_session_id_response if !@stateless && !session_id
|
return missing_session_id_response if !@stateless && !session_id
|
||||||
|
|
@ -277,17 +277,17 @@ module MCP
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_request_body(body_string)
|
def parse_request_body(body_string)
|
||||||
JSON.parse(body_string)
|
JSON.parse(body_string, symbolize_names: true)
|
||||||
rescue JSON::ParserError, TypeError
|
rescue JSON::ParserError, TypeError
|
||||||
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
|
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
|
||||||
end
|
end
|
||||||
|
|
||||||
def notification?(body)
|
def notification?(body)
|
||||||
!body["id"] && !!body["method"]
|
!body[:id] && !!body[:method]
|
||||||
end
|
end
|
||||||
|
|
||||||
def response?(body)
|
def response?(body)
|
||||||
!!body["id"] && !body["method"]
|
!!body[:id] && !body[:method]
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_initialization(body_string, body)
|
def handle_initialization(body_string, body)
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ module MCP
|
||||||
attr_reader :schema
|
attr_reader :schema
|
||||||
|
|
||||||
def initialize(schema = {})
|
def initialize(schema = {})
|
||||||
@schema = deep_transform_keys(JSON.parse(JSON.dump(schema)), &:to_sym)
|
@schema = JSON.parse(JSON.dump(schema), symbolize_names: true)
|
||||||
@schema[:type] ||= "object"
|
@schema[:type] ||= "object"
|
||||||
validate_schema!
|
validate_schema!
|
||||||
end
|
end
|
||||||
|
|
@ -27,19 +27,6 @@ module MCP
|
||||||
JSON::Validator.fully_validate(to_h, data)
|
JSON::Validator.fully_validate(to_h, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def deep_transform_keys(schema, &block)
|
|
||||||
case schema
|
|
||||||
when Hash
|
|
||||||
schema.each_with_object({}) do |(key, value), result|
|
|
||||||
result[yield(key)] = deep_transform_keys(value, &block)
|
|
||||||
end
|
|
||||||
when Array
|
|
||||||
schema.map { |e| deep_transform_keys(e, &block) }
|
|
||||||
else
|
|
||||||
schema
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_schema!
|
def validate_schema!
|
||||||
schema = to_h
|
schema = to_h
|
||||||
gem_path = File.realpath(Gem.loaded_specs["json-schema"].full_gem_path)
|
gem_path = File.realpath(Gem.loaded_specs["json-schema"].full_gem_path)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue