Skip to content

feat(mcp): add a read-only mode that hides the write tools - #2147

Open
Goldloli wants to merge 2 commits into
basicmachines-co:mainfrom
Goldloli:feat/mcp-read-only-mode
Open

Goldloli wants to merge 2 commits into
basicmachines-co:mainfrom
Goldloli:feat/mcp-read-only-mode

Conversation

@Goldloli

Copy link
Copy Markdown

What this changes

Adds a read_only setting (env BASIC_MEMORY_READ_ONLY, default off). With it on, the MCP server offers only the tools annotated readOnlyHint: true, and the hidden write tools cannot be called. Indexing keeps running, so notes written on disk by another process still show up in search.

Hidden in read-only mode: bm_write, bm_edit, bm_move, bm_delete, bm_project_create. Everything else (bm_read, bm_search, bm_context, bm_list, bm_find, bm_man, bm_project_list, bm_recent, bm_schema_*) stays listed.

Why

A shared Basic Memory project is often read by several agents while all writes go through a separate reviewed process. Today every write tool is always offered, and only bm_project_delete can be hidden, so getting a read-only tool set needs a filtering proxy in front of the server. Client-side deny lists differ per harness and have to be repeated for each one.

Fixes #2134

How it works

  • BasicMemoryConfig.read_only is a plain bool field, so pydantic-settings already rejects values it cannot parse. BASIC_MEMORY_READ_ONLY=maybe fails at startup instead of silently leaving read-only off.
  • Each write tool carries a WRITES_TAG. set_read_only_visibility hides by that tag, on the local provider, like the existing set_project_delete_visibility.
  • test_writes_tag_matches_read_only_annotations asserts the tagged set equals the set of tools annotated readOnlyHint: false. A write tool added later without the tag fails that test instead of staying reachable in read-only mode.
  • The two visibility flags (read_only, enable_project_delete) are now composed into one decision. FastMCP visibility marks are "last appended wins", so marking writes visible again turned bm_project_delete back on even though enable_project_delete was still off. Both flags now recompute the hidden set together and re-mark the complement by name.

Testing

Installed the dev environment in an isolated venv (uv sync, Python 3.14) and ran, with BASIC_MEMORY_CONFIG_DIR pointed at a temp dir:

pytest tests/mcp/test_v1_registration.py tests/mcp/test_tool_bm_project.py \
       tests/mcp/test_tool_contracts.py \
       tests/test_config.py::TestLegacySyncChangesMigration \
       tests/test_config.py::TestBasicMemoryConfig -q
# 93 passed
pytest tests/mcp -q --timeout=300
# 1305 passed, 1 failed

The one failure is tests/mcp/test_tool_bm_search_query.py::test_since_filters_by_update_time, and it fails the same way on the unmodified main checkout in this environment (assert [] == ['Recent Note']), so it is not caused by this change.

Static checks, on the changed files and then on the whole tree:

ruff check src tests            # All checks passed!
ruff format --check src tests   # 1062 files already formatted
ty check <changed files> --python .venv   # All checks passed!

Mutation checks, each on the changed code, to confirm the new tests are load-bearing:

Mutation Result
ignore the read_only flag when computing the hidden set test_read_only_mode_hides_and_blocks_write_tools fails
skip re-marking the visible complement 5 tests fail, including test_project_delete_is_hidden_by_default
drop WRITES_TAG from bm_delete test_writes_tag_matches_read_only_annotations fails

All three were reverted before committing.

Behaviour

  • BASIC_MEMORY_READ_ONLY unset or false: unchanged, all tools listed.
  • BASIC_MEMORY_READ_ONLY=true: write tools are not listed, and calling one raises NotFoundError — the same path a retired tool takes. enable_project_delete keeps working independently.

Risk

Small and opt-in: the default is off, and no tool behaviour changes. The one behavioural change for existing users is that set_project_delete_visibility now also re-marks the visible tools by name; test_project_delete_is_hidden_by_default and test_config_flag_lists_project_delete_through_lifespan cover that path.

A shared knowledge base can be read by several agents while writes stay
with a reviewed process. Until now every write tool was always offered;
only bm_project_delete could be hidden.

Add a `read_only` setting (env `BASIC_MEMORY_READ_ONLY`, default off).
When it is on, the MCP server lists only the tools annotated
readOnlyHint: true, and the hidden ones cannot be called. Indexing keeps
running, so notes written on disk still appear in search.

