What is the difference between MCP and RAG?
RAG, retrieval-augmented generation, is a technique. The name comes from a 2020 paper by Patrick Lewis and colleagues, presented at NeurIPS, which paired a language model with a search index so the model could look things up and say where an answer came from. Today the word covers any setup where your code retrieves text and adds it to the prompt.
MCP, the Model Context Protocol, is a protocol. It fixes how an AI application finds and calls tools and reads data on a separate server. The MCP spec calls tools "model-controlled", which means the model picks them, and calls resources "application-driven", which means the app decides when to read them. If you're new to the protocol, what is MCP covers it from the start.
So the two answer different questions. RAG is about getting the right text in front of the model, and MCP is about how the model reaches anything outside itself, and that includes text, but also actions like issuing a refund.
| Question | RAG | MCP |
|---|---|---|
| What is it? | A technique | A protocol |
| Who decides to fetch? | Your app, before every answer | The model, when it calls a tool |
| What comes back? | Passages of text | Whatever the tool returns: text, JSON, a created record |
| Can it change anything? | No, it reads | Yes, if a tool writes |
| What do you build? | An index and a search step | A server that lists tools |

How RAG works, in about 30 lines
To compare them fairly we built both over the same six docs pages for a made-up Orders API, with pages on auth, pagination, rate limits, creating orders, refunds and webhooks. The RAG side ranks the pages with BM25, the keyword scoring a lot of search engines start from, and pastes the top two into the prompt.
from docs import search
def build_prompt(question):
found = search(question)
for slug, score, _ in found:
print(f"retrieved {slug} (score {score})")
context = "\n\n".join(f"[{slug}] {text}" for slug, _, text in found)
return f"Answer from these docs only.\n\n{context}\n\nQuestion: {question}"Here is what it retrieved for two questions:
> How do I refund part of an order?
retrieved refunds (score 3.68)
retrieved create-order (score 3.55)
prompt is 363 characters
> Refund $20 on order ord_123
retrieved refunds (score 2.78)
retrieved pagination (score 1.55)
prompt is 376 charactersThe refunds page came first both times, which is what you want. The second pick on the second question is the pagination page, and it got there only because "$20" matched "20 items by default". Keyword search does that with numbers, and vector search has its own blind spots, so production RAG usually runs more than one ranking and merges them.
The bigger thing is what RAG can't do with the second question. The user asked for a refund, and the best a RAG pipeline can produce is an answer that explains how refunds work. Nothing gets refunded.
How MCP works, with the same docs
The MCP side is a small server written with the official Python SDK, version 2.3.0. It has the same search as a tool, plus a tool that does something:
from typing_extensions import TypedDict
from mcp.server import MCPServer
from docs import DOCS, search
server = MCPServer("acme-orders")
refunds = []
class Refund(TypedDict):
id: str
order_id: str
amount_cents: int
status: str
@server.tool()
def search_docs(query: str, limit: int = 2) -> list[dict]:
"""Search the Acme Orders API docs. Returns the best matching pages."""
found = search(query, limit)
return [{"slug": s, "score": score, "text": t} for s, score, t in found]
@server.tool()
def create_refund(order_id: str, amount_cents: int) -> Refund:
"""Refund part of an order. amount_cents is the amount to give back."""
refund: Refund = {
"id": f"re_{len(refunds) + 1}",
"order_id": order_id,
"amount_cents": amount_cents,
"status": "pending",
}
refunds.append(refund)
return refund
@server.resource("docs://acme/{slug}")
def page(slug: str) -> str:
"""One docs page, for apps that want to read it directly."""
return DOCS[slug]A client that connects over stdio sees two tools and one resource template, and can call both tools:
tool: search_docs
Search the Acme Orders API docs. Returns the best matching pages.
tool: create_refund
Refund part of an order. amount_cents is the amount to give back.
resource template: docs://acme/{slug}
search_docs -> {'result': [
{'slug': 'refunds', 'score': 3.68, ...},
{'slug': 'create-order', 'score': 3.55, ...}]}
create_refund -> {'id': 're_1', 'order_id': 'ord_123',
'amount_cents': 2000, 'status': 'pending'}Tip: give a tool a typed return value. Our first version of create_refund returned a plain dict, and the SDK sent it back as text only, with no structured content. With the TypedDict the client gets the fields as data, and a host can check them before it shows anything to the user.
The client here is a script, so it calls the tools itself. In an AI application the host passes the tool list to the model, and the model decides which tool to call and with which arguments. That's also why the search_docs query can be better than the user's words: the model writes its own query, so it can search for "partial refund" and skip the "$20" that confused the keyword ranking.
Where RAG and MCP meet
Look at search_docs again and you'll see it is RAG's search step with an MCP wrapper around it. That is the most common way the two get used together: you keep your index and your ranking, and you publish the search as a tool, so any MCP client can use it and the model decides when it needs it.
We called a public example to see what that looks like at full size. Cloudflare runs a docs server at docs.mcp.cloudflare.com/mcp with a search_cloudflare_documentation tool. We asked it "What is the rate limit for the Workers KV write API?" and got back 10 chunks of docs from 8 pages, 16,267 characters, which is 3,745 tokens with the o200k tokenizer. The answer, one write per second per key, was in the first chunk.
Rule of thumb: the search behind a tool is still RAG, and everything you know about RAG still applies to it. Chunk size, ranking and how many results you return decide how many tokens each call costs and whether the answer is in them, and the protocol doesn't change any of that.
What we learned building retrieval for a docs chat
Octri's docs sites have a chat that answers questions from the docs, which is RAG for people reading the site. The first version ranked pages with vector search alone and threw away anything that scored under 0.7. "What's the rate limit" scored 0.65 against the one page that stated it, so the chat said the docs didn't cover it. Questions that named an endpoint path did badly too, because embeddings don't pay much attention to /users/{user}.
The retrieval now runs four rankings, the vector one, a keyword one over every endpoint and guide, an exact match on method and path, and the page the reader has open, and merges them with reciprocal rank fusion. Each one can fail without taking the others down. When none of them finds the answer, the chat says so and points at the closest page.

