npm package: @notbnull/mcp-rag-context
Description: A lightweight MCP server for RAG-based memory context management with local vector store
Author: notbnull
Latest version: 1.0.1

# RAG Context MCP Server

A lightweight Model Context Protocol (MCP) server that provides persistent memory and context management using local vector storage and database. This server enables AI assistants to store and retrieve contextual information efficiently using both semantic search and indexed retrieval.

## Features

- **Local Vector Storage**: Uses Vectra for efficient vector similarity search
- **Persistent Memory**: SQLite database for reliable data persistence
- **Semantic Search**: Automatic text embedding using Xenova/all-MiniLM-L6-v2 model
- **Hybrid Retrieval**: Combines semantic search with indexed database queries
- **Simple API**: Just two tools - `setContext` and `getContext`
- **Lightweight**: Minimal dependencies, runs entirely locally
- **Privacy-First**: All data stored locally, no external API calls

## Installation

### Using npm

```bash
npm install -g @notbnull/mcp-rag-context
```

### Using npx (no installation required)

```bash
npx @notbnull/mcp-rag-context
```

## Configuration

### For Claude Desktop

Add the following to your Claude Desktop configuration file:

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

```json
{
  "mcpServers": {
    "rag-context": {
      "command": "npx",
      "args": ["@notbnull/mcp-rag-context"],
      "env": {
        "RAG_CONTEXT_DATA_DIR": "/path/to/your/data/directory"
      }
    }
  }
}
```

### For Cursor

Add the following to your Cursor MCP configuration file:

**Location**: `~/.cursor/mcp.json`

```json
{
  "mcp-rag-context": {
    "command": "npx",
    "args": ["@notbnull/mcp-rag-context@latest"]
  }
}
```

To specify a custom data directory, use environment variables:

```json
{
  "mcp-rag-context": {
    "command": "npx",
    "args": ["@notbnull/mcp-rag-context@latest"],
    "env": {
      "RAG_CONTEXT_DATA_DIR": "/path/to/your/data/directory"
    }
  }
}
```

### Environment Variables

- `RAG_CONTEXT_DATA_DIR`: Directory where the database and vector index will be stored (default: `~/.rag-context-mcp`)

## Usage

The server exposes two main tools:

### setContext

Store information in memory with automatic vectorization:

```json
{
  "tool": "setContext",
  "arguments": {
    "key": "user_preferences",
    "content": "The user prefers dark mode and uses VS Code as their primary editor",
    "metadata": {
      "category": "preferences",
      "timestamp": "2024-01-15"
    }
  }
}
```

### getContext

Retrieve relevant context using semantic search:

```json
{
  "tool": "getContext",
  "arguments": {
    "query": "What are the user's editor preferences?",
    "limit": 5,
    "threshold": 0.7
  }
}
```

## System Prompt for AI Assistants

To effectively use this MCP server, add the following to your AI assistant's system prompt:

````markdown
## Memory and Context Management

You have access to a persistent memory system through the RAG Context MCP server. This allows you to store and retrieve information across conversations.

### When to Store Context

Store information when:

- Users share preferences, settings, or personal information
- Important project details or configurations are discussed
- Key decisions or agreements are made
- Useful code snippets or solutions are created
- Learning about user's workflow, tools, or environment

### How to Store Context

`Use the`setContext` tool with:

- A descriptive, unique key (e.g., "project_setup_nextjs", "user_pref_editor")
- Clear, concise content that captures the essential information
- Relevant metadata (category, project, date, etc.)

Example:

```json
{
  "key": "project_api_structure",
  "content": "The project uses a REST API with /api/v1 prefix. Authentication is handled via JWT tokens in the Authorization header. Main endpoints: /users, /posts, /comments",
  "metadata": {
    "project": "blog-platform",
    "type": "architecture",
    "date": "2024-01-15"
  }
}
```
````

### When to Retrieve Context

Retrieve context when:

- Starting a new conversation about a previously discussed topic
- Users reference past discussions or decisions
- You need to recall specific technical details or preferences
- Building upon previous work or solutions

### How to Retrieve Context

Use the `getContext` tool with:

- A natural language query describing what you're looking for
- Appropriate limit (usually 3-5 results)
- Threshold of 0.7 for balanced precision/recall

Example:

```json
{
  "query": "API authentication setup for the blog project",
  "limit": 3,
  "threshold": 0.7
}
```

### Best Practices

1. **Be Selective**: Store important, reusable information, not every detail
2. **Use Clear Keys**: Make keys descriptive and searchable
3. **Add Metadata**: Include project names, categories, and dates
4. **Update Existing**: Use the same key to update information rather than creating duplicates
5. **Query Naturally**: Write queries as you would ask a colleague

Remember: This memory persists across all conversations, making you more helpful over time by remembering important context and user preferences.

```

## Architecture

The server uses a hybrid approach for optimal performance:

1. **SQLite Database**: Stores the actual content with metadata, provides fast key-based lookups
2. **Vector Index**: Enables semantic search using embeddings
3. **Local Embeddings**: Uses Xenova/transformers for privacy-preserving, local text embedding

## Data Storage

All data is stored locally in the specified data directory:

```

<RAG_CONTEXT_DATA_DIR>/
├── memories.db # SQLite database
└── vectors.index # Vectra vector index

````

### Data Persistence

**Your data persists across sessions!** The MCP server uses file-based storage, which means:

- ✅ **Closing Cursor/AI client**: Data remains intact on disk
- ✅ **Stopping the MCP server**: All memories are preserved
- ✅ **Restarting your computer**: Data automatically reloads on next startup
- ✅ **Long-term storage**: Build up context and preferences over weeks/months

When you restart Cursor or the MCP server, it automatically reconnects to your existing database and vector index, giving you immediate access to all previously stored memories and context.

**Default location**: `~/.rag-context-mcp/` (or your custom `RAG_CONTEXT_DATA_DIR`)

This persistent memory enables truly helpful AI assistance that improves over time by remembering your preferences, project details, and conversation history.

## Development

### Building from Source

```bash
# Clone the repository
git clone https://github.com/yourusername/rag-context-mcp.git
cd rag-context-mcp

# Install dependencies
npm install

# Build the project
npm run build

# Run in development mode
npm run dev
````

### Running Tests

```bash
npm test
```

## Privacy and Security

- All data is stored locally on your machine
- No external API calls for embeddings (uses local model)
- No telemetry or data collection
- You control where data is stored via `RAG_CONTEXT_DATA_DIR`

## Troubleshooting

### Common Issues

1. **"VectorStore not initialized" error**

   - Ensure the data directory exists and has write permissions
   - Check that the `RAG_CONTEXT_DATA_DIR` path is valid

2. **Slow first startup**

   - The embedding model is downloaded on first use (~30MB)
   - Subsequent starts will be much faster

3. **High memory usage**
   - The embedding model requires ~200MB RAM
   - Consider limiting the number of stored contexts

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

MIT License - see LICENSE file for details

## Acknowledgments

Inspired by the MCP memory server example from Anthropic, but enhanced with:

- Local vector storage for better retrieval
- SQLite for reliable persistence
- Hybrid search capabilities
- Privacy-focused design