Skip to main content

Neo4j Knowledge Graph MCP Server

MCP server for enterprise knowledge graph retrieval over Neo4j. Client LLM generates Cypher; this server provides schema resources, validation, and read-only execution.

中文详细说明请参阅:docs/知识图谱检索MCP服务说明.md

Architecture

  • 10 Resources: 7 static (schema, constraints, conventions, semantics, glossary, patterns, safety) + 3 dynamic (stats, samples, indexes)
  • 7 Tools: get_graph_context, get_cypher_prompt, validate_cypher, execute_cypher, explain_cypher, resolve_entity, refresh_graph_stats
  • 4 Prompts: generate_cypher_query, refine_cypher_query, explain_query_result, decompose_complex_question

Context Tools (get_graph_context_tool, get_cypher_prompt_tool) provide the same schema context as Resources/Prompts for Tools-only MCP clients that do not support resources/read or prompts/get.

Setup

python3 -m venv .venv
source .venv/bin/activate
pip install mcp neo4j pyyaml pydantic pydantic-settings

Environment variables:

Variable Default Description
NEO4J_URI bolt://localhost:7687 Neo4j Bolt URI
NEO4J_USER neo4j Neo4j user
NEO4J_PASSWORD password Neo4j password
NEO4J_DATABASE neo4j Neo4j database
MCP_TRANSPORT stdio stdio | streamable-http | sse
MCP_HOST 127.0.0.1 HTTP/SSE bind host
MCP_PORT 8000 HTTP/SSE bind port
MCP_SSE_PATH /sse SSE endpoint path
MCP_STREAMABLE_HTTP_PATH /mcp Streamable HTTP endpoint path

Copy .env.example to .env and adjust as needed.

Import graph data

From ontology_system:

curl -o graph-import.cypher http://localhost:8183/graph/export/cypher
cypher-shell -u neo4j -p password -f graph-import.cypher

Run MCP server

stdio(默认,Cursor 子进程模式)

PYTHONPATH=. python -m src.server

streamable-http(推荐 HTTP 模式)

PYTHONPATH=. python -m src.server --transport streamable-http --host 127.0.0.1 --port 8000

Endpoint: http://127.0.0.1:8000/mcp

SSE

PYTHONPATH=. python -m src.server --transport sse --host 127.0.0.1 --port 8000

Endpoint: http://127.0.0.1:8000/sse

局域网访问(Cherry Studio 等远程客户端)

绑定 0.0.0.0 时,必须配置允许的 Host 头,否则会出现 421 Misdirected Request

PYTHONPATH=. python -m src.server \
  --transport streamable-http \
  --host 0.0.0.0 \
  --port 7688 \
  --allowed-host 192.168.0.18:7688

或使用环境变量:

export MCP_ALLOWED_HOSTS=192.168.0.18:7688
PYTHONPATH=. python -m src.server --transport streamable-http --host 0.0.0.0 --port 7688

局域网临时调试可加 --disable-transport-security(不推荐生产环境)。

绑定 0.0.0.0 前请确认网络访问控制;生产环境建议置于反向代理之后。

Cursor MCP configuration

stdio(command)

{
  "mcpServers": {
    "knowledge-graph": {
      "command": "/path/to/mcp_knowledge_graph/.venv/bin/python",
      "args": ["-m", "src.server"],
      "cwd": "/path/to/mcp_knowledge_graph",
      "env": {
        "PYTHONPATH": "/path/to/mcp_knowledge_graph",
        "NEO4J_URI": "bolt://192.168.0.18:7687",
        "NEO4J_PASSWORD": "password"
      }
    }
  }
}

streamable-http(url,含 Cherry Studio)

先启动服务(局域网需 --allowed-host):

PYTHONPATH=. python -m src.server \
  --transport streamable-http \
  --host 0.0.0.0 \
  --port 7688 \
  --allowed-host 192.168.0.18:7688

Cherry Studio JSON 配置:

{
  "mcpServers": {
    "knowledge-graph": {
      "url": "http://192.168.0.18:7688/mcp"
    }
  }
}

也可通过 args 指定 transport:

{
  "mcpServers": {
    "knowledge-graph": {
      "command": "/path/to/mcp_knowledge_graph/.venv/bin/python",
      "args": ["-m", "src.server", "--transport", "streamable-http", "--port", "8000"],
      "cwd": "/path/to/mcp_knowledge_graph",
      "env": {
        "PYTHONPATH": "/path/to/mcp_knowledge_graph"
      }
    }
  }
}

Verify pipeline

PYTHONPATH=. python scripts/verify_import_pipeline.py

Tests

PYTHONPATH=. pytest tests/ -v

Workflow

Full MCP client (Resources + Prompts)

  1. Client reads graph://schema, graph://ontology-constraints, graph://query-patterns
  2. Optional: resolve_entity for name disambiguation
  3. Client LLM uses generate_cypher_query prompt to produce Cypher JSON
  4. validate_cypherexecute_cypherexplain_query_result

Tools-only client

  1. get_cypher_prompt_tool(question=...) or get_graph_context_tool(scope="core")
  2. Optional: resolve_entity → pass result as entity_hints
  3. Platform LLM generates Cypher JSON from returned prompt fields
  4. validate_cypherexecute_cypher → platform LLM explains records

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_knowledge_graph-0.1.2.tar.gz (89.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_knowledge_graph-0.1.2-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file mcp_knowledge_graph-0.1.2.tar.gz.

File metadata

  • Download URL: mcp_knowledge_graph-0.1.2.tar.gz
  • Upload date:
  • Size: 89.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mcp_knowledge_graph-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a53c11de4bffaebaa61aa4714b0a381be959b84dcc475861128eede1d7fc2843
MD5 7c15fb2738690e1f76240b41d6801320
BLAKE2b-256 9d1aed1bb728720baed191d11a0b88264492d39c4386091dc2ca2ff3e777a7ce

See more details on using hashes here.

File details

Details for the file mcp_knowledge_graph-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mcp_knowledge_graph-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mcp_knowledge_graph-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b7002dff45a09fe3c530a4a5b135d9d4b7985ab0c02849cca7d9bdcff3ddfb10
MD5 9a2078b36461be11ff9b922700528f44
BLAKE2b-256 4c3bec84624e9ac48743ea9dff176978397c3108ae3e6bd23265887609af6ef7

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page