Skip to content

feat(mcp): run a Site Audit via MCP (run_site_audit + get_site_audit), quota-charged - #305

Merged
gkhngyk merged 1 commit into
mainfrom
feature/mcp-site-audit
Jun 26, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/mcp-site-audit

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

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 existing generate_content_brief pattern.

Two new MCP tools:

  • run_site_audit (write) — inputs brand_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 endpoint POST /api/internal/site-audits, which resolves the brand's org, enforces enforceSiteAuditQuota, and starts the detached audit job. Returns the new audit id + status: "running". The audit runs async (~30s).
  • get_site_audit (read) — input audit_id. Pure DB read in the web layer (org-ownership enforced via the brands!inner(organization_id) join). Returns status, total score, per-category scores, evaluated signals, and AI fix recommendations. No quota consumed; the assistant polls this after run_site_audit.

REST parity under /api/mcp/site-audits (POST) and /api/mcp/site-audits/[id] (GET), same data-layer functions.

Implementation notes

  • Server (routes/audits.js) — extracted createAndRunAudit(brandId, url, orgId) (quota → insert running row → fire runAuditJob) and startSiteAuditForBrand(brandId, url) (resolves the brand's org, then runs). The dashboard POST /api/audits handler now shares createAndRunAudit, so there's a single path for quota + creation.
  • Server (server.js) — POST /api/internal/site-audits follows the exact shape of the existing /api/internal/content/:id/brief endpoint (CRON_SECRET bearer auth; 404 on unknown brand; relays PlanLimitError status — 402 inactive subscription / 429 quota exhausted — as quota_exceeded).
  • Feature/quota gating — every subscribed plan (Starter/Growth/Enterprise/self-hosted) includes content_optimization + a maxSiteAudits limit, and enforceSiteAuditQuota(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.
  • Read shape — get_site_audit reads the audit + signal rows directly in the web layer (no server round-trip), returning raw signals (key, category, status, score, evidence) plus the AI recommendations. The rubric display copy (label/what/why/howToFix) that the dashboard enriches via assembleAudit is intentionally not duplicated into the web layer — the actionable fixes live in recommendations.

Related issue

Closes #269

Type of change

  • feat — New feature

How to test

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

# 1) start an audit — charges one credit, returns the running audit
curl -s -X POST http://localhost:3000/api/mcp/site-audits \
  -H "Authorization: Bearer ans_XXX" -H "Content-Type: application/json" \
  -d '{"brand_id":"<BRAND_UUID>","url":"https://example.com/page"}'
# → { id, brandId, url, status: "running" }

# 2) ~30s later, read the result — no credit consumed
curl -s http://localhost:3000/api/mcp/site-audits/<AUDIT_ID> \
  -H "Authorization: Bearer ans_XXX"
# → status: "completed", totalScore, categoryScores, signals[], recommendations[]
  • A foreign/unknown brand_id returns not-found with no audit created.
  • When the monthly quota is exhausted, the tool returns the upgrade/limit message and no audit is created.
  • Via an MCP client: run_site_audit then poll get_site_audit until completed.

Checklist

  • Branch follows the naming convention (feature/)
  • Commits follow Conventional Commits
  • Server lint (eslint) + prettier --check pass on changed files
  • 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

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
@gkhngyk
gkhngyk merged commit 7b31088 into main Jun 26, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the feature/mcp-site-audit branch June 26, 2026 13:23
@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): run a Site Audit via MCP (run_site_audit + get_site_audit), quota-charged

1 participant