Skip to main content
Glama
retrieval-mcp

retrieval-mcp

by ZhiJiu-An

retrieval-mcp

让 AI(Claude Code / Cursor / Cherry Studio 等 MCP 客户端)能够查询你自己的文档库: 只返回当前身份可见的片段,并告诉 AI 这条结果可不可信。

retrieval-mcp 是一个只读检索 MCP server,也可以作为接入自己语料的模板。 它不生成答案,只负责把相关文档片段交给 AI。

目录

Related MCP server: SignalRank MCP

1. 项目介绍

它提供三个工具和一个资源:

动作

输入

输出

用途

list_sources

无

文档/块数量、可过滤字段、检索能力

确认库里有什么

search

查询 + 过滤条件

片段列表(chunk_id + confidence)

找依据

fetch

chunk_id 或 doc_id

片段上下文 / 整篇文档

展开依据

rag://docs/{doc_id}

—

整篇 Markdown

直接读取整篇

适合:

  • 想让 AI 查内部制度、手册、FAQ、知识库。

  • 需要按部门、密级、文档类型做权限隔离。

  • 希望 AI 只拿片段,避免一次塞入整篇文档。

不适合:

  • 需要网页后台、上传管理界面或多租户 HTTP 服务。

  • 需要由检索服务直接生成答案或写报告。

  • 需要修改、删除文档(入库是独立进程)。

2. 快速开始

环境

  • Python 3.11+

  • 可选:jieba,用于中文词级分词;不装会退化为 bigram

安装

git clone <repo>
cd retrieval-mcp

python -m venv .venv
source .venv/bin/activate
pip install -e ".[wordseg]"

用样例语料自检

仓库自带 6 篇样例制度,不需要额外准备语料:

python -m rag_mcp.doctor

看到索引、向量模型、重排模型三段输出即可。样例默认使用本地词法桩,不需要 key。

接入 MCP 客户端

把 command 指向安装后的可执行文件,路径使用绝对路径:

{
  "mcpServers": {
    "retrieval-mcp": {
      "command": "/absolute/path/to/retrieval-mcp/.venv/bin/rag-mcp"
    }
  }
}

使用真实语料时,在客户端配置里加 env:

{
  "mcpServers": {
    "retrieval-mcp": {
      "command": "/absolute/path/to/retrieval-mcp/.venv/bin/rag-mcp",
      "env": {
        "RAG_ENV_FILE": "config/tcm.env"
      }
    }
  }
}

可选:接入真实语料

rag.db 不包含在仓库中,需要先用 ingest 生成:

python -m rag_mcp.ingest \
  --source json \
  --data 你的语料.json \
  --out rag.db

RAG_ENV_FILE=config/tcm.env .venv/bin/rag-mcp

3. 配置

配置文件分两层:

  • 根目录 .env:密钥和默认配置,不进入版本库。

  • RAG_ENV_FILE:按语料/场景切换的 profile,例如 config/tcm.env。

加载优先级:真实环境变量 > RAG_ENV_FILE > 根 .env。

3.1 语料与后端

变量

默认

说明

RAG_BACKEND

memory

memory(JSON 样例)或 sqlite(rag.db)

RAG_DB_PATH

空

sqlite 索引路径

RAG_DATA_PATH

内置样例

memory 后端的 JSON 语料

RAG_REGISTRY_PATH

内置词表

可过滤字段的词表快照

3.2 身份与权限

变量

默认

说明

RAG_PRINCIPAL_ID

local-dev

调用方身份

RAG_PRINCIPAL_GROUPS

all

可见组,逗号分隔

RAG_PRINCIPAL_MAX_LEVEL

internal

public / internal / confidential

身份来自进程启动环境,不接受工具参数传入。

3.3 模型

变量

默认

说明

RAG_EMBEDDING_BASE_URL

空

OpenAI 兼容 embedding 端点

RAG_EMBEDDING_MODEL

空

embedding 模型名

RAG_EMBEDDING_API_KEY

空

API key,也认 DASHSCOPE_API_KEY / BAILIAN_API_KEY

RAG_RERANK_PROVIDER

openai

openai / dashscope / heuristic

RAG_RERANK_BASE_URL

空

rerank 端点

RAG_RERANK_MODEL

空

rerank 模型名

RAG_RERANK

on

off 表示关闭重排,此时 confidence 为 unknown

不填 embedding 三项时使用本地词法桩,仍可运行,但没有语义召回。 不填 rerank 端点时使用本地启发式重排。

3.4 检索预算

变量

默认

说明

RAG_MAX_TOP_K

20

返回条数上限

RAG_DEFAULT_TOP_K

8

默认返回条数

RAG_SNIPPET_CHARS

300

片段字数

RAG_MAX_FETCH_CHARS

4000

fetch 文本上限

RAG_MAX_QUERY_CHARS

2000

查询原文上限

更多配置见 .env.example。

3.5 阈值

RAG_RELEVANCE_FLOOR、RAG_CONFIDENCE_HIGH、RAG_CONFIDENCE_MEDIUM、RAG_DENSE_FLOOR 依赖具体语料和模型,不建议手调。换语料后运行:

python -m rag_mcp.calibrate \
  --db rag.db \
  --golden eval/golden_新场景.jsonl \
  --out config/新场景.env

4. 接入一个新场景

  1. 入库语料

    python -m rag_mcp.ingest --source json --data 你的语料.json --out rag.db
  2. 更新可过滤词表

    修改 RAG_REGISTRY_PATH 指向的 registry.json,填入实际的部门、文档类型和日期字段。

  3. 写 golden 用例

    准备 20–40 条真实问题,放到 eval/golden_新场景.jsonl。

  4. 标定阈值

    python -m rag_mcp.calibrate \
      --db rag.db \
      --golden eval/golden_新场景.jsonl \
      --out config/新场景.env
  5. 跑评测

    RAG_ENV_FILE=config/新场景.env pytest eval -q

5. 测试与评测

# 离线单元测试
pytest tests -q

# 离线样例评测
pytest eval -q

# 代码检查
ruff check src tests eval
ruff format --check src tests eval

评测用例在 eval/,指标和门禁由评测脚本负责。

6. 如何贡献

  • 有明确场景或改动建议,优先先开 issue 描述背景。

  • 提交前运行:

    pytest tests -q
    ruff check src tests eval
    ruff format --check src tests eval
  • 不要提交 .env、真实语料、rag.db、缓存目录或私有记录。

  • PR 尽量保持单一主题,说明改动目的、影响范围和验证方式。

7. 开源协议

本项目采用 Apache License 2.0 开源。

Available Tools

3 tools
fetchA
Read-onlyIdempotent

取命中片段周围的上下文,或按 doc_id 读取整篇文档。这是取依据的主力工具。

何时用:片段不足以判断、需要完整条款或前后文。 何时不用:只想确认文档是否存在 —— search 已经足够。

window=0 时返回该块所属小节的完整文本(小块入库、父块返回), 比只给这一小块更有用。truncated=true 表示被字数上限截断。

NameRequiredDescriptionDefault
doc_idNosearch 返回的 doc_id,读取整篇;与 chunk_id 二选一
windowNochunk 模式下向前后各扩展几块。schema 硬上限 10;实际还会按服务端 RAG_MAX_WINDOW 收敛
chunk_idNosearch 返回的 chunk_id;与 doc_id 二选一

Output Schema

NameRequiredDescription
hintNo
textYes
titleYes
doc_idYes
chunk_idYes
truncatedYes
updated_atYes
resolved_byNo
section_pathYes
next_chunk_idNo
prev_chunk_idNo
chars_returnedYes
corpus_versionNo

TDQS

A4.6/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint, idempotentHint and openWorldHint=false, so safety is covered. The description adds genuinely non-obvious behavior: window=0 returns the full parent section rather than the small indexed block, and truncated=true signals a character-limit cut. It doesn't discuss ordering or size limits, but the important behavioral quirk is disclosed.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Front-loaded with the action, then two labeled sections for when/when-not, then the window=0 caveat. Efficient overall, though the bold emphasis markers and the 'main tool for evidence' positioning sentence are slightly redundant given the surrounding text.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Three optional parameters, an output schema already present, and annotations covering safety — the description supplies exactly the missing pieces (mode selection, window=0 behavior, truncation flag) and need not restate return values.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the baseline is 3; however the description explains the special semantics of window=0 (parent-block return) that the schema's 'expand N chunks' wording does not convey, and frames doc_id/chunk_id as alternative whole-document vs. chunk reads. This is meaningful meaning added on top of the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description states a specific action pair — fetch surrounding context for a matched chunk, or read a whole document by doc_id — and labels itself the primary evidence-retrieval tool. It is clearly distinguishable from 'search' (existence checks) and 'list_sources' (enumeration).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Contains explicit 何时用 (when the snippet is insufficient / full clause needed) and 何时不用 (just confirming a document exists — search suffices) sections, naming the sibling tool that covers the excluded case. Routing is fully determined.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

list_sourcesA
Read-onlyIdempotent

列出知识库,以及 filters 支持的字段、值域和当前实例的检索能力。

何时用:不确定有哪些库、或准备按部门/类型/时间过滤之前。一次即可。 何时不用:已经知道要查什么,直接 search。

返回里的 capabilities.retrieval_mode 说明本次检索是否具备语义能力: lexical 表示向量通道用的是本地词法桩,同义改写提问可能搜不到。

NameRequiredDescriptionDefault

No parameters

Output Schema

NameRequiredDescription
noteYes
sourcesYes

TDQS

A4.6/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already cover read-only, idempotent, and closed-world safety, and the 'once is enough' note largely restates idempotence. The description earns credit beyond that by explaining that `capabilities.retrieval_mode` reports whether semantic retrieval is actually available, warning that `lexical` means synonym rephrasing may fail — a meaningful behavioral caveat the annotations cannot express.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Front-loads the purpose, then uses labelled sections for when-to-use, when-not-to-use, and the return-value caveat. Every sentence is doing work, though the markdown bolding and multi-section layout make it slightly heavier than the small scope requires.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

An output schema exists, so return values need no explanation, yet the description still flags the one return field whose meaning affects downstream behaviour (retrieval_mode). For a zero-parameter discovery tool this leaves nothing an agent needs ambiguous.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool takes zero parameters, so the baseline is 4. The description still adds a little value by framing what the returned filter metadata is intended to support (department/type/time filtering), but there is no parameter semantics to clarify.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States a specific verb and resource (list knowledge bases) and then explicitly enumerates the three things returned: filters' supported fields, value domains, and the instance's retrieval capability. This clearly separates it from the sibling `search`, which does the actual querying.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides explicit 'when to use' (unsure which libraries exist, or before filtering by department/type/time) and 'when not to use' (you already know what to look for → go straight to `search`), naming the alternative directly. It even caps invocation frequency with 'once is enough'.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

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

  1. 3 tool updatesv0.1.0
    • First observedfetch
    • First observedlist_sources
    • First observedsearch

TDQS

A4.6/5.0

Scored across 3 tools

Disambiguation5/5

Each tool occupies a distinct stage of the retrieval workflow: list_sources for discovery, search for candidate chunks, fetch for full context. The descriptions explicitly state 'when to use' and 'when not to use' for every tool, including the boundary case of 'search is enough to confirm existence, use fetch for content'. No two tools can be reasonably confused.

Naming Consistency4/5

All three names are lowercase snake_case verbs, which is readable and conventional. Minor deviation: list_sources follows verb_noun while search and fetch are bare verbs, but they are standard terms for this domain and no mixing of styles (camelCase, capitalization) occurs.

Tool Count5/5

Three tools is exactly right for a read-only retrieval server: one for capability/filter discovery, one for search, one for expansion. Each tool has a clear, non-redundant role and nothing feels padded or missing at the surface level.

Completeness4/5

The read-only lifecycle (discover sources → search → fetch full text by doc_id or window) is fully closed, with guidance on empty results and truncation. A minor gap remains: there is no way to browse or enumerate documents within a source, so an agent cannot list what exists without first searching.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues

Related MCP Connectors

Related MCP Servers

  • A
    license
    A
    quality
    C
    maintenance
    Enables MCP-compatible clients to search SignalRank with dense, BM25, or hybrid retrieval and receive ranked evidence with provenance.
    1
    MIT
  • A
    license
    Not graded
    quality
    B
    maintenance
    Enables MCP clients to search technical document collections using hybrid BM25 and embedding retrieval, returning cited page-level passages to support grounded, evidence-based answers.
    MIT