Skip to main content
Glama
mcp-knowledge-base

mcp-knowledge-base

by shelldon0x
README.md
# mcp-knowledge-base

A local, self-contained MCP (Model Context Protocol) server that lets AI assistants search, retrieve, and manage a local document collection using BM25 retrieval with stemming and stopword filtering. No external APIs required.

Built with the [official MCP TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk). Works with Claude Desktop, Cursor, Windsurf, and any MCP-compatible client.

## Features

- **BM25 search** with Porter stemming and stopword filtering for high-quality retrieval
- **Document chunking** (~500 tokens with overlap) preserving original Markdown formatting
- **6 MCP tools**: search, list, get, ingest, update, delete
- **Stdio transport** (standard for local MCP servers)
- **File locking** with stale lock detection for safe concurrent access
- **Corrupt store recovery** with automatic backup
- **Zero external dependencies** for search (no OpenAI/Anthropic API calls needed)
- **9 unit tests** with Vitest
- Example document auto-ingested on first run

## Requirements

- Node.js 20+
- npm

## Quick Start

```bash
git clone https://github.com/shelldon0x/mcp-knowledge-base.git
cd mcp-knowledge-base
npm install
npm run build
```

## MCP Tools

| Tool | Description |
|---|---|
| `search_knowledge_base` | BM25 search across indexed document chunks with relevance scoring |
| `list_documents` | List all indexed documents with metadata and timestamps |
| `get_document` | Retrieve full content of a specific document by ID |
| `ingest_document` | Add a new document (Markdown or plain text) to the knowledge base |
| `update_document` | Update title, content, or metadata of an existing document (re-indexes on content change) |
| `delete_document` | Remove a document and all its chunks from the knowledge base |

## MCP Client Configuration

### Claude Desktop

Add to your Claude Desktop config (`~/Library/Application Support/Claude/claude_desktop_config.json` on macOS):

```json
{
  "mcpServers": {
    "knowledge-base": {
      "command": "node",
      "args": ["/absolute/path/to/mcp-knowledge-base/dist/index.js"]
    }
  }
}
```

### Cursor / Windsurf

Add the same `mcpServers` block to your editor's MCP settings.

## Usage Examples

### Search the knowledge base

```json
{
  "query": "retrieval augmented generation",
  "top_k": 5
}
```

Returns ranked chunks with scores, source attribution, and original formatting preserved.

### Ingest a new document

```json
{
  "title": "Team onboarding notes",
  "content": "# Welcome\nThese are the team onboarding notes...",
  "metadata": {
    "source": "docs/onboarding.md",
    "tags": ["onboarding", "internal"]
  }
}
```

### Update a document

```json
{
  "document_id": "abc-123",
  "title": "Updated title",
  "content": "New content here..."
}
```

Content changes automatically trigger re-chunking and re-indexing.

### Delete a document

```json
{
  "document_id": "abc-123"
}
```

## How It Works

### Search Pipeline

1. **Tokenization**: Text is lowercased and split into alphanumeric tokens
2. **Stopword removal**: 90+ common English words filtered out
3. **Stemming**: Basic Porter stemmer handles `-ing`, `-ed`, `-s`, `-ly`, `-tion`, `-ness` suffixes with double-consonant reduction
4. **BM25 scoring**: Okapi BM25 with k1=1.5, b=0.75 ranks chunks by relevance
5. **Source attribution**: Results include document title, chunk snippet (with original formatting), and metadata

### Document Storage

- Documents are split into ~500 token chunks with 50-token overlap
- Chunks preserve the original text (including Markdown formatting) while using stemmed tokens for indexing
- All data stored in `data/knowledge-base.json` (created automatically)
- File locking prevents concurrent write corruption (stale locks auto-expire after 30s)
- Corrupt JSON files are backed up to `.corrupt` and the store reinitializes gracefully

## Development

```bash
npm run dev    # Run with tsx (hot reload)
npm run build  # Compile TypeScript
npm test       # Run Vitest test suite
```

## Architecture

```
src/
├── index.ts          # Entry point, stdio transport setup
├── server.ts         # MCP server with 6 registered tools
├── knowledge-base.ts # Document store, BM25 search, chunking, stemming
├── types.ts          # TypeScript interfaces
└── __tests__/
    └── knowledge-base.test.ts  # 9 unit tests
```

## Tech Stack

- TypeScript (strict, no `any`)
- [@modelcontextprotocol/sdk](https://github.com/modelcontextprotocol/typescript-sdk) for MCP server
- [Zod](https://github.com/colinhacks/zod) for input validation
- [Vitest](https://vitest.dev/) for testing
- ESM modules with NodeNext resolution

## License

MIT

TDQS

A3.5/5.0

Scored across 6 tools

Disambiguation5/5

Each tool targets a distinct resource+action: search across docs, list metadata, get content, and create/update/delete lifecycle operations. The boundaries between search, list, and get are clear from the descriptions, and CRUD operations are unambiguous.

Naming Consistency5/5

All six tools follow a consistent snake_case verb_noun pattern (search_knowledge_base, list_documents, get_document, ingest_document, delete_document, update_document). The convention is predictable throughout with no deviations.

Tool Count5/5

Six tools is well-scoped for a knowledge base server, covering search, listing, retrieval, ingestion, update, and deletion without redundancy. Each tool earns its place.

Completeness5/5

The surface covers full document lifecycle: create (ingest), read (get/list/search), update (with reindex), and delete. No obvious gaps for a knowledge base domain.

Maintenance

ActivityInactive
ResponsivenessNo recent community issues