1313 AsyncResult ,
1414 AsyncTransaction ,
1515)
16+ from neo4j .exceptions import ClientError , Neo4jError
1617from pydantic import Field
1718
1819logger = 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