Skip to content

feat(mcp): add --read-only flag for DuckDB MCP access - #329

Merged
ofriw merged 6 commits into
chunkhound:mainfrom
e91d82c73:read_only
Jun 21, 2026
Merged

ofriw merged 6 commits into
chunkhound:mainfrom
e91d82c73:read_only

Conversation

@e91d82c73

Copy link
Copy Markdown
Collaborator

Note: This PR was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!


Add --read-only flag for safe DuckDB MCP access

Adds a --read-only flag to chunkhound mcp that opens the DuckDB file read-only for the MCP session and turns off every write path that would otherwise run alongside the query server (initial scan, realtime watcher, WAL cleanup, checkpoint-on-close). The flag is DuckDB-only and Config.validate_for_command rejects read_only=True for any non-mcp subcommand or non-DuckDB provider, so a stray JSON/env value can't silently break writers.

Read-only also forces single-process stdio (no_daemon = True). The daemon exists to coordinate multi-client writers against a single DuckDB lock — when nothing writes, the coordination layer is just overhead and an extra failure surface, so we skip it. A few defensive details worth highlighting: we drop the flag for :memory: DBs (nothing to read), we never run WAL recovery under read-only (recovery rewrites files), and a pending WAL produces an explicit error pointing the user at the fix rather than a cryptic DuckDB message.

The value here is letting you point an MCP client at a pre-indexed DB — shared, mounted read-only, or otherwise untouchable — without risking writes from the indexer or daemon. Tests cover the three user-visible contracts: read-only open serves regex search, writes through the provider's public API raise, and the config validator rejects the flag for non-MCP commands.

AGENT_REVIEW.md

e91d82c73 added 2 commits June 9, 2026 12:47
  Opens the DuckDB database read-only for the duration of the MCP session
  and disables every write path that would otherwise run alongside a query
  server.

@ofriw ofriw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note: This review was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!


Nice direction overall, but I think this still needs a couple of fixes before we can merge it. The biggest issue is that --read-only does not fully uphold its contract yet: startup still has write-capable paths in places that run before the read-only guards really take effect. Concretely, DatabaseConfig.get_db_path() still creates directories, MCP init still unconditionally creates the DB parent directory, and the connection manager's _load_extensions() runs INSTALL vss unconditionally after connection (distinct from _preemptive_wal_cleanup, which the PR correctly skips). The executor thread's _create_connection() in duckdb_provider.py also runs INSTALL vss. If the extension cache is empty, either path can write to the filesystem or fail on an immutable mount, contradicting the feature's promise.

There is also a user-visible status problem: in read-only mode you skip realtime startup and the initial scan, but daemon_status still derives readiness exclusively from scan_completed_at. In practice that means a pre-indexed read-only session can serve queries while still reporting query_ready=false / initializing, which is misleading for clients. Finally, the config surface and tests need one more pass: database.read_only was added to the model but not wired through env loading, and the new tests do not cover the highest-risk behavior changes yet (forced stdio/no-daemon, skipped scan/watcher startup, and the explicit read-only WAL failure path).


Attached is an agent optimized description of the changes in this PR - AGENT_REVIEW.md

@e91d82c73
e91d82c73 requested a review from ofriw June 15, 2026 07:25

@ofriw ofriw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@ofriw
ofriw added this pull request to the merge queue Jun 21, 2026
Merged via the queue into chunkhound:main with commit 4f82072 Jun 21, 2026
17 checks passed
ofriw added a commit that referenced this pull request Jun 22, 2026
Brings in the read-only mode feature from PR #329 and resolves
conflicts with the compaction subsystem:

- database_config.py: kept both read_only and fragmentation_threshold_pct fields
- connection_manager.py: compaction recovery first, then read_only guard on WAL cleanup
- duckdb_provider.py: read-only DDL skip + elaborated checkpoint skip reasons
HananKavitzAmat pushed a commit to HananKavitzAmat/chunkhound that referenced this pull request Jun 28, 2026
* feat(mcp): add --read-only flag for safe DuckDB MCP access

  Opens the DuckDB database read-only for the duration of the MCP session
  and disables every write path that would otherwise run alongside a query
  server.

* docs(changelog): document `chunkhound mcp --read-only` and warn when it forces single-process stdio

* feat(mcp): finish --read-only wiring and rename scan_completed_at → query_ready_at

* test(mcp): cover stale-WAL hint, query_ready_at, and forced-stdio for --read-only

* test: stabilize chrome headless flags and stub deferred connect in watchman fallback tests

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
ofriw added a commit that referenced this pull request Jul 6, 2026
…CP read-only, and fixes

Merge main into matryoshka-fix-6-26.

main changes incorporated:
- DuckDB compaction with atomic swap (#339)
- PowerShell (#350) and Metal (#351) language parsers
- MCP --read-only flag (#329)
- Research per-step timing (#355)
- Various fixes: websearch late-completion guard, quickresearch
  orphan cleanup, token literal counting, claude-cli auto-updater
- openai_compatible in factory's supported_providers()

Conflict resolution: chunkhound/services/realtime_indexing_service.py
was deleted on main (dead code, no production callers). The branch's
ModuleType __class__ hack + its test were removed to match.
@e91d82c73
e91d82c73 deleted the read_only branch July 20, 2026 10:56
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.

2 participants