Postgres-Neo4j MCP Server

Postgres-Neo4j MCP Server

Enables natural language queries to a Neo4j knowledge graph built from PostgreSQL data, integrating with Claude Desktop and REST API.

Category
访问服务器

README

PostgreSQL to Neo4j Knowledge Graph Pipeline with MCP Integration

Python Neo4j PostgreSQL License: MIT

A production-ready data pipeline that transforms structured content from PostgreSQL into a Neo4j knowledge graph, with Model Context Protocol (MCP) integration for AI/LLM interactions.

🌟 Key Features

  • Automated ETL Pipeline: Seamlessly transfer data from PostgreSQL to Neo4j
  • Entity & Relationship Extraction: Automatic identification of people, organizations, and topics
  • MCP Integration: Natural language queries through Claude Desktop or REST API
  • Graph Analytics: Discover patterns and relationships in your data
  • Docker Support: Easy deployment with containerization
  • Extensible Architecture: Ready for AI/LLM enhancements

🏗️ Architecture Overview

Architecture Overview

📋 Table of Contents

💻 System Requirements

  • Python 3.8 or higher
  • PostgreSQL 14+
  • Neo4j 5.0+ (Community or Enterprise)
  • Node.js 16+ (for Claude Desktop integration)
  • 4GB RAM minimum (8GB recommended)
  • 10GB free disk space

🚀 Quick Start

# Clone the repository
git clone https://github.com/your-username/postgres-neo4j-mcp.git
cd postgres-neo4j-mcp

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your database credentials

# Run the ETL pipeline
python src/etl_pipeline.py

# Start the MCP server
python src/mcp_server.py

# Test the setup
python src/test_mcp_client.py

📦 Module Overview

Core Modules

1. ETL Pipeline (src/etl_pipeline.py)

The heart of the data transformation process.

Key Features:

  • Connects to PostgreSQL and extracts structured content
  • Transforms relational data into graph-ready format
  • Creates nodes for Articles, People, Organizations, Topics, and Domains
  • Establishes relationships based on content analysis
  • Handles deduplication and data validation

Main Classes:

  • PostgreSQLConnector: Manages PostgreSQL connections and data retrieval
  • Neo4jConnector: Handles Neo4j operations and graph creation
  • PostgresToNeo4jETL: Orchestrates the complete ETL process

2. MCP Server (src/mcp_server.py)

Provides AI/LLM integration through a REST API.

Key Features:

  • REST API endpoints for query execution
  • Natural language to Cypher query conversion
  • Schema introspection capabilities
  • Support for both read and write operations
  • Compatible with Claude Desktop and other LLM tools

API Endpoints:

  • /health: Service health check
  • /schema: Get graph schema
  • /execute: Execute Cypher or natural language queries
  • /analyze: Analyze content for entity extraction

3. Test Client (src/test_mcp_client.py)

Comprehensive testing and demonstration tool.

Key Features:

  • Automated test suite for all functionality
  • Interactive query mode for manual testing
  • Performance benchmarking
  • Example queries and use cases

Data Schema

PostgreSQL Schema (sql/create_schema.sql)

structured_content
├── id (PRIMARY KEY)
├── domain (VARCHAR)
├── url (TEXT, UNIQUE)
├── title (TEXT)
├── content (TEXT)
├── author (VARCHAR)
├── published_date (TIMESTAMP)
├── category (VARCHAR)
├── tags (TEXT[])
├── entities (JSONB)
├── metadata (JSONB)
└── scraped_at (TIMESTAMP)

Neo4j Graph Schema (cypher/create_constraints.cypher)

Nodes:
├── Article (url, title, content, author, published_date, category)
├── Person (name)
├── Organization (name)
├── Topic (name)
└── Domain (name, type)

Relationships:
├── PUBLISHED_ON (Article → Domain)
├── MENTIONS_PERSON (Article → Person)
├── MENTIONS_ORGANIZATION (Article → Organization)
├── TAGGED_WITH (Article → Topic)
├── RELATED_TO (Article → Article)
└── SIMILAR_TO (Article → Article)

🔧 Installation

Step 1: Clone the Repository

git clone https://github.com/your-username/postgres-neo4j-mcp.git
cd postgres-neo4j-mcp

Step 2: Set Up Python Environment

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Step 3: Set Up Databases

PostgreSQL Setup

# Create database
createdb content_scraper

