Skip to content

fix(mcp): expose prompt-level visibility rate and fix competitor avg denominator (#491) - #502

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
Maqbool61:fix/491-mcp-visibility-rate
Jul 22, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
Maqbool61:fix/491-mcp-visibility-rate

Conversation

@Maqbool61

Copy link
Copy Markdown
Contributor

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:

  1. getVisibilitySummaryFor returned only totals.avgVisibility (average visibility score across all result rows) with no rate fields. It also had a denominator bug: topCompetitors[].avgVisibility divided 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.
  2. getCompetitorComparisonFor called competitor_aggregates but only mapped the old average fields. Since feat(insights): visibility rate as the headline metric across Insights #490, the RPC already returns brand_prompt_count, brand_visible_prompts, and by_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.ts

getVisibilitySummaryFor — rate fields added to totals

Added visiblePrompts, promptCount, and visibilityRatePct to totals. 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-shopping excluded)
  • 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 fixed

topCompetitors[].avgVisibility previously divided by Math.max(agg.mentions, 1) (the competitor's own mention count). It now divides by results.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 RPC

Updated the CompetitorAggRpc internal interface to include brand_prompt_count, brand_visible_prompts, and by_competitor[].visible_prompts (fields the RPC has returned since #490 but were not being mapped).

Added to CompetitorComparisonOutput for both brand and each competitor entry:

Field Description
visibility_rate_pct visible_prompts / prompt_count × 100 (one decimal)
visible_prompts Distinct prompts this entry appeared in
prompt_count Shared denominator (brand_prompt_count) for all entries

All entries use brand_prompt_count as the denominator so rates are directly comparable — matching the Insights dashboard leaderboard semantics from #490.

avg_visibility_score is kept as-is for backward compatibility.

web/src/lib/mcp/server.ts

Updated the tool descriptions for get_visibility_summary and get_competitor_comparison so an assistant knows:

  • visibilityRatePct / visibility_rate_pct is the headline metric to use when describing brand visibility
  • avgVisibility / avg_visibility_score is the intensity score (kept for backward compatibility)

Acceptance Criteria

  • get_visibility_summary returns totals.visiblePrompts, totals.promptCount, and totals.visibilityRatePct — numbers match the Insights KPI card for the same brand/window/filters
  • get_competitor_comparison entries expose visibility_rate_pct, visible_prompts, and prompt_count consistent with the dashboard leaderboard, same denominator for all entries
  • topCompetitors[].avgVisibility no longer divides by mention count
  • Existing response fields keep their names and meanings (additive change only)
  • yarn lint, yarn typecheck, yarn format:check all pass from web/

Out of Scope

A rate-based get_visibility_trend is explicitly deferred — the visibility_trend_aggregates RPC would need distinct-prompt counters added first (follow-up issue).

Related

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

Verified against the schema and the #490 RPCs — args match, the shared denominator semantics mirror the dashboard leaderboard, and the competitor average fix is exactly the #478 correction applied to the MCP layer. Clean, additive work. Thanks!

@gkhngyk
gkhngyk merged commit fec153f into ansvisor:main Jul 22, 2026
4 checks passed
@Maqbool61
Maqbool61 deleted the fix/491-mcp-visibility-rate branch July 22, 2026 12:21
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.

MCP: expose the prompt-level visibility rate (and fix the competitor average denominator)

2 participants