feat(mcp): get_stats tool for the writing stats, in preset views #68

Merged
jfro merged 4 commits from feat/get-stats-mcp into main 2026-11-01 15:36:42 +00:00
Owner

Closes #67

Gives the chat and API-key agents read-only access to the writing stats, so draw_page / make_pdf reports can be built from them.

What

  • get_stats, a new lookup in Chat.Tools (so AgentTools gets it too), over a new Chat.WritingStats that wraps RmAnalyzer.Stats. It returns preset views, never raw entries:
    • summary (Stats.summary/2), by_day (latest 366 days, truncated flag), over_time (by week|month, count 1–104, default 12), top_words (Stats.word_cloud/3, so hidden words stay hidden; limit 1–100, default 25), on_this_day (per earlier year: words and up to 20 pages with name, number and link), and pages (see below).
    • filter is the Stats page's (all, folder:<id>, notebook:<id>, tag:<name>). from and to take ISO dates, and to also sets as_of for streaks, periods and on this day. Bad input gets a plain error the agent can fix.
  • Scope: a key limited to some folders only counts entries in Chat.Notes.reachable_documents/1 (new; Notes.documents/1 now uses it too). That applies to tag filters as well, and a folder:/notebook: filter it doesn't reach is refused with the same "There's no …" message the other tools use.
  • by_app: unchanged, because Stats.entries/2 already skips app-written versions. It's tested here so a drawn report doesn't count toward the next one.
  • Diagnostics: Logger.info per call (user id, views, filter, date range, all/limited library, entries, days, ms) and Logger.warning on a refused call, with the input.
  • Descriptions: the tool description tells the agent to put the numbers in a Markdown table for make_pdf/draw_page, or a fenced svg chart for draw_page. The MCP instructions and the chat's system prompt mention it too.
  • Settings: it's a read tool, so keys with every tool get it. Keys limited to some tools don't get it until it's ticked: a new Writing stats row in Access (screenshots below).
  • Docs: docs/mcp.md (Tools, Limiting a key) and docs/stats.md (new "For the assistant and agents").

Tests

  • test/rm_analyzer/chat/writing_stats_test.exs (11): each view, dates and as_of, filters, the by_app page (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, no eggs/milk in top words, no groceries anywhere in the JSON), a tag over both notebooks gives 15 not 17, and notebook:groceries / folder:home are refused.
  • notes_mcp_agent_test.exs: the same over real /mcp HTTP with a Work-only key.
  • Checked that the tests catch a leak: with the reachable-documents filter taken out, the three scope tests fail (3 of 22).
  • mix precommit at ee8c132: 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}:

{
  "filter": "all",
  "to": null,
  "from": null,
  "as_of": "2026-11-01",
  "pages_written": 507,
  "on_this_day": [
    {
      "words": 24,
      "year": 2025,
      "pages": [
        {
          "link": "/remarkable/documents/03a3ae33-9013-4c2d-bfc8-1141def35419?page=2",
          "page": 2,
          "document_id": "03a3ae33-9013-4c2d-bfc8-1141def35419",
          "document": "Quick sheets",
          "words_added": 24
        }
      ]
    }
  ],
  "over_time": {
    "periods": [
      {
        "start": "2026-09-01",
        "words": 8782,
        "days": 26
      },
      {
        "start": "2026-10-01",
        "words": 9062,
        "days": 27
      },
      {
        "start": "2026-11-01",
        "words": 292,
        "days": 1
      }
    ],
    "by": "month"
  },
  "summary": {
    "current_streak": 23,
    "longest_streak": {
      "to": "2026-11-01",
      "from": "2026-10-10",
      "days": 23
    },
    "days_so_far": 305,
    "days_this_year": 188,
    "total_days": 251,
    "total_words": 83739,
    "words_month": 292,
    "words_per_day": 329,
    "words_week": 2189,
    "words_year": 61943
  },
  "top_words": [
    {
      "times": 6,
      "word": "plant"
    },
    {
      "times": 5,
      "word": "review"
    }
  ]
}

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:

{
  "filter": "all",
  "to": null,
  "from": "2026-10-26",
  "as_of": "2026-11-01",
  "pages_written": 15,
  "by_day": {
    "days": [
      {
        "date": "2026-10-26",
        "words": 239,
        "pages": 1
      },
      {
        "date": "2026-10-27",
        "words": 252,
        "pages": 1
      }
    ],
    "truncated": false
  },
  "summary": {
    "current_streak": 7,
    "longest_streak": {
      "to": "2026-11-01",
      "from": "2026-10-26",
      "days": 7
    },
    "days_so_far": 305,
    "days_this_year": 7,
    "total_days": 7,
    "total_words": 1313,
    "words_month": 198,
    "words_per_day": 188,
    "words_week": 1313,
    "words_year": 1313
  }
}

