solidus_mcp 0.2.0.rc2 → 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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +63 -0
  3. data/README.md +321 -3
  4. data/app/assets/stylesheets/solidus_mcp/admin.css +39 -0
  5. data/app/assets/stylesheets/solidus_mcp/consent.css +77 -0
  6. data/app/controllers/solidus_admin/mcp_activity_controller.rb +67 -0
  7. data/app/controllers/solidus_mcp/endpoint_controller.rb +36 -0
  8. data/app/models/solidus_mcp/activity_event.rb +37 -0
  9. data/app/models/solidus_mcp/audit_context.rb +7 -0
  10. data/app/models/solidus_mcp/stock_adjustment.rb +16 -0
  11. data/app/models/solidus_mcp/tool_request.rb +10 -0
  12. data/app/services/solidus_mcp/action_audit.rb +169 -0
  13. data/app/services/solidus_mcp/image_context.rb +44 -0
  14. data/app/services/solidus_mcp/order_workflow_authorization.rb +76 -0
  15. data/app/services/solidus_mcp/tool_request_execution.rb +76 -0
  16. data/app/tools/solidus_mcp/tools/add_product_option_type.rb +36 -0
  17. data/app/tools/solidus_mcp/tools/add_product_to_taxon.rb +40 -0
  18. data/app/tools/solidus_mcp/tools/adjust_stock.rb +98 -0
  19. data/app/tools/solidus_mcp/tools/attach_product_image.rb +77 -0
  20. data/app/tools/solidus_mcp/tools/bulk_update_variant_prices.rb +1 -1
  21. data/app/tools/solidus_mcp/tools/cancel_order.rb +44 -0
  22. data/app/tools/solidus_mcp/tools/create_product.rb +1 -0
  23. data/app/tools/solidus_mcp/tools/create_variant_price.rb +59 -0
  24. data/app/tools/solidus_mcp/tools/list_option_types.rb +34 -0
  25. data/app/tools/solidus_mcp/tools/list_option_values.rb +27 -0
  26. data/app/tools/solidus_mcp/tools/list_product_images.rb +26 -0
  27. data/app/tools/solidus_mcp/tools/list_shipping_categories.rb +24 -0
  28. data/app/tools/solidus_mcp/tools/list_stock_locations.rb +27 -0
  29. data/app/tools/solidus_mcp/tools/list_taxons.rb +40 -0
  30. data/app/tools/solidus_mcp/tools/publish_product.rb +32 -0
  31. data/app/tools/solidus_mcp/tools/refund_payment.rb +63 -0
  32. data/app/tools/solidus_mcp/tools/remove_product_image.rb +33 -0
  33. data/app/tools/solidus_mcp/tools/reorder_product_images.rb +41 -0
  34. data/app/tools/solidus_mcp/tools/ship_shipment.rb +63 -0
  35. data/app/tools/solidus_mcp/tools/unpublish_product.rb +29 -0
  36. data/app/tools/solidus_mcp/tools/update_variant.rb +54 -0
  37. data/app/tools/solidus_mcp/tools/update_variant_price.rb +1 -1
  38. data/app/views/solidus_admin/mcp_activity/index.html.erb +53 -0
  39. data/app/views/solidus_admin/mcp_connections/index.html.erb +2 -0
  40. data/app/views/solidus_mcp/authorizations/new.html.erb +59 -41
  41. data/config/routes.rb +1 -0
  42. data/db/migrate/20261112000000_create_solidus_mcp_stock_adjustments.rb +19 -0
  43. data/db/migrate/20261112010000_create_solidus_mcp_tool_requests.rb +19 -0
  44. data/db/migrate/20261112020000_create_solidus_mcp_activity_events.rb +34 -0
  45. data/db/migrate/20261112020100_add_actor_role_to_mcp_activity_events.rb +7 -0
  46. data/lib/solidus_mcp/audited_server.rb +25 -0
  47. data/lib/solidus_mcp/engine.rb +8 -3
  48. data/lib/solidus_mcp/policy.rb +4 -4
  49. data/lib/solidus_mcp/registry.rb +5 -2
  50. data/lib/solidus_mcp/server.rb +1 -2
  51. data/lib/solidus_mcp/tool.rb +9 -7
  52. data/lib/solidus_mcp/version.rb +1 -1
  53. data/lib/solidus_mcp.rb +5 -1
  54. metadata +37 -2