When to use RAG, MCP or both
Use RAG on its own when the job is answering questions from a body of text and nothing needs to happen afterwards. A help center chat, a search box that answers in sentences, internal docs Q&A. You control the retrieval completely, and the model never has to decide to look something up.
Use MCP when an AI application has to do things, or when you want your data available to many AI clients without building an integration for each one. Claude, Cursor, VS Code and other clients can all connect to the same server. The MCP vs API guide covers how that compares to calling your API directly, and MCP vs function calling covers how the model's tool calls fit in.
Use both when an agent needs to read before it acts, which is most agents that work with an API. It searches the docs for how refunds work, then calls the refund tool. Put the search behind an MCP tool and the agent can do both through one server. When the work is split across several agents, MCP vs A2A builds this same refund flow one level up.
Checklist for choosing between them
- Write down whether the task only reads, or also has to change something.
- If it only reads, decide who should trigger retrieval: your app on every question, or the model when it needs to.
- Test retrieval with questions your users ask, including ones with numbers and endpoint paths, and look at what comes back.
- Run more than one ranking, keyword and vector at least, if users ask in their own words.
- Count the tokens a single search returns, and cap the number of results.
- Give every MCP tool a typed return value and a description that says what it returns.
- Mark search tools read-only, and keep tools that write behind the user's permission.
- When retrieval finds nothing, have the answer say so.
RAG and MCP with Octri
Octri docs sites come with the chat from the section above, so readers can ask questions in plain words and get an answer that cites the endpoint or guide it came from. When you publish a change to your docs, Octri indexes the new pages for the chat, and there's no separate index for you to rebuild or keep in sync.
The same project gives you a hosted MCP server for your API. Every endpoint becomes a tool, and seven documentation tools come with it, search_docs, get_endpoint and get_sdk_methods among them, so an agent can look up how your API works and then call it through the same connection. The docs tools read published content only and are marked read-only for clients. Writes ask the person first. To build a server yourself, how to build an MCP server walks through it with the same SDK we used here.
