Repository navigation
feat(mcp): expose list_citations tool + REST endpoint - #117
Merged
Merged
Conversation
Adds the fourth A-group MCP read tool: cited URLs and domains classified by source type (news / review / owned / social / forum / competitor / you), plus totals and a per-source breakdown. Ports the JS-side aggregation from getCitationsOverview in actions/citations.ts (citations live as JSONB inside prompt_results.citations and need the classifyDomain + classifyArticleType helpers to label each URL, so this can't be folded into a Postgres aggregate the way the competitor / SoV surfaces were in #114). Ownership-check first — the prompt_results scan only runs after the brand belongs to the caller's org. Wrong-org or missing brand returns null (→ 404) before any rows are pulled. Shape mirrors the existing CitationsOverview but slimmed for tool consumption: - totals (domains, urls, citations, results_with_citations, avg) - source_type_breakdown (per category count + pct) - top_domains (default 50, max 200) with citation count, results citing, usage %, models, article-type guesses - top_urls (same caps) with title + article type Reuses the URL-normalization (strip query/fragment + trailing slash) so the same article cited with tracking params aggregates as one URL. - web/src/lib/mcp/data.ts: listCitationsFor(auth, params) - web/src/lib/mcp/server.ts: registers list_citations - web/src/app/api/mcp/citations/route.ts: GET handler Closes #97.
Closed
7 tasks
This branch was successfully deployed
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.
Closes #97. Fourth A-group MCP read tool — exposes citation URLs and domains the existing dashboard already surfaces.
What
Shape of the response
```json
{
"totals": {
"domains": 42, "urls": 87, "citations": 234,
"results_with_citations": 156, "avg_citations_per_result": 1.5
},
"source_type_breakdown": [
{ "category": "news", "count": 12, "pct": 28.5 },
{ "category": "review", "count": 8, "pct": 19.0 }
],
"top_domains": [
{
"domain": "...", "category": "news",
"total_citations": 23, "results_citing": 18, "usage_pct": 11.5,
"models": ["..."],
"article_types": [{ "type": "article", "count": 15 }]
}
],
"top_urls": [
{
"url": "https://...", "domain": "...", "category": "news",
"title": "...", "total_citations": 5, "results_citing": 4,
"usage_pct": 2.6, "article_type": "article"
}
]
}
```
Implementation
Ports the JS-side aggregation from `getCitationsOverview` in `actions/citations.ts` — citations live as JSONB inside `prompt_results.citations` and each URL needs the `classifyDomain` + `classifyArticleType` helpers to label it, so this can't be folded into a Postgres aggregate the way the competitor / SoV surfaces were in #114. Re-uses the URL normalization (strip query/fragment + trailing slash) so the same article cited with different tracking params still aggregates as one URL.
Auth + safety
Ownership check first via `supabaseAdmin` — wrong-org or missing `brand_id` returns null (→ 404) before any rows are pulled. Same pattern as the other MCP data fns.
Why a snapshot, not a delta
V1 returns a single-window snapshot — consistent with `get_visibility_summary`, `get_competitor_comparison` (just merged in #116), `list_topics`, `list_prompts`. A caller wanting a delta issues a second tool call with an earlier window and diffs in its own reducer.
Filters
Two existing UI-side toggles are intentionally out of the MCP shape: `excludeOwnDomain` and `competitorOnly`. Both are post-filters an LLM caller can apply on the returned data; baking them into the tool surface would add opinionated options without expanding capability.
Files
How verified
Up next
After this lands, #96 (`get_visibility_trend`) is the last A-group tool needed before the assistant epic #94 can start in earnest.