working-with-notion-programmatically
When to use
Drive the Notion MCP server via mcpc for search, fetch, comments, and follow-on Notion tools using deterministic scripts. This skill favors Code Mode patterns: auth preflight, dynamic schema caching, file-backed outputs, and small templates.
Requirements
- •Interactive auth: Notion MCP login needs network, a browser, and a foreground shell. The agent cannot automate it. If preflight fails, the user must run
mcpc https://mcp.notion.com/mcp login --profile <name>manually, then retry. - •Shell: PowerShell (quote
@notionas'@notion'). - •Tools:
mcpcon PATH; Python 3.12+ for scripts;jqoptional for local inspection.
Degrees of freedom & trust
- •Always (explore): Read local files, inspect
references/,templates/,mcp_tools/, view script help, and run auth preflight that only reads profiles. - •Ask (shape/execute): Any
mcpccall (network), connecting sessions, and all helper scripts (they callmcpc). Prompt the user to login in their own shell/browser if auth is missing. - •Never: Destructive Notion moves/deletes without explicit user instruction; backgrounding or auto-opening
mcpc ... login.
Canonical loop
- •Run auth preflight (built into scripts via
notion_common.ensure_auth) to ensure profile + connectivity; if it fails, instruct the user to login manually. - •Cache tool schemas as needed into
mcp_tools/(see instructions below). - •Shape a plan using
templates/plan.search.jsonortemplates/plan.fetch.json; note expected outputs. - •Execute scripts (search/fetch/comments) — they write outputs under
results/and keep stdout minimal. - •Append progress/errors; reuse cached outputs instead of re-running when possible.
Artifacts & caching
- •
templates/:plan.*.json,results.*.json,progress.logskeleton. - •
mcp_tools/: Cache of tool schemas pulled viamcpc --json … tools-get …to avoid large responses in context. - •
references/: Minimal pointers for filters, tool names, and PowerShell quoting. - •Runtime outputs: scripts write to
results/(JSON + snippets) so the model can re-open files instead of re-calling MCP.
Scripts (all gate on auth preflight)
- •
scripts/notion_search.py: search with filters; supports--pretty,--ids-only,--output. Writes parsed payload toresults/search.jsonby default. - •
scripts/notion_fetch.py: fetch page/database;--outputwrites raw JSON (defaultresults/fetch.json). - •
scripts/notion_fetch_plain.py: fetch + compact snippet;--outputwrites summary text (defaultresults/fetch_snippet.txt) and payload JSON if requested. - •
scripts/notion_comments.py: fetch comments, optional author filter;--outputwrites parsed comments (defaultresults/comments.json). - •
scripts/notion_pretty.py: pretty-printer for anymcpc --jsonoutput (no network).
Authentication (manual, interactive)
- •User must run login in a foreground shell with a browser:
mcpc https://mcp.notion.com/mcp login --profile <name>
Default profile:default. Server override: setMCP_SERVER. - •Scripts check
~/.mcpc/profiles.jsonfor the profile/server and then run a lightweight connectivity probe (tools-list). On failure they exit with clear instructions to login manually; they do not open a browser.
Restart/resume
- •Reuse
results/files before re-running network calls. - •Keep notes in
progress.log. If interrupted, read existing outputs and continue from step 3 of the loop.
Avoid
- •Dumping large MCP JSON into chat; point to files under
results/ormcp_tools/. - •Mixing freedom levels in one step; decide → configure → execute.
New rails for search→fetch→mentions→users
- •
scripts/notion_search_and_fetch.py: search + fetch in one run; outputs search payload + search IDs (results/search_and_fetch.search_ids.txt), fetch IDs (results/search_and_fetch.fetch_ids.txt), snippets, fetch JSON, optional payloads dir. - •
scripts/notion_bulk_fetch.py: fetch IDs from--ids/--ids-file; outputs JSON, snippets, andresults/bulk_fetch.ids.txtfor downstream steps. - •
scripts/notion_extract_mentions.py: scan payload JSONs (defaultresults/payloads) foruser://…mentions; outputsresults/mentions.ids.txtandresults/mentions.json(counts per file/total). - •
scripts/notion_resolve_users.py: resolve user IDs vianotion-get-user; outputsresults/user_lookup.jsonandresults/user_lookup.txt.
Recommended pipeline
- •
notion_search_and_fetch.py --query "<text>" --payload-dir results/payloads - •
notion_extract_mentions.py --payload-dir results/payloads - •
notion_resolve_users.py --ids-file results/mentions.ids.txt