Skip to content

Commit 06e4e4f

Browse files
committed
Move camelCase out of function to data structure
1 parent 2eaa240 commit 06e4e4f

File tree

2 files changed

+10
-41
lines changed

2 files changed

+10
-41
lines changed

lib/modelcontextprotocol.rb

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,7 @@ class Error < StandardError; end
1010
# Module to automatically convert snake_case keys to camelCase when serializing to JSON.
1111
module CamelCaseJson
1212
def to_json(*options)
13-
JSON.generate(camelize_hash_keys(to_h), *options)
14-
end
15-
16-
private
17-
18-
def camelize_hash_keys(obj)
19-
case obj
20-
when Array
21-
obj.map { |item| camelize_hash_keys(item) }
22-
when Hash
23-
obj.each_with_object({}) do |(key, value), new_hash|
24-
new_key = modify_key(key.to_s)
25-
new_hash[new_key] = camelize_hash_keys(value)
26-
end
27-
else
28-
obj
29-
end
30-
end
31-
32-
def modify_key(key)
33-
# In some cases, we want to emit keys that don't get snake cased, so for that
34-
# we freeze strings and leave them alone.
35-
if key.is_a?(String) and key.frozen?
36-
key
37-
else
38-
camelize key
39-
end
40-
end
41-
42-
def camelize(snake_str)
43-
parts = snake_str.split('_')
44-
parts[0] + parts[1..-1].map(&:capitalize).join
13+
JSON.generate(as_json, *options)
4514
end
4615
end
4716

@@ -62,7 +31,7 @@ def initialize(name:, type:, description:, required: false)
6231
@required = required
6332
end
6433

65-
def to_h
34+
def as_json
6635
{
6736
type: type,
6837
description: description
@@ -96,11 +65,11 @@ def property(...)
9665

9766
def properties_hash
9867
properties.each_with_object Hash.new do |prop, hash|
99-
hash[prop.name.freeze] = prop.to_h
68+
hash[prop.name] = prop.as_json
10069
end
10170
end
10271

103-
def to_h
72+
def as_json
10473
{
10574
type: type,
10675
properties: properties_hash,
@@ -133,11 +102,11 @@ def input(...)
133102
self
134103
end
135104

136-
def to_h
105+
def as_json
137106
{
138107
name: name,
139108
description: description,
140-
input_schema: input_schema ? input_schema.to_h : nil
109+
inputSchema: input_schema ? input_schema.as_json : nil
141110
}
142111
end
143112
end
@@ -161,13 +130,13 @@ def add_tool(tool)
161130
self
162131
end
163132

164-
def to_h
133+
def as_json
165134
{
166135
jsonrpc: "2.0",
167136
id: id,
168137
result: {
169-
tools: tools.map(&:to_h),
170-
next_cursor: next_cursor
138+
tools: tools.map(&:as_json),
139+
nextCursor: next_cursor
171140
}
172141
}
173142
end

spec/modelcontextprotocol_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
)
2020
end
2121

22-
input_schema = tool.to_h[:input_schema]
22+
input_schema = tool.as_json[:inputSchema]
2323
expect(input_schema[:type]).to eq("object")
2424
expect(input_schema[:properties]["test_property"][:type]).to eq("string")
2525
expect(input_schema[:properties]["test_property"][:description]).to eq("A test property")

0 commit comments

Comments
 (0)