Skip to content

feat(mcp): read the prompts across their locations, as the Locations view shows them - #1727

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-prompt-locations
Oct 29, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-prompt-locations

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

MCP next round, B7: compare tracked prompts across their locations over MCP and REST, the way Tracked Prompts' Locations view does.

  • get_prompt_locations + GET /api/mcp/prompt-locations?brand_id=&locations=US,DE&days=7|30|90&date_from=&date_to=&limit=&offset=.
    • Each tracked prompt in each location side by side: visibility, mentions, citations, average position and runs. A location is a country, a US state or a city, named with regionLabel.
    • Per prompt: tracked_in and visibility_gap.
    • Per location: the averages.
    • The view's cards as insights: weakest and strongest location, and the prompt that differs most (prompt_id, low_at, high_at).
    • locations is optional. Codes are trimmed and upper-cased; at most 12 are used. Without it, the brand's most-tracked locations are used. Codes that name no location, or come after the twelfth, are returned in ignored_locations.
    • Paged by prompt: default 50, max 100. Prompts are read in chunks of 1,000.
  • Same reads as the panel, no copy. The matrix (getPromptLocationMatrix) runs under the service role inside withDbClient(supabaseAdmin, …), after brandInOrg confirms the brand is the key's (the pattern the 24h Insights cache uses).
    • In prompt.ts, positionSums now takes dbClient().
    • includedPositionPlatforms now reads the plan from the brand's organization row (organizations!brands_organization_id_fkey(...), planFromOrgRow): the same plan getOrgPlan gives, in one query instead of two. A failed read is now logged.
    • The panel still reads with the member's RLS client.
  • The tool carries title and readOnlyHint; the guide lists it (54 tools on this base).

Speed: with 12 locations and 90 days this is the same work as opening the Locations view at that size (two reads per location, three at a time), and the MCP recomputes it per page because the averages and cards need every prompt. It should be measured on the largest brand against the MCP's 10-second rule; if it's slow, the default location count can drop.

Merge note: #1713, #1715 and the catalog PR 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, B7; PROMPT-015).

Type of change

  • feat — New feature

How to test

  1. For a brand tracking prompts in several locations, call get_prompt_locations with an API key. Rows and averages should match Tracked Prompts › Locations for the same period.
  2. locations: ["us", " de", "XX"]: US and DE columns; XX comes back in ignored_locations.
  3. limit: 100 with offset pages through the prompts; next_offset is null on the last page.
  4. REST: GET /api/mcp/prompt-locations?brand_id=<id>&locations=US,DE. Another organization's brand returns 404.
  5. Tracked Prompts › Table and Locations views and the positions panel still show the same engines for the plan.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: yarn lint (0 errors), yarn typecheck, yarn test (558/558 files) and yarn build pass on the first commit. After the review fixes: tsc --noEmit, eslint and prettier on the changed files, and 7 related files / 69 tests pass.
  • Tests added (data layer incl. code normalising and ignored codes, REST route, the plan read in prompt.ts)
  • 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 2 commits October 29, 2026 00:10
…view shows them

get_prompt_locations (REST GET /api/mcp/prompt-locations, MCP plan B7):
each tracked prompt in each location side by side, as Tracked Prompts'
Locations view reads it (getPromptLocationMatrix): visibility, mentions,
citations, average position and runs per prompt and country, US state
or city, with each location's averages and the view's cards (weakest
and strongest location, the prompt that differs most). The locations
asked (at most 12), else the most tracked; paged by prompt.

The matrix is read under the service role once the brand is the key's
(withDbClient, as the 24h Insights cache reads): its position reads now
take dbClient, and the engines the plan includes are read from the
brand's organization row (planFromOrgRow, the FK named), the same plan
getOrgPlan gave the member.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…of the comparison

- Upper-case and trim the `locations` given before comparing them: the
  prompts keep `US`, so `us` or ` DE` read an empty column before.
