Skip to main content
Glama
Simple RAG MCP Server

Simple RAG MCP Server

Simple RAG

本地 RAG(检索增强生成)服务,提供文档索引与语义检索问答两项能力:先把文档切块存入向量库,提问时检索相关内容并交给大语言模型生成答案。

父子切块机制:索引时先按文档标题结构切出保留完整原文的父块,再把父块细分为用于向量化入库的子块;检索时只匹配小块召回,命中后回查子块所属的父块全文交给模型,兼顾小块匹配的精准与大块上下文的完整。

检索链路:父子切块 → 稠密 + 稀疏混合召回(RRF 融合)→ cross-encoder 精排 → 父块回查 → LLM 生成答案。

支持的文件格式:Markdown(.md / .markdown)、PDF(.pdf)、纯文本(其他后缀按文本处理)。

部署

环境要求

  • Python 3.12+

  • Qdrant 向量库(可用 Docker Compose 启动,也可自行安装运行)

  • 大模型服务 API Key(OpenAI 兼容端点即可,模型自选,在 config.yaml 中配置)

步骤

  1. 安装依赖(建议使用虚拟环境):

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
  1. 启动 Qdrant(二选一):

  • 使用项目自带的 Docker Compose(需 Docker):

docker compose up -d

服务地址为 http://localhost:26333,数据持久化在 data/qdrant_data/。

  • 或自行安装并启动 Qdrant,然后在 config.yaml 中把 base.database.qdrant.url 改为实际地址。

  1. 设置 API Key 环境变量(变量名与 config.yaml 中 rag.llm.api_key_env 保持一致):

$env:TOKEN_PLAN_API_KEY = "你的APIKey"
  1. 按需修改 config.yaml(见下文配置说明)。

首次运行会自动从魔塔社区(ModelScope)下载三个本地模型(合计约 2GB)到配置的模型目录(rag.model_dir),耗时数分钟,请耐心等待:

模型

作用

说明

BAAI/bge-large-zh-v1.5

稠密向量

中文语义嵌入模型,负责检索的语义召回

Qdrant/bm25

稀疏向量

BM25 关键词打分,体量极小,补足专有术语的精确匹配

Qwen/Qwen3-Reranker-0.6B

精排

cross-encoder,对双路召回融合后的候选块重排序

Related MCP server: RAG MCP Server

使用方式

交互式命令行

python cli.py

启动后进入 rag> 提示符,可用命令:

命令

说明

/index <文件路径>

索引文档,标题自动取文件名,重复文档自动跳过

/ask <问题>

检索并流式生成答案,结束后显示引用的上下文来源

/list

列举知识库中的文档文件名

/delete <文件名>

按文件名删除文档及其全部数据

/help

查看帮助

/exit

退出(也可按 Ctrl+D)

输入 / 后按 Tab 补全命令;/index 时补全文件路径;/delete 时补全知识库中的文档名。

示例:

rag> /index rag_test/md_docs/docker_networking.md
rag> /ask Docker 的 bridge 网络是怎么工作的?

MCP 服务

python mcp_server.py

以 streamable-http 传输启动,端点为 http://127.0.0.1:9933/mcp(可在 config.yaml 的 mcp 段修改)。提供两个工具,均为非流式,且都需传入 collection(集合名前缀):

  • index_doc(source_path, collection):索引服务端本机的文档

  • retrieve(query, collection, top_k):检索并生成答案,top_k 可省略

MCP 客户端超时建议设置 ≥300 秒(首次加载模型耗时较长)。

配置说明

配置集中在根目录 config.yaml,常用项:

配置项

说明

base.database.qdrant.url

Qdrant 地址,默认 http://localhost:26333

rag.collection_prefix

CLI 使用的默认集合名前缀

rag.model_dir

本地模型根目录

rag.llm

问答模型名、API 端点、API Key 环境变量名

rag.chunking

子块大小与重叠

rag.retrieval

召回候选数、精排阈值、top_k

mcp

MCP 服务监听地址与端口

每个前缀自动派生三张集合:{前缀}_chunks(子块向量)、{前缀}_parents(父块全文)、{前缀}_meta(文档元信息)。日志写入 data/logs/rag.log。

更多架构与开发细节参见 AGENTS.md。

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      B
      maintenance
      Enables any MCP-compatible AI assistant to search, filter, and retrieve information from a local document collection using a hybrid search pipeline with vector, BM25, reranking, and LLM enrichment.
      4
      -
    • F
      license
      Not graded
      quality
      Not graded
      maintenance
      Provides tools for ingesting documents into a local vector database and retrieving relevant information via semantic search, enabling retrieval-augmented generation for MCP clients.
      9
      -
    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables local document question-answering and retrieval via MCP, supporting multi-turn conversation, intent recognition, and tools for document search, Q&A, and summarization.
      5
      -