Repository navigation
feat(mcp): read the product catalog: its sources and its products - #1729
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]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Text fix: |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good, with follow-ups
At merge time, on a plan without shopping analytics, list_catalog_products (and GET /api/mcp/catalog/products) keeps the product list but returns readiness, merchant_center_issues, ai_visits_30d, orders_30d, revenue_30d and revenue_currency as null, without reading them, as Product Visibility's Products tab is Growth's. New tests cover both plans and fail without it.
Reviewed this PR's own change (origin/main...80ad7423). No migration.
What I checked
- Org boundary. Both tools and both REST routes call
brandInOrgfirst, so another organization's brand (or an ID that isn't a UUID) is not found / 404. After that, every service-role read is filtered bybrand_id: products, variants, images, categories, issues, sources, the per-source counts andhasMerchantCenter.catalog_product_ai_statsskips its membership check for the service role and reads onlyp_brand_id. - Moves, no copies.
readCatalogProducts,readCatalogRows,readForIds,readCatalogSourcesandhasMerchantCentermoved unchanged intolib/shopping/catalog-read.tsand take the client as a parameter.listCatalogSources,shopping.tsandshopping-path.tspass the member's client, so the app behaves as before. - Search.
literal()escapes every regex metacharacter beforeregexIMatch, so50%and5*match as written. It's input-capped at 200 and runs in linear time. Under Postgres it's a~*over one brand's titles. - Paging and bounds.
limitis 1–200. An offset past the end (PGRST103) comes back as an empty page with the righttotal. Variants and issues are read 100 IDs per request in 1,000-row pages, up toCARD_MAX_ROWS. Issues are counted once per product per (severity, code, attribute). Response size is bounded by 200 products. - PostgREST embeds:
catalog_product_categories → category:category_id (name)(moved, unchanged). It has 1 FK tocatalog_categoriesand the hint is the column. No new embeds. - Read-only workspace: two
get_/list_tools withreadOnlyHint: trueand two GET routes, so nothing new to gate.subscription-guard-coverage.test.tspasses. - REST mirrors match the tools (same function, same caps; REST trims
searchto 200). - Docs: 55 tools on this base, matching
registerToolcount; the 80ad742 wording (no Shopify connector) is right. - Tests on this tree:
src/lib/mcp,src/app/api/mcp,lib/actions/shopping*,lib/actions/catalog,lib/shopping, the shopping page, the catalog settings card andsubscription-guard-coverage(68 files / 463 tests) pass.tsc --noEmitis clean, and eslint and prettier pass on the touched files.
Merging
Merges clean on ce9a3dc and with #1764. It conflicts with #1728 in docs/guides/mcp-server.mdx and web/src/lib/mcp/server.ts: adjacent tool additions and the count.
Plan gating (with #1764)
#1764 gates list_shopping_cards and get_product_visibility on shopping_analytics (Growth and up). This PR's tools have no plan check:
get_catalog_sources: none needed. Starter has a catalog (5,000 products) and sees these sources in Settings.list_catalog_products: the rows themselves are catalog data.readiness, the per-product Merchant Center issues and the 30-day AI visits/orders/revenue are Product Visibility's Products tab, though, and the app shows that tab only withshopping_analytics. Once #1764 lands, a Starter key would read them here but not throughlist_shopping_cards.
This is a product call. If those fields should follow the page, the small change after #1764 is to have listCatalogProductsFor use #1764's plan check (export planIncludes) and, on Starter, either answer plan_limit or return those three fields as null. Then say so in the description and the guide.
Follow-ups
catalog_product_ai_statsruns for the whole brand on every page, as the PR notes. It returns only products with a visit or an order, so the rows stay few, but the function scans the brand's 30 days ofai_traffic_logs/eventseach call. Passing the page's IDs needs a migration. Time it on the largest brand first.- The REST routes don't validate
brand_idas a UUID; a bad one is a 404 throughbrandInOrg. That's fine, just different from the content routes' 400.
Summary
MCP next round, B6a: an AI assistant can read the brand's product catalog over MCP and REST: where the catalog comes from, and each product with its readiness, Merchant Center issues and AI results.
get_catalog_sources+GET /api/mcp/catalog/sources?brand_id=: the sources, as Settings › Integrations › Catalog shows them. Each has its kind (feed URL, upload or connected store), its URL or account and label, and its last sync (status,error,last_counts). Also the product count.list_catalog_products+GET /api/mcp/catalog/products?brand_id=&search=&limit=&offset=: products by title, paged (default 50, max 200;total,next_offset). An offset past the end returns an empty page with the righttotal.searchis a case-insensitive match on the title as written: every special character is escaped,*included, which PostgREST would otherwise read as a wildcard. Each product has:readiness(score, missing, unknown), from Product Visibility'sreadiness();merchant_center_issues: distinct issues by severity, each code + attribute counted once per product however many variants and destinations repeat it.nullwhen the brand has no Merchant Center source whose last sync went through, the same check the editor's shopping path uses (hasMerchantCenter, now shared). Variants and issues are read 100 ids at a time in 1,000-row pages, so a large page loses none.catalog_product_ai_stats).readCatalogProducts,readCatalogRows,readForIds), Settings'listCatalogSourcesbody andhasMerchantCentermoved tolib/shopping/catalog-read.ts, taking the client as a parameter.shopping.ts,catalog.tsandshopping-path.tsimport them; behaviour is unchanged. OneCARD_MAX_ROWSremains.brandInOrgruns first, so another organization's brand is not found (REST 404). Every service-role read is filtered to the brand, and the tests check it.titleandreadOnlyHint. The guide lists them (55 tools on this base) with the REST routes.Speed (for the maintainer):
catalog_product_ai_statsis computed for the whole brand on each call, as each Product Visibility page load does. On a 50,000-product catalog that should be measured against the MCP's 10-second rule. A follow-up can pass the page's product ids (p_product_ids; needs a migration).Merge note: #1713, #1715, #1727 and #1728 also add tools. Whichever merges later recounts
server.registerTool(, the gate test's read count and the guide's "N tools" on the merged main.Related issue
None (MCP next round, B6a; Product Visibility / #1270).
Type of change
feat— New featureHow to test
get_catalog_sourcesfor a brand with a catalog. It should match Settings › Integrations › Catalog.list_catalog_productswithsearch(try50%and5*: matched literally), then page withoffsetup tototaland beyond (empty page).merchant_center_issues: null./api/mcp/catalog/sources?brand_id=<id>and/api/mcp/catalog/products?brand_id=<id>&limit=10. Another organization's brand returns 404.Checklist
yarn lint(0 errors),yarn typecheckandyarn buildpass.yarn test: 553/557 files; the editor page, Library page, action table and Menu access section timed out under load and pass alone (284, 63, 10, 8). After the fixes:tsc --noEmit, eslint and prettier on the changed files, and 10 related files / 112 tests pass.🤖 Generated with Claude Code