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.

Stop sending every read result to the model twice

Damian Legawiec14a22b609b69
diff --git a/spree/mcp/lib/spree/mcp/tool_adapter.rb b/spree/mcp/lib/spree/mcp/tool_adapter.rb
index 736e60c1d7..527d029301 100644
--- a/spree/mcp/lib/spree/mcp/tool_adapter.rb
+++ b/spree/mcp/lib/spree/mcp/tool_adapter.rb
@@ -97,10 +97,32 @@ 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.
def text_for(result)
return result.to_s unless result.is_a?(Hash)
- result[:summary].presence || JSON.generate(result)
+ result[:summary].presence || describe(result)
+ 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
diff --git a/spree/mcp/spec/lib/spree/mcp/server_spec.rb b/spree/mcp/spec/lib/spree/mcp/server_spec.rb
index afe4ed9ffb..35ece5533c 100644
--- a/spree/mcp/spec/lib/spree/mcp/server_spec.rb
+++ b/spree/mcp/spec/lib/spree/mcp/server_spec.rb
@@ -139,6 +139,29 @@ 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
+ 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
+ end
+
+ it 'says what a payload holds when the tool offers no summary' do
+ create_list(:product, 2, store: store)
+
+ response = call('tools/call', name: 'search_resources', arguments: { 'resource' => 'products' })
+
+ expect(response.dig('result', 'content', 0, 'text')).to match(/Found \d+ match/)
+ end
+
it 'returns structured content from a read tool' do
create(:product, store: store, name: 'Rotary Shaver 9000')