active_scaffold_mcp 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +46 -1
- data/lib/active_scaffold/config/mcp.rb +31 -31
- data/lib/active_scaffold/mcp/action_config.rb +2 -2
- data/lib/active_scaffold/mcp/engine.rb +2 -2
- data/lib/active_scaffold/mcp/filtered_tools.rb +1 -1
- data/lib/active_scaffold/mcp/generator_factory.rb +3 -3
- data/lib/active_scaffold/mcp/list_tool.rb +7 -7
- data/lib/active_scaffold/mcp/list_tool_filters.rb +204 -0
- data/lib/active_scaffold/mcp/list_tool_generator.rb +49 -209
- data/lib/active_scaffold/mcp/list_views_tool.rb +3 -4
- data/lib/active_scaffold/mcp/presenter.rb +155 -24
- data/lib/active_scaffold/mcp/profile_user.rb +2 -2
- data/lib/active_scaffold/mcp/settings_validator.rb +1 -1
- data/lib/active_scaffold/mcp/show_tool.rb +7 -6
- data/lib/active_scaffold/mcp/show_tool_generator.rb +40 -21
- data/lib/active_scaffold/mcp/tool.rb +7 -38
- data/lib/active_scaffold/mcp/tool_generator.rb +6 -6
- data/lib/active_scaffold/mcp/tool_params.rb +49 -0
- data/lib/active_scaffold/mcp/version.rb +1 -1
- data/lib/active_scaffold/mcp.rb +17 -18
- data/lib/active_scaffold_mcp.rb +5 -5
- data/lib/tasks/active_scaffold_mcp_tasks.rake +2 -2
- metadata +6 -2
|
@@ -6,12 +6,26 @@ module ActiveScaffold
|
|
|
6
6
|
class << self
|
|
7
7
|
def generate_tool_for(controller_class:, action_name:, action_config:, mcp_config:, settings:)
|
|
8
8
|
core_config = mcp_config.core
|
|
9
|
+
primary_key, lookup_columns = show_identifiers(core_config, settings)
|
|
10
|
+
build_show_tool(
|
|
11
|
+
controller_class:, action_name:, core_config:, mcp_config:, settings:, primary_key:, lookup_columns:
|
|
12
|
+
)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def show_identifiers(core_config, settings)
|
|
9
18
|
primary_key = core_config.model.primary_key
|
|
10
|
-
raise ArgumentError,
|
|
11
|
-
raise ArgumentError,
|
|
19
|
+
raise ArgumentError, 'cannot generate a show tool without a primary key' if primary_key.blank?
|
|
20
|
+
raise ArgumentError, 'composite primary keys are not supported by the show tool' if primary_key.is_a?(Array)
|
|
21
|
+
|
|
12
22
|
lookup_columns = Array(settings[:lookup_columns]).map(&:to_sym).uniq - [primary_key.to_sym]
|
|
13
23
|
validate_lookup_columns!(core_config, lookup_columns)
|
|
24
|
+
[primary_key, lookup_columns]
|
|
25
|
+
end
|
|
14
26
|
|
|
27
|
+
def build_show_tool(controller_class:, action_name:, core_config:, mcp_config:, settings:, primary_key:,
|
|
28
|
+
lookup_columns:)
|
|
15
29
|
Class.new(ActiveScaffold::MCP::ShowTool).tap do |tool_class|
|
|
16
30
|
configure_tool_class(tool_class, controller_class:, action_name:, core_config:, mcp_config:, settings:)
|
|
17
31
|
tool_class.mcp_lookup_column_names = lookup_columns.freeze
|
|
@@ -20,24 +34,27 @@ module ActiveScaffold
|
|
|
20
34
|
tool_class.include ActiveScaffold::Actions::Show
|
|
21
35
|
tool_class.description(settings[:description] || description(tool_class.title, lookup_columns))
|
|
22
36
|
tool_class.read_only
|
|
23
|
-
tool_class
|
|
24
|
-
|
|
25
|
-
description: "Database ID of the #{singular_label(mcp_config)} to return",
|
|
26
|
-
required: lookup_columns.empty?
|
|
27
|
-
mappings = {id: {target: :id, operator: :equal}}
|
|
28
|
-
lookup_columns.each do |column_name|
|
|
29
|
-
tool_class.property column_name,
|
|
30
|
-
**schema_options(core_config._columns_hash[column_name.to_s]),
|
|
31
|
-
description: "#{column_name.to_s.humanize} of the #{singular_label(mcp_config)} to return"
|
|
32
|
-
mappings[column_name] = {target: column_name, operator: :equal}
|
|
33
|
-
end
|
|
34
|
-
tool_class.mcp_param_mappings = mappings.freeze
|
|
35
|
-
|
|
37
|
+
mappings = add_show_properties(tool_class, core_config, mcp_config, primary_key, lookup_columns)
|
|
38
|
+
tool_class.mcp_param_mappings = mappings
|
|
36
39
|
include_extensions(tool_class, settings)
|
|
37
40
|
end
|
|
38
41
|
end
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
def add_show_properties(tool_class, core_config, mcp_config, primary_key, lookup_columns)
|
|
44
|
+
tool_class.property :id,
|
|
45
|
+
**schema_options(core_config._columns_hash[primary_key.to_s]),
|
|
46
|
+
description: "Database ID of the #{singular_label(mcp_config)} to return",
|
|
47
|
+
required: lookup_columns.empty?
|
|
48
|
+
mappings = {id: {target: :id, operator: :equal}}
|
|
49
|
+
lookup_columns.each do |column_name|
|
|
50
|
+
property_description = "#{column_name.to_s.humanize} of the #{singular_label(mcp_config)} to return"
|
|
51
|
+
tool_class.property column_name,
|
|
52
|
+
**schema_options(core_config._columns_hash[column_name.to_s]),
|
|
53
|
+
description: property_description
|
|
54
|
+
mappings[column_name] = {target: column_name, operator: :equal}
|
|
55
|
+
end
|
|
56
|
+
mappings.freeze
|
|
57
|
+
end
|
|
41
58
|
|
|
42
59
|
def validate_lookup_columns!(core_config, lookup_columns)
|
|
43
60
|
unknown_columns = lookup_columns.reject { |name| core_config._columns_hash.key?(name.to_s) }
|
|
@@ -55,13 +72,15 @@ module ActiveScaffold
|
|
|
55
72
|
end
|
|
56
73
|
|
|
57
74
|
def description(generated_title, lookup_columns)
|
|
58
|
-
identifiers = [
|
|
75
|
+
identifiers = ['id', *lookup_columns.map(&:to_s)]
|
|
59
76
|
human_identifiers = identifiers.map { |name| name.humanize.downcase }
|
|
60
77
|
arguments = identifiers.map { |name| "`#{name}`" }
|
|
61
|
-
resource_label = generated_title.delete_prefix(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
78
|
+
resource_label = generated_title.delete_prefix('Show ').singularize
|
|
79
|
+
identifiers_desc = human_identifiers.to_sentence(two_words_connector: ' or ', last_word_connector: ' or ')
|
|
80
|
+
arguments_desc = arguments.to_sentence(two_words_connector: ' or ', last_word_connector: ' or ')
|
|
81
|
+
"Retrieve one #{resource_label} by #{identifiers_desc}. " \
|
|
82
|
+
'Use this tool when the user supplies one of these identifiers. ' \
|
|
83
|
+
"Pass exactly one of #{arguments_desc}."
|
|
65
84
|
end
|
|
66
85
|
end
|
|
67
86
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'active_scaffold/mcp/tool_params'
|
|
4
|
+
|
|
3
5
|
module ActiveScaffold
|
|
4
6
|
module MCP
|
|
5
7
|
class Tool < ActiveScaffold::Config::MCP.resolve_tool(:base)
|
|
@@ -45,6 +47,7 @@ module ActiveScaffold
|
|
|
45
47
|
include ActiveScaffold::Finder
|
|
46
48
|
include ActiveScaffold::Constraints
|
|
47
49
|
include ActiveScaffold::Actions::Core
|
|
50
|
+
include ActiveScaffold::MCP::ToolParams
|
|
48
51
|
|
|
49
52
|
around_perform :with_active_scaffold_context
|
|
50
53
|
|
|
@@ -93,8 +96,8 @@ module ActiveScaffold
|
|
|
93
96
|
end
|
|
94
97
|
rescue ActiveScaffold::ActionNotAllowed
|
|
95
98
|
report_error(action_not_allowed_message)
|
|
96
|
-
rescue StandardError =>
|
|
97
|
-
report_active_scaffold_exception(
|
|
99
|
+
rescue StandardError => e
|
|
100
|
+
report_active_scaffold_exception(e)
|
|
98
101
|
raise
|
|
99
102
|
ensure
|
|
100
103
|
ActiveScaffold::Registry.current_user_proc = previous_user_proc
|
|
@@ -107,10 +110,10 @@ module ActiveScaffold
|
|
|
107
110
|
"#{exception.full_message(highlight: false, order: :top)}"
|
|
108
111
|
)
|
|
109
112
|
notify_active_scaffold_exception(exception) if respond_to?(:notify_active_scaffold_exception, true)
|
|
110
|
-
rescue StandardError =>
|
|
113
|
+
rescue StandardError => e
|
|
111
114
|
Rails.logger.error(
|
|
112
115
|
"[ActiveScaffold::MCP] exception notification failed for #{self.class.tool_name}: " \
|
|
113
|
-
"#{
|
|
116
|
+
"#{e.full_message(highlight: false, order: :top)}"
|
|
114
117
|
)
|
|
115
118
|
end
|
|
116
119
|
|
|
@@ -140,36 +143,6 @@ module ActiveScaffold
|
|
|
140
143
|
self.class.tool_authorized?(current_user)
|
|
141
144
|
end
|
|
142
145
|
|
|
143
|
-
def build_active_scaffold_params
|
|
144
|
-
filter_values = {}
|
|
145
|
-
ranges = Hash.new { |hash, key| hash[key] = {} }
|
|
146
|
-
|
|
147
|
-
self.class.mcp_param_mappings.each do |property_name, mapping|
|
|
148
|
-
next unless @provided_mcp_arguments.include?(property_name.to_s)
|
|
149
|
-
|
|
150
|
-
value = public_send(property_name)
|
|
151
|
-
target = mapping.fetch(:target).to_s
|
|
152
|
-
case mapping.fetch(:operator)
|
|
153
|
-
when :equal, :in
|
|
154
|
-
filter_values[target] = value
|
|
155
|
-
when :not_equal, :not_in
|
|
156
|
-
filter_values["#{target}!"] = value
|
|
157
|
-
when :between_from, :between_to, :not_between_from, :not_between_to
|
|
158
|
-
ranges[[target, mapping[:operator].to_s.start_with?("not_")]][mapping[:operator].to_s.end_with?("from") ? :from : :to] = value
|
|
159
|
-
when :null
|
|
160
|
-
filter_values[target] = "" if value
|
|
161
|
-
when :not_null
|
|
162
|
-
filter_values["#{target}!"] = "" if value
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
ranges.each do |(target, negated), endpoints|
|
|
167
|
-
filter_values["#{target}#{'!' if negated}"] = "#{endpoints[:from]}..#{endpoints[:to]}"
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
filter_values.merge(format: "json")
|
|
171
|
-
end
|
|
172
|
-
|
|
173
146
|
def mcp_output_columns
|
|
174
147
|
configured_columns = mcp_configured_output_column_names
|
|
175
148
|
columns = if configured_columns
|
|
@@ -226,10 +199,6 @@ module ActiveScaffold
|
|
|
226
199
|
raise NotImplementedError
|
|
227
200
|
end
|
|
228
201
|
|
|
229
|
-
def mcp_argument_provided?(name)
|
|
230
|
-
@provided_mcp_arguments.include?(name.to_s)
|
|
231
|
-
end
|
|
232
|
-
|
|
233
202
|
# The controller implementation branches on request methods and renders
|
|
234
203
|
# an HTTP response. MCP operations already represent the submitted branch.
|
|
235
204
|
def process_action_link_action(_render_action = :action_update, crud_type_or_security_options = nil)
|
|
@@ -61,22 +61,22 @@ module ActiveScaffold
|
|
|
61
61
|
def json_type(column)
|
|
62
62
|
case column&.type&.to_sym
|
|
63
63
|
when :integer, :bigint
|
|
64
|
-
|
|
64
|
+
'integer'
|
|
65
65
|
when :float, :decimal
|
|
66
|
-
|
|
66
|
+
'number'
|
|
67
67
|
when :boolean
|
|
68
|
-
|
|
68
|
+
'boolean'
|
|
69
69
|
else
|
|
70
|
-
|
|
70
|
+
'string'
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def json_format(column)
|
|
75
75
|
case column&.type&.to_sym
|
|
76
76
|
when :date
|
|
77
|
-
|
|
77
|
+
'date'
|
|
78
78
|
when :datetime, :timestamp, :timestamptz
|
|
79
|
-
|
|
79
|
+
'date-time'
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveScaffold
|
|
4
|
+
module MCP
|
|
5
|
+
module ToolParams
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def build_active_scaffold_params
|
|
9
|
+
filter_values = {}
|
|
10
|
+
ranges = Hash.new { |hash, key| hash[key] = {} }
|
|
11
|
+
|
|
12
|
+
self.class.mcp_param_mappings.each do |property_name, mapping|
|
|
13
|
+
next unless @provided_mcp_arguments.include?(property_name.to_s)
|
|
14
|
+
|
|
15
|
+
add_mapped_filter_value(filter_values, ranges, mapping, public_send(property_name))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
add_range_filter_values(filter_values, ranges)
|
|
19
|
+
|
|
20
|
+
filter_values.merge(format: 'json')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def add_mapped_filter_value(filter_values, ranges, mapping, value)
|
|
24
|
+
target = mapping.fetch(:target).to_s
|
|
25
|
+
operator = mapping.fetch(:operator)
|
|
26
|
+
case operator
|
|
27
|
+
when :equal, :in then filter_values[target] = value
|
|
28
|
+
when :not_equal, :not_in then filter_values["#{target}!"] = value
|
|
29
|
+
when :between_from, :between_to, :not_between_from, :not_between_to
|
|
30
|
+
operator_name = operator.to_s
|
|
31
|
+
range_key = [target, operator_name.start_with?('not_')]
|
|
32
|
+
ranges[range_key][operator_name.end_with?('from') ? :from : :to] = value
|
|
33
|
+
when :null then filter_values[target] = '' if value
|
|
34
|
+
when :not_null then filter_values["#{target}!"] = '' if value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def add_range_filter_values(filter_values, ranges)
|
|
39
|
+
ranges.each do |(target, negated), endpoints|
|
|
40
|
+
filter_values["#{target}#{'!' if negated}"] = "#{endpoints[:from]}..#{endpoints[:to]}"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def mcp_argument_provided?(name)
|
|
45
|
+
@provided_mcp_arguments.include?(name.to_s)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/active_scaffold/mcp.rb
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require "set"
|
|
3
|
+
require 'active_scaffold/mcp/filtered_tools'
|
|
5
4
|
|
|
6
5
|
module ActiveScaffold
|
|
7
6
|
module Actions
|
|
8
|
-
autoload :MCP,
|
|
7
|
+
autoload :MCP, 'active_scaffold/actions/mcp'
|
|
9
8
|
end
|
|
10
9
|
|
|
11
10
|
module Config
|
|
12
|
-
autoload :MCP,
|
|
11
|
+
autoload :MCP, 'active_scaffold/config/mcp'
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
module MCP
|
|
16
|
-
autoload :ActionConfig,
|
|
17
|
-
autoload :Flash,
|
|
18
|
-
autoload :GeneratorFactory,
|
|
19
|
-
autoload :ListToolGenerator,
|
|
20
|
-
autoload :Presenter,
|
|
21
|
-
autoload :ProfileUser,
|
|
22
|
-
autoload :SettingsValidator,
|
|
23
|
-
autoload :Tool,
|
|
24
|
-
autoload :ToolGenerator,
|
|
25
|
-
autoload :ListTool,
|
|
26
|
-
autoload :ShowTool,
|
|
27
|
-
autoload :ListViewsTool,
|
|
28
|
-
autoload :ShowToolGenerator,
|
|
29
|
-
autoload :VERSION,
|
|
15
|
+
autoload :ActionConfig, 'active_scaffold/mcp/action_config'
|
|
16
|
+
autoload :Flash, 'active_scaffold/mcp/flash'
|
|
17
|
+
autoload :GeneratorFactory, 'active_scaffold/mcp/generator_factory'
|
|
18
|
+
autoload :ListToolGenerator, 'active_scaffold/mcp/list_tool_generator'
|
|
19
|
+
autoload :Presenter, 'active_scaffold/mcp/presenter'
|
|
20
|
+
autoload :ProfileUser, 'active_scaffold/mcp/profile_user'
|
|
21
|
+
autoload :SettingsValidator, 'active_scaffold/mcp/settings_validator'
|
|
22
|
+
autoload :Tool, 'active_scaffold/mcp/tool'
|
|
23
|
+
autoload :ToolGenerator, 'active_scaffold/mcp/tool_generator'
|
|
24
|
+
autoload :ListTool, 'active_scaffold/mcp/list_tool'
|
|
25
|
+
autoload :ShowTool, 'active_scaffold/mcp/show_tool'
|
|
26
|
+
autoload :ListViewsTool, 'active_scaffold/mcp/list_views_tool'
|
|
27
|
+
autoload :ShowToolGenerator, 'active_scaffold/mcp/show_tool_generator'
|
|
28
|
+
autoload :VERSION, 'active_scaffold/mcp/version'
|
|
30
29
|
|
|
31
30
|
class << self
|
|
32
31
|
def generated_controller_names
|
data/lib/active_scaffold_mcp.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
3
|
+
require 'action_mcp'
|
|
4
|
+
require 'active_scaffold'
|
|
5
|
+
require 'active_scaffold/extensions/localize'
|
|
6
6
|
|
|
7
|
-
require
|
|
8
|
-
require
|
|
7
|
+
require 'active_scaffold/mcp'
|
|
8
|
+
require 'active_scaffold/mcp/engine'
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
namespace :action_mcp do
|
|
4
4
|
task load_profile_user: :environment do
|
|
5
|
-
next if ENV[
|
|
5
|
+
next if ENV['MCP_USER'].blank?
|
|
6
6
|
|
|
7
|
-
ActionMCP::Current.user = ActiveScaffold::MCP::ProfileUser.resolve(ENV.fetch(
|
|
7
|
+
ActionMCP::Current.user = ActiveScaffold::MCP::ProfileUser.resolve(ENV.fetch('MCP_USER'))
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
task show_profile: :load_profile_user
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_scaffold_mcp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sergio Cambra
|
|
@@ -57,6 +57,7 @@ executables: []
|
|
|
57
57
|
extensions: []
|
|
58
58
|
extra_rdoc_files: []
|
|
59
59
|
files:
|
|
60
|
+
- CHANGELOG.md
|
|
60
61
|
- LICENSE.txt
|
|
61
62
|
- README.md
|
|
62
63
|
- lib/active_scaffold/actions/mcp.rb
|
|
@@ -68,6 +69,7 @@ files:
|
|
|
68
69
|
- lib/active_scaffold/mcp/flash.rb
|
|
69
70
|
- lib/active_scaffold/mcp/generator_factory.rb
|
|
70
71
|
- lib/active_scaffold/mcp/list_tool.rb
|
|
72
|
+
- lib/active_scaffold/mcp/list_tool_filters.rb
|
|
71
73
|
- lib/active_scaffold/mcp/list_tool_generator.rb
|
|
72
74
|
- lib/active_scaffold/mcp/list_views_tool.rb
|
|
73
75
|
- lib/active_scaffold/mcp/presenter.rb
|
|
@@ -77,13 +79,15 @@ files:
|
|
|
77
79
|
- lib/active_scaffold/mcp/show_tool_generator.rb
|
|
78
80
|
- lib/active_scaffold/mcp/tool.rb
|
|
79
81
|
- lib/active_scaffold/mcp/tool_generator.rb
|
|
82
|
+
- lib/active_scaffold/mcp/tool_params.rb
|
|
80
83
|
- lib/active_scaffold/mcp/version.rb
|
|
81
84
|
- lib/active_scaffold_mcp.rb
|
|
82
85
|
- lib/tasks/active_scaffold_mcp_tasks.rake
|
|
83
86
|
homepage: https://github.com/activescaffold/active_scaffold_mcp
|
|
84
87
|
licenses:
|
|
85
88
|
- MIT
|
|
86
|
-
metadata:
|
|
89
|
+
metadata:
|
|
90
|
+
rubygems_mfa_required: 'true'
|
|
87
91
|
rdoc_options: []
|
|
88
92
|
require_paths:
|
|
89
93
|
- lib
|