@@ -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: ["string", "null"]}}}
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
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMcp
4
+ module Tools
5
+ class RefundPayment < Base
6
+ tool_name :refund_payment
7
+ description "Refund an explicit payment amount and currency through Solidus. Requires refund_reason_id and an idempotency key. Uncertain gateway outcomes block further external operations on the order until reconciled."
8
+ category "Orders"
9
+ permission "refund_payment"
10
+ read_only false
11
+ audit_action "create"
12
+ ability :update, "Spree::Order"
13
+ ability :update, "Spree::Payment"
14
+ ability :read, "Spree::RefundReason"
15
+ ability :create, "Spree::Refund"
16
+ input :payment_id, type: "integer", minimum: 1, required: true
17
+ input :refund_reason_id, type: "integer", minimum: 1, required: true
18
+ input :currency, type: "string", pattern: "^[A-Z]{3}$", required: true
19
+ input :amount, type: "string", required: true
20
+ input :idempotency_key, type: "string", minLength: 1, maxLength: 128, required: true
21
+
22
+ protected
23
+
24
+ def call(payment_id:, refund_reason_id:, currency:, amount:, idempotency_key:)
25
+ unless amount.match?(/\A(?:0|[1-9][0-9]{0,7})(?:\.[0-9]{1,2})?\z/) && BigDecimal(amount).positive?
26
+ raise ToolError, "Amount must be a positive decimal string with at most two decimal places"
27
+ end
28
+ payment = policy.scope(Spree::Payment, :update)
29
+ .where(order_id: policy.scope(Spree::Order, :update).select(:id)).find(payment_id)
30
+ order = payment.order
31
+ reason = policy.authorize_record!(:read, policy.scope(Spree::RefundReason).find(refund_reason_id))
32
+ refund = Spree::Refund.new(payment_id: payment.id, refund_reason_id: reason.id, amount: BigDecimal(amount))
33
+ policy.authorize_record!(:create, refund)
34
+ policy.authorize_record!(:update, payment.order)
35
+ unless currency.match?(/\A[A-Z]{3}\z/) && payment.currency == currency && refund.money.cents == BigDecimal(amount) * refund.money.currency.subunit_to_unit
36
+ raise ToolError, "Currency must match the payment and support the requested cent precision"
37
+ end
38
+ request = ToolRequestExecution.new(credential, operation: "refund_payment", key: idempotency_key,
39
+ payload: {payment_id: payment_id, refund_reason_id: refund_reason_id, currency: currency, amount: amount})
40
+ request.external(guard_key: "order:#{order.id}") do |effects|
41
+ order.with_lock do
42
+ payment.with_lock do
43
+ authorize!
44
+ raise ToolError, "Payment order changed; retrieve it again" unless payment.order_id == order.id
45
+ policy.authorize_record!(:update, payment.order)
46
+ policy.authorize_record!(:update, payment)
47
+ policy.authorize_record!(:read, reason.reload)
48
+ unless payment.completed? && payment.currency == currency && BigDecimal(amount) <= payment.credit_allowed
49
+ raise ToolError, "Payment is not refundable for the requested amount and currency"
50
+ end
51
+ refund = Spree::Refund.new(payment_id: payment.id, refund_reason_id: reason.id, amount: BigDecimal(amount))
52
+ policy.authorize_record!(:create, refund)
53
+ refund.save!
54
+ effects.call
55
+ refund.perform!
56
+ {refund_id: refund.id, payment_id: payment.id, amount: refund.amount.to_s("F"), currency: currency, refunded: true}
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMcp
4
+ module Tools
5
+ class RemoveProductImage < Base
6
+ tool_name :remove_product_image
7
+ description "Permanently remove one image from an explicit product/variant parent. Missing or already removed IDs fail safely; attachment cleanup follows Solidus and ActiveStorage callbacks."
8
+ category "Products"
9
+ permission "remove_product_image"
10
+ read_only false
11
+ audit_action "delete"
12
+ ability :update, "Spree::Product"
13
+ ability :update, "Spree::Variant"
14
+ ability :destroy, "Spree::Image"
15
+ input :product_id, type: "integer", minimum: 1, required: true
16
+ input :variant_id, type: "integer", minimum: 1
17
+ input :image_id, type: "integer", minimum: 1, required: true
18
+
19
+ protected
20
+
21
+ def call(product_id:, image_id:, variant_id: nil)
22
+ context = ImageContext.new(policy, product_id: product_id, variant_id: variant_id, action: :update)
23
+ context.with_lock do
24
+ authorize!
25
+ image = context.images(:destroy).find(image_id)
26
+ policy.authorize_record!(:destroy, image)
27
+ image.destroy!
28
+ end
29
+ {image_id: image_id, removed: true}
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMcp
4
+ module Tools
5
+ class ReorderProductImages < Base
6
+ tool_name :reorder_product_images
7
+ description "Replace the complete image ordering for one product master or explicit variant. All image IDs must be supplied exactly once; changes are atomic."
8
+ category "Products"
9
+ permission "reorder_product_images"
10
+ read_only false
11
+ ability :update, "Spree::Product"
12
+ ability :update, "Spree::Variant"
13
+ ability :update, "Spree::Image"
14
+ input :product_id, type: "integer", minimum: 1, required: true
15
+ input :variant_id, type: "integer", minimum: 1
16
+ input :image_ids, type: "array", items: {type: "integer", minimum: 1}, uniqueItems: true, maxItems: 100, required: true
17
+
18
+ protected
19
+
20
+ def call(product_id:, image_ids:, variant_id: nil)
21
+ context = ImageContext.new(policy, product_id: product_id, variant_id: variant_id, action: :update)
22
+ context.with_lock do
23
+ authorize!
24
+ # Check the complete parent collection, not just its readable subset.
25
+ actual = Spree::Image.where(viewable: context.variant).limit(101).pluck(:id)
26
+ unless actual.sort == image_ids.sort
27
+ raise ToolError, "Supply every image in this parent exactly once"
28
+ end
29
+ images = context.images(:update).where(id: image_ids).index_by(&:id)
30
+ raise AccessDenied, "Record is unavailable" unless images.length == actual.length
31
+ image_ids.each do |id|
32
+ policy.authorize_record!(:update, images.fetch(id), attribute: :position)
33
+ end
34
+ image_ids.each_with_index { |id, index| images.fetch(id).insert_at(index + 1) }
35
+ images.each_value { |image| policy.authorize_record!(:update, image.reload, attribute: :position) }
36
+ end
37
+ {product_id: product_id, variant_id: context.variant.id, image_ids: image_ids}
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMcp
4
+ module Tools
5
+ class ShipShipment < Base
6
+ tool_name :ship_shipment
7
+ description "Ship one ready shipment through Solidus carton fulfillment. Explicitly choose notify_customer; tracking is optional. Requires an idempotency key and record permissions for affected fulfillment records."
8
+ category "Orders"
9
+ permission "ship_shipment"
10
+ read_only false
11
+ ability :update, "Spree::Order"
12
+ ability :ship, "Spree::Shipment"
13
+ ability :update, "Spree::InventoryUnit"
14
+ ability :create, "Spree::Carton"
15
+ input :shipment_id, type: "integer", minimum: 1, required: true
16
+ input :tracking, type: "string", minLength: 1, maxLength: 255
17
+ input :notify_customer, type: "boolean", required: true
18
+ input :idempotency_key, type: "string", minLength: 1, maxLength: 128, required: true
19
+
20
+ protected
21
+
22
+ def call(shipment_id:, notify_customer:, idempotency_key:, tracking: nil)
23
+ shipment = policy.scope(Spree::Shipment, :ship)
24
+ .where(order_id: policy.scope(Spree::Order, :update).select(:id)).find(shipment_id)
25
+ order = shipment.order
26
+ authorization = OrderWorkflowAuthorization.new(policy)
27
+ authorization.shipping(shipment, tracking: tracking || shipment.tracking, notify_customer: notify_customer)
28
+ request = ToolRequestExecution.new(credential, operation: "ship_shipment", key: idempotency_key,
29
+ payload: {shipment_id: shipment_id, tracking: tracking, notify_customer: notify_customer})
30
+ request.external(guard_key: "order:#{order.id}") do |effects|
31
+ order.with_lock do
32
+ shipment.with_lock do
33
+ authorize!
34
+ raise ToolError, "Shipment order changed; retrieve it again" unless shipment.order_id == order.id
35
+ units = shipment.inventory_units.reload.to_a
36
+ units.each(&:lock!)
37
+ authorization.shipping(shipment, tracking: tracking || shipment.tracking, notify_customer: notify_customer)
38
+ if shipment.shipped?
39
+ if tracking && tracking != shipment.tracking
40
+ raise ToolError, "Shipment is already shipped with different tracking"
41
+ end
42
+ else
43
+ eligible = shipment.state == "ready" && shipment.order.completed? && units.any?(&:on_hand?) &&
44
+ units.all? { |unit| unit.on_hand? || unit.shipped? || unit.canceled? }
45
+ unless eligible
46
+ raise ToolError, "Shipment must be ready on a completed order, with shippable units and no backorders"
47
+ end
48
+ policy.authorize_record!(:update, shipment, attribute: :tracking) if tracking
49
+ shipment.tracking = tracking if tracking
50
+ policy.authorize_record!(:ship, shipment)
51
+ policy.authorize_record!(:update, shipment, attribute: :tracking) if tracking
52
+ shipment.suppress_mailer = !notify_customer
53
+ effects.call
54
+ shipment.ship!
55
+ end
56
+ {shipment_id: shipment.id, order_id: shipment.order_id, state: shipment.state, tracking: shipment.tracking}
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SolidusMcp
4
+ module Tools
5
+ class UnpublishProduct < Base
6
+ tool_name :unpublish_product
7
+ description "Withdraw product availability without deleting it, its variants, or order history."
8
+ category "Products"
9
+ permission "unpublish_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
+ product.available_on = nil
22
+ policy.authorize_record!(:update, product, attribute: :available_on)
23
+ product.save! if product.changed?
24
+ end
25
+ {product_id: product.id, available: !!product.available?}
26
+ end
27
+ end
28
+ end
29
+ end