Skip to content

feat(mcp): read the demo brand and refuse its writes over MCP (demo brand, part 6) - #1739

Merged
gkhngyk merged 158 commits into
ansvisor:mainfrom
cihan-gyk:feature/demo-brand-mcp
Oct 29, 2026
Merged

gkhngyk merged 158 commits into
ansvisor:mainfrom
cihan-gyk:feature/demo-brand-mcp

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Demo brand, part 6 of 7 (web, MCP). An AI assistant with an API key reads the demo brand like any brand, and writes, runs or spends nothing on it, whatever the plan.

  • The MCP tools run in the web app, so the API's 402 demo_brand didn't reach them. Now, on Ansvisor Cloud, every tool that writes, runs or spends answers the demo brand with the web app's DEMO_BRAND body, which tells the agent the same thing runs on the workspace's own brand once a plan is picked. The check runs in gateWriteTools, before the read-only check.
  • lib/mcp/demo-gate.ts: WRITE_TOOL_BRANDS says how each of the 14 write tools finds the brand it acts on (a suggestion, opportunity, task, prompt, answer or draft, read back to its brand).
    • demo-gate.test.ts fails on a new write tool until it is added there, and checks that each one refuses the demo brand.
    • Main's newer tools (get_me, get_ai_usage, get_action_scorecard) only read, so they stay open.
  • The costly calls also refuse it inside their data functions, beside assertSubscriptionActive, so their REST routes (402 demo_brand) and the in-product agent do too: a brief, a Site Audit, an Improve action.
  • list_brands marks the demo brand (is_demo) and leaves it out once the organization hid it. Its description tells the agent what that means.
  • Reads stay open.

Migration / deploy notes: none in this part.

Related issue

None (demo brand).

Stacked on #1738 (demo brand, part 5: web).

Type of change

  • feat — New feature

How to test

  1. With an API key for the demo account, call list_brands: Brewline Coffee is there with is_demo: true. Hide it from the brand menu and call again: it's gone.
  2. get_visibility_summary or list_prompts (any get_/list_ tool) on the demo brand returns its sample data.
  3. run_site_audit with the demo brand's id, or create_improve_action on one of its prompts: an error with code: "demo_brand", and nothing starts.
  4. The same calls on your own brand behave as before.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: yarn typecheck passes. Tests: src/lib/mcp (with demo-gate), src/app/api, lib/demo, components/billing and subscription-guard (44 files, 372 tests) pass.
  • Server: eslint src and the src/lib/demo tests pass.
  • The embed guard from test(web): fail on an embed between twice-joined tables that doesn't name its foreign key #1714 passes on this branch
  • Full CI-like run: done at the top of the stack (docs PR), see there
  • Live call with a real key: the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 30 commits October 27, 2026 17:58
…e "Choose a plan" dialog

On the cloud, an ended trial, a cancel during the trial and a cancel
after the first payment can now sign in and look around. They used to
see a full-screen "subscription expired" notice that only signed out.
Onboarding is unchanged: an admin who never started a trial still goes
to the plan step.

- The dashboard layout lets these workspaces in, read-only. A thin
  banner says so, with "See plans".
- UpgradeDialogProvider and useUpgradeGate ({ guard, isLocked,
  openUpgrade }) open one "Choose a plan to keep going" dialog, with the
  action that was clicked as a label.
  - Admins go to Billing; members are told to ask their admin.
  - A failed payment (past_due, unpaid) says so and opens Stripe's
    portal.
  - A 402 subscription_inactive from the API opens the dialog
    (use-failure-toast, api-failure's subscription class, and the
    Webflow, catalog, publish and Send to CMS clients).
