Repository navigation
feat(mcp): read Product Visibility's shelf, readiness and what AI says - #1733
Conversation
For an AI assistant working on a shop's catalog (MCP plan B6a): - get_catalog_sources (REST GET /api/mcp/catalog/sources): where the catalog is read from (feeds, uploads, connected stores) and how each last sync went, with its products in the catalog now. - list_catalog_products (REST GET /api/mcp/catalog/products): the live products, by title, as Product Visibility reads them, each with its readiness for AI shopping, its Google Merchant Center issues by severity and the AI visits and orders its page brought over 30 days; `search`, and paged with `limit` and `offset`. Product Visibility's catalog reads (readCatalogProducts and its paged readers) and Settings' source read (readCatalogSources) move to lib/shopping/catalog-read.ts and take the client: the member's session on the pages, the service role for the MCP once the brand is the key's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # docs/guides/mcp-server.mdx # web/src/lib/mcp/server.ts # web/src/lib/mcp/subscription-gate.test.ts
…wer past its end - Read the page's variants and their issues 100 ids and 1,000 rows a request, so a page past PostgREST's row cap keeps every issue. - Count each issue (code and attribute) once per product, however many variants and destinations it is on, and only for a Merchant Center account whose last sync went through (hasMerchantCenter, now shared with the editor's shopping check from lib/shopping/catalog-read). - Answer an empty page for an offset past the matches, which PostgREST refuses (PGRST103) once it counts them. - Search titles as written: a case-insensitive regex with its special characters escaped, since PostgREST turns every `*` of a like pattern into `%`. - One CARD_MAX_ROWS for the card and catalog reads. - Tests for the REST mirrors, the paged reads and the brand scoping; readiness.unknown, revenue_30d and the REST `search` in the tool description and docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Three read tools and their REST mirrors (MCP plan B6b), on top of the catalog tools (B6a): - get_shopping_shelf: each shopping question's latest shelf per AI channel, the brand's place on it and its last 7 days, and "Why not you?" on the shelves it isn't on (its catalog product the question asks for most, with its readiness). Paged; period, platform, market and absent_only filters. - get_shopping_readiness: the Readiness checklist and score, each check's text as the tab writes it (en.json) and where its fix starts, and each AI channel's state. - get_product_ai_says: a catalog product's "What AI says": answers, quotes and the pages they cited. They run the page's own loaders with the service role (dbClient / withDbClient) once the brand is the key's, so the tools and the page read the same. Like the page's tabs they need a plan with shopping_analytics (403 over REST). copyValues moves to lib/shopping/readiness.ts for both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Plan: read the plan the organization is on (usagePlanFromOrgRow), as the page does, so a workspace whose trial ended still reads; the locked message names the first plan with Product Visibility. - Shelf: read the products for "Why not you?" beside the shelves; pages of 10 shelves (50 at most), each with its first 10 products and products_total. - Readiness: read which channels hold the brand's products one row a channel (getShoppingOwnChannels) instead of every card on the shelves. - Guard test: the page's loaders through the MCP client with the member's client failing, every read and RPC naming the brand; plan lock and not found answered as errors. - Comments on the shared loaders say the MCP runs them with the service role; docs table no longer split by a blank line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good, with follow-ups
At merge time I made the plan check the same as #1764's: access() is brandInOrg then planIncludes(…, 'shopping_analytics'), and a plan without shopping analytics gets the same plan_limit answer (403 SHOPPING_NOT_IN_PLAN on REST). The PR's own plan read and PlanLocked are gone; the tests expect plan_limit and fail on the old check. The test's "1 products" is now "1 product" (#1741).
Reviewed this PR's own change (7974ce39..d868083e; it's stacked on #1729 at 7974ce3 and merges clean with #1729's head 80ad742). No migration.
What I checked
- The page's own reads under the service role. The tools call the existing Product Visibility loaders. The only change to those loaders is
createClient()→dbClient(), anddbClient()is per call chain (AsyncLocalStorage,lib/supabase/scoped.ts), so outsidewithDbClientthe app still uses the member's RLS client.brandInOrgruns before any read. Every table read in the changed loaders is filtered bybrand_id. The RPCs they call (shopping_merchant_split,shopping_own_product_offers,shopping_amazon_share_weekly) skip their membership check only forservice_roleand read byp_brand_id.shopping_filter_optionsis SQL filtered byp_brand_id.get_product_ai_sayschecks that the product is the brand's before it reads.product-visibility-scope.test.tsmakescreateClientthrow and checks each read's brand filter, so a loader left on the member's client, or a dropped filter, fails it. - Bounds. At most 50 shelves a page with 10 products each, plus up to 5 quotes and 5 sources.
getShoppingOwnChannelsmakes onelimit(1)read per channel. The shelf and product reads are the page's own, with their existing row caps. - Plan.
access()reads the organization's plan as the page does (usagePlanFromOrgRow): Starter is refused, a Growth plan that ended still reads, and self-hosted resolves to the self-hosted plan, which has the feature. - PostgREST embeds: none added. The new reads are
organizations (ORG_PLAN_COLUMNS),catalog_products (id, title)andprompt_result_shopping_cards (id). - Read-only workspace: three
get_tools withreadOnlyHint: trueand three GET routes, so nothing new to gate.subscription-guard-coverage.test.tspasses. - Docs: 58 tools on this base, matching the
registerToolcount. The REST routes and the plan note are listed. - Tests on this tree:
src/lib/mcp,src/app/api/mcp,lib/actions/shopping*,lib/actions/catalog,lib/shopping, the shopping page andsubscription-guard-coverage(71 files / 459 tests) pass.tsc --noEmitis clean, and eslint and prettier pass on the touched files.
Merging
Merges clean on ce9a3dc, with #1729's head and with #1764. It conflicts with #1728 in docs/guides/mcp-server.mdx, web/src/lib/mcp/server.ts and subscription-gate.test.ts: adjacent tool additions and counts.
With #1764
The two PRs gate on the same thing (shopping_analytics from usagePlanFromOrgRow, ended plans still read), but each has its own check and its own answer:
#1764 (list_shopping_cards, get_product_visibility) |
this PR (Shelf, Readiness, What AI says) | |
|---|---|---|
| helper | planIncludes() in lib/mcp/data.ts |
access() in lib/mcp/product-visibility.ts |
| MCP error | JSON { "error": "plan_limit", "message": … } |
plain text message |
| REST 403 body | { error: "plan_limit", message } |
{ error: <message> } |
A client would see two shapes for "your plan doesn't include shopping". Whichever merges second should use the other's check and its answer. I suggest #1764's plan_limit body, since it matches the API's requireFeature: export planIncludes and SHOPPING_NOT_IN_PLAN, and have access() and pvAnswer return them. Once that's done, the docs' plan note can name plan_limit.
Follow-ups
- The REST routes default an unknown
periodto 30d and ignore an unknownmarket, where the MCP tool rejects both (zod enums). A 400 would match the tool. This is minor. getShoppingShelfwithperiod=allreads every card of the brand, as the page does. Time it on the largest brand with #1732's script before choosingmaxDuration.
Summary
MCP next round, B6b: an assistant can ask "on which shopping questions am I not on the shelf, and why not me?", "how ready are we for AI shopping?" and "what does AI say about this product?" from Product Visibility's own data.
Stacked on #1729 (catalog tools). Review the commits after
7974ce39; merge after #1729.get_shopping_shelf+GET /api/mcp/product-visibility/shelf. The latest shelf for each shopping question × channel:product_id), the first 10 per shelf withproducts_total;own_rank) and its last 7 days;why_not_you: the catalog product the question asks for most and its readiness (score + missing).period(7d/30d/90d/all),platform,market(us/europe; default is the brand's own market, as on the page) andabsent_only. Paged:limitdefault 10, max 50, plusoffset.get_shopping_readiness+GET /api/mcp/product-visibility/readiness:key,state,titleanddetail(the Readiness tab's own en.json strings viacreateTranslator) and afixpointing to products or settings;scoreis null andunreadnames what's missing. A reseller read that fails leaves its item out instead of passing it.get_product_ai_says+GET /api/mcp/product-visibility/ai-says: for one catalog product, how many answers showed it, up to 5 quotes and up to 5 sources. Another brand's product is not found (REST 404).getShoppingShelf,getShoppingProducts,getProductAiSays,getResellerPriceAlerts,getShoppingAmazonShareDropAlert,getShoppingFilterOptions,getShoppingMerchantSplit,getOpenAiIndexRead,getBrandMarketCountries, and the new lightweightgetShoppingOwnChannels) under the service role:dbClient(), and the MCP wraps them inwithDbClient(supabaseAdmin)afterbrandInOrg;dbClient()is the member's RLS client;product-visibility-scope.test.ts) runs the three tools through an MCP client with the real reads whilecreateClientthrows, and checks each read is scoped to the brand. Two deliberate breaks (one read back on the RLS client, onebrand_idfilter dropped) both fail it;copyValuesmoved tolib/shopping/readiness.ts, shared by the Readiness tab and the tool.shopping_analytics, like the page's tabs (Growth and up; self-hosted open). A workspace whose plan has ended still reads, like the page (read-only rule). Starter gets an error naming the plan ("Growth or above"); REST answers 403. The olderlist_shopping_cards/get_product_visibilitystay as they are (user's call).maxDurationon/api/mcpand the REST routes would be your call, so it isn't changed here.Related issue
None (MCP next round, B6b; Product Visibility).
Type of change
feat— New featureHow to test
get_shopping_shelfwithabsent_only: true. The shelves match Product Visibility › Shelf for the same period and market, and each haswhy_not_you.get_shopping_readiness: the items and score match the Readiness tab.get_product_ai_sayswith a catalog product id: the quotes match the product page's "What AI says". Another brand's product returns not found./api/mcp/product-visibility/*routes with the same key.Checklist
yarn lint(0 errors),yarn typecheck,yarn buildpass.yarn test: 562/563 files; the Library page timed out under load and passes alone. After the review fixes:tsc --noEmit, eslint and prettier pass, andlib/mcp,app/api/mcp,lib/actions/shopping*,lib/shoppingand the shopping page pass (70 files / 454 tests).🤖 Generated with Claude Code