Add single-instance guard for MCP stdio server#31
Open
Hohlas wants to merge 1 commit intolyonzin:masterfrom
Open
Add single-instance guard for MCP stdio server#31Hohlas wants to merge 1 commit intolyonzin:masterfrom
Hohlas wants to merge 1 commit intolyonzin:masterfrom
Conversation
285427f to
683500a
Compare
683500a to
32c5f35
Compare
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.
Hi @lyonzin,
This PR adds a single-instance guard for the
knowledge-ragMCP server.Problem
Some MCP stdio clients can start multiple
knowledge-ragprocesses even during a single visible user session/request. In my case this happened from one chat: the client opened additional internal MCP connections during approval/review flow, so several independentknowledge-ragprocesses were spawned although I was not intentionally running parallel chats.Each process loads its own embedding model, ChromaDB client, BM25 state, and file watcher. On larger local indexes this can quickly consume several GB of RAM and may exhaust system memory.
Fix
The server now creates
data/knowledge-rag.lockon startup and refuses to start a second instance while the first one is still alive.The guard:
75if another server is active;The lock is applied in
server.main(), so it protects all entry points that run the MCP server, including directknowledge-rag,python -m mcp_server.server, and wrapper-based launches.Validation
I reproduced the issue by starting multiple MCP stdio server instances with open stdin. Each instance stayed alive independently and loaded its own model/watcher.
After this patch, the second server exits immediately with:
knowledge-rag MCP server is already runningAdded tests cover: