Skip to content

Commit d31fd03

Browse files
feat(mcp): make background sync opt-in
1 parent 6a51b08 commit d31fd03

3 files changed

Lines changed: 85 additions & 7 deletions

File tree

packages/mcp/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ CUSTOM_IGNORE_PATTERNS=temp/**,*.backup,private/**,uploads/**
183183

184184
These settings work in combination with tool parameters - patterns from both sources will be merged together.
185185

186+
#### Background Sync Configuration (Optional)
187+
188+
By default, the MCP server does **not** run background sync. This avoids duplicated rescans when multiple local MCP sessions are open at the same time.
189+
190+
If you want automatic reindexing, enable it explicitly:
191+
192+
```bash
193+
# Opt in to startup + periodic background sync
194+
CLAUDE_CONTEXT_BACKGROUND_SYNC=true
195+
196+
# Optional: control how often sync runs (default: 300000 = 5 minutes)
197+
CLAUDE_CONTEXT_SYNC_INTERVAL_MS=60000
198+
```
199+
200+
This is most useful when you run Claude Context as a single long-lived MCP server. For per-session local stdio setups, leaving background sync disabled usually avoids unnecessary CPU churn.
201+
186202
## Usage with MCP Clients
187203

188204
<details>

packages/mcp/src/config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ Environment Variables:
203203
MILVUS_ADDRESS Milvus address (optional, can be auto-resolved from token)
204204
MILVUS_TOKEN Milvus token (optional, used for authentication and address resolution)
205205
206+
MCP Sync Configuration:
207+
CLAUDE_CONTEXT_BACKGROUND_SYNC Enable background sync for indexed codebases (default: false)
208+
CLAUDE_CONTEXT_SYNC_INTERVAL_MS Background sync interval in milliseconds when enabled (default: 300000)
209+
206210
Examples:
207211
# Start MCP server with OpenAI (default) and explicit Milvus address
208212
OPENAI_API_KEY=sk-xxx MILVUS_ADDRESS=localhost:19530 npx @zilliz/claude-context-mcp@latest
@@ -221,5 +225,8 @@ Examples:
221225
222226
# Start MCP server with Ollama and specific model (using EMBEDDING_MODEL)
223227
EMBEDDING_PROVIDER=Ollama EMBEDDING_MODEL=nomic-embed-text MILVUS_TOKEN=your-token npx @zilliz/claude-context-mcp@latest
228+
229+
# Start MCP server with background sync enabled every minute
230+
OPENAI_API_KEY=sk-xxx MILVUS_TOKEN=your-token CLAUDE_CONTEXT_BACKGROUND_SYNC=true CLAUDE_CONTEXT_SYNC_INTERVAL_MS=60000 npx @zilliz/claude-context-mcp@latest
224231
`);
225-
}
232+
}

packages/mcp/src/sync.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
import * as fs from "fs";
2-
import { Context, FileSynchronizer } from "@zilliz/claude-context-core";
2+
import { Context, FileSynchronizer, envManager } from "@zilliz/claude-context-core";
33
import { SnapshotManager } from "./snapshot.js";
44

5+
const DEFAULT_INITIAL_SYNC_DELAY_MS = 5_000;
6+
const DEFAULT_SYNC_INTERVAL_MS = 5 * 60 * 1000;
7+
const MIN_SYNC_INTERVAL_MS = 1_000;
8+
9+
function isBackgroundSyncEnabled(): boolean {
10+
const value = envManager.get("CLAUDE_CONTEXT_BACKGROUND_SYNC");
11+
if (!value) {
12+
return false;
13+
}
14+
15+
switch (value.trim().toLowerCase()) {
16+
case "1":
17+
case "true":
18+
case "yes":
19+
case "on":
20+
return true;
21+
case "0":
22+
case "false":
23+
case "no":
24+
case "off":
25+
return false;
26+
default:
27+
console.warn(
28+
`[SYNC-DEBUG] Invalid CLAUDE_CONTEXT_BACKGROUND_SYNC value '${value}'. ` +
29+
"Expected true/false. Background sync will remain disabled."
30+
);
31+
return false;
32+
}
33+
}
34+
35+
function getBackgroundSyncIntervalMs(): number {
36+
const value = envManager.get("CLAUDE_CONTEXT_SYNC_INTERVAL_MS");
37+
if (!value) {
38+
return DEFAULT_SYNC_INTERVAL_MS;
39+
}
40+
41+
const intervalMs = Number.parseInt(value, 10);
42+
if (!Number.isFinite(intervalMs) || intervalMs < MIN_SYNC_INTERVAL_MS) {
43+
console.warn(
44+
`[SYNC-DEBUG] Invalid CLAUDE_CONTEXT_SYNC_INTERVAL_MS value '${value}'. ` +
45+
`Falling back to ${DEFAULT_SYNC_INTERVAL_MS}ms.`
46+
);
47+
return DEFAULT_SYNC_INTERVAL_MS;
48+
}
49+
50+
return intervalMs;
51+
}
52+
553
export class SyncManager {
654
private context: Context;
755
private snapshotManager: SnapshotManager;
@@ -114,8 +162,15 @@ export class SyncManager {
114162
public startBackgroundSync(): void {
115163
console.log('[SYNC-DEBUG] startBackgroundSync() called');
116164

165+
if (!isBackgroundSyncEnabled()) {
166+
console.log('[SYNC-DEBUG] Background sync is disabled. Set CLAUDE_CONTEXT_BACKGROUND_SYNC=true to enable it.');
167+
return;
168+
}
169+
170+
const syncIntervalMs = getBackgroundSyncIntervalMs();
171+
117172
// Execute initial sync immediately after a short delay to let server initialize
118-
console.log('[SYNC-DEBUG] Scheduling initial sync in 5 seconds...');
173+
console.log(`[SYNC-DEBUG] Scheduling initial sync in ${DEFAULT_INITIAL_SYNC_DELAY_MS}ms...`);
119174
setTimeout(async () => {
120175
console.log('[SYNC-DEBUG] Executing initial sync after server startup');
121176
try {
@@ -129,15 +184,15 @@ export class SyncManager {
129184
throw error;
130185
}
131186
}
132-
}, 5000); // Initial sync after 5 seconds
187+
}, DEFAULT_INITIAL_SYNC_DELAY_MS);
133188

134189
// Periodically check for file changes and update the index
135-
console.log('[SYNC-DEBUG] Setting up periodic sync every 5 minutes (300000ms)');
190+
console.log(`[SYNC-DEBUG] Setting up periodic sync every ${syncIntervalMs}ms`);
136191
const syncInterval = setInterval(() => {
137192
console.log('[SYNC-DEBUG] Executing scheduled periodic sync');
138193
this.handleSyncIndex();
139-
}, 5 * 60 * 1000); // every 5 minutes
194+
}, syncIntervalMs);
140195

141196
console.log('[SYNC-DEBUG] Background sync setup complete. Interval ID:', syncInterval);
142197
}
143-
}
198+
}

0 commit comments

Comments
 (0)