Repository navigation
feat(mcp): list_site_audits + get_site_audit_quota read tools - #306
Merged
Merged
Conversation
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
There was a problem hiding this comment.
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_auditsandget_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-auditsfor listing and/api/mcp/site-audits/quotafor 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 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 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
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.
Summary
Two read-only MCP tools that complement
run_site_audit/get_site_audit(#269, merged):list_site_audits— inputbrand_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 callingrun_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 asite_auditsread) andgetSiteAuditQuotaFor(auth).getOrgPlanin 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. Soget_site_audit_quotadelegates to the server's existinggetSiteAuditQuotaStatusvia a new CRON_SECRET internal endpointGET /api/internal/site-audit-quota— one source of truth for plan limits + usage counting, same trust model as therun_site_auditinternal call.web/src/lib/mcp/server.ts— registerslist_site_audits+get_site_audit_quota.GET /api/mcp/site-audits?brand_id=…(list, added to the existing route) andGET /api/mcp/site-audits/quota(the staticquotasegment is matched before the dynamic[id]route).Related issue
Closes #270
Type of change
feat— New featureHow to test
With an
ans_API key for an org that owns a brand:brand_idreturns not-found fromlist_site_audits.site_audit_usage(no quota charged).list_site_auditsfinds a prior audit;get_site_audit_quotareports remaining credits before arun_site_audit.Checklist
feature/)eslint) +prettier --checkpass; server test suite passes (vitest run— 93/93)yarn typecheck,eslint,prettier --checkpass on changed files