Dec 6, 2026
visibility 7
star star star star star
(0 votes)

I Added Semantic Search to My Bookshelf MCP Server

Overview

In my last post I turned my pile of tech ebooks into an MCP server in .NET, so Claude Code could answer questions from books I already own and cite the page. In it I wrote "No vector database and no embeddings ... I can add embeddings later if keyword search misses things."

Well, it missed things. So I added embeddings. They run locally on my laptop, the server is still ASP.NET Core, and there is still no vector database.

Spoiler alert: my first version wasn't very good, and I only found out because we tested it.

I'll also be upfront: I built this with Claude Code as a pair. I'm not an embeddings expert. Claude explained the options, wrote most of the code and the tests, and ran the experiments. I decided what to build, asked a lot of questions, pushed back when something looked off, and checked the results.

Where Keyword Search Falls Down

Keyword search is great when you know the name of the thing. Ask for IContentLoader or IOrderEvents and SQLite's full-text search finds it in milliseconds.

It's not so great when you don't know the name, which is a lot of the time. Say you ask "how do I stop editors from putting the wrong blocks in a content area". The answer is AllowedTypes, and that word isn't anywhere in the question.

My keyword search finds pages from Episerver: The Missing Manual about designing content areas, but not the pages with the actual answer (p. 32 of the same book and p. 129 of the CMS Development Fundamentals course book). Further down it drifts into a 2007 web design book that matched on words like "the" and "putting."

Embeddings on a Laptop

An embedding model turns text into a list of numbers (a vector) so that text about the same thing ends up close together, even when it uses different words. Embed every passage once, embed the question, and grab the closest passages.

I didn't want to send 1,294 books to a cloud API, so the model runs in Ollama on my laptop's GPU (an RTX 5070 with 8 GB). A new shelf.py embed command stores the vectors in a separate SQLite file.