Work-only key, {"filter":"notebook:<Journal, in Personal>"}:

{"content":[{"type":"text","text":"There's no notebook with id d9c2ae90-394d-46b3-bfe8-15521a335bb2 here. List the documents to find one."}],"isError":true}

Server log for those calls:

[info] get_stats for user 1: views summary,over_time,top_words,on_this_day, filter :all, all dates, library all, 507 entries over 251 days, in 169 ms
[info] get_stats for user 1: views summary,by_day, filter :all, 2026-10-26..now, library limited, 15 entries over 7 days, in 8 ms
[warning] get_stats for user 1 refused: There's no notebook with id d9c2ae90-… here. List the documents to find one. (input %{"filter" => "notebook:d9c2ae90-…"})

tools/list for 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:

Access, desktop

Phone, 390×844:

Access, phone

pages view (added in 534ab37, ee8c132; month-boundary order fixed in 748078b)

For month-end reports (team decision D5): get_stats from=2026-10-01 to=2026-10-31 views=[summary,by_day,top_words,pages], then read_pages for what the listed pages say.

  • One row per page written in the filter/from/to range: 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) and words_added summed over the range. A page since deleted from its notebook comes without page/link.
  • Newest first. Capped at 200 with total and truncated; optional page_limit (1–200). It's a separate parameter because limit is already top_words' (max 100).
  • Same scope as the other views (built from the same entries): a limited key lists only pages in documents it reaches, and by_app versions don't appear.
  • Order and each page's date use the calendar (Date.to_gregorian_days), not Date structs inside tuples, which compare day before month: before 748078b Oct 31 sorted above Nov 2, page_limit kept 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.info per call: pages listed of total, and the cap when it's hit.
  • Tests (5 new, plus the limited-key test now asks for pages): range and order, words summed with the last date, a deleted page, an only-by_app day giving no rows, the cap/page_limit/its error, and the Work-only key listing only Meetings' pages.
  • Docs: 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):

{
  "filter": "all",
  "from": "2026-10-01",
  "to": "2026-10-31",
  "pages_written": 63,
  "pages": {
    "total": 63,
    "pages": [
      {
        "date": "2026-10-31",
        "link": "/remarkable/documents/a45dd78a-29d3-4eb4-96ce-08d64f58a2a7?page=1",
        "document": "Reading list",
        "page": 1,
        "document_id": "a45dd78a-29d3-4eb4-96ce-08d64f58a2a7",
        "words_added": 19
      },
      {
        "date": "2026-10-31",
        "document": "Team meetings",
        "document_id": "cab40030-00aa-4b0f-b6f1-8a814b390628",
        "words_added": 239
      },
      {
        "date": "2026-10-31",
        "link": "/remarkable/documents/d9c2ae90-394d-46b3-bfe8-15521a335bb2?page=2",
        "document": "Journal",
        "page": 2,
        "document_id": "d9c2ae90-394d-46b3-bfe8-15521a335bb2",
        "words_added": 20
      }
    ],
    "truncated": true
  }
}

Same call with the Work-only key (documents listed, rows left out):

{
  "pages_written": 21,
  "pages": {
    "total": 21,
    "truncated": true,
    "documents": [
      "Garden app",
      "Q4 roadmap",
      "Team meetings"
    ]
  }
}

Log lines:

[info] get_stats pages for user 1: 5 of 63 pages (truncated at 5)
[info] get_stats for user 1: views summary,pages, filter :all, 2026-10-01..2026-10-31, library all, 63 entries over 27 days, in 22 ms
[info] get_stats pages for user 1: 5 of 21 pages (truncated at 5)
[info] get_stats for user 1: views summary,pages, filter :all, 2026-10-01..2026-10-31, library limited, 21 entries over 18 days, in 11 ms

(The mock's "Team meetings" row has no page because the mock's history rows use made-up page ids, which is the deleted-page case.)

