Skip to content

feat(mcp/agent): expose prompt-level performance tool (get_prompt_performance) #139

Closed

Description

@gkhngyk

What

Add a prompt-level performance tool so the in-product agent (and external MCP clients) can answer questions like "what's the best-performing prompt today?", "which prompts went down this week?", "show me the top 10 prompts by mentions".

Today there's no path to those answers — the data lives in prompt_results row-by-row but no aggregation tool exposes it grouped by prompt. The agent either declines (best case, per its "acknowledge tool gaps" rule) or hallucinates a ranking from list_prompts metadata (worst case — the symptom that surfaced this gap).

Where it sits in the current tool surface

Tool Returns Per-prompt?
list_prompts text, topic, platforms, regions, is_active no scores
get_visibility_summary brand-level totals brand-aggregate only
get_visibility_trend time-series across all prompts per-bucket only
get_competitor_comparison competitor breakdown brand vs competitors only
list_citations URLs / domains citation-level only
(missing) per-prompt performance —

Proposed tool

get_prompt_performance(brandId, dateFrom?, dateTo?, topicId?, sortBy?, order?, limit?, model?, region?) — aggregates prompt_results by prompt_id over the given window. Mirrors the existing tool shape (org-scoped via mcp-auth, date-range expansion already handled by lib/dates.ts).

Input

Field Type Notes
brand_id uuid required
date_from string optional, ISO or bare date
date_to string optional, ISO or bare date
topic_id uuid optional — filter to one topic
sort_by 'visibility' | 'mentions' | 'citations' | 'appearances' default 'visibility'
order 'desc' | 'asc' default 'desc'
limit number default 10, max 100
model string optional, single slug or csv
region string optional

Output

{
  brand: { id, name },
  window: { dateFrom: string | null, dateTo: string | null },
  prompts: Array<{
    prompt_id: string;
    prompt_text: string;
    topic_name: string | null;
    result_count: number;          // number of rows in prompt_results in the window
    avg_visibility: number;        // 0–100
    total_mentions: number;
    total_citations: number;
    avg_competitor_score: number;  // for context
  }>
}

Why a new RPC (vs. a JS-side aggregate)

Same reasoning as #93 / #114 — pulling rows into Node for per-brand-prompt aggregation gets expensive on orgs with many prompts × platforms × dates. A small prompt_performance_aggregates SQL function lets Postgres do the grouping, mirrors the existing insights_aggregates / visibility_trend_aggregates pattern, and stays fast.

Where it plugs in

  • New migration supabase/migrations/000xx_prompt_performance_aggregates.sql defining the SQL function (org-membership check inside; mirror the existing aggregate functions' shape).
  • New data function getPromptPerformanceFor(auth, params) in web/src/lib/mcp/data.ts that calls the RPC. Apply expandDateToEndOfDay to dateTo per lib/dates.ts.
  • New REST endpoint web/src/app/api/mcp/prompt-performance/route.ts matching the existing REST pattern (api key auth → getPromptPerformanceFor).
  • New tool get_prompt_performance in web/src/lib/agent/tools.ts — thin wrapper around the data function.
  • Add the tool to the MCP server registration in web/src/lib/mcp/server.ts (whichever file enumerates the public MCP tools).
  • Update AGENT_SYSTEM_PROMPT so the model knows to call this tool for "best / worst / top / bottom prompt" questions, and to pair it with render_chart(type='bar') when a chart helps.

Acceptance criteria

  • Asking the agent "what's my best-performing prompt today?" returns a real top-N list backed by prompt_results, not a hallucination from prompt metadata
  • Asking "which prompts dropped this week?" works (sort_by='visibility', order='asc', paired with the model interpreting "dropped" against the window)
  • The bar chart variant renders when the model decides a chart helps
  • Tool surfaces in Claude Desktop / Cursor / Zed via the existing MCP server
  • pnpm tsc --noEmit + yarn lint + pnpm format:check clean
  • Wrong-org calls return null / 404 like the other ownership-scoped tools

Out of scope

  • A per-prompt time-series tool (would be a separate get_prompt_trend). The single-window aggregate this issue proposes is enough for the "best of today" / "worst of week" class of questions; trend per-prompt comes when someone asks for it.
  • Promoting the tool to a UI page. The dashboard's Prompts page already exists; this issue is purely the analyst surface (agent + MCP).
  • Cross-brand comparison.

Activity

  1. added
    enhancement
    help wanted
    on Jun 1, 2026
  2. Pallavikumarimdb commented on Jun 4, 2026

    @Pallavikumarimdb
    Contributor

    @gkhngyk , Can I pick this one?

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

    enhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions