Repository navigation
feat(mcp): read the prompts across their locations, as the Locations view shows them - #1727
Conversation
…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]>
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
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_locationsandGET /api/mcp/prompt-locationsgo throughauthenticateMcpRequest.brandInOrgchecks 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_summariesandprompt_position_summaries_json(both takep_brand_idand letservice_rolethrough), andincludedPositionPlatforms(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.positionSumsandgetPromptVisibilitySummariesboth read throughdbClient(), 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 columnsgetOrgPlanreads (ORG_PLAN_COLUMNS→planFromOrgRow), under the same member RLS on the page. Self-hosted still answersself_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.getTrackedPromptCountalready 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 inignored_locations. The tool schema capslocationsat 12 strings of 60 characters. - REST mirror. Same parameters as the tool.
daysmust 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.tsand the guard coverage test: see below.tscclean.
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
- 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 inignored_locations. The URL length bounds it, but.slice(0, 50)and the same 60-character cap as the tool would make the two match. - The REST route takes
brand_idwithout a UUID check: a bad id comes back as 404 "Brand not found" instead of the 400 the content-plan route gives. UsingcontentUuidthere too would be consistent. limit/offseton REST are clamped silently (limit=500gives 100) where the tool refuses them. That's fine, but the guide could say so.- Speed is still to be measured, as the PR says: 12 locations × 90 days on the largest brand, against the 10-second rule.
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=.regionLabel.tracked_inandvisibility_gap.insights: weakest and strongest location, and the prompt that differs most (prompt_id,low_at,high_at).locationsis 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 inignored_locations.getPromptLocationMatrix) runs under the service role insidewithDbClient(supabaseAdmin, …), afterbrandInOrgconfirms the brand is the key's (the pattern the 24h Insights cache uses).prompt.ts,positionSumsnow takesdbClient().includedPositionPlatformsnow reads the plan from the brand's organization row (organizations!brands_organization_id_fkey(...),planFromOrgRow): the same plangetOrgPlangives, in one query instead of two. A failed read is now logged.titleandreadOnlyHint; 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 featureHow to test
get_prompt_locationswith an API key. Rows and averages should match Tracked Prompts › Locations for the same period.locations: ["us", " de", "XX"]: US and DE columns;XXcomes back inignored_locations.limit: 100withoffsetpages through the prompts;next_offsetis null on the last page.GET /api/mcp/prompt-locations?brand_id=<id>&locations=US,DE. Another organization's brand returns 404.Checklist
yarn lint(0 errors),yarn typecheck,yarn test(558/558 files) andyarn buildpass on the first commit. After the review fixes:tsc --noEmit, eslint and prettier on the changed files, and 7 related files / 69 tests pass.prompt.ts)🤖 Generated with Claude Code