- `ignored_locations`: the codes given that name no location, or come
  past the twelfth (the REST mirror now passes every code on).
- The cards (`insights`) in snake_case, as the rest of the answer.
- At most 100 prompts a page (default 50), in the tool, docs and REST.
- Log a failed read of the brand's plan for the Positions column, which
  then reads — without saying why.
- Tests: codes in any case, the codes left out, the page cap, and the
  plan read from the organization the brand's row embeds.

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 e6e956b.

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

Reviewed this PR's own change (5e945c6, branched from ce9a3dc, so its own diff is origin/main...5e945c64). No migration. Read-only tool, no write.

What I checked

  • Auth and scope. get_prompt_locations and GET /api/mcp/prompt-locations go through authenticateMcpRequest. brandInOrg checks the brand against the key's organization before anything is read, and an id of another organization (or one that isn't a UUID) reads as 404. Every read after that filters on the brand: the prompt list (prompt_sets.brand_id), prompt_visibility_summaries and prompt_position_summaries_json (both take p_brand_id and let service_role through), and includedPositionPlatforms (the brand by id).
  • withDbClient(supabaseAdmin, …). The service role is scoped through AsyncLocalStorage only for the matrix call, so a member's own call of the 'use server' export still gets their RLS client. positionSums and getPromptVisibilitySummaries both read through dbClient(), so no read falls back to a sessionless client under the MCP.
  • includedPositionPlatforms. One query instead of two, same answer: it reads the organization through the brand row with the same columns getOrgPlan reads (ORG_PLAN_COLUMNS → planFromOrgRow), under the same member RLS on the page. Self-hosted still answers self_hosted. A failed read is now logged instead of reading as "no engines".
  • PostgREST embeds.
    • brands → organizations!brands_organization_id_fkey(...): 1 foreign key between the two (brands_organization_id_fkey), and it's hinted anyway.
    • prompts → prompt_sets!inner(brand_id): 1 foreign key (prompts_prompt_set_id_fkey), none the other way. getTrackedPromptCount already uses the same embed.
  • Bounds. At most 12 locations are compared (MATRIX_REGIONS), with the heavy reads run three at a time as in the view. Prompts come in pages of 1,000 until a short page. Output is paged by prompt: default 50, max 100, so at most 100 × 12 cells. Location codes are trimmed and upper-cased, and invalid ones come back in ignored_locations. The tool schema caps locations at 12 strings of 60 characters.
  • REST mirror. Same parameters as the tool. days must be 7/30/90, and dates must parse.
  • Docs. The tool row and REST line match. The count of 54 is right on this base.
  • Tests on this tree: web src/lib/mcp, src/app/api/mcp, src/lib/actions/prompt.test.ts and the guard coverage test: see below. tsc clean.

Merging on current main

Merges clean on ce9a3dc. With #1715 it conflicts in web/src/lib/mcp/server.ts and docs/guides/mcp-server.mdx. subscription-gate.test.ts merges silently to 40 reads when it should be 41: the test fails, so whoever merges second must bump it. With #1732 it merges clean, and #1732's server.test.ts passes with this tool (title, readOnlyHint: true, openWorldHint: false).

Follow-ups

  1. The REST route doesn't apply the tool's caps on locations. Any number of comma-separated codes of any length are accepted, and every one not compared comes back in ignored_locations. The URL length bounds it, but .slice(0, 50) and the same 60-character cap as the tool would make the two match.
  2. The REST route takes brand_id without a UUID check: a bad id comes back as 404 "Brand not found" instead of the 400 the content-plan route gives. Using contentUuid there too would be consistent.
  3. limit/offset on REST are clamped silently (limit=500 gives 100) where the tool refuses them. That's fine, but the guide could say so.
  4. Speed is still to be measured, as the PR says: 12 locations × 90 days on the largest brand, against the 10-second rule.

@gkhngyk
gkhngyk merged commit 075cfba 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