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.
Generate & Validate Multi-Client MCP Config
One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.
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
- βΈA running Neo4j instance (either locally via Neo4j Desktop/Docker or cloud-hosted via Neo4j AuraDB).
- βΈThe
neo4j-drivernpm package installed if building a custom server.
Architecture Diagram
[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:
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)oradd_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
nameoruuid).
Troubleshooting
- βΈ
Neo.ClientError.Security.Unauthorized: Your credentials are incorrect. Remember that the default username is usuallyneo4jand 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:
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.
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.
Related Guides
How to Use the MySQL MCP Server for Database Queries
Connect MySQL databases to your AI agent for querying, schema exploration, and data analysis using the MySQL MCP server.
DatabasesHow to Connect PostgreSQL to AI Agents Using MCP
Step-by-step guide to setting up the PostgreSQL MCP server. Give your AI agent read-only access to query your PostgreSQL databases safely.
DatabasesHow to Use the SQLite MCP Server for Local Database Analysis
Learn how to connect SQLite databases to your AI agent for local data analysis, querying, and schema exploration using the MCP protocol.