CRITICAL FIX - Database Parameter (graphiti_core):
- Fixed graphiti_core/driver/neo4j_driver.py execute_query method
- database_ parameter was incorrectly added to params dict instead of kwargs
- Now correctly passed as keyword argument to Neo4j driver
- Impact: All queries now execute in configured database (not default 'neo4j')
- Root cause: Violated Neo4j Python driver API contract
Technical Details:
Previous code (BROKEN):
params.setdefault('database_', self._database) # Wrong - in params dict
result = await self.client.execute_query(cypher_query_, parameters_=params, **kwargs)
Fixed code (CORRECT):
kwargs.setdefault('database_', self._database) # Correct - in kwargs
result = await self.client.execute_query(cypher_query_, parameters_=params, **kwargs)
FIX - Index Creation Error Handling (MCP server):
- Added graceful handling for Neo4j IF NOT EXISTS bug
- Prevents MCP server crash when indices already exist
- Logs warning instead of failing initialization
- Handles EquivalentSchemaRuleAlreadyExists error gracefully
Files Modified:
- graphiti_core/driver/neo4j_driver.py (3 lines changed)
- mcp_server/src/graphiti_mcp_server.py (12 lines added error handling)
- mcp_server/pyproject.toml (version bump to 1.0.5)
Testing:
- Python syntax validation: PASSED
- Ruff formatting: PASSED
- Ruff linting: PASSED
Closes issues with:
- Data being stored in wrong Neo4j database
- MCP server crashing on startup with EquivalentSchemaRuleAlreadyExists
- NEO4J_DATABASE environment variable being ignored
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
"""
|
|
Copyright 2024, Zep Software, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
"""
|
|
|
|
import logging
|
|
from collections.abc import Coroutine
|
|
from typing import Any
|
|
|
|
from neo4j import AsyncGraphDatabase, EagerResult
|
|
from typing_extensions import LiteralString
|
|
|
|
from graphiti_core.driver.driver import GraphDriver, GraphDriverSession, GraphProvider
|
|
from graphiti_core.graph_queries import get_fulltext_indices, get_range_indices
|
|
from graphiti_core.helpers import semaphore_gather
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Neo4jDriver(GraphDriver):
|
|
provider = GraphProvider.NEO4J
|
|
default_group_id: str = ''
|
|
|
|
def __init__(
|
|
self,
|
|
uri: str,
|
|
user: str | None,
|
|
password: str | None,
|
|
database: str = 'neo4j',
|
|
):
|
|
super().__init__()
|
|
self.client = AsyncGraphDatabase.driver(
|
|
uri=uri,
|
|
auth=(user or '', password or ''),
|
|
)
|
|
self._database = database
|
|
|
|
# Schedule the indices and constraints to be built
|
|
import asyncio
|
|
|
|
try:
|
|
# Try to get the current event loop
|
|
loop = asyncio.get_running_loop()
|
|
# Schedule the build_indices_and_constraints to run
|
|
loop.create_task(self.build_indices_and_constraints())
|
|
except RuntimeError:
|
|
# No event loop running, this will be handled later
|
|
pass
|
|
|
|
self.aoss_client = None
|
|
|
|
async def execute_query(self, cypher_query_: LiteralString, **kwargs: Any) -> EagerResult:
|
|
# Extract query parameters from kwargs
|
|
# Support both 'params' (legacy) and 'parameters_' (standard) keys
|
|
params = kwargs.pop('params', None) or kwargs.pop('parameters_', None)
|
|
if params is None:
|
|
params = {}
|
|
|
|
# CRITICAL FIX: database_ must be a keyword argument to Neo4j driver's execute_query,
|
|
# NOT a query parameter in the parameters dict.
|
|
# Previous code incorrectly added it to params dict, causing all queries to go to
|
|
# the default 'neo4j' database instead of the configured database.
|
|
kwargs.setdefault('database_', self._database)
|
|
|
|
try:
|
|
result = await self.client.execute_query(cypher_query_, parameters_=params, **kwargs)
|
|
except Exception as e:
|
|
logger.error(f'Error executing Neo4j query: {e}\n{cypher_query_}\n{params}')
|
|
raise
|
|
|
|
return result
|
|
|
|
def session(self, database: str | None = None) -> GraphDriverSession:
|
|
_database = database or self._database
|
|
return self.client.session(database=_database) # type: ignore
|
|
|
|
async def close(self) -> None:
|
|
return await self.client.close()
|
|
|
|
def delete_all_indexes(self) -> Coroutine:
|
|
return self.client.execute_query(
|
|
'CALL db.indexes() YIELD name DROP INDEX name',
|
|
)
|
|
|
|
async def build_indices_and_constraints(self, delete_existing: bool = False):
|
|
if delete_existing:
|
|
await self.delete_all_indexes()
|
|
|
|
range_indices: list[LiteralString] = get_range_indices(self.provider)
|
|
|
|
fulltext_indices: list[LiteralString] = get_fulltext_indices(self.provider)
|
|
|
|
index_queries: list[LiteralString] = range_indices + fulltext_indices
|
|
|
|
await semaphore_gather(
|
|
*[
|
|
self.execute_query(
|
|
query,
|
|
)
|
|
for query in index_queries
|
|
]
|
|
)
|
|
|
|
async def health_check(self) -> None:
|
|
"""Check Neo4j connectivity by running the driver's verify_connectivity method."""
|
|
try:
|
|
await self.client.verify_connectivity()
|
|
return None
|
|
except Exception as e:
|
|
print(f'Neo4j health check failed: {e}')
|
|
raise
|