Skip to content

feat(mcp): read Product Visibility's shelf, readiness and what AI says - #1733

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-product-visibility
Oct 29, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-product-visibility

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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:
    • products in order (rank, title, price, rating, reviews, seller, own, via, product_id), the first 10 per shelf with products_total;
    • the brand's place (own_rank) and its last 7 days;
    • on a shelf the brand isn't on, why_not_you: the catalog product the question asks for most and its readiness (score + missing).
    • Filters: period (7d/30d/90d/all), platform, market (us/europe; default is the brand's own market, as on the page) and absent_only. Paged: limit default 10, max 50, plus offset.
  • get_shopping_readiness + GET /api/mcp/product-visibility/readiness:
    • the readiness checklist and its 0–100 score; each item has key, state, title and detail (the Readiness tab's own en.json strings via createTranslator) and a fix pointing to products or settings;
    • the five AI channels' state: showing, notShowing or notTracked;
    • if sources or products can't be read, score is null and unread names 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).
  • The page's own reads, no copy. The tools run Product Visibility's server actions (getShoppingShelf, getShoppingProducts, getProductAiSays, getResellerPriceAlerts, getShoppingAmazonShareDropAlert, getShoppingFilterOptions, getShoppingMerchantSplit, getOpenAiIndexRead, getBrandMarketCountries, and the new lightweight getShoppingOwnChannels) under the service role:
    • those functions take dbClient(), and the MCP wraps them in withDbClient(supabaseAdmin) after brandInOrg;
    • in the app nothing changes: outside that wrapper dbClient() is the member's RLS client;
    • every table read and RPC is filtered to the brand. A new test (product-visibility-scope.test.ts) runs the three tools through an MCP client with the real reads while createClient throws, and checks each read is scoped to the brand. Two deliberate breaks (one read back on the RLS client, one brand_id filter dropped) both fail it;
    • copyValues moved to lib/shopping/readiness.ts, shared by the Readiness tab and the tool.
  • Plan: the three tools need 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 older list_shopping_cards / get_product_visibility stay as they are (user's call).
  • Speed: the product list starts beside the shelf read, and Readiness counts each channel's own cards with one small read per channel instead of the whole shelf. Big brands are still worth timing with feat(mcp): give every tool a title and annotations, and tell clients tool results are data #1732's script. maxDuration on /api/mcp and the REST routes would be your call, so it isn't changed here.
  • Guide: 58 tools on this base; three rows, the plan note and the REST routes. Skills follow in the B8 docs PR.

Related issue

None (MCP next round, B6b; Product Visibility).

Type of change

  • feat — New feature

How to test

  1. On a Growth brand with shopping data, with an API key: get_shopping_shelf with absent_only: true. The shelves match Product Visibility › Shelf for the same period and market, and each has why_not_you.
  2. get_shopping_readiness: the items and score match the Readiness tab.
  3. get_product_ai_says with a catalog product id: the quotes match the product page's "What AI says". Another brand's product returns not found.
  4. On a Starter workspace: an error naming Growth (REST 403). On a workspace whose trial ended: the reads still answer.
  5. REST: the three /api/mcp/product-visibility/* routes with the same key.
  6. Product Visibility pages behave as before.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web, first commit: yarn lint (0 errors), yarn typecheck, yarn build pass. 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, and lib/mcp, app/api/mcp, lib/actions/shopping*, lib/shopping and the shopping page pass (70 files / 454 tests).
  • Tests added: the tools (incl. plan lock, ended trial reads, not-found), the scope guard, the three REST routes
  • The embed guard from test(web): fail on an embed between twice-joined tables that doesn't name its foreign key #1714 passes on this branch
  • Docs updated
  • Pre-PR review done; its findings are fixed in this branch
  • Live call with a real key, and timing on the largest brand: the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 6 commits October 28, 2026 23:16
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]>
@mintlify

mintlify Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit b690b74.

Approve preview

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(), and dbClient() is per call chain (AsyncLocalStorage, lib/supabase/scoped.ts), so outside withDbClient the app still uses the member's RLS client. brandInOrg runs before any read. Every table read in the changed loaders is filtered by brand_id. The RPCs they call (shopping_merchant_split, shopping_own_product_offers, shopping_amazon_share_weekly) skip their membership check only for service_role and read by p_brand_id. shopping_filter_options is SQL filtered by p_brand_id. get_product_ai_says checks that the product is the brand's before it reads. product-visibility-scope.test.ts makes createClient throw 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. getShoppingOwnChannels makes one limit(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) and prompt_result_shopping_cards (id).
  • Read-only workspace: three get_ tools with readOnlyHint: true and three GET routes, so nothing new to gate. subscription-guard-coverage.test.ts passes.
  • Docs: 58 tools on this base, matching the registerTool count. 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 and subscription-guard-coverage (71 files / 459 tests) pass. tsc --noEmit is 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

  1. The REST routes default an unknown period to 30d and ignore an unknown market, where the MCP tool rejects both (zod enums). A 400 would match the tool. This is minor.
  2. getShoppingShelf with period=all reads every card of the brand, as the page does. Time it on the largest brand with #1732's script before choosing maxDuration.

@gkhngyk
gkhngyk merged commit b3fc875 into ansvisor:main Oct 29, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants