Repository navigation
fix(mcp): expose prompt-level visibility rate and fix competitor avg denominator (#491) - #502
Merged
Merged
Conversation
…denominator (#491) Three changes, all additive: 1. getVisibilitySummaryFor — adds visiblePrompts, promptCount and visibilityRatePct to totals. Counts distinct prompt_ids from the same filtered prompt_results rows (chatgpt-shopping excluded). A prompt is 'visible' when mention_count > 0 OR citation_count > 0, matching the visible_prompt_stats RPC and the Insights KPI card. 2. getVisibilitySummaryFor — fixes topCompetitors[].avgVisibility to divide by the total result-row count (same denominator as the brand) instead of the competitor's own mention count. Prevents inflated scores for competitors that only appear in their best runs (#478). 3. getCompetitorComparisonFor — maps brand_prompt_count, brand_visible_prompts and by_competitor[].visible_prompts from the competitor_aggregates RPC (already present since #490) to new output fields: visibility_rate_pct, visible_prompts, prompt_count on both brand and every competitor. All rates share brand_prompt_count as the denominator so entries are directly comparable, matching the dashboard leaderboard. avg_visibility_score kept for backward compat. Updates MCP tool descriptions so an assistant knows visibilityRatePct / visibility_rate_pct is the headline and avgVisibility is the intensity score. yarn lint, yarn typecheck, yarn format:check all pass from web/.
gkhngyk
approved these changes
Jul 22, 2026
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The dashboard's visibility narrative moved to a prompt-level Visibility Rate (distinct prompts a brand appeared in ÷ distinct prompts that produced results) in #490. The MCP data layer (
web/src/lib/mcp/data.ts) was still reporting the old all-rows score average, so an assistant connected over MCP would describe a brand's visibility as a near-zero score while the dashboard showed a meaningful percentage for the same window.Two specific issues:
getVisibilitySummaryForreturned onlytotals.avgVisibility(average visibility score across all result rows) with no rate fields. It also had a denominator bug:topCompetitors[].avgVisibilitydivided each competitor's score sum by their mention count, grading them only on their best runs — the same family of bugs fixed for the dashboard leaderboard in fix(insights): use the same denominator for brand and competitor averages #478.getCompetitorComparisonForcalledcompetitor_aggregatesbut only mapped the old average fields. Since feat(insights): visibility rate as the headline metric across Insights #490, the RPC already returnsbrand_prompt_count,brand_visible_prompts, andby_competitor[].visible_prompts— the rate was a pure mapping exercise with no backend change needed.Changes
All changes are additive — no existing response fields are renamed or removed.
web/src/lib/mcp/data.tsgetVisibilitySummaryFor— rate fields added tototalsAdded
visiblePrompts,promptCount, andvisibilityRatePcttototals. Rate fields are sourced from the same two RPCs that power the Insights KPI card, fired in parallel with the existing raw-row query:visible_prompt_stats→visiblePrompts(numerator: distinct prompts the brand appeared in, where appeared =mention_count > 0 OR citation_count > 0,chatgpt-shoppingexcluded)tracked_prompt_count→promptCount(denominator: distinct prompts that produced results, same shopping exclusion)Using the RPCs as the single source of truth guarantees the numbers match the dashboard for the same brand/window/filters — no JS-side approximation.
getVisibilitySummaryFor— competitor denominator bug fixedtopCompetitors[].avgVisibilitypreviously divided byMath.max(agg.mentions, 1)(the competitor's own mention count). It now divides byresults.length— the total result-row count, the same denominator used for the brand. Competitors that only appear in a fraction of prompts are graded on the same scale.getCompetitorComparisonFor— rate fields mapped from RPCUpdated the
CompetitorAggRpcinternal interface to includebrand_prompt_count,brand_visible_prompts, andby_competitor[].visible_prompts(fields the RPC has returned since #490 but were not being mapped).Added to
CompetitorComparisonOutputfor bothbrandand eachcompetitorentry:visibility_rate_pctvisible_prompts / prompt_count × 100(one decimal)visible_promptsprompt_countbrand_prompt_count) for all entriesAll entries use
brand_prompt_countas the denominator so rates are directly comparable — matching the Insights dashboard leaderboard semantics from #490.avg_visibility_scoreis kept as-is for backward compatibility.web/src/lib/mcp/server.tsUpdated the tool descriptions for
get_visibility_summaryandget_competitor_comparisonso an assistant knows:visibilityRatePct/visibility_rate_pctis the headline metric to use when describing brand visibilityavgVisibility/avg_visibility_scoreis the intensity score (kept for backward compatibility)Acceptance Criteria
get_visibility_summaryreturnstotals.visiblePrompts,totals.promptCount, andtotals.visibilityRatePct— numbers match the Insights KPI card for the same brand/window/filtersget_competitor_comparisonentries exposevisibility_rate_pct,visible_prompts, andprompt_countconsistent with the dashboard leaderboard, same denominator for all entriestopCompetitors[].avgVisibilityno longer divides by mention countyarn lint,yarn typecheck,yarn format:checkall pass fromweb/Out of Scope
A rate-based
get_visibility_trendis explicitly deferred — thevisibility_trend_aggregatesRPC would need distinct-prompt counters added first (follow-up issue).Related