Skip to content

Commit 060ab22

Browse files
authored
feat: signal indexing not done during search (#47)
1 parent b14b2d8 commit 060ab22

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ cocoindex-code index
9898

9999
This lets you monitor the indexing process and ensure everything is ready. After the initial build, the MCP server will automatically keep the index up-to-date in the background as files change.
100100

101-
> For small projects you can skip this step — the MCP server will build the index automatically on first use.
101+
For small projects you can skip this step — the MCP server will build the index automatically on first use.
102102

103103
## When Is the MCP Triggered?
104104

@@ -273,7 +273,7 @@ claude mcp add cocoindex-code \
273273
-- cocoindex-code
274274
```
275275

276-
> **Note:** Switching models requires re-indexing your codebase (the vector dimensions differ).
276+
**Note:** Switching models requires re-indexing your codebase (the vector dimensions differ).
277277

278278
## MCP Tools
279279

src/cocoindex_code/server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
# Lock to prevent concurrent index updates
3131
_index_lock = asyncio.Lock()
3232

33+
# Event set once the initial background index is ready
34+
_initial_index_done = asyncio.Event()
35+
3336

3437
async def _refresh_index() -> None:
3538
"""Refresh the index. Uses lock to prevent concurrent updates."""
@@ -123,6 +126,16 @@ async def search(
123126
),
124127
) -> SearchResultModel:
125128
"""Query the codebase index."""
129+
if not _initial_index_done.is_set():
130+
return SearchResultModel(
131+
success=False,
132+
message=(
133+
"The index is still being built — this may take a while"
134+
" for a large codebase or after significant changes."
135+
" Please try again shortly."
136+
),
137+
)
138+
126139
try:
127140
# Refresh index if requested
128141
if refresh_index:
@@ -167,8 +180,15 @@ async def search(
167180

168181
async def _async_serve() -> None:
169182
"""Async entry point for the MCP server."""
183+
170184
# Refresh index in background so startup isn't blocked
171-
asyncio.create_task(_refresh_index())
185+
async def _initial_index() -> None:
186+
try:
187+
await _refresh_index()
188+
finally:
189+
_initial_index_done.set()
190+
191+
asyncio.create_task(_initial_index())
172192
await mcp.run_stdio_async()
173193

174194

0 commit comments

Comments
 (0)