feat(mcp): get_stats tool for the writing stats, in preset views #68
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/get-stats-mcp"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #67
Gives the chat and API-key agents read-only access to the writing stats, so
draw_page/make_pdfreports can be built from them.What
get_stats, a new lookup inChat.Tools(soAgentToolsgets it too), over a newChat.WritingStatsthat wrapsRmAnalyzer.Stats. It returns preset views, never raw entries:summary(Stats.summary/2),by_day(latest 366 days,truncatedflag),over_time(byweek|month,count1–104, default 12),top_words(Stats.word_cloud/3, so hidden words stay hidden;limit1–100, default 25),on_this_day(per earlier year: words and up to 20 pages with name, number and link), andpages(see below).filteris the Stats page's (all,folder:<id>,notebook:<id>,tag:<name>).fromandtotake ISO dates, andtoalso setsas_offor streaks, periods and on this day. Bad input gets a plain error the agent can fix.Chat.Notes.reachable_documents/1(new;Notes.documents/1now uses it too). That applies to tag filters as well, and afolder:/notebook:filter it doesn't reach is refused with the same "There's no …" message the other tools use.by_app: unchanged, becauseStats.entries/2already skips app-written versions. It's tested here so a drawn report doesn't count toward the next one.Logger.infoper call (user id, views, filter, date range, all/limited library, entries, days, ms) andLogger.warningon a refused call, with the input.make_pdf/draw_page, or a fencedsvgchart fordraw_page. The MCPinstructionsand the chat's system prompt mention it too.docs/mcp.md(Tools, Limiting a key) anddocs/stats.md(new "For the assistant and agents").Tests
test/rm_analyzer/chat/writing_stats_test.exs(11): each view, dates andas_of, filters, theby_apppage (50 words) and the trashed page not counting, hidden words, input errors. Under a key limited to some folders: it counts only Work (15 of 17 words, noeggs/milkin top words, nogroceriesanywhere in the JSON), a tag over both notebooks gives 15 not 17, andnotebook:groceries/folder:homeare refused.notes_mcp_agent_test.exs: the same over real/mcpHTTP with a Work-only key.mix precommitatee8c132: 1104 passed (2 doctests, 1102 tests), 1 excluded.A real MCP call
These calls went to the app running on the screenshots mock library (
scripts/screenshots/serve.exs, Postgres in Docker), using API keys made for the dev user. It's mock data, so none of it is anyone's journal. Arrays are trimmed.Key with everything,
{"views":["summary","over_time","top_words","on_this_day"],"by":"month","count":3,"limit":5}:Work-only key,
{"views":["summary","by_day"],"from":"2026-10-26"}. It finds 15 pages and 1313 words, where the full key finds 33 pages and 2189 words for the same dates:Work-only key,
{"filter":"notebook:<Journal, in Personal>"}:Server log for those calls:
tools/listfor the Work-only key:list_documents, search_notes, read_pages, get_stats, view_page, list_suggestions.Settings → API & MCP → Access
Desktop, 1440×900. The new Writing stats row is between Read pages and View pages:
Phone, 390×844:
pagesview (added in534ab37,ee8c132; month-boundary order fixed in748078b)For month-end reports (team decision D5):
get_stats from=2026-10-01 to=2026-10-31 views=[summary,by_day,top_words,pages], thenread_pagesfor what the listed pages say.from/torange:document_id,document,page,link(Notes.page_link),date(the last date it counts on in the range, the content date where that's on) andwords_addedsummed over the range. A page since deleted from its notebook comes withoutpage/link.totalandtruncated; optionalpage_limit(1–200). It's a separate parameter becauselimitis already top_words' (max 100).by_appversions don't appear.Date.to_gregorian_days), not Date structs inside tuples, which compare day before month: before748078bOct 31 sorted above Nov 2,page_limitkept the wrong pages, and a page written Oct 31 and Nov 2 was dated Oct 31. A test now covers a range across Oct/Nov, with the cap.Logger.infoper call: pages listed of total, and the cap when it's hit.pages): range and order, words summed with the last date, a deleted page, an only-by_appday giving no rows, the cap/page_limit/its error, and the Work-only key listing only Meetings' pages.docs/mcp.md,docs/stats.md, the tool description, MCP instructions, chat prompt, and the Access row's hint.Real MCP call on the mock app at
ee8c132,{"from":"2026-10-01","to":"2026-10-31","views":["summary","pages"],"page_limit":5}, key with everything (rows trimmed to 3):Same call with the Work-only key (documents listed, rows left out):
Log lines:
(The mock's "Team meetings" row has no
pagebecause the mock's history rows use made-up page ids, which is the deleted-page case.)