Skip to content

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

Closed

Description

@gkhngyk

Problem

The dashboard's visibility narrative moved to a prompt-level Visibility Rate — distinct prompts a brand appeared in ÷ distinct prompts that produced results (#490). The MCP data layer (web/src/lib/mcp/data.ts) still reports the old all-rows score average, so an assistant connected over MCP describes a brand's visibility as a near-zero score while the dashboard shows a meaningful percentage for the same window.

Specifically:

  1. getVisibilitySummaryFor returns totals.avgVisibility (all-rows average) and has no rate. It also has a real bug: topCompetitors[].avgVisibility divides the competitor's score sum by their mention count — the same skewed-denominator family of bugs fixed for the dashboard leaderboard in fix(insights): use the same denominator for brand and competitor averages #478 (competitors get graded only on their best runs).
  2. getCompetitorComparisonFor calls the competitor_aggregates RPC but only maps 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 is a pure mapping exercise, no backend change needed.

Fix

  • getVisibilitySummaryFor: add visiblePrompts, promptCount and visibilityRatePct to totals, matching the semantics of the visible_prompt_stats RPC (appeared = mention_count > 0 OR citation_count > 0, distinct prompt_id, chatgpt-shopping excluded — see web/src/lib/actions/tracking.ts getVisibilityRateKpi for the reference consumer). Calling the RPC via supabaseAdmin.rpc(...) with the brand filter is fine; keep the existing filters (dateFrom/dateTo/region/model) applied.
  • getVisibilitySummaryFor: fix topCompetitors[].avgVisibility to divide by the same result-row count as the brand (every filtered row), not the competitor's mention count.
  • getCompetitorComparisonFor: add visibility_rate_pct, visible_prompts and prompt_count per entry using the fields the RPC already returns, with the shared denominator (brand_prompt_count) for the brand and every competitor. Keep avg_visibility_score for backward compatibility.
  • Update the MCP tool descriptions so an assistant knows which number is the headline (rate) and which is the intensity score.
  • Out of scope (follow-up): a rate-based get_visibility_trend — the visibility_trend_aggregates RPC would need the same distinct-prompt counters first.

Acceptance criteria

  • get_visibility_summary returns the rate fields and its numbers match the Insights KPI card for the same brand/window/filters.
  • get_competitor_comparison entries expose visibility_rate_pct 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 pass from web/.

Getting started

Depends on #490 (the RPC fields this maps) — start after it merges. See CONTRIBUTING.md for setup. Comment here to claim the issue before opening a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions