retrieval-mcp
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@retrieval-mcpsearch my policy docs for remote work rules and show confidence scores"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
retrieval-mcp
让 AI(Claude Code / Cursor / Cherry Studio 等 MCP 客户端)能够查询你自己的文档库: 只返回当前身份可见的片段,并告诉 AI 这条结果可不可信。
retrieval-mcp 是一个只读检索 MCP server,也可以作为接入自己语料的模板。
它不生成答案,只负责把相关文档片段交给 AI。
目录
Related MCP server: SignalRank MCP
1. 项目介绍
它提供三个工具和一个资源:
动作 | 输入 | 输出 | 用途 |
| 无 | 文档/块数量、可过滤字段、检索能力 | 确认库里有什么 |
| 查询 + 过滤条件 | 片段列表( | 找依据 |
|
| 片段上下文 / 整篇文档 | 展开依据 |
| — | 整篇 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-mcp3. 配置
配置文件分两层:
根目录
.env:密钥和默认配置,不进入版本库。RAG_ENV_FILE:按语料/场景切换的 profile,例如config/tcm.env。
加载优先级:真实环境变量 > RAG_ENV_FILE > 根 .env。
3.1 语料与后端
变量 | 默认 | 说明 |
|
|
|
| 空 | sqlite 索引路径 |
| 内置样例 | memory 后端的 JSON 语料 |
| 内置词表 | 可过滤字段的词表快照 |
3.2 身份与权限
变量 | 默认 | 说明 |
|
| 调用方身份 |
|
| 可见组,逗号分隔 |
|
|
|
身份来自进程启动环境,不接受工具参数传入。
3.3 模型
变量 | 默认 | 说明 |
| 空 | OpenAI 兼容 embedding 端点 |
| 空 | embedding 模型名 |
| 空 | API key,也认 |
|
|
|
| 空 | rerank 端点 |
| 空 | rerank 模型名 |
|
|
|
不填 embedding 三项时使用本地词法桩,仍可运行,但没有语义召回。 不填 rerank 端点时使用本地启发式重排。
3.4 检索预算
变量 | 默认 | 说明 |
| 20 | 返回条数上限 |
| 8 | 默认返回条数 |
| 300 | 片段字数 |
| 4000 | fetch 文本上限 |
| 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/新场景.env4. 接入一个新场景
入库语料
python -m rag_mcp.ingest --source json --data 你的语料.json --out rag.db更新可过滤词表
修改
RAG_REGISTRY_PATH指向的registry.json,填入实际的部门、文档类型和日期字段。写 golden 用例
准备 20–40 条真实问题,放到
eval/golden_新场景.jsonl。标定阈值
python -m rag_mcp.calibrate \ --db rag.db \ --golden eval/golden_新场景.jsonl \ --out config/新场景.env跑评测
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 toolsfetchARead-onlyIdempotent
取命中片段周围的上下文,或按 doc_id 读取整篇文档。这是取依据的主力工具。
何时用:片段不足以判断、需要完整条款或前后文。 何时不用:只想确认文档是否存在 —— search 已经足够。
window=0 时返回该块所属小节的完整文本(小块入库、父块返回), 比只给这一小块更有用。truncated=true 表示被字数上限截断。
| Name | Required | Description | Default |
|---|---|---|---|
| doc_id | No | search 返回的 doc_id,读取整篇;与 chunk_id 二选一 | |
| window | No | chunk 模式下向前后各扩展几块。schema 硬上限 10;实际还会按服务端 RAG_MAX_WINDOW 收敛 | |
| chunk_id | No | search 返回的 chunk_id;与 doc_id 二选一 |
Output Schema
| Name | Required | Description |
|---|---|---|
| hint | No | |
| text | Yes | |
| title | Yes | |
| doc_id | Yes | |
| chunk_id | Yes | |
| truncated | Yes | |
| updated_at | Yes | |
| resolved_by | No | |
| section_path | Yes | |
| next_chunk_id | No | |
| prev_chunk_id | No | |
| chars_returned | Yes | |
| corpus_version | No |
TDQS
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.
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.
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.
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.
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.
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_sourcesARead-onlyIdempotent
列出知识库,以及 filters 支持的字段、值域和当前实例的检索能力。
何时用:不确定有哪些库、或准备按部门/类型/时间过滤之前。一次即可。 何时不用:已经知道要查什么,直接 search。
返回里的 capabilities.retrieval_mode 说明本次检索是否具备语义能力:
lexical 表示向量通道用的是本地词法桩,同义改写提问可能搜不到。
| Name | Required | Description | Default |
|---|---|---|---|
No parameters | |||
Output Schema
| Name | Required | Description |
|---|---|---|
| note | Yes | |
| sources | Yes |
TDQS
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.
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.
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.
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.
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.
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.
searchARead-onlyIdempotent
在企业知识库中做混合检索(词法 + 语义),返回片段级命中。
返回的是引用:chunk_id + 300 字以内的片段 + 小节路径。 片段只用于判断「值不值得展开」,真正的依据要用 fetch 取。
何时用:需要事实、制度、流程、文档原文作为依据。 何时不用:已知 doc_id 且要完整内容 → 用 fetch 或 resource。
空结果或结果被截断时 hint 里有下一步建议,格式为 CODE: 说明。
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 自然语言问题或关键词。优先用文档里可能出现的原词,不要写长句 | |
| top_k | No | 返回条数上限。schema 硬上限 100;实际还会按服务端 RAG_MAX_TOP_K 收敛,越界不会报错 | |
| doc_type | No | 按文档类型过滤。合法值见 list_sources;不确定就留空 | |
| department | No | 按部门过滤。合法值见 list_sources;不确定就留空 | |
| updated_after | No | YYYY-MM-DD,只保留该日期后更新的文档 |
Output Schema
| Name | Required | Description |
|---|---|---|
| hint | No | |
| hits | Yes | |
| query | Yes | |
| total | Yes | |
| returned | Yes | |
| truncated | Yes | |
| config_hash | Yes | |
| chars_returned | Yes | |
| corpus_version | Yes | |
| schema_version | Yes | |
| filters_applied | Yes | |
| query_truncated | No | |
| truncated_reason | No |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
Annotations already declare readOnly/idempotent/closed-world, so safety is covered; the description goes further by explaining that results are citations meant only to judge whether a chunk is worth expanding, that snippets are capped at 300 characters, and that empty or truncated results carry a `CODE: 说明` hint with next steps. It does not, however, describe ranking/scoring behavior or how many hints might appear, leaving modest gaps for a retrieval tool.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
Front-loaded with purpose, then return shape, then use/don't-use, then edge-case handling, using bold labels as visual anchors. Every line carries distinct information and nothing is repeated.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
An output schema exists, so return structure need not be re-specified, and the description still adds the crucial framing that snippets are citation previews rather than evidence. Combined with explicit when/when-not routing against both siblings and hint-handling for empty/truncated results, nothing an agent needs to call this correctly is missing.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 100%, so every parameter — including the RAG_MAX_TOP_K convergence note and the list_sources pointer for doc_type/department — is already documented in the schema. The description adds no parameter-level detail beyond what the schema provides, so the baseline 3 applies.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The opening line names a specific verb and resource — hybrid retrieval (lexical + semantic) over an enterprise knowledge base returning snippet-level hits — and immediately defines the return unit (chunk_id + ≤300-character snippet + section path). This cleanly separates it from `fetch`, which is explicitly framed as the tool for full document content.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
It states both sides explicitly: use when you need facts, policies, processes, or document原文 as evidence; do not use when the doc_id is known and full content is wanted — go to `fetch` or `resource`. The alternative is named with the exact condition that selects it, so no inference is required.
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.
3 tool updates
v0.1.0- First observed
fetch - First observed
list_sources - First observed
search
TDQS
Scored across 3 tools
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.
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.
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.
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
Related MCP Connectors
Query your own PDFs and documents from any MCP client. Every answer cites the page it came from.
Agent-driven search: build, import, tune, search, and score result quality — all over MCP.
Agentic search over your Dewey document collections from any MCP-compatible client.
Query any docs site via MCP. Submit a URL, ask questions, get cited answers.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceProvides hybrid retrieval (dense + BM25 + RRF) with collection-based isolation and document ingestion for private knowledge access via MCP.MIT
- AlicenseAqualityCmaintenanceEnables MCP-compatible clients to search SignalRank with dense, BM25, or hybrid retrieval and receive ranked evidence with provenance.1MIT
- AlicenseNot gradedqualityBmaintenanceEnables 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
- AlicenseNot gradedqualityCmaintenanceEnables MCP clients to ask questions over the indexed document corpus and search handbook passages, returning cited answers and ranked results.MIT