Closes #67 Gives the chat and API-key agents read-only access to the writing stats, so `draw_page` / `make_pdf` reports can be built from them. ## What - **`get_stats`**, a new lookup in `Chat.Tools` (so `AgentTools` gets it too), over a new `Chat.WritingStats` that wraps `RmAnalyzer.Stats`. It returns preset views, never raw entries: - `summary` (`Stats.summary/2`), `by_day` (latest 366 days, `truncated` flag), `over_time` (`by` week|month, `count` 1–104, default 12), `top_words` (`Stats.word_cloud/3`, so hidden words stay hidden; `limit` 1–100, default 25), `on_this_day` (per earlier year: words and up to 20 pages with name, number and link), and `pages` (see below). - `filter` is the Stats page's (`all`, `folder:<id>`, `notebook:<id>`, `tag:<name>`). `from` and `to` take ISO dates, and `to` also sets `as_of` for streaks, periods and on this day. Bad input gets a plain error the agent can fix. - **Scope**: a key limited to some folders only counts entries in `Chat.Notes.reachable_documents/1` (new; `Notes.documents/1` now uses it too). That applies to tag filters as well, and a `folder:`/`notebook:` filter it doesn't reach is refused with the same "There's no …" message the other tools use. - **`by_app`**: unchanged, because `Stats.entries/2` already skips app-written versions. It's tested here so a drawn report doesn't count toward the next one. - **Diagnostics**: `Logger.info` per call (user id, views, filter, date range, all/limited library, entries, days, ms) and `Logger.warning` on a refused call, with the input. - **Descriptions**: the tool description tells the agent to put the numbers in a Markdown table for `make_pdf`/`draw_page`, or a fenced `svg` chart for `draw_page`. The MCP `instructions` and the chat's system prompt mention it too. - **Settings**: it's a read tool, so keys with every tool get it. Keys limited to some tools don't get it until it's ticked: a new **Writing stats** row in Access (screenshots below). - **Docs**: `docs/mcp.md` (Tools, Limiting a key) and `docs/stats.md` (new "For the assistant and agents"). ## Tests - `test/rm_analyzer/chat/writing_stats_test.exs` (11): each view, dates and `as_of`, filters, the `by_app` page (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, no `eggs`/`milk` in top words, no `groceries` anywhere in the JSON), a tag over both notebooks gives 15 not 17, and `notebook:groceries` / `folder:home` are refused. - `notes_mcp_agent_test.exs`: the same over real `/mcp` HTTP with a Work-only key. - Checked that the tests catch a leak: with the reachable-documents filter taken out, the three scope tests fail (3 of 22). - `mix precommit` at ee8c132: **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}`: ```json { "filter": "all", "to": null, "from": null, "as_of": "2026-11-01", "pages_written": 507, "on_this_day": [ { "words": 24, "year": 2025, "pages": [ { "link": "/remarkable/documents/03a3ae33-9013-4c2d-bfc8-1141def35419?page=2", "page": 2, "document_id": "03a3ae33-9013-4c2d-bfc8-1141def35419", "document": "Quick sheets", "words_added": 24 } ] } ], "over_time": { "periods": [ { "start": "2026-09-01", "words": 8782, "days": 26 }, { "start": "2026-10-01", "words": 9062, "days": 27 }, { "start": "2026-11-01", "words": 292, "days": 1 } ], "by": "month" }, "summary": { "current_streak": 23, "longest_streak": { "to": "2026-11-01", "from": "2026-10-10", "days": 23 }, "days_so_far": 305, "days_this_year": 188, "total_days": 251, "total_words": 83739, "words_month": 292, "words_per_day": 329, "words_week": 2189, "words_year": 61943 }, "top_words": [ { "times": 6, "word": "plant" }, { "times": 5, "word": "review" } ] } ``` 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: ```json { "filter": "all", "to": null, "from": "2026-10-26", "as_of": "2026-11-01", "pages_written": 15, "by_day": { "days": [ { "date": "2026-10-26", "words": 239, "pages": 1 }, { "date": "2026-10-27", "words": 252, "pages": 1 } ], "truncated": false }, "summary": { "current_streak": 7, "longest_streak": { "to": "2026-11-01", "from": "2026-10-26", "days": 7 }, "days_so_far": 305, "days_this_year": 7, "total_days": 7, "total_words": 1313, "words_month": 198, "words_per_day": 188, "words_week": 1313, "words_year": 1313 } } ``` Work-only key, `{"filter":"notebook:<Journal, in Personal>"}`: ```json {"content":[{"type":"text","text":"There's no notebook with id d9c2ae90-394d-46b3-bfe8-15521a335bb2 here. List the documents to find one."}],"isError":true} ``` Server log for those calls: ``` [info] get_stats for user 1: views summary,over_time,top_words,on_this_day, filter :all, all dates, library all, 507 entries over 251 days, in 169 ms [info] get_stats for user 1: views summary,by_day, filter :all, 2026-10-26..now, library limited, 15 entries over 7 days, in 8 ms [warning] get_stats for user 1 refused: There's no notebook with id d9c2ae90-… here. List the documents to find one. (input %{"filter" => "notebook:d9c2ae90-…"}) ``` `tools/list` for 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: ![Access, desktop](https://git.knope.dev/attachments/cd5b249b-ddee-4b32-8051-dac47501b439) Phone, 390×844: <img src="https://git.knope.dev/attachments/c38fa0c1-d084-41bb-9d11-a971918e3e2d" width="390" alt="Access, phone"> ## `pages` view (added in 534ab37, ee8c132; month-boundary order fixed in 748078b) For month-end reports (team decision D5): `get_stats from=2026-10-01 to=2026-10-31 views=[summary,by_day,top_words,pages]`, then `read_pages` for what the listed pages say. - One row per page written in the filter/`from`/`to` range: `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) and `words_added` summed over the range. A page since deleted from its notebook comes without `page`/`link`. - Newest first. Capped at 200 with `total` and `truncated`; optional **`page_limit`** (1–200). It's a separate parameter because `limit` is already top_words' (max 100). - Same scope as the other views (built from the same entries): a limited key lists only pages in documents it reaches, and `by_app` versions don't appear. - Order and each page's date use the calendar (`Date.to_gregorian_days`), not Date structs inside tuples, which compare day before month: before 748078b Oct 31 sorted above Nov 2, `page_limit` kept 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.info` per call: pages listed of total, and the cap when it's hit. - Tests (5 new, plus the limited-key test now asks for `pages`): range and order, words summed with the last date, a deleted page, an only-`by_app` day giving no rows, the cap/`page_limit`/its error, and the Work-only key listing only Meetings' pages. - Docs: `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): ```json { "filter": "all", "from": "2026-10-01", "to": "2026-10-31", "pages_written": 63, "pages": { "total": 63, "pages": [ { "date": "2026-10-31", "link": "/remarkable/documents/a45dd78a-29d3-4eb4-96ce-08d64f58a2a7?page=1", "document": "Reading list", "page": 1, "document_id": "a45dd78a-29d3-4eb4-96ce-08d64f58a2a7", "words_added": 19 }, { "date": "2026-10-31", "document": "Team meetings", "document_id": "cab40030-00aa-4b0f-b6f1-8a814b390628", "words_added": 239 }, { "date": "2026-10-31", "link": "/remarkable/documents/d9c2ae90-394d-46b3-bfe8-15521a335bb2?page=2", "document": "Journal", "page": 2, "document_id": "d9c2ae90-394d-46b3-bfe8-15521a335bb2", "words_added": 20 } ], "truncated": true } } ``` Same call with the Work-only key (documents listed, rows left out): ```json { "pages_written": 21, "pages": { "total": 21, "truncated": true, "documents": [ "Garden app", "Q4 roadmap", "Team meetings" ] } } ``` Log lines: ``` [info] get_stats pages for user 1: 5 of 63 pages (truncated at 5) [info] get_stats for user 1: views summary,pages, filter :all, 2026-10-01..2026-10-31, library all, 63 entries over 27 days, in 22 ms [info] get_stats pages for user 1: 5 of 21 pages (truncated at 5) [info] get_stats for user 1: views summary,pages, filter :all, 2026-10-01..2026-10-31, library limited, 21 entries over 18 days, in 11 ms ``` (The mock's "Team meetings" row has no `page` because the mock's history rows use made-up page ids, which is the deleted-page case.)
feat(mcp): get_stats tool for the writing stats, in preset views
Some checks failed
CI / test (pull_request) Failing after 56s
88a7e06d19
Chat and API-key agents get a read-only get_stats lookup over
RmAnalyzer.Stats: summary, by_day, over_time, top_words and on_this_day,
narrowed like the Stats page and by date, as compact JSON. A key limited
to some folders counts only what it reaches and can't filter outside it;
pages the app wrote still don't count.

Closes #67
jfro added this to the v1.6.0 milestone 2026-11-01 14:38:51 +00:00
feat(mcp): get_stats pages view, the pages written in a range
All checks were successful
CI / test (pull_request) Successful in 52s
534ab37abb
One row per page written in the filter and from/to range, newest first:
document id and name, page number, link, the last date it counts on and
the words added over the range. Capped at 200 (page_limit) with total and
truncated; scope and by_app as the other views. For month-end reports:
the month's pages to read with read_pages.

Refs #67
test(mcp): get_stats pages view names a deleted page without a link
All checks were successful
CI / test (pull_request) Successful in 57s
ee8c132546
Refs #67
fix(mcp): get_stats pages view orders by the calendar across months
All checks were successful
CI / test (pull_request) Successful in 54s
748078b937
Date structs inside tuples compare field by field (day before month), so
Oct 31 sorted above Nov 2, page_limit kept the wrong pages, and a page
written Oct 31 and Nov 2 was dated Oct 31. Key on gregorian days.
jfro merged commit f77db656e0 into main 2026-11-01 15:36:42 +00:00
jfro deleted branch feat/get-stats-mcp 2026-11-01 15:36:42 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/rm-analyzer!68
No description provided.