Repository navigation
feat(mcp): run a Site Audit via MCP (run_site_audit + get_site_audit), quota-charged - #305
Merged
Merged
Conversation
Expose Site Audit through the MCP server so an AI assistant can trigger an audit and read the result, charging the same monthly site_audit_usage quota as the dashboard. Mirrors the generate_content_brief pattern. - run_site_audit (write): ownership-checked in the web layer, then calls a new CRON_SECRET-authed internal endpoint POST /api/internal/site-audits which resolves the brand's org, enforces enforceSiteAuditQuota, and starts the detached audit job. Returns the running audit id + status. - get_site_audit (read): pure DB read in the web layer (org-ownership via the brands!inner join), returns status + total/category scores + signals + AI recommendations. No quota consumed. - REST parity under /api/mcp/site-audits (POST) and /api/mcp/site-audits/[id] (GET). - Server: extract createAndRunAudit() + startSiteAuditForBrand() in routes/audits.js (the dashboard POST handler now shares createAndRunAudit). Closes #269
4 tasks
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
Exposes Site Audit through the MCP server so an AI assistant (Claude Desktop / Code / Cursor) can trigger an audit and read the result — charging the same monthly Site Audit quota (
site_audit_usage) as the dashboard. Mirrors the existinggenerate_content_briefpattern.Two new MCP tools:
run_site_audit(write) — inputsbrand_id+url. The web layer verifies the caller's org owns the brand first (wrong/unknown brand → not-found, no audit created, no quota charged), then calls a new CRON_SECRET-authed internal endpointPOST /api/internal/site-audits, which resolves the brand's org, enforcesenforceSiteAuditQuota, and starts the detached audit job. Returns the new audit id +status: "running". The audit runs async (~30s).get_site_audit(read) — inputaudit_id. Pure DB read in the web layer (org-ownership enforced via thebrands!inner(organization_id)join). Returns status, total score, per-category scores, evaluated signals, and AI fix recommendations. No quota consumed; the assistant polls this afterrun_site_audit.REST parity under
/api/mcp/site-audits(POST) and/api/mcp/site-audits/[id](GET), same data-layer functions.Implementation notes
routes/audits.js) — extractedcreateAndRunAudit(brandId, url, orgId)(quota → insertrunningrow → firerunAuditJob) andstartSiteAuditForBrand(brandId, url)(resolves the brand's org, then runs). The dashboardPOST /api/auditshandler now sharescreateAndRunAudit, so there's a single path for quota + creation.server.js) —POST /api/internal/site-auditsfollows the exact shape of the existing/api/internal/content/:id/briefendpoint (CRON_SECRET bearer auth; 404 on unknown brand; relaysPlanLimitErrorstatus — 402 inactive subscription / 429 quota exhausted — asquota_exceeded).content_optimization+ amaxSiteAuditslimit, andenforceSiteAuditQuota(orgId)already throws 402 for orgs without an active/trialing subscription. So the quota check authoritatively gates the MCP path without a separate feature middleware.get_site_auditreads the audit + signal rows directly in the web layer (no server round-trip), returning raw signals (key,category,status,score,evidence) plus the AIrecommendations. The rubric display copy (label/what/why/howToFix) that the dashboard enriches viaassembleAuditis intentionally not duplicated into the web layer — the actionable fixes live inrecommendations.Related issue
Closes #269
Type of change
feat— New featureHow to test
With an
ans_API key for an org that owns a brand:brand_idreturns not-found with no audit created.run_site_auditthen pollget_site_audituntilcompleted.Checklist
feature/)eslint) +prettier --checkpass on changed filesvitest run— 93/93)yarn typecheck,eslint,prettier --checkpass on changed files