graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

[BUG] clear_graph MCP tool fails silently due to async session context manager bug in clear_data function

Open akannk001 opened this issue 4 months ago • 4 comments

Bug Description

The clear_graph MCP tool fails silently and does not actually clear any data from the Neo4j database. The issue is caused by a bug in the clear_data function in graphiti_core/utils/maintenance/graph_data_operations.py which attempts to use async with driver.session() on a synchronous Neo4j driver session.

Steps to Reproduce

  1. Set up Graphiti MCP server with Neo4j database
  2. Add some episodes using add_memory tool (verify they are stored in Neo4j)
  3. Call the clear_graph MCP tool
  4. Check Neo4j database - data is still present (not cleared)
  5. Check server logs - no clear operation appears in Neo4j logs

Expected Behavior

  • clear_graph should delete all nodes and relationships from the Neo4j database
  • The operation should complete successfully and log the clearing operation
  • Neo4j logs should show the MATCH (n) DETACH DELETE n query execution

Actual Behavior

  • clear_graph returns success message but no data is actually cleared
  • Neo4j never receives the delete query
  • The operation fails silently due to a TypeError exception

Root Cause

The bug is in graphiti_core/utils/maintenance/graph_data_operations.py line 66:

async def clear_data(driver: GraphDriver, group_ids: list[str] | None = None):
    async with driver.session() as session:  # ❌ BUG: Sync driver doesn't support async context manager

Problem: Graphiti uses GraphDatabase.driver() (synchronous driver) but clear_data tries to use async with driver.session() which requires AsyncGraphDatabase.driver().

Error thrown: TypeError: 'Session' object does not support the asynchronous context manager protocol

Environment

  • Graphiti version: 0.18.9
  • Neo4j driver version: 5.28.2
  • Python version: 3.13
  • Neo4j version: Latest (Docker container)

Verification

You can verify this by checking the Neo4j driver session capabilities:

from neo4j import GraphDatabase
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
session = driver.session()
print(f'Session has __aenter__: {hasattr(session, "__aenter__")}')  # False
print(f'Session has __aexit__: {hasattr(session, "__aexit__")}')    # False

Proposed Solution

Option 1 (Minimal fix): Change clear_data to use synchronous context manager:

async def clear_data(driver: GraphDriver, group_ids: list[str] | None = None):
    with driver.session() as session:  # ✅ Use sync context manager
        # ... rest of the function

Option 2 (Comprehensive fix): Use async driver throughout Graphiti:

from neo4j import AsyncGraphDatabase
# Use AsyncGraphDatabase.driver() and async sessions consistently

Impact

  • Severity: Medium - Feature completely non-functional but fails silently
  • Affected components: MCP server clear_graph tool, any code using clear_data function
  • Workaround: Manual database clearing via Neo4j browser/cypher-shell

Additional Context

This bug has likely gone unnoticed because:

  1. clear_graph is not frequently used
  2. The failure is silent (exception caught but not properly logged)
  3. Users may not immediately verify that data was actually cleared

Related Code

  • graphiti_core/utils/maintenance/graph_data_operations.py (line 66)
  • mcp_server/graphiti_mcp_server.py (clear_graph tool implementation)

akannk001 avatar Aug 20 '25 14:08 akannk001

@akannk001 Is this still an issue? Please confirm within 14 days or this issue will be closed.

claude[bot] avatar Oct 06 '25 00:10 claude[bot]

@akannk001 Is this still an issue? Please confirm within 14 days or this issue will be closed.

claude[bot] avatar Oct 22 '25 00:10 claude[bot]

@akannk001 Is this still an issue? Please confirm within 14 days or this issue will be closed.

claude[bot] avatar Nov 17 '25 00:11 claude[bot]

i faced a similar situation with clear_graph i am using the graphiti mcp server for falkordb. The index is destroyed but the nodes are still there.

simkimsia avatar Nov 19 '25 05:11 simkimsia