Class: MCP::Rails::ServerGenerator::RouteCollector
- Inherits:
-
Object
- Object
- MCP::Rails::ServerGenerator::RouteCollector
- Defined in:
- lib/mcp/rails/server_generator/route_collector.rb
Class Method Summary collapse
- .build_param_structure(param) ⇒ Object
- .collect_routes(routes, prefix = "", engine = nil) ⇒ Object
- .escape_for_ruby_string(str) ⇒ Object
- .extract_url_params(path) ⇒ Object
- .process_routes(routes) ⇒ Object
Class Method Details
.build_param_structure(param) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mcp/rails/server_generator/route_collector.rb', line 63 def self.build_param_structure(param) structure = { name: param[:name], type: param[:type] || :string, required: param[:required] } structure[:item_type] = param[:item_type] if param[:item_type] # Add item_type for scalar arrays structure[:description] = escape_for_ruby_string(param[:example]) if param[:example] structure[:nested] = param[:nested].map { |n| build_param_structure(n) } if param[:nested] structure end |
.collect_routes(routes, prefix = "", engine = nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mcp/rails/server_generator/route_collector.rb', line 4 def self.collect_routes(routes, prefix = "", engine = nil) routes.map do |route| app = route.app.app if app.respond_to?(:routes) && app < ::Rails::Engine new_prefix = [ prefix, route.path.spec.to_s ].join collect_routes(app.app.routes.routes, new_prefix, app) else path = [ prefix, route.path.spec.to_s ].join { route: route, path: path.present? ? path : route.path.spec.to_s, engine: engine } end end.flatten end |
.escape_for_ruby_string(str) ⇒ Object
75 76 77 |
# File 'lib/mcp/rails/server_generator/route_collector.rb', line 75 def self.escape_for_ruby_string(str) str.to_s.gsub(/[\\"]/) { |m| "\\#{m}" } end |
.extract_url_params(path) ⇒ Object
79 80 81 |
# File 'lib/mcp/rails/server_generator/route_collector.rb', line 79 def self.extract_url_params(path) path.scan(/:([a-zA-Z0-9_]+)/).flatten.map { |name| { name: name, type: "string", required: true } } end |
.process_routes(routes) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mcp/rails/server_generator/route_collector.rb', line 22 def self.process_routes(routes) candidate_routes = routes.select do |wrapped_route| route = wrapped_route[:route] action = route.defaults[:action]&.to_s mcp = route.defaults[:mcp] mcp == true || (mcp.is_a?(Array) && action&.in?(mcp.map(&:to_s))) end candidate_routes.map do |wrapped_route| route = wrapped_route[:route] next unless route.defaults[:controller] && route.defaults[:action] next unless route.defaults[:action].to_s.in?(%w[create index show update destroy]) next if route.verb.downcase == "put" begin controller_class = "#{route.defaults[:controller].camelize}Controller".constantize action = route.defaults[:action].to_sym params_def = controller_class.permitted_params(action) rescue NameError ::Rails.logger.warn("Controller not found for route: #{route.defaults[:controller]}") next end full_path = (wrapped_route[:path] || route.path.spec.to_s).sub(/\(\.:format\)$/, "") || "" url_params = extract_url_params(full_path) params_def += url_params unless params_def.any? { |p| url_params.map { |up| up[:name] }.include?(p[:name]) } description = controller_class.tool_description(action) || "Handles #{action} for #{route.defaults[:controller]}" { tool_name: "#{action}_#{route.defaults[:controller].parameterize}", description: escape_for_ruby_string(description), method: route.verb.downcase.to_sym, path: full_path, url_parameters: url_params, engine: wrapped_route[:engine], accepted_parameters: params_def.map { |param| build_param_structure(param) } } end.compact end |