Skip to content

feat(mcp): list_site_audits + get_site_audit_quota read tools - #306

Merged
gkhngyk merged 2 commits into
mainfrom
feature/mcp-site-audit-read-tools
Jun 26, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/mcp-site-audit-read-tools

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two read-only MCP tools that complement run_site_audit / get_site_audit (#269, merged):

  • list_site_audits — input brand_id. Returns the brand's recent audits (id, url, status, total score, signals evaluated/total, created_at) so an agent can find a past audit by url/date instead of needing the id. Ownership-checked DB read.
  • get_site_audit_quota — returns the org's monthly Site Audit allowance ({ used, limit, remaining }; limit: -1 = unlimited) so an agent can check remaining credits before calling run_site_audit.

Both are pure reads — neither charges the quota.

Implementation

  • web/src/lib/mcp/data.ts — listSiteAuditsFor(auth, brandId) (ownership via the brand belonging to the caller's org, then a site_audits read) and getSiteAuditQuotaFor(auth).
  • Quota single-sourcing — getOrgPlan in the web layer uses the cookie-based Supabase client, which isn't available in the MCP (API-key) context, and re-deriving plan limits + month-boundary usage in the web layer would risk drifting from the server. So get_site_audit_quota delegates to the server's existing getSiteAuditQuotaStatus via a new CRON_SECRET internal endpoint GET /api/internal/site-audit-quota — one source of truth for plan limits + usage counting, same trust model as the run_site_audit internal call.
  • web/src/lib/mcp/server.ts — registers list_site_audits + get_site_audit_quota.
  • REST parity — GET /api/mcp/site-audits?brand_id=… (list, added to the existing route) and GET /api/mcp/site-audits/quota (the static quota segment is matched before the dynamic [id] route).

Related issue

Closes #270

Type of change

  • feat — New feature

How to test

With an ans_ API key for an org that owns a brand:

# recent audits for a brand (no credit consumed)
curl -s "http://localhost:3000/api/mcp/site-audits?brand_id=<BRAND_UUID>" \
  -H "Authorization: Bearer ans_XXX"
# → [{ id, url, status, totalScore, signalsEvaluated, signalsTotal, createdAt }, …]

# monthly allowance (no credit consumed)
curl -s http://localhost:3000/api/mcp/site-audits/quota \
  -H "Authorization: Bearer ans_XXX"
# → { used, limit, remaining }   (limit -1 = unlimited)
  • A foreign/unknown brand_id returns not-found from list_site_audits.
  • Neither tool inserts into site_audit_usage (no quota charged).
  • Via an MCP client: list_site_audits finds a prior audit; get_site_audit_quota reports remaining credits before a run_site_audit.

Checklist

  • Branch follows the naming convention (feature/)
  • Commits follow Conventional Commits
  • Server lint (eslint) + prettier --check pass; server test suite passes (vitest run — 93/93)
  • Web yarn typecheck, eslint, prettier --check pass on changed files
  • Changes are focused — one concern per PR

Two read-only MCP tools that complement run_site_audit / get_site_audit (#269):

- list_site_audits (brand_id): ownership-checked DB read of the brand's recent
  site_audits (id, url, status, total score, signals, created_at) so an agent
  can find a past audit by url/date without the id.
- get_site_audit_quota: the org's monthly Site Audit allowance
  ({ used, limit, remaining }; -1 = unlimited). Delegates to the server's
  getSiteAuditQuotaStatus via a new CRON_SECRET internal endpoint
  GET /api/internal/site-audit-quota so plan-limit + usage logic stays
  single-sourced on the server.
- Neither charges the quota. REST parity: GET /api/mcp/site-audits?brandId=
  and GET /api/mcp/site-audits/quota.

Closes #270

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds two new read-only Site Audit MCP capabilities so agents can (1) discover recent audits for a brand and (2) check remaining monthly audit credits without consuming quota, plus REST endpoints for parity.

Changes:

  • Register new MCP tools: list_site_audits and get_site_audit_quota.
  • Implement data-layer reads for recent audits and quota status (quota delegated to server source-of-truth via CRON_SECRET internal endpoint).
  • Add REST endpoints under /api/mcp/site-audits for listing and /api/mcp/site-audits/quota for quota.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
web/src/lib/mcp/server.ts Registers the two new MCP read tools and returns JSON payloads as text.
web/src/lib/mcp/data.ts Adds listSiteAuditsFor DB read and getSiteAuditQuotaFor internal-quota fetch.
web/src/app/api/mcp/site-audits/route.ts Adds GET list endpoint alongside existing POST run endpoint.
web/src/app/api/mcp/site-audits/quota/route.ts Adds REST quota endpoint mirroring get_site_audit_quota.
server/src/server.js Adds CRON_SECRET-protected internal GET /api/internal/site-audit-quota.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +15 to +18
const brandId = new URL(req.url).searchParams.get('brandId');
if (!brandId) {
return NextResponse.json({ error: 'brandId is required' }, { status: 400 });
}
Comment thread web/src/lib/mcp/data.ts Outdated
Comment on lines +558 to +564
const { data } = await supabaseAdmin
.from('site_audits')
.select('id, url, status, total_score, signals_evaluated, signals_total, created_at')
.eq('brand_id', brandId)
.order('created_at', { ascending: false })
.limit(50);

Comment thread server/src/server.js Outdated
Comment on lines +274 to +278
const { orgId } = req.query;
if (!orgId) {
return res.status(400).json({ success: false, message: 'orgId is required' });
}
const quota = await getSiteAuditQuotaStatus(orgId);
- list_site_audits REST: use brand_id query param to match the other MCP REST
  routes (topics/prompts/citations/...), not brandId
- listSiteAuditsFor: surface Supabase query errors instead of returning a
  misleading empty list
- internal site-audit-quota: coerce a possibly-array req.query.orgId to a
  single string before use
@gkhngyk
gkhngyk merged commit 0ac973a into main Jun 26, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the feature/mcp-site-audit-read-tools branch June 26, 2026 15:39
@gkhngyk gkhngyk mentioned this pull request Jul 6, 2026
5 tasks done
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): list_site_audits + get_site_audit_quota read tools

2 participants