DatabasesΒ·
advanced
Β·14 min readΒ·Jul 9, 2026

Building Local Knowledge Graphs with Neo4j and the Graph DB MCP Server

Go beyond basic semantic search. Connect Neo4j knowledge graphs to your AI agent and run graph Cypher queries via MCP to uncover hidden relationships.

Neo4jgraph databaseCypherknowledge graphRAGarchitecture
Interactive Tool
1-Click Export

Generate & Validate Multi-Client MCP Config

One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.

Open in Generator

Building Local Knowledge Graphs with Neo4j and MCP

While vector databases excel at finding independent, semantically similar text fragments, they fundamentally fail to grasp interconnected entity hierarchies (e.g., matching who reported to which team, what dependencies are tied to which service).

A graph database like Neo4j maps relationships explicitly using Nodes and Edges, making it the perfect companion for a Reasoning Agent that needs to perform root-cause analysis or organizational lookups.

Prerequisites

  1. β–ΈA running Neo4j instance (either locally via Neo4j Desktop/Docker or cloud-hosted via Neo4j AuraDB).
  2. β–ΈThe neo4j-driver npm package installed if building a custom server.

Architecture Diagram

code
[Agent] --> [Neo4j MCP Server] --> [Executes Cypher Query] --> [Neo4j DB]
                                                                  |
                                              Stores Node: Person (Sarah)
                                              Stores Node: Project (Alpha)
                                              Stores Edge: (Sarah)-[:LEADS]->(Alpha)

Node/Edge Schema Setup Example

If you are exposing a custom tool to your agent, you can wrap the Neo4j driver. Here is an example of a tool that allows the agent to define a "Leadership" relationship between a person and a project dynamically:

typescript
import neo4j from 'neo4j-driver';

const driver = neo4j.driver(
  process.env.NEO4J_URI || 'bolt://',
  neo4j.auth.basic(process.env.NEO4J_USER || 'neo4j', process.env.NEO4J_PASSWORD || 'password')
);

async function addTeamRelation(leader: string, projectName: string) {
  const session = driver.session();
  try {
    await session.run(
      `MERGE (p:Person {name: $leader})
       MERGE (pr:Project {name: $projectName})
       MERGE (p)-[:LEADS]->(pr)
       RETURN p, pr`,
      { leader, projectName }
    );
  } finally {
    await session.close();
  }
}

Best Practices

  • β–ΈStrict Schema Enforcement: LLMs are notoriously bad at writing perfect SQL or Cypher queries. Instead of giving the agent a generic "execute_cypher" tool, give it specific parameterized tools (like find_project_dependencies(projectName) or add_team_relation(person, project)) to prevent syntax errors and database corruption.
  • β–ΈIndex Heavily: Agents expect fast responses. Ensure you place indexes on commonly queried Node properties (like name or uuid).

Troubleshooting

  • β–ΈNeo.ClientError.Security.Unauthorized: Your credentials are incorrect. Remember that the default username is usually neo4j and the database forces a password change on first login.
  • β–ΈTimeout or Out of Memory: If the agent executes a broad query like MATCH (n) RETURN n, it will attempt to fetch the entire database. Enforce hard limits in your Cypher queries (e.g., LIMIT 50) inside the MCP tool layer.

Using with OpenAI Codex

You can use this MCP server with the OpenAI Codex CLI by adding it to your configuration:

bash
codex mcp add --name neo4j --command "npx -y @modelcontextprotocol/server-neo4j" --env NEO4J_URI=bolt://

Note: You may also need NEO4J_USER and NEO4J_PASSWORD environment variables.

For a full list of recommended servers, see Best MCP Servers for OpenAI Codex.

Ready to Deploy?

Build your full agent toolstack in the Visual Generator

Combine Building Local Knowledge Graphs with Neo4j and the Graph DB with databases, search APIs, and memory graphs in a single configuration file.

Customize in Generator

Related Guides