lightrag-mcp
by d8corp
README.md
<div align="center">
# lightrag-mcp
[](https://www.npmjs.com/package/lightrag-mcp)
[](https://www.npmjs.com/package/lightrag-mcp)
[](https://packagephobia.com/result?p=lightrag-mcp)
[](https://github.com/d8corp/lightrag-mcp/blob/main/LICENSE)
[](https://www.typescriptlang.org/)
Model Context Protocol server for seamless [LightRAG](https://github.com/HKUDS/LightRAG) integration.
</div>
<blockquote>
β π <a href="#requirements">Requirements</a><br>
β π¦ <a href="#installation">Installation</a><br>
β π <a href="#getting-started">Getting Started</a><br>
β βοΈ <a href="#configuration">Configuration</a><br>
β π <a href="#mcp-client-configuration">MCP Client Configuration</a><br>
β π οΈ <a href="#available-tools">Tools (26)</a><br>
β π― <a href="#usage-examples">Usage Examples</a><br>
β π» <a href="#programmatic-usage">Programmatic Usage</a><br>
β π§ <a href="#development">Development</a><br>
β π§ <a href="#troubleshooting">Troubleshooting</a><br>
β π <a href="#links">Links</a>
</blockquote>
**lightrag-mcp** is a Model Context Protocol (MCP) server that bridges AI assistants with [LightRAG](https://github.com/HKUDS/LightRAG)'s powerful knowledge graph capabilities.
It enables your AI tools like Claude Desktop, Cline, or custom integrations to interact with LightRAG's Retrieval-Augmented Generation system through a standardized protocol.
- π **MCP Protocol** - Stdio transport for seamless client integration
- π‘οΈ **Type Safety** - Full TypeScript support with Zod validation
- π€ **Auto-generated SDK** - Types and client from OpenAPI spec
- π§° **26 Tools** - Complete LightRAG API coverage
- π **Knowledge Graphs** - Build and explore entity relationships
- π **Multiple Query Modes** - Local, global, hybrid, and more
- π **Document Management** - Insert, track, and reprocess documents
- β‘ **Fast & Lightweight** - Minimal dependencies, maximum performance
[](https://github.com/d8corp/lightrag-mcp/stargazers)
[](https://github.com/d8corp/lightrag-mcp/watchers)
## Requirements
###### [π οΈ](#lightrag-mcp) / Requirements [β](#installation)
Before you begin, make sure your environment meets these requirements:
- Node.js 18+ (LTS recommended)
- Running LightRAG server (default port 9621)
- API key for authentication
## Installation
###### [π οΈ](#lightrag-mcp) / Installation [β](#requirements) [β](#getting-started)
Choose the installation method that fits your use case:
Install globally (for CLI usage):
```sh
npm i -g lightrag-mcp
```
Add to project dependencies:
```sh
npm i lightrag-mcp
```
Add as dev dependency (for development tools):
```sh
npm i -D lightrag-mcp
```
## Getting Started
###### [π οΈ](#lightrag-mcp) / Getting Started [β](#installation) [β](#configuration)
LightRAG MCP server connects your AI assistant to LightRAG's knowledge graph capabilities through the Model Context Protocol. The server runs as a stdio transport, making it compatible with any MCP client like Claude Desktop, Cline, or custom integrations.
```
βββββββββββββββββββββββ
β MCP Client β
β (Claude Desktop, β
β Cline, Custom) β
ββββββββββββ¬βββββββββββ
β stdio
β (MCP Protocol)
βΌ
βββββββββββββββββββββββ
β MCP Server β
β (lightrag-mcp) β
ββββββββββββ¬βββββββββββ
β HTTP/REST
β (OpenAPI)
βΌ
βββββββββββββββββββββββ
β LightRAG Server β
βββββββββββββββββββββββ
```
**How it works:**
1. **MCP Client** sends tool requests via stdio transport
2. **lightrag-mcp** validates requests and translates them to LightRAG API calls
3. **LightRAG Server** processes requests and returns results
4. **lightrag-mcp** formats responses back to MCP protocol
5. **MCP Client** receives structured data for the AI assistant
With this server, you can:
- **Build knowledge graphs** from your documents automatically
- **Query information** using multiple search strategies (local, global, hybrid)
- **Manage entities and relationships** in your knowledge base
- **Track document processing** and reprocess failed items
- **Explore connections** between concepts through graph visualization
While MCP servers are designed to work with MCP clients, you can test them directly:
**Using MCP Inspector (recommended for testing):**
```sh
npx @modelcontextprotocol/inspector lightrag-mcp --token your-api-key
```
**Direct stdio testing with echo:**
```sh
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"query_text","arguments":{"query":"What is LightRAG?","mode":"hybrid"}}}' | lightrag-mcp --token your-api-key
```
**Note:** Replace `your-api-key` with your actual API key and add `-u https://your-server.com` if using a remote LightRAG server.
## Configuration
###### [π οΈ](#lightrag-mcp) / Configuration [β](#getting-started) [β](#mcp-client-configuration)
You can configure the lightrag-mcp server in multiple ways, with the following priority (highest to lowest):
1. **CLI options** - Direct command-line arguments
2. **Environment variables** - System or shell environment
3. **`.env` file** - Local configuration file
4. **MCP client configuration** - Settings in your MCP client (Claude Desktop, etc.)
### CLI Options
Pass configuration directly via command-line flags:
| Option | Description |
|-----------------------|-----------------------------------------------------|
| `-u, --url <url>` | LightRAG server URL (overrides `LIGHTRAG_BASE_URL`) |
| `-t, --token <token>` | API key (overrides `LIGHTRAG_API_KEY`) |
| `-v, --version` | Display version |
| `-h, --help` | Display help |
**Example:**
```sh
lightrag-mcp --url https://rag.example.com --token your-api-key
```
### Environment Variables
Set configuration via environment variables:
| Variable | Description | Default |
|---------------------|----------------------------|-------------------------|
| `LIGHTRAG_BASE_URL` | LightRAG server URL | `http://localhost:9621` |
| `LIGHTRAG_API_KEY` | API key for authentication | - |
**Example:**
```sh
export LIGHTRAG_BASE_URL=https://rag.example.com
export LIGHTRAG_API_KEY=your-api-key
lightrag-mcp
```
### `.env` File
Create a `.env` file in your project root:
```sh
LIGHTRAG_BASE_URL=https://rag.example.com
LIGHTRAG_API_KEY=your-api-key
```
Then simply run:
```sh
lightrag-mcp
```
## MCP Client Configuration
###### [π οΈ](#lightrag-mcp) / MCP Client Configuration [β](#configuration) [β](#available-tools)
### Claude Desktop
Add to your Claude Desktop configuration file:
**macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
**Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
**Linux**: `~/.config/Claude/claude_desktop_config.json`
```json
{
"mcpServers": {
"lightrag": {
"command": "lightrag-mcp",
"env": {
"LIGHTRAG_BASE_URL": "https://rag.example.com",
"LIGHTRAG_API_KEY": "your-api-key"
}
}
}
}
```
### Other MCP Clients (YAML)
For MCP clients and plugins that support YAML configuration:
```yaml
name: LightRAG MCP Server
version: 0.0.1
schema: v1
mcpServers:
- name: LightRag
type: stdio
command: lightrag-mcp
env:
LIGHTRAG_BASE_URL: "https://rag.example.com"
LIGHTRAG_API_KEY: "your-api-key"
```
## Available Tools
###### [π οΈ](#lightrag-mcp) / Available Tools [β](#mcp-client-configuration) [β](#usage-examples)
The server provides **26 MCP tools** organized into 4 categories:
<details>
<summary><b>π Documents (11 tools)</b> - Manage your knowledge base</summary>
### Document Management
#### `insert_text`
Add text content to LightRAG knowledge base.
```typescript
interface InsertTextParams {
text: string // Text content to add
file_source?: string // Optional source identifier
}
```
**Example:**
```json
{
"tool": "insert_text",
"arguments": {
"text": "TypeScript is a typed superset of JavaScript",
"file_source": "docs/typescript.md"
}
}
```
#### `insert_texts`
Add multiple text documents at once.
```typescript
interface InsertTextsParams {
texts: string[] // Array of text contents
file_sources?: string[] // Optional source identifiers
}
```
#### `scan_documents`
Trigger scanning process for new documents in the input directory.
#### `get_documents_paginated`
Retrieve documents with pagination support.
```typescript
interface GetDocumentsPaginatedParams {
status_filter?: 'pending' | 'processing' | 'completed' | 'failed'
page?: number // Default: 1
page_size?: number // Default: 20
sort_field?: string
sort_direction?: 'asc' | 'desc'
}
```
#### `get_document_status_counts`
Get counts of documents by status.
#### `get_track_status`
Get the processing status of documents by tracking ID.
```typescript
interface GetTrackStatusParams {
track_id: string
}
```
#### `delete_document`
Delete documents by their IDs.
```typescript
interface DeleteDocumentParams {
doc_ids: string[] // Document IDs to delete
delete_file?: boolean // Delete source files
delete_llm_cache?: boolean // Clear LLM cache
}
```
#### `clear_documents`
Clear all documents from the RAG system.
#### `clear_cache`
Clear all cache data from the LLM response cache storage.
#### `reprocess_failed`
Reprocess failed and pending documents.
</details>
<details>
<summary><b>π Queries (2 tools)</b> - Search and retrieve information</summary>
### Query Operations
#### `query_text`
Query the RAG system with various modes.
```typescript
interface QueryTextParams {
query: string
mode?: 'local' | 'global' | 'hybrid' | 'naive' | 'mix' | 'bypass'
top_k?: number
enable_rerank?: boolean
include_references?: boolean
}
```
**Modes:**
- `local` - Search within local context
- `global` - Global knowledge graph search
- `hybrid` - Combines local and global (default)
- `naive` - Simple vector search
- `mix` - Knowledge graph + vector search
- `bypass` - Direct LLM query without RAG
**Example:**
```json
{
"tool": "query_text",
"arguments": {
"query": "What is LightRAG?",
"mode": "hybrid",
"top_k": 5,
"enable_rerank": true
}
}
```
#### `query_data`
Advanced data retrieval endpoint for structured RAG analysis without LLM generation.
</details>
<details>
<summary><b>πΈοΈ Knowledge Graph (11 tools)</b> - Build and explore entity relationships</summary>
### Graph Operations
#### `get_graph_labels`
Get all graph labels (entity types).
#### `get_popular_labels`
Get popular labels by node degree (most connected entities).
```typescript
interface GetPopularLabelsParams {
limit?: number // Default: 10
}
```
#### `search_labels`
Search labels with fuzzy matching.
```typescript
interface SearchLabelsParams {
q: string // Search query
limit?: number // Default: 10
}
```
**Example:**
```json
{
"tool": "search_labels",
"arguments": {
"q": "machine learning",
"limit": 10
}
}
```
#### `get_knowledge_graph`
Retrieve a connected subgraph of nodes.
```typescript
interface GetKnowledgeGraphParams {
label: string // Starting entity label
max_depth?: number // Default: 2
max_nodes?: number // Default: 50
}
```
**Example:**
```json
{
"tool": "get_knowledge_graph",
"arguments": {
"label": "LightRAG",
"max_depth": 2,
"max_nodes": 50
}
}
```
#### `check_entity_exists`
Check if an entity with the given name exists.
```typescript
interface CheckEntityExistsParams {
name: string
}
```
#### `create_entity`
Create a new entity in the knowledge graph.
```typescript
interface CreateEntityParams {
entity_name: string
entity_data: {
entity_type?: string
description?: string
source_id?: string
}
}
```
#### `update_entity`
Update an entity's properties.
```typescript
interface UpdateEntityParams {
entity_name: string
updated_data: object
allow_rename?: boolean // Allow renaming entity
allow_merge?: boolean // Allow merging with existing
}
```
#### `delete_entity`
Remove an entity and all its relationships.
```typescript
interface DeleteEntityParams {
entity_name: string
}
```
#### `merge_entities`
Merge multiple entities into a single entity, preserving all relationships.
```typescript
interface MergeEntitiesParams {
entities_to_change: string[] // Entities to merge
entity_to_change_into: string // Target entity
}
```
**Example:**
```json
{
"tool": "merge_entities",
"arguments": {
"entities_to_change": ["ML", "machine learning"],
"entity_to_change_into": "Machine Learning"
}
}
```
#### `create_relation`
Create a new relationship between two entities.
```typescript
interface CreateRelationParams {
source_entity: string
target_entity: string
relation_data: {
keywords?: string
weight?: number
description?: string
source_id?: string
}
}
```
#### `update_relation`
Update a relation's properties.
```typescript
interface UpdateRelationParams {
source_id: string
target_id: string
updated_data: object
}
```
#### `delete_relation`
Remove a relationship between two entities.
```typescript
interface DeleteRelationParams {
source_entity: string
target_entity: string
}
```
</details>
<details>
<summary><b>βοΈ System (2 tools)</b> - Monitor and control pipeline</summary>
### System Management
#### `get_pipeline_status`
Get the current status of the document indexing pipeline.
#### `cancel_pipeline`
Request cancellation of the currently running pipeline.
</details>
## Usage Examples
###### [π οΈ](#lightrag-mcp) / Usage Examples [β](#available-tools) [β](#programmatic-usage)
<details>
<summary><b>π Basic Workflow</b> - Insert, process, and query documents</summary>
### Basic Workflow
Insert documents
```json
{
"tool": "insert_text",
"arguments": {
"text": "LightRAG is a simple and fast RAG system.",
"file_source": "intro.txt"
}
}
```
Wait for processing
```json
{
"tool": "get_pipeline_status"
}
```
Query your knowledge base
```json
{
"tool": "query_text",
"arguments": {
"query": "What is LightRAG?",
"mode": "hybrid"
}
}
```
</details>
<details>
<summary><b>π Query with Different Modes</b> - Explore various search strategies</summary>
### Query Modes
Hybrid mode (default)
```json
{
"tool": "query_text",
"arguments": {
"query": "What is LightRAG?",
"mode": "hybrid"
}
}
```
Mix mode (knowledge graph + vector search)
```json
{
"tool": "query_text",
"arguments": {
"query": "Explain RAG systems",
"mode": "mix"
}
}
```
</details>
<details>
<summary><b>πΈοΈ Knowledge Graph Operations</b> - Manage entities and relationships</summary>
### Graph Management
Search for entities
```json
{
"tool": "search_labels",
"arguments": {
"q": "machine learning",
"limit": 10
}
}
```
Get entity subgraph
```json
{
"tool": "get_knowledge_graph",
"arguments": {
"label": "LightRAG",
"max_depth": 2,
"max_nodes": 50
}
}
```
Merge duplicate entities
```json
{
"tool": "merge_entities",
"arguments": {
"entities_to_change": ["ML", "machine learning"],
"entity_to_change_into": "Machine Learning"
}
}
```
</details>
## Programmatic Usage
###### [π οΈ](#lightrag-mcp) / Programmatic Usage [β](#usage-examples) [β](#development)
```ts server.ts
import { LightRagServer } from 'lightrag-mcp'
const server = new LightRagServer({
clientOptions: {
baseUrl: 'https://rag.example.com',
},
apiKey: 'your-api-key',
})
await server.start()
```
## Development
###### [π οΈ](#lightrag-mcp) / Development [β](#programmatic-usage) [β](#troubleshooting)
```sh
# Install dependencies
npm install
# Build
npm run build
# Lint
npm run lint
# Update API types from OpenAPI spec
npm run update:api
```
## Troubleshooting
###### [π οΈ](#lightrag-mcp) / Troubleshooting [β](#development) [β](#links)
<details>
<summary><b>π API key not found</b></summary>
**Symptoms:** `Error: Use apiKey option or LIGHTRAG_API_KEY`
**Solution:**
- Set `LIGHTRAG_API_KEY` in your `.env` file
- Pass via `--token` flag in CLI
- Provide in constructor options
```sh
# .env
LIGHTRAG_API_KEY=your-secret-key
```
</details>
<details>
<summary><b>π Cannot connect to LightRAG</b></summary>
**Symptoms:** `Connection refused` or `ECONNREFUSED`
**Solution:**
- Verify LightRAG server is running on port 9621
- Check `LIGHTRAG_BASE_URL` configuration
- Verify firewall settings
- Test connection: `curl http://localhost:9621/health`
</details>
<details>
<summary><b>β³ Documents not processing</b></summary>
**Symptoms:** Documents stuck in `pending` or `processing` state
**Check pipeline status:**
```sh
# Using CLI
curl http://localhost:9621/health
# Or use MCP tool
{
"tool": "get_pipeline_status"
}
```
**Common causes:**
- LightRAG server overloaded
- Document format not supported
- Insufficient memory
**Solutions:**
- Check pipeline status: use `get_pipeline_status` tool
- Review LightRAG server logs
- Try reprocessing: use `reprocess_failed` tool
- Verify document format and size limits
- Increase server resources
- Split large documents
</details>
<details>
<summary><b>πΈοΈ Knowledge graph queries return empty results</b></summary>
**Symptoms:** `get_knowledge_graph` returns no nodes
**Solution:**
- Verify entity exists: use `check_entity_exists`
- Try fuzzy search: use `search_labels`
- Check if documents are fully processed
- Increase `max_depth` and `max_nodes` parameters
</details>
## Links
###### [π οΈ](#lightrag-mcp) / Links [β](#troubleshooting) [β](#issues)
You can find more MCP servers and tools on [NPM](https://www.npmjs.com/search?q=mcp)
- [LightRAG](https://github.com/HKUDS/LightRAG) - Knowledge graph-powered RAG system
- [MCP Specification](https://modelcontextprotocol.io/) - Model Context Protocol documentation
## Issues
###### [π οΈ](#lightrag-mcp) / Issues [β](#links)
If you find a bug or have a suggestion, please file an issue on [GitHub](https://github.com/d8corp/lightrag-mcp/issues)
[](https://github.com/d8corp/lightrag-mcp/issues)
This server cannot be deployed
Maintenance
ActivityInactive
ResponsivenessNo issues