Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

I built a local RAG server for Claude and Copilot in C#. Here is what I learned about search.

I wanted Claude and GitHub Copilot to answer questions from my own notes and documentation, without uploading anything. So I built docs-rag-mcp, an MCP server in C# that indexes a folder of documents and lets any MCP cli

I wanted Claude and GitHub Copilot to answer questions from my own notes and documentation, without uploading anything. So I built docs-rag-mcp, an MCP server in C# that indexes a folder of documents and lets any MCP client search it.

You point it at a folder of Markdown, text, HTML, Word or PDF files. It splits them into passages, embeds each passage, and stores everything in one SQLite file. When you ask your assistant a question, it calls search_docs and answers from the passages it gets back, with the file and section for each one.

claude mcp add docs-rag -- dnx DocsRagMcp --yes

In VS Code and Cursor, the README has one-click install buttons.

With Ollama, nothing leaves your machine. It also works with OpenAI and Azure OpenAI embeddings.

Getting a basic pipeline working is the easy part. Making the results good is harder, and that is what this post is about.

1. Split documents the way their authors did

Most RAG tutorials cut text into fixed-size chunks. That loses the most useful piece of information a document has: which section a sentence belongs to.

docs-rag-mcp splits Markdown, HTML and Word files at their headings first, and PDFs by page. Each passage remembers its location, such as remote-work.md > Working from another country. Only then are long sections cut into chunks, at paragraph boundaries where possible, with a small overlap.

Each format has its own traps:

  • Markdown: lines starting with # inside code blocks are not headings. YAML front matter from Jekyll, Hugo or Obsidian is not content, but its title is a good name for the text before the first heading.
  • HTML: a saved page is mostly navigation. Reading only the <main> element when a page has one removes sidebars and "Skip to main content" links without any site-specific rules. But a page's <header> is chrome, while an article's <header> usually holds its <h1>, so the rule depends on where it sits.
  • Word: style ids are translated. A German Word writes berschrift1 instead of Heading1. The style names are always heading 1 in English, so headings are found by name. A .docx file is just a zip of XML, so this needed no extra library.

2. Embed each chunk with its context

A passage like "Up to 220 EUR in Paris" is useless on its own. Which budget? Which policy?

Before a chunk is embedded, the server puts the file name and heading in front of it: travel-and-expenses.md > Hotels. The vector then carries the context, and the passage matches a question about hotel budgets even though it never says "hotel".

This was the cheapest improvement in the whole project.

3. Meaning is not enough: hybrid search

Embeddings match meaning, which is why "Can I spend a month working from Spain?" finds a section about "working from another country for up to 30 days a year". Neither "Spain" nor "month" appears in it.

But meaning is fuzzy where developers need precision. In testing, a question about pages that "require a login" matched a page about logging. API names, error codes and option names need exact matching.

So every chunk is also indexed with SQLite's built-in FTS5 full-text search, with porter stemming and headings weighted twice as much as body text. A search scores candidates both ways, scales both scores to 0..1, and blends them: 90% meaning, 10% keywords.

The obvious choice was reciprocal rank fusion, the usual way to combine two rankings. I tried it first, and it made results worse: any passage that shared a word with the question could beat the best meaning match, and plain-language questions suffered. A small keyword weight settles close calls without outvoting a clearly better match.

I measured this instead of guessing. The repository has two evaluation sets: 14 employee questions about a sample handbook, and 22 developer questions about the MCP C# SDK documentation (30 pages, 556 chunks), each with the page that answers it. With Ollama and nomic-embed-text:

Search mode Handbook: right section first SDK docs: right page in the top 3 SDK docs: mean reciprocal rank
Vector only 14 of 14 15 of 22 0.61
Hybrid 14 of 14 17 of 22 0.66

Both sets are small, so I read these as a direction, not a benchmark. Keywords help most when the question names the feature, such as cancellation or elicitation, and can't help when the question shares no distinctive word with the answer.

4. Keep the index in sync without trusting file events

An index that is out of date gives confident wrong answers. So indexed folders are watched with FileSystemWatcher.

File system events are unreliable. They arrive in bursts, can be dropped when too many happen at once, and behave differently on Windows, macOS and Linux. Handling them one by one is a bug factory.

Instead, an event only means "something changed". The watcher waits until the folder has been quiet for 2 seconds, then runs the normal sync. The sync compares each file's SHA-256 with the stored hash, so it only re-embeds files whose content actually changed. When the server starts, it runs the same sync once per folder to catch up on changes made while it was off.

One code path for indexing, re-indexing and watching meant one code path to test.

5. No vector database

Vectors are stored as blobs in SQLite and compared with TensorPrimitives.CosineSimilarity, which uses SIMD. Brute force sounds naive, but for personal and team document sets, up to tens of thousands of chunks, it is fast and needs no native extension. The whole index is one file you can delete or copy.

Small details that matter

  • The index remembers its embedding model. Vectors from different models can't be compared, so searching with another model is refused instead of returning nonsense.
  • Folder limits. DOCSRAG_ALLOWED_PATHS stops an assistant from indexing your home folder. Paths are resolved before the check, so docs/../secrets doesn't get around it.
  • list_sources says where the index is and which model built it. When results look wrong, that is the first thing to check.

What I would tell someone building their own

  1. Keep structure. Headings are the best metadata you will get for free.
  2. Put context into the embedded text, not only into metadata.
  3. Write down 20 real questions with known answers before tuning anything. Every change I made was decided by that list.
  4. Treat file events as hints, not facts.

docs-rag-mcp is open source (MIT), on NuGet and in the official MCP Registry. Issues and pull requests are welcome, and if it is useful to you, a star on GitHub helps other people find it.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.