Skip to content

Fix(mcp): a server switching its own vault stops calls until vault_info - #37

Merged
adibhanna merged 3 commits into
mainfrom
claude/infallible-galileo-3af628
Nov 4, 2026
Merged

adibhanna merged 3 commits into
mainfrom
claude/infallible-galileo-3af628

Conversation

@adibhanna

@adibhanna adibhanna commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

zn mcp promises that a vault switch never redirects work silently: once the vault changes, every call but vault_info stops until vault_info confirms it. That did not hold when a self-hosted server changed its own vault, because backend.SameVault compared only the URL.

After POST /api/vault/select, on current main:

  • A client scoped with --allow-path Projects listed and read the new vault's Projects/ with no stop.
  • An unscoped write_note overwrote the note at the same path in the new vault.

What changes

  • The check. Each call on a server first asks GET /api/vault and compares the reported path. The existing confirm-with-vault_info flow then covers this switch.
  • When the session moves. Only once vault_info has its answer ready, for every kind of switch, local vaults included. Before this, the session moved before vault_info ran, so a vault_info that failed still cleared the stop and the assistant was never told, and a write sent beside vault_info could run in the new vault.
  • Parallel batches. Every call that begins before that move stops, vault_info beside it included, even if its own answer from the server arrives after the move.
  • Not a switch. A new display name or a new token. Leaving a vault and returning with no call in between does not stop anything either, the same as local vaults today.
  • A server that cannot answer. The call stops with that error and the session stays on its vault. A 401 still gets the token hint that names the server.
  • Moving to another server or a local vault. A call that will stop anyway does not contact the new server, so it still gets the switch error even when that server is down.
  • The error. A server's vault goes by its name, never its path: it was the vault "a" on the server at … and is now the vault "b" on the server at …. Two vaults with one name read a different vault with the same name.
  • Scoped clients and local vaults. They were getting absolute paths in the switch error and the vault_info note. They now get the folder name.

Cost

One more request per call on a server, none for a local vault. Measured with 5,003 notes on loopback through a counting proxy:

Tool Unscoped requests Scoped requests Unscoped p50 Scoped p50
read_note 1 to 2 6 to 7 1.6 to 1.5 ms 32.2 to 36.5 ms
list_notes 1 to 2 5 to 6 22.7 to 26.8 ms 31.8 to 34.0 ms
search_text 1 to 2 6 to 7 18.0 to 21.3 ms 50.0 to 53.8 ms

These are single runs on the first commit, so small differences may be noise. Later commits did not add a request, and TestRunAsksTheServerOncePerCall pins that for an unscoped call. Over a slow network, expect one extra round trip per call.

Known limits

  • This does not make a switch safe mid-call. A switch that lands between the check and the call's own requests still gets through. The tools that read a note and write it back (replace_in_note, append_to_note, prepend_to_note, insert_at_line, edit_section, toggle_task, update_task) carry the old vault's body across those requests. If the switch lands between the read and the write, the call overwrites the note at that path in the other vault with the old vault's content and reports success. add_comment, reply_to_comment and resolve_comment do the same with a note's comments. The next call's stop says "Nothing ran", which is true of that call, and nothing mentions the earlier write. An independent review reproduced this with replace_in_note; the other tools follow from the same read-then-write code. Closing it needs the server to refuse a request meant for another vault.
  • A call that begins after vault_info has moved the session, but was sent before the client had the answer, can still run in the new vault.
  • A launch-flag scope such as --allow-path applies to the new vault once the assistant has called vault_info.
  • The switch error shows a scoped client the vault's display name, which its vault_info does not include.
  • A server that reports no path falls back to comparing the URL, as before.
  • The desktop's VaultSession (apps/desktop/src/mcp/server.ts) has the same gap. That was confirmed by reading its code, not by running it, and it is not fixed here.

Testing

  • On this change merged with main, including Feat(mcp): attachments, task edits, daily notes and note sections as tools #35: go test ./... passes, and internal/mcp and internal/backend also pass with -race -count=3. TestRunStopsWhenTheServerSwitchesItsVault fails if the path comparison is removed, and TestRunFailedVaultInfoKeepsTheStop and TestRunWriteBesideARunningVaultInfoStops both fail on the first commit of this PR.
  • A throwaway driver, not in this PR, ran the stock zn mcp binary over stdio against a real server 2.82.0. All 49 checks passed: the stops for scoped and unscoped clients, a parallel batch, confirming, a real display name change, a vault pinned by ZENNOTES_VAULT_PATH (409), a wrong token, a server restart, an injected 500 during vault_info, and 60 batches pairing write_note with vault_info, 30 in each order.
  • The same driver shows the bugs. On main, a write overwrites the other vault's note on disk. On this PR's first commit, the failed vault_info case fails, and 31 of the 60 batched writes ran and changed a note. With this change, none did. That driver run is evidence for this race, not proof the window is gone: see Known limits.

zn mcp promises that a vault switch never redirects work silently, but a
remote target was compared by URL alone. After POST /api/vault/select a running
client kept working in the new vault with no stop: a client scoped with
--allow-path read notes there, and an unscoped write_note overwrote the note at
the same path.

Each call on a server now asks GET /api/vault first and compares the reported
path, so the existing confirm-with-vault_info flow covers this switch too. A
new display name or token is not a switch. A server that cannot answer stops
the call with that error and the session stays on its vault. A call that will
stop anyway because the session is on another server or a local vault does not
contact the new server.

The error names a server's vault by its name, never its path. A client with
scoped access also no longer gets a local vault's absolute path in the switch
error or the vault_info note.

This costs one request per call on a server. A switch landing between that
check and the call's own requests still gets through; closing that needs the
server to refuse a request meant for another vault.
The session moved to a new vault before vault_info ran. A vault_info that
failed still cleared the stop, so the retried write ran in the new vault and
the assistant was never told. The SDK runs handlers concurrently, so a write
sent beside vault_info could also run in the new vault.

The session now moves only when vault_info's answer is ready, for every kind
of switch. A call also keeps the vault it began on, so one still waiting for
the server's answer when vault_info finishes stops too, and does not pull the
session back.

A call that begins after the move, but was sent before the client had the
answer, can still pass. The session's comment now also says plainly what a
switch in the middle of a call does: a tool that reads a note and writes it
back overwrites the other vault's note and reports success.
@adibhanna
adibhanna merged commit f7b4c99 into main Nov 4, 2026
5 checks passed
@adibhanna adibhanna mentioned this pull request Nov 4, 2026
adibhanna added a commit to ZenNotes/zennotes that referenced this pull request Nov 4, 2026
The session compared only a server's URL, so after a server changed its vault
the next call ran in the new one. Each call on a server now first asks which
vault it is serving and compares the path. The existing confirm-with-vault_info
flow then covers the switch.

The session moves only once vault_info has its answer ready, for every kind of
switch, so a vault_info that fails leaves the stop in place, and a call that
began before the move stops. A new display name or token is not a switch. A
server that cannot answer stops the call and the session stays on its vault. A
call that will stop anyway does not contact a new server.

The same behaviour as ZenNotes/tui#37.
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.

1 participant