The first version used nomic-embed-text, kept 256 of its 768 numbers per passage (it's trained so the first ones carry most of the meaning), and stored one vector per page. It took about an hour and fifteen minutes to embed the library, the demos looked pretty good, and I started writing this post. Yeah, about that.

(A side quest that had nothing to do with code: the GPU was only pulling 35 of its 65 watts. Claude's first answer was that I was on battery. I wasn't; the laptop was plugged in. It had trusted a Windows battery status field that turned out to be wrong, and when I pushed back it found the real cause: Windows was set to the "Best power efficiency" power mode while plugged in. Switching to "Best performance" on AC got it to 55 watts. So if you run models locally on a laptop, check that setting, and don't take a confident AI answer about your own machine as the final word.)

Then We Tested It

The problem with trying it out yourself is that you ask a handful of questions you already know the answers to, so of course it looks good. So I had Claude build a small test: about 50 books from my CMS and ASP.NET Core shelves (around a dozen Optimizely ones; the rest are Sitecore, Umbraco, WordPress and friends, which make great distractors), and 20 developer-style questions with answers we could check. "Give editors a dropdown list of options for a property" is answered by a page that mentions ISelectionFactory. A question scores if a page with the answer is in the top five.

Then it compared three local models: nomic-embed-text, mxbai-embed-large and qwen3-embedding (the 0.6B one). qwen3 won, and we were about to switch.

Before kicking off a four-hour re-embed, I asked for a second opinion: a fresh Claude session with none of our context and one job, to poke holes in what we'd concluded. It found plenty:

  • Twenty questions can't tell the models apart. One or two questions either way changed the winner.
  • The change that clearly mattered was chunking. One vector per page meant the model only saw the first 2,000 characters of a page, and 22% of my pages are longer than that. The AllowedTypes explanation in the Missing Manual sits right at the 2,000-character cutoff of a 6,600-character page, so the first version barely saw it. Splitting pages into overlapping 1,200-character chunks took nomic from 9 to 14 of 20.
  • My keyword half wasn't helping plain-English questions. It searched for every word, including "how," "the" and "putting." Even with those dropped, keywords added little for questions written in plain English.
  • The test was too strict and too easy. It only counted a page if it had the exact API name, so a page that explains scheduled jobs without saying ScheduledPlugIn was a miss. And a few of my questions gave away the answer in their wording.

Ouch. But it was all fair, and none of it was something I'd have caught by looking at demos.

What Changed

  • Chunks, not pages. Each chunk gets a vector, and a page scores by its best chunk.
  • qwen3-embedding at full size (1,024 numbers). Cutting it to 256 tended to push the right page lower in the list.
  • One byte per number instead of four. The test showed no difference.
  • Exact names get special treatment. If the question has something that looks like an API (IContentLoader, UrlResolver.GetUrl), keyword search looks for it as an exact phrase and counts a lot. For plain-English questions, keywords barely count.

The new test has 45 questions: the original 20, 10 harder Optimizely ones ("restrict which page types can be created under a page"), and 15 where the developer already knows the name and just types IPromotionEngine. A page counts as a hit if it has the API name or clearly covers the concept (scheduled jobs, visitor groups, the options pattern).

  Top 5 (of 45) Plain English (of 30) Exact name ranked first (of 15)
Keyword search only 28 13 15
First version's logic (nomic, every word as a keyword, but with chunking) 31 16 15
qwen3 embeddings only 37 23 14
New version (qwen3 + exact-name keywords) 39 24 15

 

Some honesty about these numbers. The first-version row got chunking for free, so it flatters the old code. Swapping nomic for qwen3 was a bigger jump than I expected (the old keyword logic with qwen3 already scores 36), though with 45 questions that's "probably better," not proven. The new version's weights are the best of a handful Claude tried on these same questions, so 39 is a little optimistic, and 37 versus 39 is within the noise. Keywords really earn their place when you type an exact name.

Did It Work?

After re-embedding all 1,294 books (3 hours and 50 minutes), here's that content-area question again. Keyword search's top five are Missing Manual pp. 9 and 10, a Contentful book, the course book p. 53 and a 2007 web design book. Semantic search returns:

  1. Episerver: The Missing Manual (2021), p. 32, the page with AllowedTypes
  2. Episerver: The Missing Manual (2021), p. 31, the block library page right before it
  3. WordPress 5 Complete (2019), p. 71, the block editor
  4. EPiServer 7 CMS Development (2013), p. 277, content areas and partial renderers
  5. Umbraco .NET Core Mastery (2022), p. 66

From not making the top five to number one! The rest are at least about blocks and content areas.

It's not perfect though. "Run some code on a schedule in the background in the CMS" comes back with a .NET 9 page on distributed locking and a page about scheduled tasks in a Claude book, and no Optimizely scheduled jobs page. In the test, the new version still misses 6 of the 30 plain-English questions in the top five.

Still No Vector Database (or Vector Index)

The library is now 597,508 chunks. At 1,024 bytes each that's about 600 MB of vectors in memory, small enough to just loop over. The server loads them from SQLite on the first semantic question and compares the question against every chunk with a SIMD dot product from System.Numerics.Vector<T>.

The first semantic question in a session takes about 2.4 seconds while the vectors load. After that it's 170 to 240 ms end to end, and about 70 ms of that is Ollama embedding the question.

One catch: every Claude Code window runs its own copy of the server, so each one that asks a semantic question loads its own ~680 MB. With three windows open that's about 2 GB. Fine on my laptop, and the first thing I'd fix if the library got ten times bigger, along with looking at a real vector index.

Keywords Still Matter

Embeddings have the opposite weakness. Ask for ISelectionFactory and they find passages about editor dropdowns, which is close, but if I typed the name I want the page that says it. So the search runs both and combines them with reciprocal rank fusion, which just means each page gets points for how high it ranked in each list:

var (keyword, hasIdentifier) = books.KeywordRowIds(question, 300);
for (var rank = 0; rank < dense.Count; rank++)
    score[dense[rank]] = 1.0 / (60 + rank);                        // meaning
var weight = hasIdentifier ? 2.0 : 0.1;                            // exact names count; "the" doesn't
for (var rank = 0; rank < keyword.Count; rank++)
    score[keyword[rank]] = score.GetValueOrDefault(keyword[rank]) + weight / (60 + rank);

If this sounds familiar, Optimizely Graph does something similar. Its semantic search adds a semantic score to the regular BM25 keyword score when you set _ranking: SEMANTIC, and _semanticWeight (default 0.2) controls how much it counts (Junte Zhang's launch post is a good intro). Since the weight is set per query, you could do what I did: lean on keywords when the search text looks like a product code, and on meaning when it reads like a question.

Letting the Agent Loose

The MCP server got a fourth tool, semantic_search_books, and its description tells the agent to use it for plain-English questions and search_books for exact names. To see what an agent actually does with both, I started a fresh Claude Code session in an empty folder with only the Shelf tools allowed and asked: "I'm on Optimizely CMS 12. How do I stop editors from putting the wrong kinds of blocks into a content area? Answer from my book library and cite the books and pages you used."

It made six calls to the Shelf tools in about 34 seconds. It already knew the name AllowedTypes, so it started with keyword search (which is what the tool descriptions tell it to do), then ran a plain-English semantic search, read the two best pages, searched for a couple of related APIs, and checked whether any book in the library covers CMS 12:

A headless Claude Code run answering the content-area question with only the Shelf tools: the six tool calls with their top hits, then an answer citing The Missing Manual p. 32 and the course book p. 129, a correction to a code sample in the book, and a note that none of the books covers CMS 12. Rendered from the session transcript and trimmed.

To be fair, the keyword search for AllowedTypes already had p. 32 at number five. Semantic search moved it to the top. It really earns its keep when the agent doesn't already know the name.

The answer covered allow-lists, block-lists, and a pattern from Jon D. Jones's Missing Manual I hadn't seen: put a marker interface on the blocks you want to keep out and restrict the interface instead of listing every block type. Great tip, thanks Jon (and thanks to Linda Mohacsi for the course book). It also caught a small typo in one of the book's code samples (it should be AllowedTypes(RestrictedTypes = ...)), said that fix came from its own knowledge rather than the library, and pointed out that none of my books covers CMS 12 specifically. That last part is exactly what I was hoping for.

The Version Problem, Again

Last time the big worry was an agent quietly applying EPiServer 7 advice to a CMS 12 project, and semantic search does pull across versions. "Load a page by its reference without casting it" returns the Missing Manual (2021) first, and then three dated pages from 2013 and 2017 about ContentReference and PageReference. Right idea, older API. Turn on includeLegacy and the top hit becomes Beginning ASP.NET 3.5 in C# 2008, because to an embedding model "load a page" sounds a lot like a Web Forms page. So the current/dated/legacy flags matter even more now: legacy books stay out unless you ask for them, and dated results come back labeled so the agent knows to double-check.

One Gotcha

Claude Code locks your DLL. Every open Claude Code window runs its own copy of the server out of my publish folder, so dotnet publish couldn't overwrite ShelfServer.dll. I didn't want to close the sessions I was using. Claude's fix was one I wouldn't have thought of: Windows lets you rename a DLL that's in use, so rename it to .old and publish again. Running sessions keep the old code and new ones get the new tool.

Wrapping Up

Was it worth it? I think so. Chunking mattered most. The model swap helped more than I expected. My clever keyword handling helped the least, except when you type an exact name. What really paid off was building a test and then having a second session try to break our conclusions.

That's also my biggest takeaway about working with AI on something like this: having it build the thing was the easy part. Having it try to prove the thing didn't work is what made it good.

Same caveats as last time: DRM-free books only, and "dated" is a guess. Everything still runs on one machine, and the books never leave it. I'm sure there are better ways to do some of this, so feel free to let me know in the comments.

If you'd like to see the code, let me know. And if you've tried Graph's semantic search on a real site, I'd love to hear how you set the weight.

Dec 06, 2026

Comments

error Please login to comment.
Latest blogs
Setting Up Cloudflare Orange-to-Orange for Optimizely DXP Customers

Large Optimizely DXP customers can often run into complex firewall, caching, and bot management requirements that don't quite fit into what is...

Jacob Pretorius | Dec 4, 2026

How to Fetch CMP DAM Listing Cards in Optimizely CMS 12 Using GraphQL

Introduction In an existing Optimizely CMS 12 application, listing cards are typically populated using CMS content retrieved through the existing...

Deepmala S | Dec 3, 2026

Which environment am I in? Adding an environment badge to Optimizely CMS 13

Sometimes it’s hard to keep track of which environment you’re working in. Development, Integration, Preproduction and Production all look pretty mu...

Pär Wissmark | Dec 2, 2026 |

Hiring Your First AI Teammate: What Optimizely's Virtual Teammates Mean for the Marketing Org Chart

The coworker who never needs the brief twice How many times this week did you paste the same context into an AI tool? Brand voice, the target...

Leonardo Bravo | Dec 2, 2026

Upgrading to the new Optimizely brand UI: update CMS and Commerce Connect together

On October 27 , we released Optimizely CMS 13.5.0 with the new Optimizely brand UI: a new logo, colors, fonts and icons, and a redesigned login pag...

Bien Nguyen | Dec 1, 2026