Repository navigation
feat(mcp): read AI usage, measured demand and who you are over MCP (B2) - #1711
Merged
Merged
Conversation
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]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
This was referenced Oct 28, 2026
gkhngyk
approved these changes
Oct 28, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
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_mereads only the key's own profile and organization row.get_ai_usagesends the server the key's organization id, never one the caller supplies. - The internal endpoint
GET /api/internal/ai-usage. Same pattern assite-audit-quota: aCRON_SECRETbearer, and if the server has no secret it refuses everything.orgIdis validated withz.guid(); an array query takes the first value. It calls the samegetAiUsagethe Billing route uses. A no-plan organization comes back as the same 402plan_limit, which the web maps toactive_plan: false. An unknown org id also reads as no plan, so nothing leaks. - Read-only workspaces.
get_meandget_ai_usagestart withget_, so the gate leaves them open, and the REST mirrors are GETs. The gate test's read count moves 36 → 38. - Measured demand.
readPromptDemandreadsprompt_demandfiltered 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 isnull. A failed read is logged and leavesmeasured_demandnull, so the volumes still answer. Each row's output is small: three questions at most, no Trends points or video lists. - whoami.
/api/v1/whoamire-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.tson this branch. The REST lines and the whoami example match the responses. - Deploy order. If the web ships before the API,
get_ai_usageand/api/mcp/ai-usagefail 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),tscclean. Server: full suite 266/267 files; the one failure is the knowncontent-peopletiming 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
- 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. AddingAbortSignal.timeout(10_000)to both would be cheap. - For a key whose user has no organization,
/api/mcp/ai-usageanswers 404 "Organization not found". 404 is fine, but the MCP tool and the REST route could say why (this account has no organization). - 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.
4 of 5 tasks
Collaborator
Author
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]>
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
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'sgetAiUsagethrough a new internal endpoint,GET /api/internal/ai-usage?orgId=(CRON_SECRET,orgIdchecked withz.guid()), the same pattern assite-audit-quota. No plan →active_plan: false; self-hosted answers unlimited without asking the server.get_prompt_volumes(and its REST mirror): each row getsmeasured_demandfromprompt_demand(00370) — level, signals read, oneread_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);nulluntil a prompt's demand has been read. Read in chunks of 100 within the brand; a failed read logs and leavesmeasured_demandnull 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.titleandannotations: { readOnlyHint: true, openWorldHint: false }(the format A1 will use for every tool); they stay open on a read-only workspace.mcp-server.mdx(52 tools, a "Your account" section,measured_demand) and the whoami example inapi-reference/metrics.mdx. Skills follow in the B8 docs PR.Deploy note:
get_ai_usageneeds 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 featureHow to test
get_meover MCP: your role, organization and work areas.get_ai_usage: the same percentage as Settings › Billing; on a workspace without a plan,active_plan: false.get_prompt_volumesfor a brand whose demand has been read (Prompts › demand): rows carrymeasured_demand; othersnull.GET /api/mcp/ai-usage,GET /api/mcp/whoamiwith the same key.Checklist
npm run lintpasses;npm test264/267 files — the 3 others (audit, no-fabrication, page-content) time out under a loaded machine and pass alone (47 tests)yarn lint(0 errors),yarn typecheck,yarn buildpass;yarn test544/546 files — the Content Editor page test (known load timeout) and shopping categories (passes alone, 8/8)🤖 Generated with Claude Code