feat(mcp): make background sync opt-in#314
Conversation
Refs zilliztech#285 (txhno's CPU-churn report on multi-instance MCP sessions) and zilliztech#314 (txhno's opt-in background-sync fix). Their analysis showed that periodic background sync should be opt-in to avoid duplicated work when N workspaces each spawn their own MCP. Our trigger watcher is the natural complement: instead of polling every 5 min, the user (or a Claude Code PostToolUse hook) touches ~/.context/.sync-trigger to request an immediate re-index. To make the two play together, lift setupTriggerWatcher() to the start of startBackgroundSync() so the watcher still runs when polling is gated off (the recommended config for multi-instance setups in zilliztech#285). Add CLAUDE_CONTEXT_TRIGGER_WATCHER (default true) so users can opt out of the watcher entirely if they don't want any filesystem watching. Credit: @txhno for zilliztech#285 + zilliztech#314.
|
Thanks @txhno for tackling this — and to @jmmaloney4 for the multi-instance CPU analysis in #285 that motivated it. I have an open PR (#332) that adds a trigger-file watcher. After reading both, I rebased it so Together the two PRs give a clean opt-in matrix:
Happy to swap merge order either way. |
|
This is still a useful fix for #285, but the PR is currently conflicting with master and needs a rebase before it can be merged. When rebasing, please also account for #332 if that lands first. The trigger watcher setup must happen before the The intended combined flow should be: this.setupTriggerWatcher();
if (!isBackgroundSyncEnabled()) {
return;
}
// initial sync + periodic interval only when enabledI also verified this branch by itself with: That build passes, so the blocker is the rebase/combined behavior rather than the basic TypeScript build. |
|
Thanks again for working on this. We shipped v0.1.10 with a lighter compatibility-oriented fix for the multi-process background sync problem: a global cross-process sync lock. Background sync remains enabled for existing users, but only one local MCP server process should perform background sync at a time; other processes skip the cycle while the lock is held. This should address a large part of the CPU churn and contention described in #285 without changing the default background sync behavior. Please try the latest version and let us know if you still see cases where making background sync opt-in is needed. |
d31fd03 to
434705d
Compare
|
Rebased this PR onto current I kept the upstream lock behavior intact, while preserving this PR's opt-in background sync behavior via Validation run locally:
For #332 compatibility, this branch leaves the background-sync gate in |
Refs zilliztech#285 (txhno's CPU-churn report on multi-instance MCP sessions) and zilliztech#314 (txhno's opt-in background-sync fix). Their analysis showed that periodic background sync should be opt-in to avoid duplicated work when N workspaces each spawn their own MCP. Our trigger watcher is the natural complement: instead of polling every 5 min, the user (or a Claude Code PostToolUse hook) touches ~/.context/.sync-trigger to request an immediate re-index. To make the two play together, lift setupTriggerWatcher() to the start of startBackgroundSync() so the watcher still runs when polling is gated off (the recommended config for multi-instance setups in zilliztech#285). Add CLAUDE_CONTEXT_TRIGGER_WATCHER (default true) so users can opt out of the watcher entirely if they don't want any filesystem watching. Credit: @txhno for zilliztech#285 + zilliztech#314.
|
Thanks for the rebase and for validating this on top of the global sync lock. #332 has now been merged, so this PR is conflicting again in When rebasing, please preserve the trigger watcher setup from #332 before any polling/background-sync gate: this.setupTriggerWatcher();
if (!isBackgroundSyncEnabled()) {
return;
}
// startup sync + periodic syncThat keeps on-demand trigger sync available even when periodic polling is disabled. One compatibility concern remains: changing A more compatible version would still add So my recommendation is:
With those changes, this becomes a safer configuration improvement instead of a breaking default-behavior change. |
Fixes #285.
This adds explicit MCP env controls for background sync so local stdio sessions do not automatically rescan indexed codebases unless the user opts in. It also makes the sync interval configurable and documents the new behavior in the MCP help text and README.
Validation:
pnpm installpnpm --filter @zilliz/claude-context-core buildpnpm --filter @zilliz/claude-context-mcp typecheckpnpm --filter @zilliz/claude-context-mcp build