local-graphrag-mcp

local-graphrag-mcp

A Model Context Protocol server implementing Graph RAG with local, embedded Knowledge Graph using FAISS for vector search and NetworkX for graph traversal, enabling hybrid retrieval through anchor discovery and relationship expansion.

Category
访问服务器

README

Local GraphRAG MCP Server

<!-- mcp-name: io.github.nonatofabio/local-graphrag-mcp -->

A Model Context Protocol (MCP) server that provides a local, embedded Knowledge Graph for Retrieval-Augmented Generation (RAG).

Unlike standard vector stores, this project implements Graph RAG: it uses vector search to find relevant entities ("anchors") and then traverses relationships to retrieve connected context. It runs entirely in a single Python process using FAISS for vector search and NetworkX for graph operations.

Why Graph RAG?

Standard RAG (like FAISS alone) retrieves chunks based on semantic similarity to the query.

Graph RAG retrieves chunks based on semantic similarity AND relationship traversal.

Example:

  • Standard RAG: User asks "Who manages the Auth Service?" → Finds chunks semantically similar to the query → May miss "Alice" if her description doesn't semantically match "manages auth service"
  • Graph RAG: Finds "Auth Service" node via semantic search → Traverses [MANAGED_BY] edge → Returns "Alice" and her full context, even if her description doesn't directly relate to the query

Features

Core Capabilities

  • Pure Python: FAISS (vector search) + NetworkX (graph traversal) - no external services required
  • Single Process: Runs entirely in-process, no Docker containers or background services
  • Hybrid Retrieval: Combines vector similarity search (anchor discovery) with graph traversal (context expansion)
  • Persistent Storage: Three-file storage system (FAISS index, NetworkX graph, metadata JSON)
  • MCP Compatible: Plug-and-play with Claude Desktop, Cursor, or any MCP client
  • Configurable Traversal: Control anchor count (top-k) and expansion depth (hops)

Installation

Prerequisites

  • Python 3.10+
  • pip

From Source

git clone https://github.com/nonatofabio/local_graphrag_mcp.git
cd local_graphrag_mcp
pip install -e .

Development Installation

pip install -e ".[dev]"

Ollama Setup (for Local Entity Extraction)

The CLI uses Ollama by default for local entity extraction. Install and set up Ollama:

# Install Ollama (macOS)
brew install ollama

# Or download from: https://ollama.ai

# Start Ollama service
ollama serve

# Pull the default model (Gemma 3 1B)
ollama pull gemma3:1b

Optional: Cloud Extraction with Claude API

For higher precision entity extraction, install the optional cloud dependencies:

pip install -e ".[cloud]"
export ANTHROPIC_API_KEY='your-key-here'

Usage

CLI Commands

The CLI provides document indexing with automatic entity extraction:

# Index documents using local Ollama (default)
local-graphrag index document.pdf

# Index multiple documents recursively
local-graphrag index -r documents/

# Index with specific Ollama model
local-graphrag index --extract-model gemma2:9b document.pdf

# Index using Claude API (requires anthropic package and API key)
local-graphrag index --use-cloud document.pdf

# Query the knowledge graph
local-graphrag search "What database does the auth service use?"

# List indexed entities
local-graphrag list

1. Running the MCP Server

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "local-graphrag": {
      "command": "local-graphrag-mcp",
      "args": [
        "--index-dir", "/absolute/path/to/graphrag_data",
        "--embed", "all-MiniLM-L6-v2"
      ]
    }
  }
}

Arguments:

  • --index-dir: Directory where graph data will be stored (default: ./graphrag_index)
  • --embed: Sentence transformer model name (default: all-MiniLM-L6-v2)
  • --debug: Enable debug logging (optional)

2. Available Tools

The server exposes two tools to the LLM:

add_knowledge_graph

Directly adds nodes and edges to the knowledge graph. Use this when the LLM has extracted structured information from documents.

Input Schema:

{
  "nodes": [
    {
      "id": "entity:auth_service",
      "label": "Service",
      "description": "The authentication microservice handling user login",
      "properties": {"language": "Python", "version": "2.1.0"}
    }
  ],
  "edges": [
    {
      "source": "entity:auth_service",
      "target": "entity:postgres_db",
      "label": "CONNECTS_TO",
      "properties": {"protocol": "TCP"}
    }
  ]
}

Returns:

{
  "success": true,
  "nodes_added": 1,
  "edges_added": 1
}

query_graph_rag

The primary retrieval tool implementing the "Anchor & Expand" strategy.

Input Schema:

{
  "query": "What database does the auth service use?",
  "top_k_anchors": 3,
  "hops": 2
}

Parameters:

  • query: Natural language query
  • top_k_anchors: Number of most similar nodes to find (default: 3)
  • hops: How many relationship hops to traverse from anchors (default: 2)

Returns:

{
  "success": true,
  "query": "What database does the auth service use?",
  "anchors": [
    {
      "id": "entity:auth_service",
      "label": "Service",
      "description": "The authentication microservice...",
      "similarity_score": 0.85
    }
  ],
  "subgraph": {
    "nodes": [...],
    "edges": [...]
  }
}

How It Works Internally

Anchor & Expand Strategy:

  1. Embed: Converts your query into a 384-dimensional vector using sentence-transformers
  2. Vector Search: Uses FAISS to find the top-k most similar nodes (anchors) via L2 distance
  3. Graph Expansion: Performs BFS traversal from anchor nodes up to N hops
    • Traverses both outgoing edges (successors)
    • Collects all connected nodes and relationships
  4. Context Assembly: Returns structured subgraph to the LLM with anchor nodes and expanded neighborhood

Example:

Query: "What database does the auth service use?"

Step 1 (Anchor): Find "entity:auth_service" (similarity: 0.85)
Step 2 (Expand 1-hop): Traverse [CONNECTS_TO] → "entity:postgres_db"
Step 3 (Expand 2-hops): Traverse from postgres_db → "entity:backup_service"

Result: Subgraph with 3 nodes and 2 edges

Architecture

Stack Overview

[ LLM Client ] <--> [ MCP Server ] <--> [ GraphRAGVectorStore ]
                                              |
                              +---------------+---------------+
                              |                               |
                         [ FAISS Index ]              [ NetworkX Graph ]
                       (vector similarity)          (relationship traversal)
                              |                               |
                              +----------- Shared IDs --------+

Data Model

Nodes:

  • id: Unique entity identifier (e.g., "entity:auth_service")
  • label: Entity type (e.g., "Service", "Database", "Person")
  • description: Natural language description (embedded as 384D vector)
  • properties: Schema-less JSON attributes

Edges:

  • source: Source node ID
  • target: Target node ID
  • label: Relationship type (e.g., "CONNECTS_TO", "MANAGED_BY")
  • properties: Schema-less JSON metadata

Storage

Three files are created in the index directory:

  1. nodes.index: FAISS IndexFlatL2 (vector embeddings)
  2. graph.gpickle: NetworkX directed graph (nodes, edges, properties)
  3. metadata.json: Node descriptions and embedding model info

Development

Running Tests

# Install dev dependencies
pip install -e ".[dev]"

# Run all tests
pytest tests/

# Run with verbose output
pytest tests/ -v

# Run standalone integration test
python tests/test_standalone.py

Project Structure

local-graphrag-mcp/
├── local_graphrag_mcp/
│   ├── __init__.py          # Package exports
│   ├── __main__.py          # CLI entry point
│   └── server.py            # Core implementation (585 lines)
├── tests/
│   ├── __init__.py
│   └── test_standalone.py   # End-to-end integration test
├── pyproject.toml           # Package configuration
├── .mcp.json.example        # Example MCP config
├── CLAUDE.md                # Development guidance
└── README.md                # This file

Key Design Decisions

Why FAISS + NetworkX instead of a graph database?

  1. Simplicity: Pure Python, no external services (Redis, Neo4j, etc.)
  2. Performance: FAISS for fast vector search, NetworkX for in-memory graph traversal
  3. Portability: Entire graph fits in three files, easy to version control or share

Why not DuckDB/DuckPGQ?

  • Initial implementation attempted SQL/PGQ property graphs
  • Compatibility issues with DuckPGQ extension across DuckDB versions
  • Complex SQL syntax for graph patterns vs. simple Python NetworkX API
  • Decision: Prioritize simplicity and reliability

Roadmap

  • [x] v0.1.0: Core FAISS + NetworkX implementation
  • [x] v0.1.0: MCP server with add/query tools
  • [x] v0.1.0: Persistent storage and reload
  • [ ] v0.2.0: CLI commands for initialization and ingestion
  • [ ] v0.3.0: Smart document ingestion (auto-extract entities via LLM)
  • [ ] v0.4.0: Graph visualization tools
  • [ ] v0.5.0: Multi-hop reasoning enhancements

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

MIT License - see LICENSE file for details

Acknowledgments

This project follows patterns established by local_faiss_mcp for MCP integration and coding style.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选