Skip to content

Commit eacf51e

Browse files
authored
Merge pull request #279 from koic/refactor__use_symbolize_names
Use `symbolize_names: true` in `JSON.parse` calls
2 parents 6b415e2 + 3a68937 commit eacf51e

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

lib/mcp/server/transports/streamable_http_transport.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def handle_post(request)
176176
body = parse_request_body(body_string)
177177
return body unless body.is_a?(Hash) # Error response
178178

179-
if body["method"] == "initialize"
179+
if body[:method] == "initialize"
180180
handle_initialization(body_string, body)
181181
else
182182
return missing_session_id_response if !@stateless && !session_id
@@ -277,17 +277,17 @@ def not_acceptable_response(required_types)
277277
end
278278

279279
def parse_request_body(body_string)
280-
JSON.parse(body_string)
280+
JSON.parse(body_string, symbolize_names: true)
281281
rescue JSON::ParserError, TypeError
282282
[400, { "Content-Type" => "application/json" }, [{ error: "Invalid JSON" }.to_json]]
283283
end
284284

285285
def notification?(body)
286-
!body["id"] && !!body["method"]
286+
!body[:id] && !!body[:method]
287287
end
288288

289289
def response?(body)
290-
!!body["id"] && !body["method"]
290+
!!body[:id] && !body[:method]
291291
end
292292

293293
def handle_initialization(body_string, body)

lib/mcp/tool/schema.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Schema
88
attr_reader :schema
99

1010
def initialize(schema = {})
11-
@schema = deep_transform_keys(JSON.parse(JSON.dump(schema)), &:to_sym)
11+
@schema = JSON.parse(JSON.dump(schema), symbolize_names: true)
1212
@schema[:type] ||= "object"
1313
validate_schema!
1414
end
@@ -27,19 +27,6 @@ def fully_validate(data)
2727
JSON::Validator.fully_validate(to_h, data)
2828
end
2929

30-
def deep_transform_keys(schema, &block)
31-
case schema
32-
when Hash
33-
schema.each_with_object({}) do |(key, value), result|
34-
result[yield(key)] = deep_transform_keys(value, &block)
35-
end
36-
when Array
37-
schema.map { |e| deep_transform_keys(e, &block) }
38-
else
39-
schema
40-
end
41-
end
42-
4330
def validate_schema!
4431
schema = to_h
4532
gem_path = File.realpath(Gem.loaded_specs["json-schema"].full_gem_path)

0 commit comments

Comments
 (0)