neo4j-query
Execute Cypher queries against Neo4j graph databases to retrieve, create, update, or delete data nodes and relationships using parameterized queries.
Instructions
Execute a Cypher query against the Neo4j database
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| parameters | No | Query parameters (optional) | |
| query | Yes | The Cypher query to execute |
Input Schema (JSON Schema)
{
"properties": {
"parameters": {
"additionalProperties": true,
"description": "Query parameters (optional)",
"type": "object"
},
"query": {
"description": "The Cypher query to execute",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:157-165 (handler)The core handler function that executes the Cypher query against the Neo4j database using a driver session and returns the result records.async executeQuery(query: string, parameters: Record<string, any> = {}) { const session = this.driver.session(); try { const result = await session.run(query, parameters); return result.records; } finally { await session.close(); } }
- src/index.ts:100-114 (schema)Input schema definition for the 'neo4j-query' tool, specifying the required 'query' parameter and optional 'parameters' object.inputSchema: { type: "object", properties: { query: { type: "string", description: "The Cypher query to execute" }, parameters: { type: "object", description: "Query parameters (optional)", additionalProperties: true } }, required: ["query"] }
- src/index.ts:97-115 (registration)Registration of the 'neo4j-query' tool in the ListTools response, including name, description, and input schema.{ name: "neo4j-query", description: "Execute a Cypher query against the Neo4j database", inputSchema: { type: "object", properties: { query: { type: "string", description: "The Cypher query to execute" }, parameters: { type: "object", description: "Query parameters (optional)", additionalProperties: true } }, required: ["query"] } }
- src/index.ts:125-134 (registration)Dispatch registration in the CallToolRequest handler switch statement that routes 'neo4j-query' calls to the executeQuery method.switch (request.params.name) { case "neo4j-query": response = await this.executeQuery( args.query as string, args.parameters as Record<string, any> ); break; default: throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}`); }
- src/index.ts:174-190 (helper)Helper function to format the Neo4j query results into a readable text string for the tool response.function formatResults(records: neo4j.Record[]) { if (!records || records.length === 0) { return "No results found."; } const output: string[] = ["Results:"]; records.forEach((record, index) => { output.push(`\nRecord ${index + 1}:`); record.keys.forEach(key => { const value = record.get(key); output.push(`${String(key)}: ${formatValue(value)}`); }); }); return output.join('\n'); }