Repository navigation
Fix(mcp): a server switching its own vault stops calls until vault_info - #37
Merged
Merged
Conversation
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.
Merged
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.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zn mcppromises that a vault switch never redirects work silently: once the vault changes, every call butvault_infostops untilvault_infoconfirms it. That did not hold when a self-hosted server changed its own vault, becausebackend.SameVaultcompared only the URL.After
POST /api/vault/select, on currentmain:--allow-path Projectslisted and read the new vault'sProjects/with no stop.write_noteoverwrote the note at the same path in the new vault.What changes
GET /api/vaultand compares the reported path. The existing confirm-with-vault_infoflow then covers this switch.vault_infohas its answer ready, for every kind of switch, local vaults included. Before this, the session moved beforevault_inforan, so avault_infothat failed still cleared the stop and the assistant was never told, and a write sent besidevault_infocould run in the new vault.vault_infobeside it included, even if its own answer from the server arrives after the move.it was the vault "a" on the server at … and is now the vault "b" on the server at …. Two vaults with one name reada different vault with the same name.vault_infonote. 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:
read_notelist_notessearch_textThese are single runs on the first commit, so small differences may be noise. Later commits did not add a request, and
TestRunAsksTheServerOncePerCallpins that for an unscoped call. Over a slow network, expect one extra round trip per call.Known limits
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_commentandresolve_commentdo 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 withreplace_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.vault_infohas moved the session, but was sent before the client had the answer, can still run in the new vault.--allow-pathapplies to the new vault once the assistant has calledvault_info.vault_infodoes not include.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
main, including Feat(mcp): attachments, task edits, daily notes and note sections as tools #35:go test ./...passes, andinternal/mcpandinternal/backendalso pass with-race -count=3.TestRunStopsWhenTheServerSwitchesItsVaultfails if the path comparison is removed, andTestRunFailedVaultInfoKeepsTheStopandTestRunWriteBesideARunningVaultInfoStopsboth fail on the first commit of this PR.zn mcpbinary 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 byZENNOTES_VAULT_PATH(409), a wrong token, a server restart, an injected 500 duringvault_info, and 60 batches pairingwrite_notewithvault_info, 30 in each order.main, a write overwrites the other vault's note on disk. On this PR's first commit, the failedvault_infocase 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.