{"slug": "i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18", "title": "I added a reranker to my local RAG server. The right page now comes first for 18 of 22 questions instead of 12.", "summary": "A developer added an optional cross-encoder reranking step to docs-rag-mcp, an open-source C# MCP server that lets Claude, GitHub Copilot and Cursor search local documents, wiring it through a standard /rerank HTTP endpoint rather than bundling a model. On a 22-question evaluation set over MCP C# SDK docs, hybrid search plus reranking put the correct page first for 18 questions versus 12 with hybrid alone, and raised mean reciprocal rank from 0.67 to 0.88, though it demoted one already-correct answer on a small handbook set. The implementation falls back to hybrid ordering if the reranker errors or times out, and keeps cosine similarity as the displayed score.", "body_md": "[docs-rag-mcp](https://github.com/Ahmed-Manai/docs-rag-mcp) is an MCP server in C# that lets Claude, GitHub Copilot and Cursor search your own documents. In [my first post](https://dev.to/ahmed_manai/i-built-a-local-rag-server-for-claude-and-copilot-in-c-here-is-what-i-learned-about-search-2h3) I wrote about hybrid search: vector similarity blended with SQLite FTS5 keyword ranking. Version 0.8.0 adds an optional last step, reranking with a cross-encoder. This post covers why, how I wired it in, and what it changed on my evaluation sets.\n\nAn embedding model turns the question and each passage into vectors separately, then compares them. It never sees the two together. That's what makes it fast enough to search a whole index, and also why it misses distinctions that matter.\n\nMy harder test set is 22 developer questions about the [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk) docs: 30 pages and 556 chunks, where many pages share the same words (server, client, tool, request). With hybrid search, the right page came first for 12 of the 22 questions.\n\nA cross-encoder reads the question and one passage together and outputs a relevance score. It is much better at telling which passage answers the question, and much too slow to run over every chunk. So it only reorders the best candidates: hybrid search picks the top 20, and the reranker decides their order.\n\nI didn't want to ship a model inside the server. The options I looked at:\n\n`/rerank` HTTP endpoint`query` and `documents` in, `index` and `relevance_score` out) is also served by Jina, vLLM, Infinity, LocalAI and llama.cpp's `llama-server --reranking`.\nI went with the endpoint. One small client covers a local model and hosted services, with nothing but `HttpClient` and `System.Text.Json`:\n\n```\nllama-server -hf gpustack/bge-reranker-v2-m3-GGUF:Q4_K_M --reranking --port 8012 -c 8192 -b 8192 -ub 8192\n\"env\": { \"DOCSRAG_RERANK_ENDPOINT\": \"http://localhost:8012/v1/rerank\" }\n```\n\nReranking is off unless that variable is set.\n\n**Asking for more results must not change the first ones.** To rerank, the search has to return more candidates than `topK`. I made sure the first `topK` of a longer list are exactly what a normal search returns, so turning reranking off gives the same results as before, and a test checks it.\n\n**A failing reranker must not fail the search.** If the endpoint is down, returns an error, returns something that doesn't match the request, or takes more than 30 seconds, the server logs a warning and returns the hybrid order. I tested this by pointing it at a port where nothing listens: the results were identical to plain hybrid search.\n\n**The score shown stays the same kind of number.** Reranker scores are not on the same scale as cosine similarity, and they differ between models. The reranker only decides the order; the score next to each passage is still the cosine similarity, so it reads the same with or without reranking.\n\nMeasured with Ollama and `nomic-embed-text` for the embeddings and `bge-reranker-v2-m3` (4-bit) on CPU:\n\n| Search mode | Handbook: right section first | SDK docs: right page first | SDK docs: in the top 3 | SDK docs: MRR | \n|---|---|---|---|---|\n| vector | 14 of 14 | 11 of 22 | 15 of 22 | 0.61 | \n| hybrid | 14 of 14 | 12 of 22 | 17 of 22 | 0.67 | \n| hybrid + rerank | 13 of 14 | 18 of 22 | 20 of 22 | 0.88 | \n\nOn the SDK docs, reranking put the right page first for six more questions and raised the mean reciprocal rank from 0.67 to 0.88. That is a bigger jump than hybrid search gave over plain vectors.\n\nIt isn't free. On the small employee handbook, which was already perfect, it moved one right answer from first to second place. And every search now waits for a second model. Both sets are small, so I read these numbers as a direction, not a benchmark. The evaluation script is in the repo, so you can run it on your own documents:\n\n```\nDOCSRAG_RERANK_ENDPOINT=http://localhost:8012/v1/rerank dotnet run samples/eval/eval.cs -- samples/eval/mcp-csharp-sdk-docs.json /tmp/csharp-sdk\nclaude mcp add docs-rag -- dnx DocsRagMcp --yes\n```\n\ndocs-rag-mcp is open source (MIT), on [NuGet](https://www.nuget.org/packages/DocsRagMcp) and in the official MCP Registry. If you try reranking with another model or a hosted service, I'd like to hear how it does on your documents: [GitHub](https://github.com/Ahmed-Manai/docs-rag-mcp).", "url": "https://wpnews.pro/news/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18", "canonical_source": "https://dev.to/ahmed_manai/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18-of-22-questions-6g2", "published_at": "2026-11-27 09:39:11+00:00", "updated_at": "2026-11-27 09:44:39.582108+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "ai-tools", "developer-tools"], "entities": ["docs-rag-mcp", "Ahmed Manai", "Claude", "GitHub Copilot", "Cursor", "MCP C# SDK", "bge-reranker-v2-m3", "Ollama"], "also_reported_by": [], "takeaway": "Adding a cross-encoder reranker over the top 20 hybrid candidates raised MRR from 0.67 to 0.88 and top-1 accuracy from 12/22 to 18/22 on MCP C# SDK docs, at the cost of one demoted answer on an already-perfect handbook set.", "recommendation": {"advice": "Rerank only the top ~20 hybrid candidates through a /rerank endpoint, and fall back to hybrid order on error or timeout so a failing reranker never breaks search.", "playbook": "rag-retrieval", "playbook_url": "https://wpnews.pro/playbooks/rag-retrieval"}, "alternates": {"html": "https://wpnews.pro/news/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18", "markdown": "https://wpnews.pro/news/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18.md", "text": "https://wpnews.pro/news/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18.txt", "jsonld": "https://wpnews.pro/news/i-added-a-reranker-to-my-local-rag-server-the-right-page-now-comes-first-for-18.jsonld"}}