# Run schema creation script
psql -U postgres -d content_scraper -f sql/create_schema.sql

Neo4j Setup

  1. Start Neo4j database
  2. Open Neo4j Browser (http://localhost:7474)
  3. Run the constraints script from cypher/create_constraints.cypher

Step 4: Configure Environment

# Copy example environment file
cp .env.example .env

# Edit .env with your credentials
nano .env

⚙️ Configuration

Environment Variables

Create a .env file with the following variables:

# PostgreSQL Configuration
PG_HOST=localhost
PG_PORT=5432
PG_DATABASE=content_scraper
PG_USER=postgres
PG_PASSWORD=your_postgres_password

# Neo4j Configuration
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=your_neo4j_password
NEO4J_DATABASE=neo4j

# MCP Server Configuration
MCP_SERVER_PORT=8080

# Optional: LLM Integration
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key

Claude Desktop Integration

Add to your Claude Desktop configuration file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "local-neo4j": {
      "command": "uvx",
      "args": ["mcp-neo4j-cypher@0.3.0"],
      "env": {
        "NEO4J_URI": "bolt://localhost:7687",
        "NEO4J_USERNAME": "neo4j",
        "NEO4J_PASSWORD": "your_password",
        "NEO4J_DATABASE": "neo4j",
        "NEO4J_NAMESPACE": "local"
      }
    }
  }
}

📖 Usage

Running the ETL Pipeline

# Run with default settings
python src/etl_pipeline.py

# Run with specific parameters
python src/etl_pipeline.py \
  --pg-password your_password \
  --neo4j-password your_password \
  --limit 100  # Process only 100 records

Starting the MCP Server

# Start the server
python src/mcp_server.py

# The server will be available at http://localhost:8080

Using the Test Client

# Run automated tests
python src/test_mcp_client.py

# Interactive mode
python src/test_mcp_client.py interactive

Example Queries

Natural Language Queries

# In interactive mode:
mcp> nl: show me all articles about AI
mcp> nl: find organizations mentioned in multiple domains
mcp> nl: count total number of nodes

Direct Cypher Queries

mcp> cypher: MATCH (a:Article)-[:TAGGED_WITH]->(t:Topic {name: 'AI'}) RETURN a.title
mcp> cypher: MATCH (p:Person)<-[:MENTIONS_PERSON]-(a:Article) RETURN p.name, count(a) as mentions

📡 API Documentation

REST API Endpoints

Health Check

GET /health

Response:

{
  "status": "healthy",
  "service": "neo4j-mcp-server"
}

Get Schema

GET /schema

Execute Query

POST /execute
Content-Type: application/json

{
  "query": "MATCH (n) RETURN count(n)",
  "type": "cypher",
  "parameters": {}
}

Natural Language Query

POST /execute
Content-Type: application/json

{
  "query": "show me all articles about AI",
  "type": "natural"
}

🐳 Docker Deployment

Using Docker Compose

# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Individual Docker Commands

# Build the MCP server image
docker build -t neo4j-mcp-server .

# Run the container
docker run -d \
  -p 8080:8080 \
  --env-file .env \
  --name mcp-server \
  neo4j-mcp-server

🧪 Testing

Run the test suite:

# Run all tests
pytest tests/

# Run specific test file
pytest tests/test_etl.py

# Run with coverage
pytest --cov=src tests/

📊 Example Use Cases

1. Finding Cross-Domain Mentions

MATCH (o:Organization)<-[:MENTIONS_ORGANIZATION]-(a:Article)
WITH o, collect(DISTINCT a.domain) as domains
WHERE size(domains) > 1
RETURN o.name, domains

2. Article Similarity Analysis

MATCH (a1:Article)-[:TAGGED_WITH]->(t:Topic)<-[:TAGGED_WITH]-(a2:Article)
WHERE id(a1) < id(a2)
WITH a1, a2, collect(t.name) as shared_topics, count(t) as similarity
WHERE similarity >= 2
RETURN a1.title, a2.title, similarity
ORDER BY similarity DESC

3. Temporal Analysis

MATCH (a:Article)
WHERE a.published_date > datetime() - duration('P30D')
RETURN a.domain, count(a) as article_count
ORDER BY article_count DESC

🤝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Neo4j team for the excellent graph database
  • Anthropic for Claude and MCP protocol
  • PostgreSQL community
  • All contributors to this project

📧 Contact


Made with ❤️ by Faaiz Shah

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选