Neo4j Knowledge Graph
STDIOMCP server implementation using Neo4j for knowledge graph management via stdio interface.
MCP server implementation using Neo4j for knowledge graph management via stdio interface.
This is a Memory Control Protocol (MCP) server implementation that uses Neo4j as the backend storage for knowledge graph management. It provides a stdio-based interface for storing and retrieving knowledge in a graph database format.
git clone <repository-url> cd neo4j_mcp_server
curl -sSL https://install.python-poetry.org | python3 -
poetry install
For Ubuntu users running Claude Desktop, you can configure the MCP server by adding it to your Claude desktop configuration file at:
~/.config/Claude/claude_desktop_config.json
Before configuring, you need to build the standalone executable:
task build
This will create a binary at dist/neo4j_mcp_server
. Make sure to update the path in your configuration to point to this built executable.
An example configuration is provided in example_mcp_config.json
. You can copy and modify this file:
cp example_mcp_config.json ~/.config/Claude/claude_desktop_config.json
Then edit the command
path in the configuration file to point to your built executable:
{ "mcpServers": [ { "name": "neo4j-knowledge-graph", "command": ["/path/to/your/dist/neo4j_mcp_server"], ... } ] }
The configuration includes:
If you have Go Task installed, you can use the provided Taskfile to manage the server:
# Show available tasks task # Start everything (Docker + Server) task run # Start development environment (Docker + Server + Test) task dev # Stop all services task down
docker-compose up -d
docker ps
)Start the server with:
poetry run python mcp_neo4j_knowledge_graph/mcp/server.py
The server will start in stdio mode, ready to accept MCP protocol messages.
Creates new entities in the knowledge graph. Each entity must have a type and properties. The ID will be automatically set from the name property if not explicitly provided.
Parameters:
entities
: List of entity objects, each containing:
type
: String - The type of entity (e.g., Person, Organization)properties
: Object - Key-value pairs of entity properties (must include either 'id' or 'name')Example input:
{ "entities": [{ "type": "Person", "properties": { "name": "John Doe", "occupation": "Developer", "age": 30 } }] }
Creates relationships between existing entities in the knowledge graph. All referenced entities must exist before creating relations.
Parameters:
relations
: List of relation objects, each containing:
type
: String - The type of relation (e.g., KNOWS, WORKS_FOR)from
: String - ID of the source entityto
: String - ID of the target entityExample input:
{ "relations": [{ "type": "KNOWS", "from": "john_doe", "to": "jane_smith" }] }
Searches for entities in the knowledge graph with powerful text matching and filtering capabilities. Can be used to search by text, list entities by type, find entities with specific properties, or any combination of these filters.
Parameters:
search_term
: String (optional) - Text to search for in entity properties. If not provided, returns entities based on other filters.entity_type
: String (optional) - Filter results by entity type (e.g., Person, Organization). If provided alone, returns all entities of that type.properties
: List[String] (optional) - List of property names to filter by:
include_relationships
: Boolean (optional, default: false) - Whether to include connected entities and relationshipsfuzzy_match
: Boolean (optional, default: true) - Whether to use case-insensitive partial matching when search_term is providedExample inputs:
// Search by text with type filter { "search_term": "John", "entity_type": "Person", "properties": ["name", "occupation"], "include_relationships": true } // List all entities of a type { "entity_type": "Person" } // Find entities with specific properties { "properties": ["email", "phone"], "entity_type": "Contact" } // Combine filters { "entity_type": "Person", "properties": ["email"], "search_term": "example.com", "fuzzy_match": true } // Return all entities (no filters) {}
Returns:
{ "results": [ { "id": "john_doe", "type": ["Entity", "Person"], "properties": { "name": "John Doe", "email": "[email protected]" }, "relationships": [ // Only included if include_relationships is true { "type": "WORKS_AT", "direction": "outgoing", "node": { "id": "tech_corp", "type": "Company", "properties": { "name": "Tech Corp" } } } ] } ] }
Notes:
IS NOT NULL
Updates existing entities in the knowledge graph. Supports adding/removing properties and labels.
Parameters:
updates
: List of update objects, each containing:
id
: String (required) - ID of the entity to updateproperties
: Object (optional) - Properties to update or addremove_properties
: List[String] (optional) - Property names to removeadd_labels
: List[String] (optional) - Labels to add to the entityremove_labels
: List[String] (optional) - Labels to remove from the entityExample input:
{ "updates": [{ "id": "john_doe", "properties": { "occupation": "Senior Developer", "salary": 100000 }, "remove_properties": ["temporary_note"], "add_labels": ["Verified"], "remove_labels": ["Pending"] }] }
Deletes entities from the knowledge graph with optional cascade deletion of relationships.
Parameters:
entity_ids
: List[String] (required) - List of entity IDs to deletecascade
: Boolean (optional, default: false) - Whether to delete connected relationshipsdry_run
: Boolean (optional, default: false) - Preview deletion impact without making changesExample input:
{ "entity_ids": ["john_doe", "jane_smith"], "cascade": true, "dry_run": true }
Returns:
success
: Boolean - Whether the operation was successfuldeleted_entities
: List of deleted entitiesdeleted_relationships
: List of deleted relationshipserrors
: List of error messages (if any)impacted_entities
: List of entities that would be affected (dry_run only)impacted_relationships
: List of relationships that would be affected (dry_run only)Retrieves comprehensive information about the Neo4j database schema, including node labels, relationship types, and their properties.
Parameters: None required
Returns:
schema
: Object containing:
node_labels
: List of all node labels in the databaserelationship_types
: List of all relationship typesnode_properties
: Map of label to list of property namesrelationship_properties
: Map of relationship type to list of property namesExample input:
{}
The project includes several test scripts for different aspects of the system:
mcp_neo4j_knowledge_graph/test_mcp_client.py
- Tests the MCP client functionality
task test-client # Run just the client test
mcp_neo4j_knowledge_graph/test_mcp_config.py
- Tests the MCP configuration
task test-config # Run just the config test
mcp_neo4j_knowledge_graph/test_neo4j_connection.py
- Tests the Neo4j database connection
task test-db # Run just the database test
You can run the tests in several ways:
Run all tests together:
task test # Runs all tests including pytest and integration tests
Run individual test types:
task test-client # Run MCP client test task test-config # Run MCP config test task test-db # Run Neo4j connection test task test-integration # Run integration tests
Run tests with pytest directly:
poetry run pytest # Run all pytest-compatible tests
The project includes several development tasks:
# Format code task format # Run linter task lint # Run tests task test # Start development environment task dev
This project uses several development tools that are automatically installed with Poetry:
black
for code formattingisort
for import sortingflake8
for lintingpytest
for testingYou can run these tools using Poetry:
# Format code poetry run black . # Sort imports poetry run isort . # Run linter poetry run flake8 # Run tests poetry run pytest
The server includes comprehensive error handling for:
All errors are returned with appropriate error messages in the MCP protocol format.
The Neo4j container is configured with the following settings:
You can modify these settings in the docker-compose.yml
file.
task
- Show available taskstask run
- Start Docker and MCP servertask dev
- Start development environment (Docker + Server + Test)task docker
- Start Neo4j databasetask server
- Run the MCP servertask test
- Run all teststask test-client
- Run MCP client teststask test-config
- Run MCP config teststask test-db
- Run database teststask test-integration
- Run integration teststask down
- Stop all Docker servicestask format
- Format code using black and isorttask lint
- Run flake8 lintertask help
- Show detailed help for all tasks