See Ash Code logo See Ash Code
Active Creator

Markdown RAG / rag-mcp

A Markdown knowledge-base search tool with a Model Context Protocol server and command-line interface.

Python RAG MCP LangChain PostgreSQL

Giving an assistant access to a Markdown knowledge base

I keep information in Markdown files, but a growing collection is difficult to search by meaning. An assistant can help answer a question only if it can find the passages that belong in the answer. I built rag-mcp to ingest a directory of Markdown documents and expose semantic retrieval through the Model Context Protocol (MCP).

The project combines a command-line interface, an MCP server, embedding providers, and a PostgreSQL vector store. The challenge was not only producing vectors. It was also knowing which files had been indexed, dealing with API limits, and giving the user a way to update the collection as the source documents changed.

How a document becomes a search result

The ingestion path walks the selected directory for Markdown files. It splits documents at Markdown headings and then divides larger sections into smaller chunks. The embedding model turns those chunks into vectors, which are stored in PostgreSQL with pgvector. A query is embedded and compared with the stored vectors; returned results include their source filename and text.

Markdown files → heading and text splitters → embeddings → PostgreSQL / pgvector
                                                                ↑
MCP client → FastMCP query tool → query embedding ──────────────┘
                                     ↓
                         matching text and source paths

The two-stage split keeps heading structure available as context while limiting the size of individual chunks. That is a practical compromise, not a guarantee that every chunk will contain exactly the context needed to answer a question. Chunk size and overlap affect what is embedded and therefore what can be retrieved.

The command-line interface handles ingestion and document management. The MCP server exposes a query tool and tools for listing, updating, refreshing, and deleting indexed documents. The query accepts a result count, and individual tools can be disabled through configuration. A client that only needs retrieval does not have to be given every management operation.

Knowing what is in the index

Each document is identified using a path relative to the selected root, such as guides/install.md. That path travels with its chunks as metadata, so a search result can point back to its source without depending on the absolute path of my checkout.

The first duplicate check looked for existing filename metadata and skipped a matching path. That avoided adding another set of chunks every time the same directory was ingested, but it couldn’t tell an unchanged file from an edited one. Both still had the same name.

The management operations address part of that problem: a document can be deleted and re-ingested, and the refresh path does that work for a selected file. It remains a distinct operation from the initial existence check. A relative filename is also meaningful within the selected root; it is not a globally unique identity across unrelated collections.

Making rate limits visible

The Google embedding path needs to account for both request and token limits. An early wrapper checked a joined batch once before sending a list of texts to the embedding client. A small number of large chunks could fit a request allowance while exceeding a token allowance, so counting wrapper calls alone was not enough to describe the work in the batch.

The revised limiter tracks timestamped usage and token counts, considers the available budgets when preparing batches, and checks work at the document level. A token-counting callable is separate from the usage tracker, which makes the accounting easier to reason about. The model still depends on the configured accounting matching how the provider and SDK charge the work; a document, a Python call, and a network request are not automatically the same unit.

There was a usability issue too. A long quota wait produced no output, so an ingestion job could look hung. Periodic messages now report that the process is still waiting. The messages don’t shorten the wait, but they help distinguish waiting from a silent failure.

Local embeddings and storage choices

The default branch supports Google embeddings and a local Ollama option. Adding Ollama changed more than the endpoint: each provider has its own initialization and configuration, and the ingestion setup can use different chunk sizes. Local embeddings avoid a hosted embedding API for that part of the workflow, but require a local model and introduce their own resource and setup costs.

Changing models also raises an index-compatibility question. Existing document vectors and new query vectors need to be produced in a compatible embedding space; switching a setting alone doesn’t establish that an old index is suitable. The project does not provide a retrieval-quality or performance comparison between the providers.

The default branch uses PostgreSQL and pgvector. A separate feature branch explores a common adapter and additional vector stores. That work is useful evidence of an architectural direction, but it is not a feature I can claim is present in the default branch. Supporting more stores would give users deployment choices while adding work to reconcile their different filtering, deletion, and connection behaviors.

What I learned

Semantic search depends on more than the model’s response. The application has to preserve source identity, keep an index current, make quota waits understandable, and present the right operations to each client. Those details determine whether someone can use the retrieval system with confidence and investigate a surprising result.