Skip to content

feat(mcp): read the product catalog: its sources and its products - #1729

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-catalog
Oct 29, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-catalog

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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 right total. search is 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:
    • price, stock, GTIN, category and image, as Product Visibility reads them;
    • readiness (score, missing, unknown), from Product Visibility's readiness();
    • merchant_center_issues: distinct issues by severity, each code + attribute counted once per product however many variants and destinations repeat it. null when 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.
    • the last 30 days' AI visits, orders and revenue (catalog_product_ai_stats).
  • Moves, no copies: Product Visibility's catalog reads (readCatalogProducts, readCatalogRows, readForIds), Settings' listCatalogSources body and hasMerchantCenter moved to lib/shopping/catalog-read.ts, taking the client as a parameter. shopping.ts, catalog.ts and shopping-path.ts import them; behaviour is unchanged. One CARD_MAX_ROWS remains.
  • Org boundary: brandInOrg runs 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.
  • Both tools carry title and readOnlyHint. The guide lists them (55 tools on this base) with the REST routes.

Speed (for the maintainer): catalog_product_ai_stats is 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 feature

How to test

  1. With an API key, call get_catalog_sources for a brand with a catalog. It should match Settings › Integrations › Catalog.
  2. list_catalog_products with search (try 50% and 5*: matched literally), then page with offset up to total and beyond (empty page).
  3. A brand with a Merchant Center source that synced: products with item issues show the counts by severity. A brand without one: merchant_center_issues: null.
  4. REST: /api/mcp/catalog/sources?brand_id=<id> and /api/mcp/catalog/products?brand_id=<id>&limit=10. Another organization's brand returns 404.
  5. Product Visibility pages and Settings › Catalog behave as before.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web, before the review fixes: yarn lint (0 errors), yarn typecheck and yarn build pass. 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.
  • Tests added: catalog data layer (brand filters, paged issues, past-the-end, search escaping), both 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 catalog: the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 4 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]>
@mintlify

mintlify Bot commented Oct 28, 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 4243488.

Approve preview

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Text fix: get_catalog_sources no longer names a Shopify connector (there is none; the sources are a feed URL, an upload, or a Merchant Center account where the server offers it).

@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, 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 brandInOrg first, 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 by brand_id: products, variants, images, categories, issues, sources, the per-source counts and hasMerchantCenter. catalog_product_ai_stats skips its membership check for the service role and reads only p_brand_id.
  • Moves, no copies. readCatalogProducts, readCatalogRows, readForIds, readCatalogSources and hasMerchantCenter moved unchanged into lib/shopping/catalog-read.ts and take the client as a parameter. listCatalogSources, shopping.ts and shopping-path.ts pass the member's client, so the app behaves as before.
  • Search. literal() escapes every regex metacharacter before regexIMatch, so 50% and 5* 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. limit is 1–200. An offset past the end (PGRST103) comes back as an empty page with the right total. Variants and issues are read 100 IDs per request in 1,000-row pages, up to CARD_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 to catalog_categories and the hint is the column. No new embeds.
  • Read-only workspace: two get_/list_ tools with readOnlyHint: true and two GET routes, so nothing new to gate. subscription-guard-coverage.test.ts passes.
  • REST mirrors match the tools (same function, same caps; REST trims search to 200).
  • Docs: 55 tools on this base, matching registerTool count; 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 and subscription-guard-coverage (68 files / 463 tests) pass. tsc --noEmit is 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 with shopping_analytics. Once #1764 lands, a Starter key would read them here but not through list_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

  1. catalog_product_ai_stats runs 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 of ai_traffic_logs/events each call. Passing the page's IDs needs a migration. Time it on the largest brand first.
  2. The REST routes don't validate brand_id as a UUID; a bad one is a 404 through brandInOrg. That's fine, just different from the content routes' 400.

@gkhngyk
gkhngyk merged commit 5e2078a 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