- Automatic calls (Library's Webflow status, the editor's link, source,
  claim and page reads, Improve's read) aren't sent while locked.
- Billing: an ended subscription starts a new Checkout ("Choose
  {plan}", monthly or annual). The checkout route takes admins only,
  refuses a second live subscription (409), and validates its body.
  Billing reads again after a change.
- MCP: list_ and get_ tools stay open; every other tool, and REST
  writes, answer subscription_inactive. Chat and new conversations
  answer 402, and runSiteAuditFor, generateBriefFor and
  createImproveActionFor check too. A chat message refused by a 402
  goes back to the input.

Self-hosted is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r Annual and a promo code

The "Choose a plan to keep going" dialog for an ended plan now carries
the plans, as the approved design has them.
- Starter and Growth (Popular) have "Choose {plan}"; Custom has "Starting
  at $499/mo" and "Contact us". Each lists the Billing cards' features,
  through a shared usePlanFeatures.
- Monthly or Annual ("Save 20%"): an annual price shows its struck 12×
  price and what it is a month.
- A promo code is checked by the API (POST /api/stripe/promo-code, admins
  only), through the same Stripe lookup ansvisor#1300 uses (findPromotionCode,
  takesPromoCode: monthly plans only).
  - A valid code shows its chip, the first month's price on each card and
    in the button ("Choose Growth · $124.50 today").
  - A bad one says so; on Annual a note offers Switch to Monthly.
- Choose opens Stripe Checkout for that plan and period, with the code
  applied as a discount. The checkout route checks the code again and
  refuses it on an annual plan.
- Members see "Ask your admin to choose a plan", with no code field.
- The footer reads "Prices in USD." with a link to the pricing page.
- A failed payment keeps its "Update payment method" dialog.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the plan has ended

Read-only, part C1. On an ended plan, every action in Prompts, Prompt
Suggestions, Topics, Query Fan-out, Brand Visibility and Competitors
opens the "Choose a plan" dialog with the action named, and runs
nothing. On an active plan they run as before. Buttons aren't disabled.
- Tracked Prompts: add, import, edit, analyze and refresh volumes, the
  bulk bar, work status and the row menu.
- Prompt and topic suggestions: generate, add, accept, dismiss.
- Manage prompts and topics: add, generate, locations, analyze, edit,
  delete.
- Query Fan-out's Track; Brand Visibility's Run All, Test Single Prompt,
  Opportunities' Track and Send, and pinning a competitor.
- Competitors: suggest (its dialog calls AI on opening), add, delete.
- The Positions panel: Improve's Generate and Regenerate, Send Actions,
  and Create or Optimize content.
- The labels live in readOnly.actions.* ("Adding prompts needs a plan").
- volumes.ts tells the API's 402 subscription_inactive from a feature
  the plan lacks, so a refused analysis opens the dialog instead of "not
  part of your current plan".
- While locked, Tracked Prompts doesn't warn that volume data is
  unavailable, and Query Fan-out doesn't send its intent classification.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…keep the editor read-only, while the plan has ended

Read-only, part C2. On an ended plan, every action that writes or costs
in Content Opportunities, the opportunity panel, Content Plan, Library,
New content and the Content Editor opens the "Choose a plan" dialog,
naming the action, and runs nothing.
- Opportunities: New, the bulk bar (send, done, dismiss and delete open
  the dialog instead of their confirm), the row menu, the webhook
  settings' Test and Save, and the panel's sends, brief, Reviewed,
  Restore and Dismiss.
- Plan: New content, Start, Assign, Priority, Hide and Show. A ?start=
  link starts nothing while locked.
- Library: New content (and ?new=1), status changes, archive, delete,
  and single and bulk Send to CMS.
- The editor:
  - Run Agent, Rewrite, Fix rules, hand-off;
  - accepting or rejecting suggestions;
  - Mark ready, Ready back to review, Mark published, Send to CMS,
    History Restore;
  - page and site type, author and reviewer;
  - Quality's confirms, sources and images, and Add line or Remove;
  - renaming, dropping files, the toolbar;
  - the Brief's run, rewrite and edit; the Sources and Rules changes;
    Your input.
- The editor itself is read-only while locked (editable: false, the
  HTML view readOnly), and autosave is off (useDraftAutosave { off }):
  nothing is saved, and leaving doesn't warn.
- Reading, filters, exports, Copy HTML and Stop stay open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d from every way to add a brand, while the plan has ended

Site Audit, Action Center, Mentions, Citations, AI Referral Traffic,
Shopping, Reports, Chat, brand settings and Settings now guard their
actions with useUpgradeGate, as Prompts (ansvisor#1606) and Content (ansvisor#1609) do.
Every way to add a brand (sidebar, /dashboard/brands, brand guard,
/dashboard/brands/new) opens the dialog. Disconnects, key revokes and
API key creation stay open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…has ended, on the server

Server actions and Next routes that write to Supabase without going
through the API now call assertWritable() first: on the cloud, an
organization without an active plan gets SubscriptionInactiveError
(routes: 402 subscription_inactive), so the read-only rule holds even if
a guarded button is bypassed. Setup's writes stay open to an
organization that never subscribed, as the API's onboarding routes do.
Profile, invitations accepted, revokes, removals, API keys and billing
stay open. A static test pins every writing action to the check or to
the open list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ng stores open while read-only, and stop the site trust card's automatic re-read

From the pre-push review: ending an access stays open, as disconnects
do; a partial site trust result isn't re-read while the plan has ended,
since that read scrapes. Adds tests for the audit page's Run audit and
the integrations' property mapping, resets shared mocks between tests,
closes the mobile menu before the dialog, and words the KPI preview for
a read-only workspace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it too, while read-only

At the limit, /dashboard/brands' Add Brand links to Billing; a read-only
workspace now gets the dialog there as on every other way to add a brand.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ansvisor's decision (2026-10-27): deleting a brand only removes what the
person gave us, so it stays open while the plan has ended. The API opens
DELETE /brands/:id in ansvisor#1611.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d the plan in one query, and pin the write check's place

From the pre-push review: markBrandAsEcommerce no longer goes through
the strictly guarded setBrandShoppingMode (a shared applyShoppingMode
does the writes), so a setup detected as e-commerce turns Shopping on
again. assertWritable reads the profile and its organization in one
query. The coverage test now requires the check as each writer's first
await, sees writers kept in variables and signed upload URLs, refuses a
setup-open function that reaches a strictly guarded one, and keeps brand
deletion open (Ansvisor, 2026-10-27). main's saveDraftShoppingPath is
guarded; catalog-client's every-code test leaves out 'subscription',
which opens the plan dialog instead of a message.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… plan has ended, and keep the buttons opening the plan dialog

With ansvisor#1611, an ended workspace's quota reads come back as `noPlan` with
0 limits. isNoPlan(quota, isLocked) keeps those from reading as used up:
Site Audit, Prompts, the content usage card, Improve and the opportunity
panel say "No active plan" instead of a red 0/0 or "limit reached", and
their buttons stay clickable so the guard opens the dialog. The AI bot
report shows the workspace's own data instead of the Starter upsell.
Setup's suggestions name the onboarding daily limit (429) on their own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…upsell and one lock rule while the plan has ended

From the pre-push review: the Prompts row menu still looks up an open
content suggestion with no plan; an ended Starter workspace keeps the
bot report's upsell (its old plan has no report), read from the plan
the web already knows; auditBlock, bulkSuggestBlock and the bot states
take isLocked as the pages do; the audit settings and the content usage
tooltip don't speak of a plan that renews; setup's prompt step keeps its
own "add your own" line. Improve's unreachable no-plan branch is gone.
Tests for the onboarding limit and the quota meter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…essage test

main's catalog-client test (ansvisor#1583) asks for words for every StoreError
code; 'subscription' opens the upgrade dialog instead of saying one, so
the test leaves it out, as ansvisor#1613 does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y-plan-dialog

Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	web/src/components/settings/plan-switcher.tsx
…nd main's other new actions while the plan has ended

main's new writes, with C3's guard pattern and labels:
- Merchant Center (ansvisor#1583): picking and adding an account open the plan
  dialog ("Connecting a store needs a plan"); the server's 'subscription'
  refusal on Add opens it too, instead of looking up a missing
  settings.catalog.stores.errors.subscription message.
- Category pages (ansvisor#1587): Change, Save, No page and Use the match are
  guarded; the catalog client reads the server's 402
  subscription_inactive as 'subscription', which opens the dialog
  instead of the generic failure.
- Brief › Shopping path (ansvisor#1591): Pick from the catalog, Pick again, Add
  product, Lock, Swap and Remove are guarded.
- Action Center Check now (AC-010, ansvisor#1565) is guarded.

New labels: changeCategoryPages, changeShoppingPath, checkTasks.
Insert › Products (ansvisor#1607) already goes through the toolbar's guard and
the read-only editor shows no insert menu; a test now says so. The
Shopping rules (ansvisor#1610) change through the rules hook, already guarded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g it during render

React's refs rule refuses passing add, which reads addingRef, to guard
while rendering. add now opens the plan dialog itself on a read-only
workspace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/brands/[id]/prompts/page.test.tsx
cihan-gyk and others added 5 commits October 29, 2026 01:20
…ck comes first

Merging main left createTopics, createTopic and updateTopic with two
plan checks each: main's, moved ahead of the name check, and the demo's,
which hands the brand over. Each now checks once, first, with its brand
where it has one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…page

⌘ or Ctrl with ⇧O (new chat), K (search), J (side panel) or \ (menu)
reach the preview while the app around it has the focus: the page hands
them to the frame, which answers them as it does inside. The frame takes
the focus when it opens, and on a Mac the preview shows ⌘ for Ctrl.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
cihan-gyk and others added 15 commits October 29, 2026 18:49
…core

# Conflicts:
#	server/src/lib/action-center/execution/run.js
#	server/src/lib/action-center/execution/run.test.js
#	supabase/schema.sql
…n's new tables

sweepCompetitorTimeline (learning C3, on main) chose brands by their
organization's plan only, so a demo brand's sample answers would have had
their cited pages read every night. It now leaves demo brands out, as the
other sweeps do. The demo coverage list names the brand tables main added:
cited_page_features, competitor_page_events and competitor_timeline_scans
(pending a demo follow-up) and chat_conversations (with Chat).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat's precedence (ansvisor#1776) left readBrandIsDemo false until the demo chain
added brands.is_demo, with a type check that stops compiling once the
column exists. It now reads the column, as the server's demo guard does,
so a tool that changes or spends on the demo brand is refused with
demo_brand; a brand that isn't found isn't one, and a failed read throws.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
POST /brands/:id/detect-site-type (ansvisor#1761, on main) reads the brand's home
page and may ask AI for its kind. The demo guard's coverage test fails on a
write route in neither list, so it is now guarded by the brand in its path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
chat_user_prefs (00400) and chat_auto_choices (00401) are a person's own
rows, not a brand's: the write guard's coverage test fails on a writable
table in no list, so both are named as no brand's rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/lib/actions/brand.ts
#	web/src/lib/actions/prompt.test.ts
#	web/src/lib/actions/prompt.ts
#	web/src/lib/subscription-guard.ts
approve_task_review, dismiss_task_review (ansvisor#1713) and
mark_content_draft_published (ansvisor#1715) write a brand's data, and the demo
gate's test fails on a write tool WRITE_TOOL_BRANDS doesn't name. A task
review finds its brand through its action; a draft through its own row.
The test's API key now carries the role main's McpAuthContext requires.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged main forward through #1738's updated branch (conflict in web/src/lib/mcp/data.ts imports: kept both the demo gate's and main's role gate imports).

One follow-up commit: main added three MCP write tools, approve_task_review / dismiss_task_review (#1713) and mark_content_draft_published (#1715). They are now in WRITE_TOOL_BRANDS (a task review finds its brand through its action, a draft through its own row) and in the test's ON_DEMO, so they answer demo_brand on the demo brand. The test's API key also gets the role main's McpAuthContext now requires.

Typecheck and the conflicted file's tests pass (demo-gate, subscription-gate, task-reviews).

…eir tools do

The tools refused a write on the demo brand in their gate, but the REST
mirrors call the same data functions with no gate in front, and those write
with the service role. Each mirror that writes now asks demoBrandRefusal with
its tool's name and arguments first, and answers 402 demo_brand. A test fails
on a REST write route that checks neither this nor a data function that
refuses the demo brand itself.
…te tools

The task review approve and dismiss routes and the draft published route
call demoBrandRefusal before their data functions, as their tools do. The
routes test's auth carries main's role, and the notes that waited for
this part say where the demo brand is refused.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good with c3a6fbc2: the REST mirrors now refuse the demo brand as their tools do. Branch take-1739: this head e0c9c7e with take-1738 merged in (part 4's fixes), then the fix. The merge with main needs the items under "Merge".

What I changed:

  • The REST mirrors. The REST routes that mirror the write tools now call demoBrandRefusal() before their data function, as the tools do through gateWriteTools, so a write on the demo brand gets the same demo_brand answer over REST as over MCP.
  • Test (demo-gate-routes.test.ts). A write on the demo brand gets 402 and the data function never runs; the own brand goes through. Every route under app/api/mcp that exports POST, PUT, PATCH or DELETE must call demoBrandRefusal, or one of the three data functions that refuse the demo brand themselves. Both tests fail without the fix.

What I checked:

  • The gate. For a write tool it runs before the plan check, so the demo brand answers demo_brand whatever the plan. Reads stay open. Another organization's demo brand reads as not a demo, and the tool's own ownership check answers it as missing. Self-hosted checks nothing.
  • list_brands. It adds is_demo and leaves out a hidden demo brand (is_demo.eq.false,demo_hidden_at.is.null). Other read tools still read a hidden demo brand by id; that's harmless.
  • Embeds. None; each lookup is a plain select by id.
  • Tests. On take-1739, lib/mcp, app/api/mcp and subscription-guard pass (26 files, 185 tests). tsc is clean, and eslint and prettier are clean on the changed files.

Merge (with main 3b7611c): besides the earlier parts' conflicts, web/src/lib/mcp/data.ts conflicts on imports (keep both: main's role-gate and this PR's demo-gate). Merged, three things fail, as they should:

  1. demo-gate.test.ts: main added approve_task_review, dismiss_task_review (#1713) and mark_content_draft_published. They need entries in WRITE_TOOL_BRANDS and ON_DEMO:
    • the two review tools: review_id → action_task_reviews.action_id → actions.brand_id, like update_task_status;
    • mark_content_draft_published: brandOfRow('content_drafts', 'draft_id').
  2. demo-gate-routes.test.ts (with this fix): main's task-reviews/[id]/approve, task-reviews/[id]/dismiss and content-drafts/[id]/published routes need demoBrandRefusal with those tools.
  3. tsc: the test's AUTH needs main's new role on McpAuthContext (#1762), e.g. role: 'admin'.

Follow-ups:

  1. generateBriefFor, runSiteAuditFor and createImproveActionFor now check the demo brand twice on the MCP path (gate, then data function). It's one small read each; fine as is.
  2. The in-product agent's tools go through the same data functions. Once main's chat (#1776) runs tools, its readBrandIsDemo should be the one place that decides, as it already plans.

@gkhngyk
gkhngyk merged commit 2841406 into ansvisor:main Oct 29, 2026
3 of 5 checks passed
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
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.

2 participants