Skip to content

feat(mcp): read AI usage, measured demand and who you are over MCP (B2) - #1711

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-usage-demand
Oct 28, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-usage-demand

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

MCP next round, B2: account, AI usage and measured demand over MCP and REST.

  • get_ai_usage + REST /api/mcp/ai-usage: the same numbers as the Billing meters — active_plan, plan, trial, period, unlimited, percent_used, and the per-feature split (improve / content / action_center / other). It reads the server's getAiUsage through a new internal endpoint, GET /api/internal/ai-usage?orgId= (CRON_SECRET, orgId checked with z.guid()), the same pattern as site-audit-quota. No plan → active_plan: false; self-hosted answers unlimited without asking the server.
  • get_prompt_volumes (and its REST mirror): each row gets measured_demand from prompt_demand (00370) — level, signals read, one read_at, and the signals (Search Console impressions/clicks/queries/top query, Google Trends trend/change, People also ask { count, questions: first 3 }, YouTube videos/views); null until a prompt's demand has been read. Read in chunks of 100 within the brand; a failed read logs and leaves measured_demand null instead of failing the tool.
  • get_me + REST whoami (/api/mcp/whoami, /api/v1/whoami): organization name, role, work areas (with their names) and whether the person can change menus (menu access, feat(settings): store who sees which menu sections, by role, person and work area (menu access, part 1) #1702–docs(settings): explain menu access (menu access, part 7) #1708). Adds fields only.
  • Both new tools carry title and annotations: { readOnlyHint: true, openWorldHint: false } (the format A1 will use for every tool); they stay open on a read-only workspace.
  • Docs: mcp-server.mdx (52 tools, a "Your account" section, measured_demand) and the whoami example in api-reference/metrics.mdx. Skills follow in the B8 docs PR.

Deploy note: get_ai_usage needs the API server with the new internal endpoint; deploy the server before (or with) the web.

Related issue

None (MCP next round, B2).

Type of change

  • feat — New feature

How to test

  1. With an API key, call get_me over MCP: your role, organization and work areas.
  2. get_ai_usage: the same percentage as Settings › Billing; on a workspace without a plan, active_plan: false.
  3. get_prompt_volumes for a brand whose demand has been read (Prompts › demand): rows carry measured_demand; others null.
  4. REST: GET /api/mcp/ai-usage, GET /api/mcp/whoami with the same key.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Server: npm run lint passes; npm test 264/267 files — the 3 others (audit, no-fabrication, page-content) time out under a loaded machine and pass alone (47 tests)
  • Web: yarn lint (0 errors), yarn typecheck, yarn build pass; yarn test 544/546 files — the Content Editor page test (known load timeout) and shopping categories (passes alone, 8/8)
  • Tests added (account, prompt-demand, prompt-volumes, ai-usage/whoami routes)
  • Docs updated
  • Live call with a real key: key work is the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 28, 2026 19:58
Three reads for an AI assistant working in Ansvisor (MCP plan B2):

- get_ai_usage (REST /api/mcp/ai-usage): this period's share of the
  monthly AI allowance, by feature, with the plan, the trial and the
  period, as the Billing meter shows it. The numbers come from the
  server's getAiUsage through a new CRON_SECRET internal endpoint,
  /api/internal/ai-usage; without an active plan, active_plan is false.
- get_prompt_volumes (and its REST mirror) adds measured_demand to each
  prompt: Search Console, Google Trends, People Also Ask and YouTube,
  each with the day it was read, and the level they come to (00370).
- get_me (REST /api/mcp/whoami gains the same fields): the user's role,
  their work areas by name and whether they may change menus (00374),
  with the organization's name.

The two new tools carry a title and readOnlyHint, and stay open on a
read-only workspace as every get_ tool does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… up when it can't be read

A list of up to 500 prompts carries one measured_demand each, so it is
kept short: one read_at for the row, People Also Ask's count and first
three questions, and no keyword (it is the prompt's top keyword). A row
the nightly sweep wrote before reading anything (signals 0, every
signal empty) reads as null. The read is limited to the brand's rows,
and when it fails the volumes still answer, each without its demand.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hosted without the server

get_me no longer reads a profile or organization it couldn't read as an
empty one: the query's error is thrown. get_ai_usage answers unlimited
self-hosted, where nothing is metered, without asking the server, and
reads "no active plan" from a 402 plan_limit only. The internal
endpoint takes orgId as a UUID.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lumes, and whoami's new fields

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

mintlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 1530db2.

Approve preview

@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 follow-ups

Reviewed this PR's own change (b09046c; it merged main at cfd8a35, so its own diff is origin/main...b09046cf). No migration. Not stacked with #1712 or #1713: all three branch from the same main.

What I checked

  • Auth. Both new tools and both REST routes go through authenticateMcpRequest (API key → its user → their organization). get_me reads only the key's own profile and organization row. get_ai_usage sends the server the key's organization id, never one the caller supplies.
  • The internal endpoint GET /api/internal/ai-usage. Same pattern as site-audit-quota: a CRON_SECRET bearer, and if the server has no secret it refuses everything. orgId is validated with z.guid(); an array query takes the first value. It calls the same getAiUsage the Billing route uses. A no-plan organization comes back as the same 402 plan_limit, which the web maps to active_plan: false. An unknown org id also reads as no plan, so nothing leaks.
  • Read-only workspaces. get_me and get_ai_usage start with get_, so the gate leaves them open, and the REST mirrors are GETs. The gate test's read count moves 36 → 38.
  • Measured demand. readPromptDemand reads prompt_demand filtered on the brand as well as the prompt ids (the brand was already checked against the key's organization), in parallel chunks of 100. At the 500-row cap that's 5 small requests. Field names match what the server's sweep writes (gsc.top.query, paa.questions[].question, trends.trend/change, youtube.videos/views). A row with nothing read yet is null. A failed read is logged and leaves measured_demand null, so the volumes still answer. Each row's output is small: three questions at most, no Trends points or video lists.
  • whoami. /api/v1/whoami re-exports the MCP route, so both get the new fields. Existing fields are unchanged; the PR only adds new ones.
  • Docs. The count of 52 tools matches server.ts on this branch. The REST lines and the whoami example match the responses.
  • Deploy order. If the web ships before the API, get_ai_usage and /api/mcp/ai-usage fail with "Internal ai-usage endpoint returned 404" and everything else works. The PR says so.
  • Tests on this tree: web src/lib/mcp, src/app/api/mcp, src/app/api/v1 (20 files, 135 passed), tsc clean. Server: full suite 266/267 files; the one failure is the known content-people timing test under load. eslint and prettier clean on the touched files.

Merging on current main

Merges clean on 2391b43. Whichever of this PR and #1712 / #1713 merges second will conflict in:

  • web/src/lib/mcp/server.ts: the imports and the new tool blocks at the end. Keep both.
  • docs/guides/mcp-server.mdx: the tool count and the action rows.
  • web/src/lib/mcp/subscription-gate.test.ts: the read count.

With all three in, the counts are 56 tools, 40 read tools and 16 gated.

Follow-ups

  1. The internal fetch has no timeout (nor does site-audit-quota's). If the API hangs, the MCP call hangs until the platform cuts it off. Adding AbortSignal.timeout(10_000) to both would be cheap.
  2. For a key whose user has no organization, /api/mcp/ai-usage answers 404 "Organization not found". 404 is fine, but the MCP tool and the REST route could say why (this account has no organization).
  3. The internal route's 500 returns err.message. Only the web layer sees that response (it's behind CRON_SECRET), but the web then puts the message in the tool's error text. A fixed message there would be safer.

@gkhngyk
gkhngyk merged commit 9aab2ae into ansvisor:main Oct 28, 2026
4 of 5 checks passed
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Heads-up @gkhngyk: the main merge into this branch (1530db2) kept its Node 20 lines in server/Dockerfile, the server CI job and server/README.md, so the squash (9aab2ae) undid #1574 on main. #1726 puts Node 24 back (same three lines as #1574); Framer's client needs Node 22+.

gkhngyk pushed a commit that referenced this pull request Oct 29, 2026
The merge of main into #1711 (1530db2) kept the branch's Node 20 lines
in the Dockerfile, the server CI job and the README, so #1711's squash
(9aab2ae) undid #1574. Framer's client needs Node 22 or newer.

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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