solidus_mcp 0.2.0.rc1 → 0.2.0.rc3
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 +71 -0
- data/CONTRIBUTING.md +51 -4
- data/README.md +412 -3
- data/app/assets/stylesheets/solidus_mcp/admin.css +39 -0
- data/app/assets/stylesheets/solidus_mcp/consent.css +77 -0
- data/app/controllers/solidus_admin/mcp_activity_controller.rb +67 -0
- data/app/controllers/solidus_mcp/endpoint_controller.rb +36 -0
- data/app/models/solidus_mcp/activity_event.rb +37 -0
- data/app/models/solidus_mcp/audit_context.rb +7 -0
- data/app/models/solidus_mcp/stock_adjustment.rb +16 -0
- data/app/models/solidus_mcp/tool_request.rb +10 -0
- data/app/services/solidus_mcp/action_audit.rb +169 -0
- data/app/services/solidus_mcp/image_context.rb +44 -0
- data/app/services/solidus_mcp/order_workflow_authorization.rb +76 -0
- data/app/services/solidus_mcp/serializers.rb +28 -4
- data/app/services/solidus_mcp/tool_request_execution.rb +76 -0
- data/app/tools/solidus_mcp/tools/add_product_option_type.rb +36 -0
- data/app/tools/solidus_mcp/tools/add_product_to_taxon.rb +40 -0
- data/app/tools/solidus_mcp/tools/adjust_stock.rb +98 -0
- data/app/tools/solidus_mcp/tools/attach_product_image.rb +77 -0
- data/app/tools/solidus_mcp/tools/bulk_update_variant_prices.rb +1 -1
- data/app/tools/solidus_mcp/tools/cancel_order.rb +44 -0
- data/app/tools/solidus_mcp/tools/create_product.rb +1 -0
- data/app/tools/solidus_mcp/tools/create_variant_price.rb +59 -0
- data/app/tools/solidus_mcp/tools/list_option_types.rb +34 -0
- data/app/tools/solidus_mcp/tools/list_option_values.rb +27 -0
- data/app/tools/solidus_mcp/tools/list_product_images.rb +26 -0
- data/app/tools/solidus_mcp/tools/list_shipping_categories.rb +24 -0
- data/app/tools/solidus_mcp/tools/list_stock_locations.rb +27 -0
- data/app/tools/solidus_mcp/tools/list_taxons.rb +40 -0
- data/app/tools/solidus_mcp/tools/publish_product.rb +32 -0
- data/app/tools/solidus_mcp/tools/refund_payment.rb +63 -0
- data/app/tools/solidus_mcp/tools/remove_product_image.rb +33 -0
- data/app/tools/solidus_mcp/tools/reorder_product_images.rb +41 -0
- data/app/tools/solidus_mcp/tools/ship_shipment.rb +63 -0
- data/app/tools/solidus_mcp/tools/unpublish_product.rb +29 -0
- data/app/tools/solidus_mcp/tools/update_variant.rb +54 -0
- data/app/tools/solidus_mcp/tools/update_variant_price.rb +1 -1
- data/app/views/solidus_admin/mcp_activity/index.html.erb +53 -0
- data/app/views/solidus_admin/mcp_connections/index.html.erb +2 -0
- data/app/views/solidus_mcp/authorizations/new.html.erb +59 -41
- data/config/routes.rb +1 -0
- data/db/migrate/20261112000000_create_solidus_mcp_stock_adjustments.rb +19 -0
- data/db/migrate/20261112010000_create_solidus_mcp_tool_requests.rb +19 -0
- data/db/migrate/20261112020000_create_solidus_mcp_activity_events.rb +34 -0
- data/db/migrate/20261112020100_add_actor_role_to_mcp_activity_events.rb +7 -0
- data/lib/solidus_mcp/audited_server.rb +25 -0
- data/lib/solidus_mcp/configuration.rb +19 -1
- data/lib/solidus_mcp/engine.rb +9 -3
- data/lib/solidus_mcp/policy.rb +6 -2
- data/lib/solidus_mcp/registry.rb +5 -2
- data/lib/solidus_mcp/server.rb +1 -2
- data/lib/solidus_mcp/tool.rb +9 -7
- data/lib/solidus_mcp/version.rb +1 -1
- data/lib/solidus_mcp.rb +5 -1
- metadata +37 -2
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
# External effects cannot be undone by a database rollback. Commit the claim
|
|
5
|
+
# before entering the workflow and keep uncertain targets blocked for review.
|
|
6
|
+
class ToolRequestExecution
|
|
7
|
+
def initialize(credential, operation:, key:, payload:)
|
|
8
|
+
unless key.match?(/\A[A-Za-z0-9_.:-]{1,128}\z/)
|
|
9
|
+
raise ToolError, "Invalid idempotency key"
|
|
10
|
+
end
|
|
11
|
+
@connection = ConnectionPolicy.find_by!(application_id: credential.token.application_id, user_id: credential.user.id)
|
|
12
|
+
@operation, @key, @payload = operation, key, payload.deep_stringify_keys
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def database
|
|
16
|
+
ToolRequest.transaction do
|
|
17
|
+
request = find_request
|
|
18
|
+
request.with_lock do
|
|
19
|
+
check_payload!(request)
|
|
20
|
+
return request.result.symbolize_keys if request.status == "succeeded"
|
|
21
|
+
result = yield
|
|
22
|
+
request.update!(status: "succeeded", result: result)
|
|
23
|
+
result
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def external(guard_key:)
|
|
29
|
+
if ToolRequest.connection.transaction_open?
|
|
30
|
+
raise ToolError, "External workflow tools must run outside an enclosing database transaction"
|
|
31
|
+
end
|
|
32
|
+
request = find_request
|
|
33
|
+
replay = nil
|
|
34
|
+
request.with_lock do
|
|
35
|
+
check_payload!(request)
|
|
36
|
+
if request.status == "succeeded"
|
|
37
|
+
replay = request.result.symbolize_keys
|
|
38
|
+
elsif %w[processing uncertain].include?(request.status)
|
|
39
|
+
raise ToolError, "Request #{request.id} requires reconciliation before retry"
|
|
40
|
+
else
|
|
41
|
+
request.update!(status: "processing", guard_key: guard_key)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
return replay if replay
|
|
45
|
+
|
|
46
|
+
effects_started = false
|
|
47
|
+
begin
|
|
48
|
+
ToolRequest.transaction do
|
|
49
|
+
result = yield(-> { effects_started = true })
|
|
50
|
+
request.update!(status: "succeeded", result: result)
|
|
51
|
+
result
|
|
52
|
+
end
|
|
53
|
+
rescue
|
|
54
|
+
request.reload.update!(status: effects_started ? "uncertain" : "failed")
|
|
55
|
+
if effects_started
|
|
56
|
+
raise ToolError, "Request #{request.id} may have reached external systems; reconcile before retry. No automatic repeat is allowed."
|
|
57
|
+
end
|
|
58
|
+
raise
|
|
59
|
+
end
|
|
60
|
+
rescue ActiveRecord::RecordNotUnique
|
|
61
|
+
raise ToolError, "Another unresolved operation exists for this order; reconcile it before retry"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def find_request
|
|
67
|
+
ToolRequest.create_or_find_by!(connection_policy_id: @connection.id, operation: @operation, idempotency_key: @key) do |record|
|
|
68
|
+
record.payload = @payload
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def check_payload!(request)
|
|
73
|
+
raise ToolError, "Idempotency key was already used with different inputs" unless request.payload == @payload
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class AddProductOptionType < Base
|
|
6
|
+
tool_name :add_product_option_type
|
|
7
|
+
description "Attach an existing option type to a product, preserving existing options and variants. Repeated calls are safe."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "add_product_option_type"
|
|
10
|
+
read_only false
|
|
11
|
+
audit_action "create"
|
|
12
|
+
ability :update, "Spree::Product"
|
|
13
|
+
ability :read, "Spree::OptionType"
|
|
14
|
+
ability :create, "Spree::ProductOptionType"
|
|
15
|
+
input :product_id, type: "integer", minimum: 1, required: true
|
|
16
|
+
input :option_type_id, type: "integer", minimum: 1, required: true
|
|
17
|
+
|
|
18
|
+
protected
|
|
19
|
+
|
|
20
|
+
def call(product_id:, option_type_id:)
|
|
21
|
+
product = policy.scope(Spree::Product, :update).find(product_id)
|
|
22
|
+
product.with_lock do
|
|
23
|
+
authorize!
|
|
24
|
+
policy.authorize_record!(:update, product)
|
|
25
|
+
option_type = policy.authorize_record!(:read, policy.scope(Spree::OptionType).find(option_type_id))
|
|
26
|
+
unless product.product_option_types.exists?(option_type_id: option_type.id)
|
|
27
|
+
attachment = product.product_option_types.build(option_type: option_type)
|
|
28
|
+
policy.authorize_record!(:create, attachment)
|
|
29
|
+
attachment.save!
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
{product_id: product.id, option_type_id: option_type_id, attached: true}
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class AddProductToTaxon < Base
|
|
6
|
+
tool_name :add_product_to_taxon
|
|
7
|
+
description "Add a product to an existing taxon without replacing its other categories. New classifications append to the taxon's product ordering; repeated calls preserve existing positions."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "add_product_to_taxon"
|
|
10
|
+
read_only false
|
|
11
|
+
audit_action "create"
|
|
12
|
+
ability :update, "Spree::Product"
|
|
13
|
+
ability :read, "Spree::Taxon"
|
|
14
|
+
ability :create, "Spree::Classification"
|
|
15
|
+
input :product_id, type: "integer", minimum: 1, required: true
|
|
16
|
+
input :taxon_id, type: "integer", minimum: 1, required: true
|
|
17
|
+
|
|
18
|
+
protected
|
|
19
|
+
|
|
20
|
+
def call(product_id:, taxon_id:)
|
|
21
|
+
product = policy.scope(Spree::Product, :update).find(product_id)
|
|
22
|
+
product.with_lock do
|
|
23
|
+
taxon = policy.scope(Spree::Taxon).find(taxon_id)
|
|
24
|
+
taxon.with_lock do
|
|
25
|
+
authorize!
|
|
26
|
+
policy.authorize_record!(:update, product)
|
|
27
|
+
policy.authorize_record!(:update, product, attribute: :taxon_ids)
|
|
28
|
+
policy.authorize_record!(:read, taxon)
|
|
29
|
+
unless product.classifications.exists?(taxon_id: taxon.id)
|
|
30
|
+
classification = product.classifications.build(taxon: taxon)
|
|
31
|
+
policy.authorize_record!(:create, classification)
|
|
32
|
+
classification.save!
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
{product_id: product.id, taxon_id: taxon_id, attached: true}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class AdjustStock < Base
|
|
6
|
+
tool_name :adjust_stock
|
|
7
|
+
description "Apply a signed stock movement to an existing variant/location stock item. Requires a reason and a connection-scoped idempotency key; retry with identical inputs. Solidus validations and backorder processing apply."
|
|
8
|
+
category "Inventory"
|
|
9
|
+
permission "adjust_stock"
|
|
10
|
+
read_only false
|
|
11
|
+
ability :read, "Spree::Product"
|
|
12
|
+
ability :read, "Spree::Variant"
|
|
13
|
+
ability :read, "Spree::StockLocation"
|
|
14
|
+
ability :update, "Spree::StockItem"
|
|
15
|
+
ability :create, "Spree::StockMovement"
|
|
16
|
+
input :variant_id, type: "integer", minimum: 1, required: true
|
|
17
|
+
input :stock_location_id, type: "integer", minimum: 1, required: true
|
|
18
|
+
input :quantity, type: "integer", minimum: -2147483647, maximum: 2147483647,
|
|
19
|
+
not: {const: 0}, required: true
|
|
20
|
+
input :reason, type: "string", minLength: 1, maxLength: 1000, required: true
|
|
21
|
+
input :idempotency_key, type: "string", minLength: 1, maxLength: 128,
|
|
22
|
+
pattern: "^[A-Za-z0-9_.:-]+$", required: true
|
|
23
|
+
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
def call(variant_id:, stock_location_id:, quantity:, reason:, idempotency_key:)
|
|
27
|
+
raise ToolError, "Reason must not be blank" if reason.strip.empty?
|
|
28
|
+
unless idempotency_key.match?(/\A[A-Za-z0-9_.:-]+\z/)
|
|
29
|
+
raise ToolError, "Invalid idempotency key"
|
|
30
|
+
end
|
|
31
|
+
connection = ConnectionPolicy.find_by!(application_id: credential.token.application_id, user_id: credential.user.id)
|
|
32
|
+
request = {variant_id: variant_id, stock_location_id: stock_location_id, quantity: quantity, reason: reason}
|
|
33
|
+
StockAdjustment.transaction do
|
|
34
|
+
# The database unique index serializes first use of a key across
|
|
35
|
+
# processes. Failed movements roll back this row as well as the stock.
|
|
36
|
+
adjustment = StockAdjustment.create_or_find_by!(connection_policy_id: connection.id, idempotency_key: idempotency_key) do |record|
|
|
37
|
+
record.assign_attributes(request)
|
|
38
|
+
end
|
|
39
|
+
adjustment.with_lock do
|
|
40
|
+
authorize!
|
|
41
|
+
unless adjustment.attributes.symbolize_keys.slice(*request.keys) == request
|
|
42
|
+
raise ToolError, "Idempotency key was already used with different inputs"
|
|
43
|
+
end
|
|
44
|
+
item = policy.scope(Spree::StockItem, :update)
|
|
45
|
+
.where(variant_id: variants.where(id: variant_id).select(:id),
|
|
46
|
+
stock_location_id: policy.scope(Spree::StockLocation).where(id: stock_location_id).select(:id)).sole
|
|
47
|
+
item.with_lock do
|
|
48
|
+
unless item.variant_id == variant_id && item.stock_location_id == stock_location_id
|
|
49
|
+
raise ToolError, "Stock item context changed; retrieve inventory again"
|
|
50
|
+
end
|
|
51
|
+
if adjustment.result && adjustment.result["stock_item_id"] != item.id
|
|
52
|
+
raise ToolError, "Stock item was replaced after this request completed"
|
|
53
|
+
end
|
|
54
|
+
# Match Solidus's stock-item -> variant lock order. Keep inventory
|
|
55
|
+
# tracking stable while the movement callback runs.
|
|
56
|
+
item.variant.with_lock do
|
|
57
|
+
authorize!
|
|
58
|
+
authorize_stock!(item)
|
|
59
|
+
movement = Spree::StockMovement.new(stock_item: item, quantity: quantity, originator: adjustment)
|
|
60
|
+
policy.authorize_record!(:create, movement)
|
|
61
|
+
unless adjustment.result
|
|
62
|
+
unless item.should_track_inventory?
|
|
63
|
+
raise ToolError, "Inventory tracking must be enabled for this variant and store"
|
|
64
|
+
end
|
|
65
|
+
unless item.stock_location.active?
|
|
66
|
+
raise ToolError, "Stock location must be active"
|
|
67
|
+
end
|
|
68
|
+
before = item.count_on_hand
|
|
69
|
+
unless (before + quantity).between?(-2147483648, 2147483647)
|
|
70
|
+
raise ToolError, "Resulting stock count exceeds the supported integer range"
|
|
71
|
+
end
|
|
72
|
+
movement.save!
|
|
73
|
+
item.reload
|
|
74
|
+
authorize_stock!(item)
|
|
75
|
+
adjustment.update!(stock_movement: movement, result: {
|
|
76
|
+
stock_movement_id: movement.id, stock_item_id: item.id,
|
|
77
|
+
variant_id: variant_id, stock_location_id: stock_location_id,
|
|
78
|
+
quantity: quantity, count_on_hand_before: before,
|
|
79
|
+
count_on_hand_after: item.count_on_hand
|
|
80
|
+
})
|
|
81
|
+
end
|
|
82
|
+
adjustment.result.symbolize_keys
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def authorize_stock!(item)
|
|
90
|
+
policy.authorize_record!(:read, item.variant.product)
|
|
91
|
+
policy.authorize_record!(:read, item.variant)
|
|
92
|
+
policy.authorize_record!(:read, item.stock_location)
|
|
93
|
+
policy.authorize_record!(:update, item)
|
|
94
|
+
policy.authorize_record!(:update, item, attribute: :count_on_hand)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "digest"
|
|
5
|
+
require "stringio"
|
|
6
|
+
|
|
7
|
+
module SolidusMcp
|
|
8
|
+
module Tools
|
|
9
|
+
class AttachProductImage < Base
|
|
10
|
+
tool_name :attach_product_image
|
|
11
|
+
description "Upload a PNG, JPEG, GIF, or WebP image as base64 (at most 512 KiB decoded) to a product master or explicit variant. Requires an idempotency key. ActiveStorage only; no remote URLs."
|
|
12
|
+
category "Products"
|
|
13
|
+
permission "attach_product_image"
|
|
14
|
+
read_only false
|
|
15
|
+
audit_action "create"
|
|
16
|
+
ability :update, "Spree::Product"
|
|
17
|
+
ability :update, "Spree::Variant"
|
|
18
|
+
ability :create, "Spree::Image"
|
|
19
|
+
input :product_id, type: "integer", minimum: 1, required: true
|
|
20
|
+
input :variant_id, type: "integer", minimum: 1
|
|
21
|
+
input :filename, type: "string", minLength: 1, maxLength: 128, required: true
|
|
22
|
+
input :content_type, type: "string", enum: %w[image/png image/jpeg image/gif image/webp], required: true
|
|
23
|
+
input :data_base64, type: "string", minLength: 1, maxLength: 699052, required: true
|
|
24
|
+
input :alt, type: "string", maxLength: 1000
|
|
25
|
+
input :idempotency_key, type: "string", minLength: 1, maxLength: 128, required: true
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
|
|
29
|
+
def call(product_id:, filename:, content_type:, data_base64:, idempotency_key:, variant_id: nil, alt: "")
|
|
30
|
+
unless Spree::Image.reflect_on_association(:attachment_attachment)
|
|
31
|
+
raise ToolError, "This upload tool requires the standard ActiveStorage image adapter"
|
|
32
|
+
end
|
|
33
|
+
if filename.match?(/[\\\/\x00-\x1f]/) || filename.strip.empty?
|
|
34
|
+
raise ToolError, "Filename must be a plain file name"
|
|
35
|
+
end
|
|
36
|
+
begin
|
|
37
|
+
data = Base64.strict_decode64(data_base64)
|
|
38
|
+
rescue ArgumentError
|
|
39
|
+
raise ToolError, "Invalid base64 image"
|
|
40
|
+
end
|
|
41
|
+
unless data.bytesize.between?(1, 524288) && Marcel::MimeType.for(StringIO.new(data)) == content_type
|
|
42
|
+
raise ToolError, "Image content or size does not match the upload constraints"
|
|
43
|
+
end
|
|
44
|
+
context = ImageContext.new(policy, product_id: product_id, variant_id: variant_id, action: :update)
|
|
45
|
+
request = ToolRequestExecution.new(credential, operation: "attach_product_image", key: idempotency_key,
|
|
46
|
+
payload: {product_id: product_id, variant_id: context.variant.id, filename: filename,
|
|
47
|
+
content_type: content_type, digest: Digest::SHA256.hexdigest(data), alt: alt})
|
|
48
|
+
blob = nil
|
|
49
|
+
context.with_lock do
|
|
50
|
+
authorize!
|
|
51
|
+
image = Spree::Image.new(viewable: context.variant, alt: alt)
|
|
52
|
+
policy.authorize_record!(:create, image)
|
|
53
|
+
request.database do
|
|
54
|
+
io = StringIO.new(data)
|
|
55
|
+
blob = ActiveStorage::Blob.build_after_unfurling(io: io, filename: filename, content_type: content_type, identify: false)
|
|
56
|
+
blob.save!
|
|
57
|
+
blob.upload_without_unfurling(io)
|
|
58
|
+
image.attachment = blob
|
|
59
|
+
policy.authorize_record!(:create, image)
|
|
60
|
+
image.save!
|
|
61
|
+
context.metadata(image)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
rescue
|
|
65
|
+
begin
|
|
66
|
+
if blob && !ActiveStorage::Attachment.exists?(blob_id: blob.id)
|
|
67
|
+
blob.service.delete(blob.key)
|
|
68
|
+
blob.destroy! if blob.persisted?
|
|
69
|
+
end
|
|
70
|
+
rescue
|
|
71
|
+
Rails.logger.warn("Solidus MCP image upload cleanup failed; inspect unattached blobs")
|
|
72
|
+
end
|
|
73
|
+
raise
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -16,7 +16,7 @@ module SolidusMcp
|
|
|
16
16
|
required: %w[sku amount currency country_iso],
|
|
17
17
|
properties: {sku: {type: "string", minLength: 1, maxLength: 255},
|
|
18
18
|
amount: {type: "string"}, currency: {type: "string"},
|
|
19
|
-
country_iso: {type:
|
|
19
|
+
country_iso: {anyOf: [{type: "string"}, {type: "null"}]}}}
|
|
20
20
|
|
|
21
21
|
protected
|
|
22
22
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class CancelOrder < Base
|
|
6
|
+
tool_name :cancel_order
|
|
7
|
+
description "Cancel a completed, cancellable order through Solidus, including shipment restocking and payment cancellation. Requires a reason and idempotency key; uncertain external outcomes require reconciliation."
|
|
8
|
+
category "Orders"
|
|
9
|
+
permission "cancel_order"
|
|
10
|
+
read_only false
|
|
11
|
+
ability :cancel, "Spree::Order"
|
|
12
|
+
input :order_id, type: "integer", minimum: 1, required: true
|
|
13
|
+
input :reason, type: "string", minLength: 1, maxLength: 1000, required: true
|
|
14
|
+
input :idempotency_key, type: "string", minLength: 1, maxLength: 128, required: true
|
|
15
|
+
|
|
16
|
+
protected
|
|
17
|
+
|
|
18
|
+
def call(order_id:, reason:, idempotency_key:)
|
|
19
|
+
raise ToolError, "Cancellation reason must not be blank" if reason.strip.empty?
|
|
20
|
+
order = policy.scope(Spree::Order, :cancel).find(order_id)
|
|
21
|
+
authorization = OrderWorkflowAuthorization.new(policy)
|
|
22
|
+
authorization.cancellation(order)
|
|
23
|
+
request = ToolRequestExecution.new(credential, operation: "cancel_order", key: idempotency_key,
|
|
24
|
+
payload: {order_id: order_id, reason: reason})
|
|
25
|
+
request.external(guard_key: "order:#{order.id}") do |effects|
|
|
26
|
+
order.with_lock do
|
|
27
|
+
authorize!
|
|
28
|
+
order.shipments.reload.each(&:lock!)
|
|
29
|
+
order.payments.reload.each(&:lock!)
|
|
30
|
+
authorization.cancellation(order)
|
|
31
|
+
unless order.canceled?
|
|
32
|
+
unless order.can_cancel? && order.shipments.all? { |shipment| %w[pending ready].include?(shipment.state) }
|
|
33
|
+
raise ToolError, "Order must be complete and every shipment must be pending or ready"
|
|
34
|
+
end
|
|
35
|
+
effects.call
|
|
36
|
+
order.canceled_by(credential.user)
|
|
37
|
+
end
|
|
38
|
+
{order_id: order.id, state: order.state, canceled: true}
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -8,6 +8,7 @@ module SolidusMcp
|
|
|
8
8
|
category "Products"
|
|
9
9
|
permission "create_product"
|
|
10
10
|
read_only false
|
|
11
|
+
audit_action "create"
|
|
11
12
|
ability :create, "Spree::Product"
|
|
12
13
|
input :name, type: "string", minLength: 1, maxLength: 255, required: true
|
|
13
14
|
input :price, type: "number", minimum: 0, required: true
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class CreateVariantPrice < Base
|
|
6
|
+
tool_name :create_variant_price
|
|
7
|
+
description "Create a variant price in an explicit currency/country context. Identical retries return the existing row; conflicting or ambiguous contexts fail. Standard Solidus pricing options only."
|
|
8
|
+
category "Pricing"
|
|
9
|
+
permission "create_variant_price"
|
|
10
|
+
read_only false
|
|
11
|
+
audit_action "create"
|
|
12
|
+
ability :update, "Spree::Product"
|
|
13
|
+
ability :update, "Spree::Variant"
|
|
14
|
+
ability :create, "Spree::Price"
|
|
15
|
+
input :variant_id, type: "integer", minimum: 1, required: true
|
|
16
|
+
input :currency, type: "string", pattern: "^[A-Z]{3}$", required: true
|
|
17
|
+
input :country_iso, anyOf: [{type: "string", pattern: "^[A-Z]{2}$"}, {type: "null"}], required: true
|
|
18
|
+
input :amount, type: "string", required: true
|
|
19
|
+
|
|
20
|
+
protected
|
|
21
|
+
|
|
22
|
+
def call(variant_id:, currency:, country_iso:, amount:)
|
|
23
|
+
unless amount.match?(/\A(?:0|[1-9][0-9]{0,7})(?:\.[0-9]{1,2})?\z/)
|
|
24
|
+
raise ToolError, "Amount must be a nonnegative decimal string with at most two decimal places"
|
|
25
|
+
end
|
|
26
|
+
valid_context = currency.match?(/\A[A-Z]{3}\z/) && (country_iso.nil? || country_iso.match?(/\A[A-Z]{2}\z/))
|
|
27
|
+
unless valid_context
|
|
28
|
+
raise ToolError, "Invalid currency or country context"
|
|
29
|
+
end
|
|
30
|
+
unless Spree::Config.pricing_options_class == Spree::Variant::PricingOptions
|
|
31
|
+
raise ToolError, "Custom pricing options require a store-specific pricing tool"
|
|
32
|
+
end
|
|
33
|
+
variant = policy.scope(Spree::Variant, :update)
|
|
34
|
+
.where(product_id: policy.scope(Spree::Product, :update).select(:id)).find(variant_id)
|
|
35
|
+
variant.with_lock do
|
|
36
|
+
authorize!
|
|
37
|
+
policy.authorize_record!(:update, variant.product)
|
|
38
|
+
policy.authorize_record!(:update, variant)
|
|
39
|
+
price = Spree::Price.new(variant: variant, currency: currency, country_iso: country_iso, amount: BigDecimal(amount))
|
|
40
|
+
policy.authorize_record!(:create, price)
|
|
41
|
+
# Inspect every active row, including rows hidden by price read scope:
|
|
42
|
+
# never create a second context because an existing row is unreadable.
|
|
43
|
+
existing = Spree::Price.where(variant_id: variant.id, currency: currency, country_iso: country_iso).limit(2).to_a
|
|
44
|
+
if existing.any?
|
|
45
|
+
unless existing.one? && existing.first.amount == price.amount
|
|
46
|
+
raise ToolError, "Price context already exists or is ambiguous; use an update tool"
|
|
47
|
+
end
|
|
48
|
+
policy.authorize_record!(:read, existing.first)
|
|
49
|
+
price = existing.first
|
|
50
|
+
else
|
|
51
|
+
price.save!
|
|
52
|
+
end
|
|
53
|
+
{price_id: price.id, variant_id: variant.id, currency: price.currency,
|
|
54
|
+
country_iso: price.country_iso, amount: price.amount.to_s("F")}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListOptionTypes < Base
|
|
6
|
+
tool_name :list_option_types
|
|
7
|
+
description "Discover option type IDs, names, and presentations, optionally filtered by name or product."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "list_option_types"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::OptionType"
|
|
12
|
+
input :query, type: "string", minLength: 1, maxLength: 100
|
|
13
|
+
input :product_id, type: "integer", minimum: 1
|
|
14
|
+
pagination
|
|
15
|
+
|
|
16
|
+
protected
|
|
17
|
+
|
|
18
|
+
def call(query: nil, after_id: 0, limit: 25, product_id: nil)
|
|
19
|
+
option_types = policy.scope(Spree::OptionType)
|
|
20
|
+
if product_id
|
|
21
|
+
product = policy.authorize_record!(:read, policy.scope(Spree::Product).find(product_id))
|
|
22
|
+
option_types = option_types.where(id: product.option_types.select(:id))
|
|
23
|
+
end
|
|
24
|
+
if query
|
|
25
|
+
pattern = "%#{ActiveRecord::Base.sanitize_sql_like(query)}%"
|
|
26
|
+
option_types = option_types.where("spree_option_types.name ILIKE :query OR spree_option_types.presentation ILIKE :query", query: pattern)
|
|
27
|
+
end
|
|
28
|
+
page(option_types, after_id: after_id, limit: limit) do |p|
|
|
29
|
+
{id: p.id, name: p.name, presentation: p.presentation}
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListOptionValues < Base
|
|
6
|
+
tool_name :list_option_values
|
|
7
|
+
description "Discover authorized values for an option type before creating variants."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "list_option_values"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::OptionValue"
|
|
12
|
+
ability :read, "Spree::OptionType"
|
|
13
|
+
input :option_type_id, type: "integer", minimum: 1, required: true
|
|
14
|
+
pagination
|
|
15
|
+
|
|
16
|
+
protected
|
|
17
|
+
|
|
18
|
+
def call(option_type_id:, after_id: 0, limit: 25)
|
|
19
|
+
policy.authorize_record!(:read, policy.scope(Spree::OptionType).find(option_type_id))
|
|
20
|
+
records = policy.scope(Spree::OptionValue).where(option_type_id: option_type_id)
|
|
21
|
+
page(records, after_id: after_id, limit: limit) do |record|
|
|
22
|
+
{id: record.id, name: record.name, presentation: record.presentation}
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListProductImages < Base
|
|
6
|
+
tool_name :list_product_images
|
|
7
|
+
description "List authorized image metadata for a product master variant or explicit variant. ID pagination includes display positions but no storage URLs."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "list_product_images"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::Product"
|
|
12
|
+
ability :read, "Spree::Variant"
|
|
13
|
+
ability :read, "Spree::Image"
|
|
14
|
+
input :product_id, type: "integer", minimum: 1, required: true
|
|
15
|
+
input :variant_id, type: "integer", minimum: 1
|
|
16
|
+
pagination
|
|
17
|
+
|
|
18
|
+
protected
|
|
19
|
+
|
|
20
|
+
def call(product_id:, variant_id: nil, after_id: 0, limit: 25)
|
|
21
|
+
context = ImageContext.new(policy, product_id: product_id, variant_id: variant_id)
|
|
22
|
+
page(context.images, after_id: after_id, limit: limit) { |image| context.metadata(image) }
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListShippingCategories < Base
|
|
6
|
+
tool_name :list_shipping_categories
|
|
7
|
+
description "Discover authorized shipping category IDs and names for product creation."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "list_shipping_categories"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::ShippingCategory"
|
|
12
|
+
pagination
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def call(after_id: 0, limit: 25)
|
|
17
|
+
records = policy.scope(Spree::ShippingCategory)
|
|
18
|
+
page(records, after_id: after_id, limit: limit) do |record|
|
|
19
|
+
{id: record.id, name: record.name}
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListStockLocations < Base
|
|
6
|
+
tool_name :list_stock_locations
|
|
7
|
+
description "Discover authorized stock location IDs, names, and operational flags, including inactive locations."
|
|
8
|
+
category "Inventory"
|
|
9
|
+
permission "list_stock_locations"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::StockLocation"
|
|
12
|
+
pagination
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def call(after_id: 0, limit: 25)
|
|
17
|
+
page(policy.scope(Spree::StockLocation), after_id: after_id, limit: limit) do |location|
|
|
18
|
+
{id: location.id, name: location.name, active: location.active,
|
|
19
|
+
default: location.default, fulfillable: location.fulfillable,
|
|
20
|
+
backorderable_default: location.backorderable_default,
|
|
21
|
+
propagate_all_variants: location.propagate_all_variants,
|
|
22
|
+
restock_inventory: location.restock_inventory}
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class ListTaxons < Base
|
|
6
|
+
tool_name :list_taxons
|
|
7
|
+
description "Browse authorized taxon IDs, names, parent IDs, taxonomy IDs, and depth. Optional literal name search, taxonomy filter, or direct-parent filter; parent_id null selects roots. Results use ID cursor pagination, not tree order."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "list_taxons"
|
|
10
|
+
read_only true
|
|
11
|
+
ability :read, "Spree::Taxon"
|
|
12
|
+
input :query, type: "string", minLength: 1, maxLength: 100
|
|
13
|
+
input :taxonomy_id, type: "integer", minimum: 1
|
|
14
|
+
input :parent_id, anyOf: [{type: "integer", minimum: 1}, {type: "null"}]
|
|
15
|
+
pagination
|
|
16
|
+
|
|
17
|
+
protected
|
|
18
|
+
|
|
19
|
+
def call(query: nil, taxonomy_id: nil, parent_id: :all, after_id: 0, limit: 25)
|
|
20
|
+
taxons = policy.scope(Spree::Taxon)
|
|
21
|
+
if taxonomy_id
|
|
22
|
+
policy.authorize_record!(:read, policy.scope(Spree::Taxonomy).find(taxonomy_id))
|
|
23
|
+
taxons = taxons.where(taxonomy_id: taxonomy_id)
|
|
24
|
+
end
|
|
25
|
+
unless parent_id == :all
|
|
26
|
+
policy.authorize_record!(:read, policy.scope(Spree::Taxon).find(parent_id)) if parent_id
|
|
27
|
+
taxons = taxons.where(parent_id: parent_id)
|
|
28
|
+
end
|
|
29
|
+
if query
|
|
30
|
+
pattern = "%#{ActiveRecord::Base.sanitize_sql_like(query)}%"
|
|
31
|
+
taxons = taxons.where("spree_taxons.name ILIKE ?", pattern)
|
|
32
|
+
end
|
|
33
|
+
page(taxons, after_id: after_id, limit: limit) do |taxon|
|
|
34
|
+
{id: taxon.id, name: taxon.name, parent_id: taxon.parent_id,
|
|
35
|
+
taxonomy_id: taxon.taxonomy_id, depth: taxon.depth}
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SolidusMcp
|
|
4
|
+
module Tools
|
|
5
|
+
class PublishProduct < Base
|
|
6
|
+
tool_name :publish_product
|
|
7
|
+
description "Make a product available now without changing discontinuation or stock."
|
|
8
|
+
category "Products"
|
|
9
|
+
permission "publish_product"
|
|
10
|
+
read_only false
|
|
11
|
+
ability :update, "Spree::Product"
|
|
12
|
+
input :product_id, type: "integer", minimum: 1, required: true
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def call(product_id:)
|
|
17
|
+
product = policy.scope(Spree::Product, :update).find(product_id)
|
|
18
|
+
product.with_lock do
|
|
19
|
+
authorize!
|
|
20
|
+
policy.authorize_record!(:update, product, attribute: :available_on)
|
|
21
|
+
if product.discontinued?
|
|
22
|
+
raise ToolError, "Discontinued products must be reinstated through the store before publishing"
|
|
23
|
+
end
|
|
24
|
+
product.available_on = Time.current unless product.available?
|
|
25
|
+
policy.authorize_record!(:update, product, attribute: :available_on)
|
|
26
|
+
product.save! if product.changed?
|
|
27
|
+
end
|
|
28
|
+
{product_id: product.id, available: !!product.available?}
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|