Skip to content

fix(cli): bill a signed-in local MCP audit_website like squirrel audit (#628) - #631

Merged
nc9 merged 2 commits into
mainfrom
fix/mcp-audit-billing-628
Oct 10, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/mcp-audit-billing-628

Conversation

@nc9

@nc9 nc9 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Closes #628. Follow-up to #598, which documented this as a known gap.

No signed-in audit is free in credits. Signed in, the local MCP audit_website tool now runs a billed cloud audit at every level, exactly like squirrel audit. Signed out, offline, against a local or private host, and in quick_check, it still runs on your machine with no cloud call.

What changes

  • Billed like the CLI. A new module, apps/cli/src/mcp/billed-audit.ts, reuses the CLI's own pieces: resolveRegisterDecision, canStartCloudAudit, computePreflightAffordability, resolveCloudRendering, and registerRun / markRunning / finalizeRun.

    • The run is registered before the crawl (50-credit base).
    • Page renders carry its runId, so each page is billed once as part of the audit.
    • The run is closed with pagesAudited, which the server settles at 2 per page.
    • Quick renders the pages that need JavaScript in the cloud browser (hybrid). Surface and full render every page and run the cloud checks.
    • Before this, signed-in surface and full ran the cloud checks without registering anything.
  • Confirmation, as in the hosted run_audit. A tool call cannot prompt. An estimate over [cloud] confirm_threshold (default 50, so every audit) comes back as a normal result with status: "confirmation_required", the estimate (credits, pages, baseCredits, creditsPerPage), the balance and sufficient: true. The agent then calls again with confirm: true. The new confirm input is optional.

  • Refused clearly, never silently. Each of these returns an error naming the cost and the balance, and nothing is registered or rendered:

    • the balance can't cover one page;
    • [cloud] max_credits_per_audit is below one page;
    • the server refuses the register (for example 402).

    If the register request gets no answer, the error says the outcome is unknown and that a run which never starts is refunded when it is cleaned up. It does not promise that nothing was charged.

  • The page cap fits the credits. When the balance or the cap covers fewer pages than the level asks for, the audit runs on what it can pay for. The confirmation and the result both say so.

  • Each run is closed exactly once.

    • One lifecycle per run: the first terminal state wins.
    • An audit that didn't really happen (site down, blocked) closes as failed: no page charge, and the base is refunded. Its result says so.
    • A SIGINT/SIGTERM/SIGHUP to the MCP server closes every in-flight run as cancelled, including a run whose register is still in flight. After that no new register starts. Then the signal is re-raised.
    • The MCP server is long-lived and can run several audits at once, so each run gets its own heartbeat. It does not use the CLI's single module-level one.
  • The report reads as signed in. Billed results start with Billed cloud audit, run <id>: N audited pages, about X credits (50 base + 2 per audited page). The report carries the same cloudPlan / cloudMode / coverageMode stamps the CLI adds.

  • Still local, with no cloud call:

    • signed out
    • offline: true
    • a localhost or private-network URL
    • quick_check
    • an expired session or an unreachable API (the result says why)

    An env token the server rejects is a hard error, as in the CLI.

  • Cleanup. resolveCloudAvailability in mcp/cloud.ts is removed (unused now). upgradeUrl gains the mcp-audit attribution source.

  • Docs.

Behaviour changes to review

  1. Signed-in audit_website costs credits at every level: 50 + 2 per audited page, up to 100 at quick (25 pages). It used to be unbilled.
  2. Signed in, the first call no longer runs the audit. It returns confirmation_required unless confirm: true is passed, or [cloud] confirm_threshold is at or above the estimate. Agents that call audit_website once will now get an estimate back instead of a report.
  3. Signed-in quick now renders in the cloud browser (it was fully local before). Signed-in surface and full render every page. Before, they rendered only when [cloud] render was set.
  4. A signed-in call with too few credits is refused. Before, it ran locally, and at surface and full it also got the cloud checks.

Evidence

  • tsgo (apps/cli): clean. Root format:check and lint, test-suites --check, detect-secrets and make validate in docs all pass.
  • New apps/cli/tests/mcp/audit-website-billing.test.ts (16 tests): a real MCP client over an in-memory transport, a stubbed API, scratch paths.
    • Signed in:
      • without confirm: an estimate of 52 for 1 page and nothing registered;
      • quick with confirm: registers, renders the client-rendered page with runId, and closes completed with pagesAudited: 1;
      • surface registers;
      • a confirm_threshold above the estimate starts without confirm;
      • a balance of 10 is refused;
      • a cap of 10 is refused;
      • a 402 register is refused, with no render.
    • Closing:
      • a blocked site closes failed;
      • a shutdown during the balance read, during the register, and mid-audit each leaves exactly one terminal state;
      • cancelActiveMcpRuns closes in-flight runs as cancelled, once.
    • Local: signed out, offline: true, quick_check and a localhost URL make no cloud call.
  • Against main's audit-tools.ts, the file fails 8 of its 11 original tests (all the signed-in ones; the local ones pass on main too). These mutations each make a test fail: dropping getRunId, dropping the once-only close, dropping register tracking, and dropping the shutdown flag.
  • Every existing MCP test file passes: audit-level-options, docs-sync (tool descriptions carry no pitch words), server, protocol, cloud, scopes, report-tools, entity-tools, feedback-tools.
  • I ran three focused codex passes on the billing paths, which caught shutdown and finalize races. Their findings are fixed.

Agent skill

Yes. The squirrelscan agent skill should say that local MCP audit_website is billed when signed in, and that the agent must show the confirmation_required estimate to the user before calling again with confirm: true.

…#628

No signed-in audit is free in credits. Signed in, audit_website now runs
as a billed cloud audit at every level, through the same register
decision, affordability preflight and cloud rendering as squirrel audit:
the run is registered (50-credit base), renders are tagged with it, and
it is closed out with its audited pages (2 each). Before, it ran the
cloud checks at surface and full without registering anything.

A tool call cannot prompt, so an estimate over [cloud] confirm_threshold
comes back as confirmation_required and the agent calls again with
confirm: true, as with the hosted run_audit. A balance or per-audit cap
that cannot cover one page, or a register the server refuses, is refused
with the cost and the balance. Each billed run is closed exactly once,
including on a shutdown signal mid-audit or mid-register.

Signed out, offline, against a non-public host, and in quick_check the
tools still run locally with no cloud call.

Closes #628

Signed-off-by: Nik Cubrilovic <[email protected]>
@claude

claude Bot commented Oct 10, 2026

Copy link
Copy Markdown

Reviewed the diff (billed-audit.ts, audit-tools.ts, server.ts, cloud.ts, changelog). I read the source only and did not run the tests. The design reuses the CLI's billing pieces, closes each run once, and handles shutdown while a register is in flight. I found no blocking problems.

  • (medium) apps/cli/src/mcp/server.ts cancelBilledRunsOnSignal: the signal is only re-raised in .finally after cancelActiveMcpRuns() settles. If finalizeRun or markRunning hangs on a stalled network, SIGINT/SIGTERM never ends the process, because the handlers were already removed. Consider racing the cancel against a short timeout (a few seconds) before process.kill.
  • (low) billed-audit.ts confirm: true: the confirmation is advisory, since the same agent that receives confirmation_required can pass confirm: true on its own. This matches the hosted run_audit, but the tool description could state that the user must approve the spend.
  • (low) audit-tools.ts runWebsiteAudit: the heartbeat's pagesFailed is hardcoded to 0, and pagesFetched is only updated in the crawling phase. That is harmless for keep-alive, but the progress the server sees is not accurate.
  • (low) billed-audit.ts lifecycles map: entries are never evicted. The comment justifies this, but a long-lived server doing many audits still grows the map. Consider evicting after a closed run settles.
  • (nit) In planMcpAudit, the unreachable-API fallback runs locally with only a note. Whoever reads the note (agent or user) may not notice that the audit was not billed. The wording is fine, just make sure the note is surfaced prominently.
  • (nit) audit-website-billing.test.ts imports getGlobalConfigPath/setGlobalConfigPath and loadConfig from @/config in two separate import statements. Merge them.

Test coverage looks good: it uses a real MCP client against a stubbed API, and covers refusal, confirmation, shutdown and local paths. One gap: I did not see a test for the signal handler in server.ts itself, only for cancelActiveMcpRuns.

@claude

claude Bot commented Oct 10, 2026

Copy link
Copy Markdown

Review of #631 (bill signed-in local MCP audit_website). I read the diff only and did not run the tests. CI was still pending when I looked.

  • (medium) apps/cli/src/mcp/billed-audit.ts:316 — confirm is a plain tool input, so the calling agent can pass confirm: true on its first call and skip the estimate. The user then never sees the spend, and the agent skill is the only thing that prevents this. This is inherent to the design and matches the hosted run_audit. Still, consider documenting it as an explicit trust boundary, and/or making max_credits_per_audit the hard guard. Docs and the skill should say agents must not pass confirm before showing the estimate.
  • (medium) apps/cli/src/mcp/billed-audit.ts:527 — cancelActiveMcpRuns awaits network PATCHes (register wait, markRunning, finalizeRun) before re-raising the signal. If the API is slow or unreachable, Ctrl-C or SIGTERM can hang for the whole client retry budget. A second signal has no handler, because the once handlers were removed, so the default action would kill the process. That is acceptable, but a bounded overall timeout (e.g. Promise.race with ~5s) would make shutdown predictable.
  • (low) apps/cli/src/mcp/billed-audit.ts:156 — the lifecycles map never evicts entries. The comment justifies this as one small entry per billed audit, which is fine. A .finally cleanup after a run closes is still cheap, as long as the "late second close sends nothing" guarantee is kept some other way.
  • (low) apps/cli/src/mcp/tools/audit-tools.ts:204 — if renderAuditResult or the report stamping threw after runAudit succeeded, the run would not be closed, because only runAudit is inside the try. The stamping code is trivial, but moving finalizeMcpRun into a finally-style structure would make "closed exactly once" robust.
  • (low) Behaviour change: agents that call audit_website once now get confirmation_required instead of a report, and quick now costs credits. This is documented well in the PR description and CHANGELOG. A one-line mention in the tool's description is already there, so this is fine.
  • (nit) apps/cli/src/mcp/billed-audit.ts:205-206 — LOCAL_HINT and LOCAL_HINT_LOWER can be derived from one string.
  • (nit) finalizeMcpRun sets reportId: null. A short comment on why the run has no cloud report would help future readers.

Test coverage looks strong: 16 tests with a real MCP client, covering the race and shutdown paths. A test for the "audit throws" path (runAudit rejecting, so the run closes as failed and the error is rethrown) would round it out if it isn't already covered. I found no high or critical issues.

@nc9
nc9 merged commit 1d72459 into main Oct 10, 2026
15 checks passed
@nc9
nc9 deleted the fix/mcp-audit-billing-628 branch October 10, 2026 02:52
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.

Local MCP audit_website runs cloud checks without billing a signed-in audit

1 participant