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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ef9f71eb1c0487c81db8eafdeb2fc1d8905812901545ab211eab421b36a26bf6
|
|
4
|
+
data.tar.gz: 7da538ce9d281e1d4c348b34453442f24baeecbee636907ecbde27148e1f9777
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6117e68052e51da8cad148e29e5fd47dc91041fd8d4830fdb6c58c057b08ee847a787d6189f484159553c120efaccd6e9b4739295d2aa2d67d1845a4e91e2763
|
|
7
|
+
data.tar.gz: 6a6eb5f970a34bc2b2f1077de368fbb9e682e324e735d2553836979d42689200932ebae5d6c88e8d4e5e354799d1950f70e06e40a4bbeb78c6c1cd1addfb0c2b
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 0.1.3 - 2026-11-17
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Allow presenter modules to handle ActiveScaffold UIs with
|
|
10
|
+
`mcp_list_<list_ui>` and `mcp_show_<show_ui>` methods.
|
|
11
|
+
- Pass the UI-specific options to MCP presenter methods.
|
|
12
|
+
- Present Active Storage, Paperclip, CarrierWave, Dragonfly, and file-column
|
|
13
|
+
attachments as structured metadata.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- Let show presenters reuse the corresponding list UI method when there is no
|
|
18
|
+
show-specific method, matching ActiveScaffold's UI inheritance.
|
|
19
|
+
|
|
20
|
+
## 0.1.2 - 2026-10-30
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- Add a protected `render_result` hook to list and show tools so configured
|
|
25
|
+
modules can customize MCP output without replacing tool logic.
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- Update the documented ActionMCP inspection commands.
|
|
30
|
+
|
|
31
|
+
## 0.1.1 - 2026-10-21
|
|
32
|
+
|
|
33
|
+
### Changed
|
|
34
|
+
|
|
35
|
+
- Require ActiveScaffold 4.4 or newer.
|
|
36
|
+
- Document the plugin's runtime requirements and installation.
|
|
37
|
+
|
|
38
|
+
## 0.1.0 - 2026-10-21
|
|
39
|
+
|
|
40
|
+
- Initial release with generated read-only list and show tools.
|
data/README.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# ActiveScaffold MCP
|
|
2
2
|
|
|
3
|
+
[](https://github.com/activescaffold/active_scaffold_mcp/actions/workflows/ci.yml)
|
|
4
|
+
[](https://qlty.sh/gh/activescaffold/projects/active_scaffold_mcp)
|
|
5
|
+
[](https://qlty.sh/gh/activescaffold/projects/active_scaffold_mcp)
|
|
6
|
+
[](https://badge.fury.io/rb/active_scaffold_mcp)
|
|
7
|
+
[](https://inch-ci.org/github/activescaffold/active_scaffold_mcp)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
3
10
|
`active_scaffold_mcp` generates ActionMCP tools from ActiveScaffold controller configuration.
|
|
4
11
|
|
|
5
12
|
The gem is under initial development. The first implementation will provide a configuration DSL, a generated-tool registry, and read-only list and show tools.
|
|
@@ -14,7 +21,7 @@ The gem is under initial development. The first implementation will provide a co
|
|
|
14
21
|
Add the gem to your application:
|
|
15
22
|
|
|
16
23
|
```ruby
|
|
17
|
-
gem "active_scaffold_mcp", "~> 0.1.
|
|
24
|
+
gem "active_scaffold_mcp", "~> 0.1.3"
|
|
18
25
|
```
|
|
19
26
|
|
|
20
27
|
Association columns use ActiveScaffold's list settings in MCP results. An
|
|
@@ -217,6 +224,44 @@ config.mcp.expose :list, presenter_modules: TicketMCPPresentation
|
|
|
217
224
|
config.mcp.expose :show, presenter_modules: TicketMCPPresentation
|
|
218
225
|
```
|
|
219
226
|
|
|
227
|
+
A presenter module may also handle every column configured with a particular
|
|
228
|
+
ActiveScaffold UI. List tools dispatch `mcp_list_#{list_ui}`. Show tools first
|
|
229
|
+
try `mcp_show_#{show_ui}`, then reuse `mcp_list_#{show_ui}` when there is no
|
|
230
|
+
show-specific method, matching ActiveScaffold's UI inheritance. The methods
|
|
231
|
+
receive the same record, column, and UI-options inputs used by the corresponding
|
|
232
|
+
ActiveScaffold helpers:
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
module AttachmentMCPPresentation
|
|
236
|
+
def mcp_list_paperclip(record, column, ui_options:)
|
|
237
|
+
attachment_metadata(record, column, ui_options)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def mcp_show_paperclip(record, column, ui_options:)
|
|
241
|
+
attachment_metadata(record, column, ui_options)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
private
|
|
245
|
+
|
|
246
|
+
def attachment_metadata(record, column, _ui_options)
|
|
247
|
+
attachment = record.public_send(column.name)
|
|
248
|
+
{filename: attachment.original_filename, content_type: attachment.content_type}
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The default presenter supports ActiveScaffold's Active Storage, Paperclip,
|
|
254
|
+
CarrierWave, Dragonfly, and file-column bridges. Attached files are represented
|
|
255
|
+
as JSON metadata with the fields available from each backend: `filename`,
|
|
256
|
+
`content_type`, `byte_size`, and `updated_at`. Missing fields are omitted,
|
|
257
|
+
missing attachments are `null`, and multiple Active Storage attachments are an
|
|
258
|
+
array of metadata objects. File contents and expiring download URLs are not
|
|
259
|
+
embedded in record JSON.
|
|
260
|
+
|
|
261
|
+
A `#{column.name}_column` method takes precedence over the `show_ui` handler.
|
|
262
|
+
If neither is defined, the presenter uses its standard association or model
|
|
263
|
+
attribute serialization.
|
|
264
|
+
|
|
220
265
|
Each generated action gets its own anonymous presenter subclass when presenter
|
|
221
266
|
modules are configured, so the modules do not modify the base presenter or
|
|
222
267
|
other MCP tools. `modules` extends the tool's query and authorization behavior;
|
|
@@ -14,11 +14,11 @@ module ActiveScaffold
|
|
|
14
14
|
cattr_accessor :actions, instance_accessor: false, default: {}.freeze
|
|
15
15
|
cattr_accessor :filter_column_sets, instance_accessor: false, default: {}.freeze
|
|
16
16
|
cattr_accessor :base_tool, instance_accessor: false,
|
|
17
|
-
|
|
17
|
+
default: -> { 'ApplicationMCPTool'.safe_constantize || ActionMCP::Tool }
|
|
18
18
|
cattr_accessor :settings_validator, instance_accessor: false,
|
|
19
|
-
|
|
19
|
+
default: ActiveScaffold::MCP::SettingsValidator
|
|
20
20
|
cattr_accessor :tool_generator, instance_accessor: false,
|
|
21
|
-
|
|
21
|
+
default: ActiveScaffold::MCP::GeneratorFactory.new
|
|
22
22
|
|
|
23
23
|
class << self
|
|
24
24
|
alias unvalidated_actions= actions=
|
|
@@ -38,9 +38,15 @@ module ActiveScaffold
|
|
|
38
38
|
|
|
39
39
|
def resolved_settings_validator
|
|
40
40
|
configured_validator = settings_validator
|
|
41
|
-
if !defined?(@resolved_settings_validator_source) ||
|
|
41
|
+
if !defined?(@resolved_settings_validator_source) ||
|
|
42
|
+
@resolved_settings_validator_source != configured_validator
|
|
42
43
|
@resolved_settings_validator_source = configured_validator
|
|
43
|
-
@resolved_settings_validator =
|
|
44
|
+
@resolved_settings_validator =
|
|
45
|
+
if configured_validator.is_a?(Class)
|
|
46
|
+
configured_validator.new
|
|
47
|
+
else
|
|
48
|
+
configured_validator
|
|
49
|
+
end
|
|
44
50
|
end
|
|
45
51
|
|
|
46
52
|
@resolved_settings_validator
|
|
@@ -91,8 +97,8 @@ module ActiveScaffold
|
|
|
91
97
|
result[action_name.to_sym] = normalize_action_settings(action_name, settings)
|
|
92
98
|
end
|
|
93
99
|
else
|
|
94
|
-
Array(value).
|
|
95
|
-
|
|
100
|
+
Array(value).to_h do |action_name|
|
|
101
|
+
[action_name.to_sym, {}]
|
|
96
102
|
end
|
|
97
103
|
end
|
|
98
104
|
end
|
|
@@ -115,24 +121,20 @@ module ActiveScaffold
|
|
|
115
121
|
|
|
116
122
|
def normalize_settings(settings)
|
|
117
123
|
settings.each_with_object({}) do |(name, value), result|
|
|
118
|
-
result[name] =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
value
|
|
133
|
-
else
|
|
134
|
-
duplicate_setting(value)
|
|
135
|
-
end
|
|
124
|
+
result[name] = normalize_setting(name, value)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def normalize_setting(name, value)
|
|
129
|
+
case name
|
|
130
|
+
when :description, :label, :name then value.to_s
|
|
131
|
+
when :columns, :lookup_columns then normalize_names(value)
|
|
132
|
+
when :filter_columns then value.is_a?(Hash) ? normalize_operator_map(value) : normalize_names(value)
|
|
133
|
+
when :filter_column_sets then normalize_filter_column_sets(value)
|
|
134
|
+
when :filters then all?(value) ? :all : normalize_names(value)
|
|
135
|
+
when :modules, :presenter_modules then Array(value).compact
|
|
136
|
+
when :presenter then value
|
|
137
|
+
else duplicate_setting(value)
|
|
136
138
|
end
|
|
137
139
|
end
|
|
138
140
|
|
|
@@ -168,8 +170,8 @@ module ActiveScaffold
|
|
|
168
170
|
|
|
169
171
|
unless value.is_a?(Hash)
|
|
170
172
|
raise ArgumentError,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
'filter_column_sets must be a hash mapping set names to operators, ' \
|
|
174
|
+
'for example: {foreign_keys: :in, timestamps: :range}'
|
|
173
175
|
end
|
|
174
176
|
|
|
175
177
|
normalize_operator_map(value)
|
|
@@ -192,7 +194,7 @@ module ActiveScaffold
|
|
|
192
194
|
end
|
|
193
195
|
|
|
194
196
|
def all?(value)
|
|
195
|
-
|
|
197
|
+
[:all, 'all'].include?(value)
|
|
196
198
|
end
|
|
197
199
|
|
|
198
200
|
def normalize_names(value)
|
|
@@ -271,9 +273,7 @@ module ActiveScaffold
|
|
|
271
273
|
value
|
|
272
274
|
end
|
|
273
275
|
|
|
274
|
-
|
|
275
|
-
self.class.validate_mcp_setting!(action_name, setting, value)
|
|
276
|
-
end
|
|
276
|
+
delegate :validate_mcp_setting!, to: :class
|
|
277
277
|
|
|
278
278
|
def method_missing(name, *args)
|
|
279
279
|
if args.empty? && mcp_action?(name)
|
|
@@ -21,8 +21,8 @@ module ActiveScaffold
|
|
|
21
21
|
|
|
22
22
|
def method_missing(name, *args)
|
|
23
23
|
method_name = name.to_s
|
|
24
|
-
if method_name.end_with?(
|
|
25
|
-
self[method_name.delete_suffix(
|
|
24
|
+
if method_name.end_with?('=') && args.length == 1
|
|
25
|
+
self[method_name.delete_suffix('=')] = args.first
|
|
26
26
|
elsif args.empty?
|
|
27
27
|
self[name]
|
|
28
28
|
else
|
|
@@ -4,10 +4,10 @@ module ActiveScaffold
|
|
|
4
4
|
module MCP
|
|
5
5
|
class Engine < Rails::Engine
|
|
6
6
|
rake_tasks do
|
|
7
|
-
load File.expand_path(
|
|
7
|
+
load File.expand_path('../../tasks/active_scaffold_mcp_tasks.rake', __dir__)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
initializer
|
|
10
|
+
initializer 'active_scaffold_mcp.initialize', after: 'initialize_active_scaffold' do
|
|
11
11
|
ActiveScaffold.defaults do |config|
|
|
12
12
|
config.actions << :mcp unless config.actions.include?(:mcp)
|
|
13
13
|
end
|
|
@@ -13,6 +13,6 @@ module ActiveScaffold
|
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
unless ActionMCP::Configuration
|
|
16
|
+
unless ActionMCP::Configuration <= ActiveScaffold::MCP::FilteredTools
|
|
17
17
|
ActionMCP::Configuration.prepend(ActiveScaffold::MCP::FilteredTools)
|
|
18
18
|
end
|
|
@@ -114,8 +114,8 @@ module ActiveScaffold
|
|
|
114
114
|
|
|
115
115
|
def matching_rule(rules, **values)
|
|
116
116
|
rules.select { |rule| rule.matches?(values) }
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
.max_by { |rule| [rule.specificity, rule.position] }
|
|
118
|
+
&.generator
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
def columns?(action_config)
|
|
@@ -144,7 +144,7 @@ module ActiveScaffold
|
|
|
144
144
|
|
|
145
145
|
def custom_action_config_for(core_config, action_name)
|
|
146
146
|
custom_actions = value_for(core_config, :custom_actions)
|
|
147
|
-
return unless custom_actions
|
|
147
|
+
return unless custom_actions.respond_to?(:[])
|
|
148
148
|
|
|
149
149
|
custom_actions[action_name.to_sym] || custom_actions[action_name.to_s]
|
|
150
150
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
module ActiveScaffold
|
|
6
6
|
module MCP
|
|
@@ -54,11 +54,11 @@ module ActiveScaffold
|
|
|
54
54
|
next unless mcp_argument_provided?(:sort)
|
|
55
55
|
|
|
56
56
|
active_scaffold_params[:sort] = sort
|
|
57
|
-
active_scaffold_params[:sort_direction] = sort_direction.presence ||
|
|
57
|
+
active_scaffold_params[:sort_direction] = sort_direction.presence || 'asc'
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
def report_disallowed_filters
|
|
61
|
+
def report_disallowed_filters # rubocop:disable Naming/PredicateMethod
|
|
62
62
|
disallowed_filters = Array(@filter_states).filter_map do |name, state|
|
|
63
63
|
next unless state[:requested] && state[:status] == :disallowed
|
|
64
64
|
|
|
@@ -71,17 +71,17 @@ module ActiveScaffold
|
|
|
71
71
|
filter_selections = disallowed_filters.map(&:last)
|
|
72
72
|
|
|
73
73
|
report_error(
|
|
74
|
-
|
|
74
|
+
'The following requested filter selections could not be applied in this call because they were not allowed ' \
|
|
75
75
|
"for its current arguments and context: #{filter_selections.to_sentence}. No records were returned. " \
|
|
76
76
|
"Retry the same call with only #{filter_names.to_sentence} omitted and keep all other arguments unchanged. " \
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
'Filter availability is evaluated for each call, so the selection may be allowed with different arguments ' \
|
|
78
|
+
'or context.'
|
|
79
79
|
)
|
|
80
80
|
true
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def action_not_allowed_message
|
|
84
|
-
return super
|
|
84
|
+
return super if params[:config_list_view].blank?
|
|
85
85
|
|
|
86
86
|
"List view #{params[:config_list_view].inspect} is not allowed for the current user."
|
|
87
87
|
end
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveScaffold
|
|
4
|
+
module MCP
|
|
5
|
+
module ListToolFilters
|
|
6
|
+
OPERATOR_ALIASES = {
|
|
7
|
+
eq: :equal,
|
|
8
|
+
equality: :equal,
|
|
9
|
+
range: :between,
|
|
10
|
+
not_eq: :not_equal,
|
|
11
|
+
neq: :not_equal,
|
|
12
|
+
not_range: :not_between
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def add_column_filter_properties(tool_class, core_config, settings, mappings)
|
|
18
|
+
configured_filter_columns(core_config, settings).each do |column_name, operators|
|
|
19
|
+
column = core_config._columns_hash[column_name.to_s]
|
|
20
|
+
next unless column
|
|
21
|
+
|
|
22
|
+
Array(operators).each do |operator|
|
|
23
|
+
add_operator_properties(tool_class, column_name, column, normalize_operator(operator), mappings)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def configured_filter_columns(core_config, settings)
|
|
29
|
+
result =
|
|
30
|
+
case settings[:filter_columns]
|
|
31
|
+
when Hash
|
|
32
|
+
settings[:filter_columns].transform_keys(&:to_sym)
|
|
33
|
+
when nil
|
|
34
|
+
{}
|
|
35
|
+
else
|
|
36
|
+
Array(settings[:filter_columns]).to_h { |name| [name.to_sym, :equal] }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
settings.fetch(:filter_column_sets, {}).each do |set_name, operators|
|
|
40
|
+
columns_for_set(core_config, set_name).each { |name| result[name] ||= operators }
|
|
41
|
+
end
|
|
42
|
+
add_polymorphic_type_columns(core_config, result)
|
|
43
|
+
result
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def add_polymorphic_type_columns(core_config, filter_columns)
|
|
47
|
+
model = core_config.model
|
|
48
|
+
return unless model.respond_to?(:reflect_on_all_associations)
|
|
49
|
+
|
|
50
|
+
model.reflect_on_all_associations(:belongs_to).select(&:polymorphic?).each do |association|
|
|
51
|
+
foreign_key = association.foreign_key.to_sym
|
|
52
|
+
foreign_type = association.foreign_type.to_sym
|
|
53
|
+
next unless filter_columns.key?(foreign_key)
|
|
54
|
+
next unless core_config._columns_hash.key?(foreign_type.to_s)
|
|
55
|
+
|
|
56
|
+
filter_columns[foreign_type] ||= filter_columns[foreign_key]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def columns_for_set(core_config, set_name)
|
|
61
|
+
columns = core_config._columns_hash.values
|
|
62
|
+
selected_columns =
|
|
63
|
+
case set_name.to_sym
|
|
64
|
+
when :foreign_keys
|
|
65
|
+
columns.select { |column| column.name.end_with?('_id') }
|
|
66
|
+
when :dates
|
|
67
|
+
columns.select { |column| column.type.to_sym == :date }
|
|
68
|
+
when :timestamps
|
|
69
|
+
columns.select do |column|
|
|
70
|
+
%i[datetime timestamp timestamptz].include?(column.type.to_sym)
|
|
71
|
+
end
|
|
72
|
+
else
|
|
73
|
+
raise ArgumentError, "unknown MCP filter column set: #{set_name}"
|
|
74
|
+
end
|
|
75
|
+
selected_columns.map { |column| column.name.to_sym }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def normalize_operator(operator)
|
|
79
|
+
operator = operator.to_sym
|
|
80
|
+
OPERATOR_ALIASES.fetch(operator, operator)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def add_operator_properties(tool_class, column_name, column, operator, mappings)
|
|
84
|
+
case operator
|
|
85
|
+
when :equal, :not_equal
|
|
86
|
+
add_equality_property(tool_class, column_name, column, operator, mappings)
|
|
87
|
+
when :in, :not_in
|
|
88
|
+
add_collection_property(tool_class, column_name, column, operator, mappings)
|
|
89
|
+
when :between, :not_between
|
|
90
|
+
add_range_properties(tool_class, column_name, column, operator, mappings)
|
|
91
|
+
when :null, :not_null
|
|
92
|
+
add_null_property(tool_class, column_name, operator, mappings)
|
|
93
|
+
else
|
|
94
|
+
raise ArgumentError, "unsupported MCP filter operator for #{column_name}: #{operator}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def add_equality_property(tool_class, column_name, column, operator, mappings)
|
|
99
|
+
property_name = operator == :equal ? column_name : :"#{column_name}_not"
|
|
100
|
+
add_scalar_property(tool_class, property_name, column, "Filter by #{column_name}")
|
|
101
|
+
mappings[property_name.to_sym] = {target: column_name.to_sym, operator: operator}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def add_collection_property(tool_class, column_name, column, operator, mappings)
|
|
105
|
+
property_name = operator == :in ? :"#{column_name}_in" : :"#{column_name}_not_in"
|
|
106
|
+
tool_class.collection property_name,
|
|
107
|
+
type: json_type(column),
|
|
108
|
+
description: "Filter #{column_name} by a list of values"
|
|
109
|
+
mappings[property_name] = {target: column_name.to_sym, operator: operator}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def add_range_properties(tool_class, column_name, column, operator, mappings)
|
|
113
|
+
prefix = operator == :between ? column_name : :"#{column_name}_not"
|
|
114
|
+
%i[from to].each do |endpoint|
|
|
115
|
+
property_name = :"#{prefix}_#{endpoint}"
|
|
116
|
+
add_scalar_property(tool_class, property_name, column,
|
|
117
|
+
"#{endpoint.to_s.humanize} value for #{column_name}")
|
|
118
|
+
mappings[property_name] = {
|
|
119
|
+
target: column_name.to_sym,
|
|
120
|
+
operator: :"#{'not_' if operator == :not_between}between_#{endpoint}"
|
|
121
|
+
}
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def add_null_property(tool_class, column_name, operator, mappings)
|
|
126
|
+
property_name = operator == :null ? :"#{column_name}_null" : :"#{column_name}_not_null"
|
|
127
|
+
description = "Match records where #{column_name} is #{'not ' if operator == :not_null}null"
|
|
128
|
+
tool_class.property property_name,
|
|
129
|
+
type: 'boolean',
|
|
130
|
+
description: description
|
|
131
|
+
mappings[property_name] = {target: column_name.to_sym, operator: operator}
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def add_scalar_property(tool_class, property_name, column, description)
|
|
135
|
+
tool_class.property(property_name, **schema_options(column), description: description)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def add_named_filter_properties(tool_class, action_config, settings, mappings)
|
|
139
|
+
selected_filters(action_config, settings[:filters]).each do |filter|
|
|
140
|
+
property_name = filter.name.to_sym
|
|
141
|
+
if mappings.key?(property_name)
|
|
142
|
+
raise ArgumentError, "named list filter #{filter.name} conflicts with an MCP column filter"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
option_names = filter.map { |option| option.name.to_s }
|
|
146
|
+
tool_class.property property_name,
|
|
147
|
+
type: 'string',
|
|
148
|
+
description: named_filter_description(filter),
|
|
149
|
+
default: filter.default_option&.to_s,
|
|
150
|
+
enum: option_names
|
|
151
|
+
mappings[property_name] = {target: filter.name, operator: :equal, source: :named_filter}
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def named_filter_description(filter)
|
|
156
|
+
sentences = [filter.description || "Apply the #{filter.name} list filter"]
|
|
157
|
+
sentences << "When omitted, the server uses the default option '#{filter.default_option}'"
|
|
158
|
+
if filter.security_method_set?
|
|
159
|
+
sentences << 'This filter may be disallowed in the current context'
|
|
160
|
+
else
|
|
161
|
+
secured_options = filter.select(&:security_method_set?).map { |option| option.name.to_s }
|
|
162
|
+
if secured_options.any?
|
|
163
|
+
sentences << "These options may be disallowed in the current context: #{secured_options.to_sentence}"
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
sentences.join('. ')
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def selected_filters(action_config, configured_filters)
|
|
170
|
+
return [] if configured_filters.nil? || !action_config.respond_to?(:filters)
|
|
171
|
+
return action_config.filters.to_a if configured_filters == :all
|
|
172
|
+
|
|
173
|
+
Array(configured_filters).filter_map { |name| action_config.filters[name] }
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def add_search_property(tool_class, core_config, settings, mappings)
|
|
177
|
+
return unless search_enabled?(core_config, settings)
|
|
178
|
+
|
|
179
|
+
description = search_property_description(core_config)
|
|
180
|
+
tool_class.include ActiveScaffold::Actions::Search
|
|
181
|
+
tool_class.property :search,
|
|
182
|
+
type: 'string',
|
|
183
|
+
description: description
|
|
184
|
+
mappings[:search] = {target: :search, operator: :equal, source: :search, description: description}
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def search_property_description(core_config)
|
|
188
|
+
columns = core_config.search.columns.visible_columns(skip_authorization: true, flatten: true)
|
|
189
|
+
labels = columns.map { |column| column.label.presence || column.name.to_s.humanize }
|
|
190
|
+
return 'Search text across the configured ActiveScaffold search columns' if labels.empty?
|
|
191
|
+
|
|
192
|
+
displayed_labels = labels.first(12)
|
|
193
|
+
remaining_count = labels.length - displayed_labels.length
|
|
194
|
+
description = "Search text across #{displayed_labels.to_sentence}"
|
|
195
|
+
description += " and #{remaining_count} more" if remaining_count.positive?
|
|
196
|
+
description
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def search_enabled?(core_config, settings)
|
|
200
|
+
settings.fetch(:search, true) && core_config.actions.include?(:search)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|