-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(mcp): handle Neo4j 5.x index creation race condition #1081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Wrap build_indices_and_constraints() in try/except to catch EquivalentSchemaRuleAlreadyExists error that occurs when Neo4j 5.x processes parallel CREATE INDEX IF NOT EXISTS statements. Fixes getzep#353
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
TLDR Version: Neo4j 5.x Index Creation Race Condition
(thanks to the Graphiti team for all their hard work) SummaryThis PR fixes a race condition that causes the Graphiti MCP server to crash on startup when using Neo4j 5.x as the database backend. The issue occurs during index creation in Problem DescriptionError MessageRoot Cause
Technical DetailsThe index creation code in async def build_indices_and_constraints(self, delete_existing: bool = False):
# ... deletion logic ...
await semaphore_gather(
*[self.execute_query(query) for query in NEO4J_INDEX_QUERIES],
*[self.execute_query(query) for query in NEO4J_CONSTRAINT_QUERIES],
)This executes ~19 index creation queries in parallel. When Neo4j 5.x processes these concurrently:
Affected Versions
SolutionWrap the Code ChangeFile: # Before:
await self.client.build_indices_and_constraints()
# After:
# Build indices - wrap in try/except to handle Neo4j 5.x race condition
# with parallel IF NOT EXISTS index creation
try:
await self.client.build_indices_and_constraints()
except Exception as idx_error:
if 'EquivalentSchemaRuleAlreadyExists' in str(idx_error):
logger.warning(
'Index creation race condition detected (Neo4j 5.x issue). '
'Indexes likely already exist. Continuing...'
)
else:
raiseWhy This Fix Works
TestingBefore Fix$ docker compose -f docker-compose-neo4j.yml up -d
$ docker logs docker-graphiti-mcp-1
# Output:
neo4j.exceptions.ClientError: {neo4j_code: Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists}
# Container exits with code 1After Fix$ docker compose -f docker-compose-neo4j.yml up -d
$ docker logs docker-graphiti-mcp-1
# Output:
2025-11-24 - graphiti_mcp_server - WARNING - Index creation race condition detected (Neo4j 5.x issue). Indexes likely already exist. Continuing...
2025-11-24 - graphiti_mcp_server - INFO - Successfully initialized Graphiti client
$ curl http://localhost:8000/health
{"status":"healthy","service":"graphiti-mcp"}Verification# Confirm indexes were created
$ docker exec docker-neo4j-1 cypher-shell -u neo4j -p demodemo "SHOW INDEXES" | wc -l
27 # All indexes present
# Confirm data operations work
$ curl -X POST http://localhost:8000/mcp -d '...' # add_memory, search_nodes, etc.
# All operations successfulRelated Issues
Alternative Solutions Considered1. Sequential Index CreationApproach: Modify Pros: Eliminates race condition at source
2. Add Retry LogicApproach: Retry failed index creation queries individually. Pros: More granular error handling
3. Pre-create Indexes via ScriptApproach: Run index creation before starting the MCP server. Pros: Separates concerns
Chosen Solution: Exception Handling in MCP ServerRationale:
Checklist
References |
Summary
Fixes the MCP server crash on startup when using Neo4j 5.x due to
EquivalentSchemaRuleAlreadyExistserror during index creation.Problem
When
build_indices_and_constraints()runs, it executes ~19 index creation queries in parallel viasemaphore_gather(). Neo4j 5.x has a race condition where concurrentCREATE INDEX ... IF NOT EXISTSstatements can fail even though the guard clause should prevent errors.Solution
Wrap the
build_indices_and_constraints()call in a try/except that catches this specific error and continues, since the indexes are successfully created by whichever parallel query completes first.Testing
Related Issues
Fixes #353
Checklist
EquivalentSchemaRuleAlreadyExistserror