Skip to content

fix(mcp): mark query tools as read-only - #502

Merged
hellodigua merged 6 commits into
ChatLab:mainfrom
moesnow:fix/mcp-readonly-annotations
Oct 24, 2026
Merged

hellodigua merged 6 commits into
ChatLab:mainfrom
moesnow:fix/mcp-readonly-annotations

Conversation

@moesnow

@moesnow moesnow commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR proposes advertising ChatLab's MCP query tools as read-only to clients, while making the underlying database access migration-free, transactionally consistent, and more narrowly scoped.

  • Register the query tools with readOnlyHint: true, destructiveHint: false, and openWorldHint: false.
  • Use MCP-specific, uncached native SQLite read-only connections inside short BEGIN read transactions. Read-only MCP requests cannot run migrations, modify logical chat records, or repair the compatibility marker. Existing CLI/Desktop writable and legacy read-analysis paths remain separate.
  • Require supported current Schema (v10) for MCP queries: reject outdated v0–v9 sessions with explicit upgrade guidance; reject future and malformed schema versions; enforce the data-directory compatibility marker.
  • Restrict MCP session IDs to real ChatLab database files in the configured session directory, rejecting traversal, unsafe paths, symlinks, and non-ChatLab databases.
  • Reject dangerous user-supplied SQL before SQLite prepare (and before opening the database); allow SELECT/WITH queries while retaining SQLite's native read-only statement check.
  • Close all per-request connections, including per-session enumeration, tool errors, and Resource failures. No full database/WAL copying or external temporary chat-data snapshots.

Maintainer decision requested: WAL/SHM versus readOnlyHint

There is an important, openly documented interpretation question. SQLite's native readonly: true connections do not intentionally modify logical chat content, Schema, or compatibility metadata, but ordinary WAL-mode reads can create or update -wal/-shm auxiliary coordination files. Consequently, they do not guarantee zero filesystem changes, and reads may fail on strictly read-only directories that require sidecar creation.

The readOnlyHint: true annotation here is proposed to preserve the original user-facing client read-only/approval behavior under a logical-data read-only interpretation. The MCP annotation's broader "does not modify its environment" wording may warrant the stricter interpretation instead. We request the upstream maintainer's decision on whether to keep this hint or adjust it and the PR's scope; the hint is not a security boundary.

A previous attempt to avoid all source-directory sidecar changes by copying the entire DB/WAL on every request was removed after independent architectural review: it had unacceptable resource/privacy costs and could read uncommitted rollback-journal pages. Native SQLite transactions give the consistent committed snapshot we need; this revision intentionally does not restore the full-copy workaround.

Verification

  • 111/111 targeted tests pass (MCP Tools/Resources, actual client annotations and SQL pre-open checks, path and symlink boundaries, real v4 legacy migrations, Schema v0–v11 and malformed values, compatibility markers, CLI analyzeAutoImport regression, live WAL snapshots, rollback spill, and connection closure).
  • pnpm --filter chatlab-mcp build, ESLint, Prettier, and git diff --check: pass.
  • The existing workspace-wide type-check:node is still not clean (55 diagnostics, primarily unrelated CLI dependencies/typings in the sandbox); there are no diagnostics in the revised/new file paths.
  • Fresh GitHub CI and actual Windows/macOS filesystem behavior not yet verified.

Scope: MCP querying is logically non-writing but is not promised to be zero-filesystem-side-effect. Historical databases must be explicitly upgraded by a writable ChatLab runtime before MCP can query them.

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-24T01:36:19.509308Z
We couldn’t complete this review. Comment @codex review to try again.
1e6890f Manual request
ℹ️ 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.

@hellodigua

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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".

@hellodigua

Copy link
Copy Markdown
Member

@codex review

@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: 42a3df17ce

ℹ️ 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 packages/mcp-server/src/server.ts Resolved

@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: 80d3076c7e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/mcp-server/src/server.ts Resolved

@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: af7e7eb144

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@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: 0c7c77fb5d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/mcp-server/src/server.ts Resolved

@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: 2109506c52

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

moesnow commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@hellodigua 你好,想请你帮忙对 PR #502 最后的一个产品语义问题做决定。🙏

这次更新到 1e6890f 后,已经撤除了此前的全库 DB/WAL 临时快照,改回 SQLite 原生只读连接 + 短读事务。同时补齐了 MCP 的旧 Schema/兼容门禁、会话路径约束、SQL prepare 前检查以及异常连接关闭。关联测试 111/111 通过,MCP build、ESLint、Prettier 也通过;完整工作区类型检查仍有 55 条既有诊断,跨平台 CI 尚待验证。

现在唯一想请你定夺的是 readOnlyHint: true 应该如何理解:

  • 从聊天数据的角度,MCP 工具不会修改聊天内容、Schema 或兼容 marker,也不会隐式迁移数据库;因此我倾向于保留 readOnlyHint: true,实现这个 PR 最初希望改善 MCP 客户端只读分类和审批体验的目标。
  • 但从整个文件系统环境的角度,SQLite 在 WAL 模式下即便用 readonly: true 打开,仍可能创建或更新 -wal / -shm 辅助协调文件;如果目录完全禁止写入,某些情况下也会读取失败。因此这里不能保证原始目录零变化,也不能不加说明地声称符合最严格的环境不修改解释。

我们试过通过每次完整复制 DB/WAL 来保证源目录不变,但独立审计发现该方案有回滚日志下读取未提交数据、一边写入一边拒绝读取、磁盘 I/O 和敏感副本残留等问题,所以已经彻底回退,避免为了一个注解引入更严重的风险。

想请你最终决定:在明确披露 WAL/SHM 协调行为的前提下,是否可以接受按“不会修改用户逻辑数据”理解并保留 readOnlyHint: true? 如果你认为规范应按更严格的“环境完全无变化”理解,我愿按你的建议调整为 false,或重新界定/拆分本 PR 的交付范围。

感谢你审阅,也抱歉这次迭代比较长!

@hellodigua

Copy link
Copy Markdown
Member

@moesnow 感谢这轮梳理和修正。同意保留 readOnlyHint: true。

ChatLab 在这里采用“用户逻辑数据只读”的产品边界:MCP 查询不得修改聊天记录、Schema 或兼容标记,也不得隐式执行迁移;接受 SQLite 原生一致性读取所需的 WAL/SHM 协调行为,但不承诺源目录零变化,也不保证严格只读挂载下始终可用。这是本项目明确采用的语义解释,不代表 MCP 规范已经明确豁免这类文件系统副作用。

请保留 README 中的限制说明。严格只读环境无法读取时,应明确报错,不绕过权限。不恢复全库 DB/WAL 复制,也不要为规避辅助文件而牺牲读取一致性或引入敏感数据副本。

后续评审以逻辑数据不变、不隐式迁移、事务一致性和连接正确关闭为边界,不再仅因正常 WAL/SHM 协调行为阻塞此 PR。

这个决定仅解决产品语义问题,不代表可以跳过合并验证。合并前请确认最新提交的 CI 结果,并核实所述 55 条类型诊断确实在相同环境的基线分支上也存在;若有本 PR 引入的问题,仍需修复。感谢!

@hellodigua

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@hellodigua

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

moesnow commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@hellodigua 感谢确认 readOnlyHint: true 的产品语义,并逐项复核之前的审查意见!

补充同步一下最新验证结果:

此前提到的 TypeScript 诊断,已确认由项目依赖安装不完整导致。按照项目锁文件补全依赖后,无需修改任何源码,完整类型检查现已通过。

重新验证结果:

  • Node + Desktop 完整 TypeScript 类型检查:通过
  • Workspace 依赖声明检查:通过
  • MCP 构建:通过
  • 111 项相关回归测试:全部通过

最新提交仍为 1e6890f,无需追加修复提交。

此前的类型检查问题现已解决,感谢审阅!

@hellodigua
hellodigua merged commit 1a214a5 into ChatLab:main Oct 24, 2026
1 check passed
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