Neo4j Knowledge Graph MCP Server Guide
Connect Neo4j knowledge graphs to AI agents via MCP. Run Cypher queries, traverse entity relationships, and build structured GraphRAG pipelines.
claude_desktop_config.json{
"mcpServers": {
"neo4j": {
"command": "uvx",
"args": [
"mcp-neo4j"
],
"env": {
"NEO4J_URI": "bolt://",
"NEO4J_USERNAME": "neo4j",
"NEO4J_PASSWORD": ""
}
}
}
}Generate & Test Neo4j Graph MCP Server Config
One-click export with environment variables & path locators for Claude Desktop, Cursor, Windsurf, and OpenAI Codex CLI.
Neo4j Knowledge Graph MCP Server Guide
Vector search and lexical matching are foundational primitives in modern artificial intelligence development. However, vector distance functions compute similarity across isolated chunks of text. When an autonomous software engineering agent needs to deduce multi-hop relational dependencies—such as "Which microservices will experience breaking API contracts if team Payments deprecates the BillingAccount entity?"—vector search frequently fails.
Graph databases like Neo4j excel at modeling complex interconnected networks using discrete Nodes, labeled Edges, and structured properties. By connecting Neo4j to artificial intelligence hosts through a Model Context Protocol (MCP) server, agents operating in Claude Desktop, Cursor, and OpenAI Codex CLI gain native graph traversal capabilities. Agents can execute Cypher queries, discover transitive entity relationships, and maintain enterprise architectural graphs in real time.
1. Problem Statement and Architecture
Implementing ad-hoc GraphRAG pipelines directly inside prompting loops presents several critical engineering hurdles:
- ▸Hallucinatory Cypher Syntax: Frontier models frequently hallucinate non-existent node labels, relationship types, or unsupported Cypher syntax when writing freeform database queries.
- ▸Unbounded Graph Scans: Naive queries such as
MATCH (n)-[r]->(m) RETURN *can inadvertently traverse millions of graph edges, locking database threads and exhausting client memory buffers. - ▸Session State Isolation: Engineering agents working across different terminals require a centralized, transactional source of truth for organizational facts and system component taxonomies.
+-----------------------------------------------------------------------------------+
| Host Boundary |
| |
| +--------------------+ JSON-RPC 2.0 (stdio) +-------------------+ |
| | Claude / Cursor | <================================> | neo4j-graph-mcp | |
| +--------------------+ +---------+---------+ |
+----------------------------------------------------------------------|------------+
| Bolt Protocol
| (bolt://7687)
v
+-------------------+
| Neo4j Aura / DBMS |
| (Nodes and Edges) |
+-------------------+The Neo4j MCP server wraps the official neo4j-driver using the Model Context Protocol TypeScript SDK. Instead of exposing raw database handles, it provides schema-constrained tools for executing read-only Cypher queries, inspecting database schemas, and upserting parameterized entity relationships with transactional safety.
2. Prerequisites and Environment Matrix
Running the Neo4j MCP server requires Node.js 18 or later and access to a running Neo4j instance (local Docker container or cloud-managed Neo4j AuraDB).
| Component | Minimum Version | Recommended | Notes |
|---|---|---|---|
| Node.js | 18.18.0+ | 20.x LTS or 22.x | Runtime for the MCP daemon |
| Neo4j DBMS | 5.15.0+ | 5.25+ or AuraDB | Native graph database engine |
| Bolt Protocol | 5.x | Latest | Binary transport between driver and Neo4j |
| Claude Desktop | 0.7.0+ | Latest | Supported desktop client |
| Cursor IDE | 0.42.0+ | Latest | Agent composer tool runner |
| OpenAI Codex CLI | 0.1.0+ | Latest | Terminal-driven workflow harness |
Launch a local Neo4j Community instance via Docker:
docker run -d --name neo4j \
-p 7474:7474 -p 7687:7687 \
-e NEO4J_AUTH=neo4j/DevelopmentSecretPass123 \
neo4j:5.25.0Verify connection readiness using the Bolt port:
curl -I 3. Server Implementation and Configuration
Below is a complete, production-ready TypeScript implementation of the Neo4j MCP server using @modelcontextprotocol/sdk and neo4j-driver.
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import neo4j from "neo4j-driver";
const uri = process.env.NEO4J_URI || "bolt://";
const user = process.env.NEO4J_USER || "neo4j";
const password = process.env.NEO4J_PASSWORD || "DevelopmentSecretPass123";
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const server = new Server(
{ name: "neo4j-graph-mcp", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "read_cypher",
description: "Execute a read-only Cypher query against the Neo4j database.",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "The MATCH Cypher query to execute" },
params: { type: "object", description: "Optional key-value query parameters", default: {} }
},
required: ["query"]
}
},
{
name: "get_schema",
description: "Inspect active Node labels, Relationship types, and database property keys.",
inputSchema: { type: "object", properties: {} }
}
]
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (name === "get_schema") {
const session = driver.session({ defaultAccessMode: neo4j.session.READ });
try {
const result = await session.run("CALL db.schema.visualization()");
return {
content: [{ type: "text", text: JSON.stringify(result.records.map(r => r.toObject()), null, 2) }]
};
} finally {
await session.close();
}
}
if (name === "read_cypher") {
const { query, params = {} } = args as any;
if (!query.trim().toUpperCase().startsWith("MATCH")) {
throw new Error("Only read-only MATCH queries are permitted through this tool.");
}
const session = driver.session({ defaultAccessMode: neo4j.session.READ });
try {
const result = await session.run(query, params);
const rows = result.records.map(r => r.toObject());
return {
content: [{ type: "text", text: JSON.stringify(rows, null, 2) }]
};
} finally {
await session.close();
}
}
throw new Error(`Tool not found: ${name}`);
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(console.error);Multi-Host Configuration Snippets
Claude Desktop
In claude_desktop_config.json:
{
"mcpServers": {
"neo4j": {
"command": "node",
"args": ["/absolute/path/to/neo4j-mcp/dist/index.js"],
"env": {
"NEO4J_URI": "bolt://",
"NEO4J_USER": "neo4j",
"NEO4J_PASSWORD": "DevelopmentSecretPass123"
}
}
}
}Cursor IDE
In .cursor/mcp.json:
{
"mcpServers": {
"neo4j": {
"command": "node",
"args": ["/absolute/path/to/neo4j-mcp/dist/index.js"],
"env": {
"NEO4J_URI": "bolt://",
"NEO4J_USER": "neo4j",
"NEO4J_PASSWORD": "DevelopmentSecretPass123"
}
}
}
}OpenAI Codex CLI
In ~/.codex/config.toml:
[mcp_servers.neo4j]
command = "node"
args = ["/absolute/path/to/neo4j-mcp/dist/index.js"]
env = { NEO4J_URI = "bolt://", NEO4J_USER = "neo4j", NEO4J_PASSWORD = "DevelopmentSecretPass123" }4. Step-by-Step Tool Discovery and Invocation
When prompted: "Find all upstream microservices that depend on the AuthGateway service", the host invokes:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "read_cypher",
"arguments": {
"query": "MATCH (s:Service)-[:DEPENDS_ON]->(target:Service {name: $targetName}) RETURN s.name AS dependentService LIMIT 10",
"params": {
"targetName": "AuthGateway"
}
}
}
}The server queries Neo4j via Bolt and returns relational matches:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{
"type": "text",
"text": "[\n { \"dependentService\": \"OrderProcessingService\" },\n { \"dependentService\": \"UserProfileService\" }\n]"
}
],
"isError": false
}
}5. Common Errors and Production Troubleshooting
1. Neo.ClientError.Security.Unauthorized
- ▸Symptom: Server initialization throws authorization failures.
- ▸Root Cause: Invalid credentials or initial password expiration. Neo4j requires changing default passwords on first startup.
- ▸Solution: Set the password via environment variable during Docker launch (
NEO4J_AUTH=neo4j/new_password) or connect to the Neo4j browser atto complete initial setup.
2. Transaction Lock Timeouts on Cypher Updates
- ▸Symptom: Concurrent agent tool calls hang and throw lock acquisition timeouts.
- ▸Root Cause: Unindexed
MERGEqueries create full table locks across dense nodes. - ▸Solution: Create unique constraints on entity keys before loading large relational datasets:
CREATE CONSTRAINT service_name_unique FOR (s:Service) REQUIRE s.name IS UNIQUE;3. Out-of-Memory Errors on Cartesian Product Queries
- ▸Symptom: Database crashes when executing complex nested relationship lookups.
- ▸Root Cause: Unbounded Cartesian products (
MATCH (a), (b)without explicit edge relationships). - ▸Solution: Enforce strict query validation in your server layer to require directed edge patterns and mandatory
LIMITclauses on all agent queries.
6. External Citations and Security Guardrails
Operating graph databases alongside autonomous agents requires strong isolation:
- ▸Read-Only Session Enforcement: Always set
defaultAccessMode: neo4j.session.READon exploratory query sessions to prevent accidentalDETACH DELETEcommands. - ▸Parameterized Cypher: Always use
$paraminjection syntax rather than string interpolation to eliminate Cypher injection vulnerabilities. - ▸Connection Pooling: Re-use the
neo4j-driverinstance across tool calls to avoid exhausting socket descriptors.
Weekly Protocol Diffs & Verified Drops
New server guides, benchmarks, and security audits. No spam. Unsubscribe anytime.
Zero marketing spam. 1-click unsubscribe anytime.
Build your full agent toolstack in the Visual Generator
Combine Neo4j Knowledge Graph with databases, search APIs, and memory graphs in a single configuration file.
Did this setup guide work with your AI host?
Help test this configuration across Claude, Cursor, and Codex CLI. Be the first to report:
Neo4j Knowledge Graph MCP Server Guide FAQ
What is the Neo4j Knowledge Graph MCP Server Guide?
Connect Neo4j knowledge graphs to AI agents via MCP. Run Cypher queries, traverse entity relationships, and build structured GraphRAG pipelines.
How do I configure Neo4j Knowledge Graph MCP Server Guide in Claude Desktop or Cursor?
You can copy the configuration JSON from our guide or launch the interactive MCP Codex Config Generator at https://mcp-codex.com/generator to export valid configs in 1 click.
Can I use Neo4j Knowledge Graph MCP Server Guide with the OpenAI Codex CLI?
Yes, OpenAI Codex CLI supports Model Context Protocol. You can add it directly to ~/.codex/config.toml or pass arguments to codex mcp add.
Specializing in Model Context Protocol (MCP) integrations, autonomous AI agent orchestration, and distributed developer toolchains. Researches and benchmarks production MCP client-server architectures across OpenAI Codex, Claude, and Cursor.
Related Guides
Connecting Turso & LibSQL Edge Databases to AI Agents using MCP
Connect globally distributed LibSQL/Turso databases to AI agents. Expose safe read-only SQL execution tools for remote data queries.
DatabasesChromaDB Vector Search MCP Server Guide
Connect ChromaDB vector search to AI agents via MCP. Build persistent semantic memory, dynamic embeddings retrieval, and production RAG tool pipelines.
DatabasesMySQL MCP Server Guide: Setup and Queries
Connect MySQL databases to Model Context Protocol agents: Step-by-step configuration, read-only permissions, schema discovery, and query execution.