Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed a search returning the count instead of the records, and released the export link

Damian Legawiecf990aefa9e0c
diff --git a/spree/core/app/tools/spree/agent_tools/create_export.rb b/spree/core/app/tools/spree/agent_tools/create_export.rb
index f32c58548c..86cde52060 100644
--- a/spree/core/app/tools/spree/agent_tools/create_export.rb
+++ b/spree/core/app/tools/spree/agent_tools/create_export.rb
@@ -56,7 +56,12 @@ module Spree
ok: true,
id: export.prefixed_id,
resource: resource.to_s,
- message: 'The export is being prepared; it will appear under Settings → Exports.'
+ # Generation is a background job, so there is no file yet. Naming
+ # the tool that finishes the job keeps the model from sending the
+ # merchant to the dashboard for something it can fetch itself.
+ message: 'The export is being prepared. Read it back with get_resource ' \
+ "(resource: \"exports\", id: \"#{export.prefixed_id}\"); once the file is ready " \
+ 'the record carries a download_url, which is a path on this API.'
}
end
diff --git a/spree/core/app/tools/spree/agent_tools/get_resource.rb b/spree/core/app/tools/spree/agent_tools/get_resource.rb
index 131d01d577..7c83ec176e 100644
--- a/spree/core/app/tools/spree/agent_tools/get_resource.rb
+++ b/spree/core/app/tools/spree/agent_tools/get_resource.rb
@@ -58,7 +58,7 @@ module Spree
# can act on, not a protocol failure — the same guard RecordSummary
# applies to the summary row.
def serialize(entry, record)
- Spree::AgentTools::RecordSummary.sanitize(entry.serializer_class.new(record).to_h)
+ Spree::AgentTools::RecordSummary.sanitize(entry.serializer_class.new(record).to_h, entry.key)
rescue StandardError => e
Rails.logger.warn("[Spree] #{entry.key} serializer failed: #{e.class}: #{e.message}")
nil
diff --git a/spree/core/lib/spree/agent_tools/record_summary.rb b/spree/core/lib/spree/agent_tools/record_summary.rb
index 4ba7fa0c12..00ecf59448 100644
--- a/spree/core/lib/spree/agent_tools/record_summary.rb
+++ b/spree/core/lib/spree/agent_tools/record_summary.rb
@@ -54,6 +54,18 @@ module Spree
acceptance_url download_url
].freeze
+ # Attributes a named resource may keep, because on that resource the
+ # value is an ordinary authenticated path rather than a link that
+ # authenticates on itself.
+ #
+ # An export's download endpoint runs the Admin API's own scope check
+ # and `authorize_resource!`, so the link grants nothing on its own and
+ # withholding it only sends the merchant to the dashboard for a file
+ # the agent just made for them. Keyed per attribute rather than per
+ # resource: exempting a whole record would also release any other
+ # credential-shaped field it later gains.
+ LINK_SAFE_ATTRIBUTES = { 'exports' => %w[download_url].freeze }.freeze
+
class << self
# Strips credential-shaped keys from a serialized record.
#
@@ -68,14 +80,20 @@ module Spree
#
# @param attributes [Hash]
# @return [Hash]
- def sanitize(value)
+ def sanitize(value, resource = nil)
case value
when Hash
+ exempt = LINK_SAFE_ATTRIBUTES.fetch(resource.to_s, [])
+
value.to_h.stringify_keys.each_with_object({}) do |(key, nested), result|
+ link_safe = exempt.include?(key)
+
next if key.match?(SECRET_PATTERN) || SECRET_ASSOCIATIONS.include?(key)
- next if SECRET_ATTRIBUTES.include?(key)
- next if credential_value?(nested)
+ next if SECRET_ATTRIBUTES.include?(key) && !link_safe
+ next if credential_value?(nested) && !link_safe
+ # Only the record's own keys are exempt. A nested association
+ # carries its own rules, so recursion drops the exemption.
result[key] = sanitize(nested)
end
when Array
diff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb
index 9971842c57..261d8e71ea 100644
--- a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb
+++ b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb
@@ -53,6 +53,34 @@ RSpec.describe Spree::AgentTools::RecordSummary do
# serializers expose a link whose path or query string IS the secret, and the
# endpoint it addresses authenticates on the link alone. Matching the key
# name cannot catch those.
+ # An export's download endpoint authenticates like any other admin route,
+ # so the link grants nothing on its own. Withholding it only sent the
+ # merchant to the dashboard for a file the agent had just made.
+ describe 'a link that is not a bearer instrument' do
+ let(:links) do
+ { 'download_url' => '/api/v3/admin/exports/exp_1/download',
+ 'acceptance_url' => 'https://example.test/accept-invitation/SECRET' }
+ end
+
+ it "keeps an export's download link" do
+ expect(described_class.sanitize(links, 'exports')).to eq(
+ 'download_url' => '/api/v3/admin/exports/exp_1/download'
+ )
+ end
+
+ it 'keeps it for no other resource' do
+ expect(described_class.sanitize(links, 'invitations')).to be_empty
+ expect(described_class.sanitize(links)).to be_empty
+ end
+
+ # The exemption belongs to the record, not to anything hanging off it.
+ it 'does not reach a nested association' do
+ result = described_class.sanitize({ 'nested' => links }, 'exports')
+
+ expect(result['nested']).to be_empty
+ end
+ end
+
describe 'a credential carried in a value' do
it 'drops an invitation acceptance link' do
cleaned = described_class.sanitize(
diff --git a/spree/mcp/lib/spree/mcp/tool_adapter.rb b/spree/mcp/lib/spree/mcp/tool_adapter.rb
index 3153659086..87b59652ed 100644
--- a/spree/mcp/lib/spree/mcp/tool_adapter.rb
+++ b/spree/mcp/lib/spree/mcp/tool_adapter.rb
@@ -97,28 +97,23 @@ module Spree
# A line the client can show beside its confirmation prompt, and the
# model can read without parsing the structured payload.
- #
- # The payload itself is NOT repeated here: it already travels as
- # structured content, and a client that renders both would put every
- # record into the model's context twice — on exactly the results the
- # design requires to stay compact. Without a summary this says what
- # came back, and the structured half carries it.
# What the model reads.
#
# A client may render the structured half, the text half, or both —
# the protocol does not say — so the text has to carry the answer
- # rather than point at it. Naming the keys and trusting the client to
- # look elsewhere leaves a model with nothing to work from.
+ # rather than point at it. Claude reads the text, so a payload that
+ # travels only as structured content reaches the model as nothing:
+ # a search answered "Found 1 match." with the match itself stripped,
+ # and the model could not name what it had just found.
#
- # The exception is a list of records: those already carry a summary
- # line, and repeating every row would put the payload into the
- # model's context twice on exactly the results meant to stay compact.
+ # A count is a description of an answer, never the answer. The only
+ # text that may stand in for a payload is one the tool wrote itself.
def text_for(result)
return result.to_s unless result.is_a?(Hash)
summary = result[:summary].presence
+ return "#{summary}\n#{JSON.generate(result)}" if summary && record_list?(result)
return summary if summary
- return describe(result) if record_list?(result)
JSON.generate(result)
end
@@ -132,22 +127,6 @@ module Spree
def record_list?(result)
result.key?(:records)
end
-
- # A sentence about a payload that carries no summary of its own: what
- # it holds, so the model knows whether to read the structured content
- # rather than having to.
- def describe(result)
- count = result[:count] || result[:row_count] || Array(result[:records] || result[:rows]).size
- total = result[:total]
-
- if total && count
- "Found #{total} #{'match'.pluralize(total)}#{", returning #{count}" if total != count}."
- elsif count.to_i.positive?
- "Returned #{count} #{'result'.pluralize(count)}."
- else
- "Returned #{result.keys.map(&:to_s).to_sentence}."
- end
- end
end
end
end
diff --git a/spree/mcp/spec/lib/spree/mcp/server_spec.rb b/spree/mcp/spec/lib/spree/mcp/server_spec.rb
index a696fc1345..400f55afaf 100644
--- a/spree/mcp/spec/lib/spree/mcp/server_spec.rb
+++ b/spree/mcp/spec/lib/spree/mcp/server_spec.rb
@@ -139,19 +139,18 @@ RSpec.describe Spree::Mcp::Server do
expect(response.dig('result', 'isError')).to be(false)
end
- # A result travels as structured content; repeating it as text would put
- # every record into the model's context twice, on exactly the payloads
- # the design requires to stay compact.
- it 'does not repeat the payload as text' do
+ # Claude reads the text block, so a payload that travels only as
+ # structured content reaches the model as nothing. This shipped the other
+ # way round once: a search answered "Found 1 match." with the match
+ # stripped out, and the model could not name what it had just found.
+ it 'carries the records in the text, not only in structured content' do
create(:product, store: store, name: 'Context Budget')
response = call('tools/call', name: 'search_resources', arguments: { 'resource' => 'products' })
text = response.dig('result', 'content', 0, 'text').to_s
- structured = JSON.generate(response.dig('result', 'structuredContent'))
- expect(structured).to include('Context Budget')
- expect(text).not_to include('Context Budget')
- expect(text.length).to be < structured.length
+ expect(text).to include('Context Budget')
+ expect(JSON.generate(response.dig('result', 'structuredContent'))).to include('Context Budget')
end
# The other half of the rule. A client may render the structured half,
@@ -167,12 +166,17 @@ RSpec.describe Spree::Mcp::Server do
expect(JSON.parse(text)).to include('families')
end
- it 'says what a payload holds when the tool offers no summary' do
+ # A count describes an answer rather than being one. An empty result may
+ # say so in a sentence, but a result holding records has to hand them
+ # over — the model cannot act on "Found 2 matches."
+ it 'never reduces a page of records to a count' do
create_list(:product, 2, store: store)
response = call('tools/call', name: 'search_resources', arguments: { 'resource' => 'products' })
+ text = response.dig('result', 'content', 0, 'text').to_s
- expect(response.dig('result', 'content', 0, 'text')).to match(/Found \d+ match/)
+ expect(text).not_to match(/\AFound \d+ match(es)?\.\z/)
+ expect(JSON.parse(text)['records'].size).to eq(2)
end
it 'returns structured content from a read tool' do