A custom Neo4j procedure that integrates spaCy's NLP capabilities with Neo4j queries, designed to work with the Model Context Protocol (MCP) server for enhanced graph database interactions.
This plugin provides natural language processing capabilities as Neo4j procedures, allowing you to:
- Extract entities from text during queries
- Create relationships based on NLP analysis
- Enable LLMs to interact with Neo4j through natural language
- Support fallback NLP functionality when other providers aren't available
- Neo4j instance with APOC procedures enabled
- Python 3.8+
- spaCy with required language models
docker build -t neo4j-spacy .
docker run -p 7474:7474 -p 7687:7687 -p 5000:5000 neo4j-spacy
CALL custom.spacy.nlp.extract_entities("John works at Google in Mountain View")
YIELD text, label, start, end
RETURN text, label;
Example output:
╒════════════╤═══════╕
│ text │ label │
╞════════════╪═══════╡
│ John │ PERSON│
├────────────┼───────┤
│ Google │ ORG │
├────────────┼───────┤
│ Mountain │ GPE │
│ View │ │
╘════════════╧═══════╛
This procedure is designed to work with the MCP (Model Context Protocol) Neo4j server, enabling:
- Natural language queries against Neo4j
- Automated relationship creation based on NLP analysis
- Enhanced context understanding for graph operations
Example MCP workflow:
# MCP server using the procedure for entity extraction
await tx.run("""
MATCH (doc:Document)
WHERE doc.text IS NOT NULL
CALL custom.spacy.nlp.extract_entities(doc.text)
YIELD text, label
CREATE (e:Entity {name: text, type: label})
CREATE (doc)-[:HAS_ENTITY]->(e)
""")
- Install the procedure
- Register it using
apoc.custom.declareProcedure
- Configure security settings to allow the procedure
- Ensure APOC and custom procedures are enabled
- Configure network access for the spaCy service
- Set appropriate security policies
The procedure can be registered with:
CALL apoc.custom.declareProcedure(
'spacy.nlp.extract_entities(text :: STRING) :: (text :: STRING, label :: STRING, start :: INTEGER, end :: INTEGER)',
'WITH $text AS text
CALL apoc.load.jsonParams("http://localhost:5000/process", {text: text}, null, null)
YIELD value
UNWIND value.entities AS entity
RETURN
entity.text AS text,
entity.label AS label,
entity.start AS start,
entity.end AS end',
'READ',
'Extract entities from text using spaCy NLP as a fallback provider'
);
- The procedure runs with READ privileges only
- Network access is required between Neo4j and the spaCy service
- Consider authentication for the spaCy service in production environments
Contributions are welcome! Please feel free to submit pull requests or open issues for:
- Additional NLP capabilities
- Performance improvements
- Documentation enhancements
- Bug fixes
This project is licensed under the MIT License - see the LICENSE file for details.