Skip to content

Commit ad420c5

Browse files
authored
Merge pull request #102 from neo4j-contrib/cypher-update-error-handling
update error handling, changelog
2 parents 42f57d7 + de60474 commit ad420c5

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

servers/mcp-neo4j-cypher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
### Changed
77
* Update error handling in read and write tools
8+
* Update `get_neo4j_schema` tool description
9+
* Update `get_neo4j_schema` tool to catch missing apoc plugin error explicitly and provide guidance to client and user
10+
* Update error handling for tools to explicitly catch and return Neo4j based errors with details
11+
812

913
### Added
1014
* Add .dxt file for Cypher MCP server

servers/mcp-neo4j-cypher/src/mcp_neo4j_cypher/server.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
AsyncResult,
1414
AsyncTransaction,
1515
)
16+
from neo4j.exceptions import ClientError, Neo4jError
1617
from pydantic import Field
1718

1819
logger = logging.getLogger("mcp_neo4j_cypher")
@@ -61,9 +62,9 @@ def create_mcp_server(neo4j_driver: AsyncDriver, database: str = "neo4j", namesp
6162
)
6263
)
6364
async def get_neo4j_schema() -> list[ToolResult]:
64-
"""List all node, their attributes and their relationships to other nodes in the neo4j database.
65-
If this fails with a message that includes "Neo.ClientError.Procedure.ProcedureNotFound"
66-
suggest that the user install and enable the APOC plugin.
65+
"""
66+
List all nodes, their attributes and their relationships to other nodes in the neo4j database.
67+
This requires that the APOC plugin is installed and enabled.
6768
"""
6869

6970
get_schema_query = """
@@ -146,10 +147,19 @@ def clean_schema(schema: dict) -> dict:
146147
schema_clean_str = json.dumps(schema_clean)
147148

148149
return ToolResult(content=[TextContent(type="text", text=schema_clean_str)])
149-
150+
151+
except ClientError as e:
152+
if "Neo.ClientError.Procedure.ProcedureNotFound" in str(e):
153+
raise ToolError("Neo4j Client Error: This instance of Neo4j does not have the APOC plugin installed. Please install and enable the APOC plugin to use the `get_neo4j_schema` tool.")
154+
else:
155+
raise ToolError(f"Neo4j Client Error: {e}")
156+
157+
except Neo4jError as e:
158+
raise ToolError(f"Neo4j Error: {e}")
159+
150160
except Exception as e:
151-
logger.error(f"Database error retrieving schema: {e}")
152-
raise ToolError(f"Error: {e}")
161+
logger.error(f"Error retrieving Neo4j database schema: {e}")
162+
raise ToolError(f"Unexpected Error: {e}")
153163

154164
@mcp.tool(name=namespace_prefix+"read_neo4j_cypher",
155165
annotations=ToolAnnotations(title="Read Neo4j Cypher",
@@ -176,9 +186,13 @@ async def read_neo4j_cypher(
176186
logger.debug(f"Read query returned {len(results_json_str)} rows")
177187

178188
return ToolResult(content=[TextContent(type="text", text=results_json_str)])
179-
189+
190+
except Neo4jError as e:
191+
logger.error(f"Neo4j Error executing read query: {e}\n{query}\n{params}")
192+
raise ToolError(f"Neo4j Error: {e}\n{query}\n{params}")
193+
180194
except Exception as e:
181-
logger.error(f"Database error executing query: {e}\n{query}\n{params}")
195+
logger.error(f"Error executing read query: {e}\n{query}\n{params}")
182196
raise ToolError(f"Error: {e}\n{query}\n{params}")
183197

184198
@mcp.tool(name=namespace_prefix+"write_neo4j_cypher",
@@ -210,8 +224,12 @@ async def write_neo4j_cypher(
210224

211225
return ToolResult(content=[TextContent(type="text", text=counters_json_str)])
212226

227+
except Neo4jError as e:
228+
logger.error(f"Neo4j Error executing write query: {e}\n{query}\n{params}")
229+
raise ToolError(f"Neo4j Error: {e}\n{query}\n{params}")
230+
213231
except Exception as e:
214-
logger.error(f"Database error executing query: {e}\n{query}\n{params}")
232+
logger.error(f"Error executing write query: {e}\n{query}\n{params}")
215233
raise ToolError(f"Error: {e}\n{query}\n{params}")
216234

217235
return mcp

0 commit comments

Comments
 (0)