Skip to content

feat(mcp): expose list_citations tool + REST endpoint - #117

Merged
gkhngyk merged 1 commit into
mainfrom
feat/mcp-list-citations
May 28, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feat/mcp-list-citations

Conversation

@gkhngyk

@gkhngyk gkhngyk commented May 28, 2026

Copy link
Copy Markdown
Contributor

Closes #97. Fourth A-group MCP read tool — exposes citation URLs and domains the existing dashboard already surfaces.

What

  • New MCP tool: `list_citations(brand_id, ...)` → totals + source-type breakdown + top domains + top URLs, each classified by source category (news / review / owned / social / forum / competitor / you).
  • New REST endpoint: `GET /api/mcp/citations` with query-param mirror of the MCP args.

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

  • `brand_id` (required)
  • `date_from` / `date_to` — ISO timestamps
  • `model` — single slug or comma-separated list (matches the family-filter pattern of other tools)
  • `region` — region code
  • `topic_id` — restrict to one topic
  • `limit` — caps top_domains / top_urls (default 50, max 200)

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

File Change
`web/src/lib/mcp/data.ts` New `listCitationsFor` data fn + `ListCitationsParams` / `CitationsOverviewOutput` types
`web/src/lib/mcp/server.ts` Registers the `list_citations` tool with a zod schema
`web/src/app/api/mcp/citations/route.ts` New GET route sharing the same data fn

How verified

  • `yarn typecheck` + `yarn format:check` clean
  • Logic mirrors the production `getCitationsOverview` byte-for-byte (URL normalization, classification, aggregation) — same code path users already rely on for the citations dashboard
  • Local smoke (curl) covers: 200 happy path with `limit`, 200 with `date_from`, 401 (missing key), 400 (missing `brand_id` and invalid `limit`), 404 (wrong-org brand)

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.

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.
@gkhngyk
gkhngyk merged commit dd4d6c1 into main May 28, 2026
2 checks passed
@gkhngyk
gkhngyk deleted the feat/mcp-list-citations branch May 28, 2026 19:42
gkhngyk added a commit that referenced this pull request May 31, 2026
Bundles the in-product Agent epic (#120, #121), the BYOK rollout on
cloud, the MCP toolkit expansion (#109, #110, #116, #117, #118), the
insights performance fix (#114), and the invite-accept flow rebuild
(#127, #129, #130) into a tagged release.

See CHANGELOG.md for the full notes.
gkhngyk added a commit that referenced this pull request May 31, 2026
Bundles the in-product Agent epic (#120, #121), the BYOK rollout on
cloud, the MCP toolkit expansion (#109, #110, #116, #117, #118), the
insights performance fix (#114), and the invite-accept flow rebuild
(#127, #129, #130) into a tagged release.

See CHANGELOG.md for the full notes.

This branch was successfully deployed

1 active deployment
Preview — 5f2ce598 Deployed May 28, 2026 by vercel[bot]
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.

feat(mcp): expose list_citations tool + REST endpoint

1 participant