The write tools carry a WRITES_TAG, and a test asserts the tag set equals
the set of tools annotated readOnlyHint: false, so a tool added later
cannot silently stay reachable in read-only mode.

The two visibility flags are now composed into a single decision. Marks
are "last appended wins", so the earlier enable/disable mark per flag
re-shown bm_project_delete whenever read-only mode was turned back off.

Refs basicmachines-co#2134

Signed-off-by: Goldloli <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T04:53:25.995410Z 927bbfa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 927bbfaae9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/basic_memory/mcp/server.py Resolved
Hiding the write tools was only half the answer: the instructions still told
every connected model to save with bm_write, and the getting_started prompt
still walked a new user through creating a first note with it, so both pointed
at calls the server now refuses.

The instructions switch with the same flag that hides the tools, and the
offline prompt takes a read-only branch. Both are pinned by tests that fail
when the switch is removed.
@Goldloli

Copy link
Copy Markdown
Author

Right on both counts — fixed in 94eb553.

The instructions switch with the mode. _apply_tool_visibility now sets server._mcp_server.instructions alongside the visibility marks, choosing between BASIC_MEMORY_INSTRUCTIONS and a new READ_ONLY_INSTRUCTIONS. The read-only text keeps the reading half (search, read, context, the project-parameter rules, the manual pointer) and drops every reference to a write tool, replacing the save-and-tell paragraph with "this server is read-only, do not offer to save anything". Both flags go through the same recompute, so turning read-only off restores the write guidance.

getting_started takes a read-only branch. It reads ConfigManager().load_config().read_only and, when set, replaces the two-path onboarding with a reading-only one: explain that notes written on disk appear in search, open one with bm_read or look a topic up with bm_search. No bm_write call, no save-and-tell sentence.

Two regression tests, both verified to fail without the change:

  • test_read_only_mode_swaps_the_server_instructions asserts no write tool is named in read-only instructions and that bm_write returns when the mode is off. Removing the instructions assignment fails it with read-only instructions must not name the hidden tool bm_write.
  • test_getting_started_drops_the_write_path_in_read_only_mode patches ConfigManager and asserts the prompt contains no bm_write while keeping bm_read and bm_search. Forcing read_only = False fails it with a read-only server must not offer a save.
$ pytest tests/mcp -q     → 1307 passed, 1 failed

The one failure is test_tool_bm_search_query.py::test_since_filters_by_update_time, which asserts [] == ['Recent Note'] and also fails on a clean checkout of this branch with my changes stashed, so it is not related to this commit.

@Flynn-Kawolsky

Copy link
Copy Markdown

Thank you for putting this together — this directly solves a real multi-harness pain point for us.

We run one primary MCP client with full write + live indexing access alongside two secondary read-only MCP clients (different harnesses pointing at the same SQLite database and markdown vault). Today we have to maintain separate per-client enabledTools allowlists that break whenever tool names change (e.g. 1.0 -> 1.1).

One subtle operational detail worth calling out in the docs (or pairing with an option / CLI flag):

Because read_only=true intentionally keeps file indexing running (index_changes=true by default):

  1. Disk mutations still happen on "read-only" instances: during sync, indexing still writes permalink: into frontmatter on markdown files (even with ensure_frontmatter_on_sync: false, see Adding the permalink re-serializes the whole frontmatter: indexing a flow-style vault rewrites every note #2125), so a read-only MCP server can still modify files on disk.
  2. Multi-client SQLite / watcher contention: when a user runs one primary read-write MCP server plus 2+ read-only MCP servers against the same local SQLite vault, leaving index_changes=true on the read-only servers spawns redundant file watchers, duplicate startup scans, and concurrent memory.db / config.json writes.

In our setup, pairing BASIC_MEMORY_READ_ONLY=true with BASIC_MEMORY_INDEX_CHANGES=false on secondary read-only harnesses gives true zero-write, single-writer safety while the primary host handles indexing. Mentioning BASIC_MEMORY_INDEX_CHANGES=false in the read-only mode documentation (and/or considering a --read-only CLI flag on bm mcp per #2134) would save multi-client users from surprising disk writes and SQLite contention.

(Also a quick heads-up: the CLAassistant check on this PR is still showing unsigned — signing at the link above should clear that gate.)

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.

[FEATURE] Read-only mode for the MCP server

3 participants