Skip to content

Commit 3eb0821

Browse files
committed
[Refactor] Drop dead code + centralize tool response helpers
1 parent b2df2cd commit 3eb0821

13 files changed

Lines changed: 23 additions & 1734 deletions

mcp/lib/ruby_ui/mcp/rack_app.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,8 @@
66
module RubyUI
77
module MCP
88
class RackApp
9-
class << self
10-
def call(env)
11-
instance.call(env)
12-
end
13-
14-
def instance
15-
@instance ||= new
16-
end
17-
18-
def reset!
19-
@instance = nil
20-
end
9+
def self.call(env)
10+
(@instance ||= new).call(env)
2111
end
2212

2313
def initialize(registry: RubyUI::MCP.registry)

mcp/lib/ruby_ui/mcp/registry.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "json"
4-
require "set"
54

65
module RubyUI
76
module MCP
@@ -22,22 +21,17 @@ def self.load(path)
2221
new(raw)
2322
end
2423

25-
attr_reader :version, :generated_at
24+
attr_reader :version
2625

2726
def initialize(raw)
2827
@version = raw[:version]
29-
@generated_at = raw[:generated_at]
3028
@components = raw[:components] || {}
3129
end
3230

3331
def list
3432
@components.values.map { |c| {name: c[:name], description: c[:description]} }
3533
end
3634

37-
def all
38-
@components.values
39-
end
40-
4135
def find(name)
4236
key = name.to_s.downcase.to_sym
4337
@components[key]

mcp/lib/ruby_ui/mcp/server.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "json"
43
require "mcp"
54
require "ruby_ui/mcp/registry"
65
require "ruby_ui/mcp/tools/get_project_registries"
@@ -109,15 +108,7 @@ def build_tool_class(definition)
109108
description: definition[:description],
110109
input_schema: definition[:input_schema]
111110
) do |server_context: nil, **args|
112-
payload = impl.call(**args)
113-
::MCP::Tool::Response.new([
114-
{type: "text", text: JSON.pretty_generate(payload)}
115-
])
116-
rescue => e
117-
::MCP::Tool::Response.new(
118-
[{type: "text", text: "error: #{e.class}: #{e.message}"}],
119-
error: true
120-
)
111+
impl.respond { impl.call(**args) }
121112
end
122113
end
123114
end

mcp/lib/ruby_ui/mcp/tools/base.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "json"
4+
35
module RubyUI
46
module MCP
57
module Tools
@@ -11,6 +13,17 @@ def initialize(registry:)
1113
def call(**args)
1214
raise NotImplementedError
1315
end
16+
17+
def gem_version
18+
@registry&.version
19+
end
20+
21+
def respond
22+
payload = yield
23+
::MCP::Tool::Response.new([{type: "text", text: ::JSON.pretty_generate(payload)}])
24+
rescue => e
25+
::MCP::Tool::Response.new([{type: "text", text: "error: #{e.class}: #{e.message}"}], error: true)
26+
end
1427
end
1528
end
1629
end

mcp/lib/ruby_ui/mcp/tools/get_add_command_for_items.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def call(items:, **)
1515
components: known,
1616
unresolved: unresolved,
1717
command_string: known.empty? ? "" : "rails g #{GENERATOR} #{known.join(" ")}",
18-
gem_version: @registry.version
18+
gem_version: gem_version
1919
}
2020
end
2121
end

mcp/lib/ruby_ui/mcp/tools/get_item_examples_from_registries.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def call(items:, **)
1111
c = @registry.find(n)
1212
c ? {name: c[:name], examples: c[:examples] || []} : nil
1313
end.compact
14-
{items: resolved, gem_version: @registry.version}
14+
{items: resolved, gem_version: gem_version}
1515
end
1616
end
1717
end

mcp/lib/ruby_ui/mcp/tools/list_items_in_registries.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module MCP
77
module Tools
88
class ListItemsInRegistries < Base
99
def call(**)
10-
{items: @registry.list, gem_version: @registry.version}
10+
{items: @registry.list, gem_version: gem_version}
1111
end
1212
end
1313
end

mcp/lib/ruby_ui/mcp/tools/search_items_in_registries.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module MCP
77
module Tools
88
class SearchItemsInRegistries < Base
99
def call(query:, limit: 10, **)
10-
{items: @registry.search(query, limit: limit), gem_version: @registry.version}
10+
{items: @registry.search(query, limit: limit), gem_version: gem_version}
1111
end
1212
end
1313
end

mcp/lib/ruby_ui/mcp/tools/view_items_in_registries.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def call(items:, **)
1313
comp = @registry.find(name)
1414
comp ? resolved << comp : unresolved << name
1515
end
16-
{items: resolved, unresolved: unresolved, gem_version: @registry.version}
16+
{items: resolved, unresolved: unresolved, gem_version: gem_version}
1717
end
1818
end
1919
end

mcp/test/fixtures/registry.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"version": "1.2.0",
3-
"generated_at": "2026-05-09T00:00:00Z",
43
"components": {
54
"button": {
65
"name": "Button",

0 commit comments

Comments
 (0)