Skip to main content
Glama
RAGFlow Claude MCP Server

RAGFlow Claude MCP Server

by norandom

RAGFlow Claude MCP 服务器

这是一个小型模型上下文协议 (MCP) 服务器,用于将 Claude Desktop(以及其他 MCP 客户端)连接到 RAGFlow 实例。它将 RAGFlow REST API 公开为一组工具,以便 LLM 可以查询知识库并将文档块提取到其上下文中。

这是我为自己的研发工作编写的个人使用软件。它并非没有 bug,代码也不够优雅。但它能满足我的需求。

功能

  • 直接检索:从 RAGFlow 的 /retrieval 端点提取带有相似度分数的原始文档块。

  • 多知识库搜索:单个查询可以同时命中多个知识库。

  • DSPy 查询深化:可选的迭代查询优化(使用 LLM 分析中间结果并重写查询)。

  • ~~重排序 (Reranking)~~ — 目前在 RAGFlow 端存在问题,请参阅 已知问题。

  • 可调结果控制:page_size、similarity_threshold、top_k、分页。

  • 文档过滤:将结果限制在数据集内的一个文档中(模糊名称匹配)。

  • 按名称(不区分大小写,模糊匹配)而不是 ID 查找数据集。

  • 当您的 RAGFlow 位于 Cloudflare Zero Trust 之后时,支持其身份验证。

Related MCP server: RAGBrain MCP

安装

  1. 克隆:

    git clone https://github.com/norandom/ragflow-claude-desktop-local-mcp
    cd ragflow-claude-desktop-local-mcp
  2. 安装:

    # On macOS, install DSPy first to dodge build issues:
    pip install git+https://github.com/stanfordnlp/dspy.git
    
    uv install
  3. 配置:复制示例并填写您的 RAGFlow 详细信息。

    cp config.json.sample config.json

    键:

    • RAGFLOW_BASE_URL:例如 http://your-ragflow-server:9380

    • RAGFLOW_API_KEY:您的 RAGFlow API 密钥

    • RAGFLOW_DEFAULT_RERANK:重排序模型(默认 rerank-multilingual-v3.0)

    • CF_ACCESS_CLIENT_ID (可选):Cloudflare Zero Trust 服务令牌 ID

    • CF_ACCESS_CLIENT_SECRET (可选):Cloudflare Zero Trust 服务令牌密钥

    • DSPY_MODEL:DSPy LM(默认 openai/gpt-4o-mini)

    • OPENAI_API_KEY:DSPy 深化所需

Cloudflare Zero Trust

如果您的 RAGFlow 位于 Cloudflare Zero Trust 之后,请从仪表板获取服务令牌并将其添加到 config.json 中:

{
  "CF_ACCESS_CLIENT_ID": "your-client-id.access",
  "CF_ACCESS_CLIENT_SECRET": "your-client-secret"
}

当两者都设置时,每个 API 请求都会携带 CF-Access-Client-Id 和 CF-Access-Client-Secret 标头。无需更改代码。

Claude Desktop 配置

{
  "mcpServers": {
    "ragflow": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/path/to/ragflow-claude-desktop-local-mcp",
        "ragflow-claude-mcp"
      ]
    }
  }
}

工具

ragflow_retrieval_by_name(我最常用的工具)

按名称检索一个或多个数据集中的块。返回带有相似度分数的原始块。

参数:

  • dataset_names(必需)— 列表,例如 ["BASF", "Quant Literature"]

  • query(必需)

  • document_name(可选)— 限制为一个文档;模糊匹配

  • top_k(可选,默认 1024)— 向量候选数

  • similarity_threshold(可选,默认 0.2)— 0.0–1.0

  • page(可选,默认 1)

  • page_size(可选,默认 10)

  • use_rerank(可选,默认 false)— 目前在上游已损坏,请参阅已知问题

  • deepening_level(可选,默认 0)— DSPy 优化,0–3

ragflow_retrieval

形状相同,但使用 dataset_ids: List[str] 而不是名称。

多知识库搜索

您可以在一次调用中搜索多个知识库。请确保它们共享一个嵌入模型 — 混合不兼容的嵌入会降低相关性分数。

Use ragflow_retrieval_by_name with dataset_names ["Finance Reports", "Legal Documents"] and query "Summarize the key financial risks and compliance requirements for new market entry."

ragflow_list_datasets

列出 RAGFlow 实例上的所有知识库。无参数。在内部遍历所有页面。

ragflow_list_documents

列出数据集中的文档。遍历所有页面。

  • dataset_id(必需)

ragflow_get_chunks

返回一个文档的块(带有引用)。

  • dataset_id(必需)

  • document_id(必需)

ragflow_list_sessions

显示每个数据集的活动聊天会话。无参数。

ragflow_list_documents_by_name

按名称查找并列出数据集中的文档。

  • dataset_name(必需)

ragflow_reset_session

删除数据集的聊天会话。

  • dataset_id(必需)

调整检索

检索工具提供三个调节旋钮:

  • page_size — 每页块数(默认 10)。

  • similarity_threshold — 丢弃低于此分数的块(默认 0.2)。

  • top_k — 过滤前向量搜索的池大小(默认 1024)。

对我有效的一些起始设置:

  • 更广泛的召回:page_size=15, similarity_threshold=0.15。

  • 更高的精度:page_size=5, similarity_threshold=0.4。

  • 深度研究:page_size=20, similarity_threshold=0.1, deepening_level=1。

  • 困难查询:deepening_level=2。

  • 速度:保持 deepening_level=0 并跳过重排序。

示例

按名称进行基本检索:

Use ragflow_retrieval_by_name with dataset_names ["BASF"] and query "What is BASF's latest income statement? Revenue, operating income, net income, and other key figures."

限制为一个文档:

Use ragflow_retrieval_by_name with dataset_names ["BASF"], document_name "annual_report_2023", and query "What were the key financial highlights for 2023?"

文档名称进行模糊匹配 — "annual" 将匹配 annual_report_2023.pdf 和 annual_report_2024.pdf。当有多个匹配项时,服务器会选择最近的一个,并在响应元数据中列出其他选项。

针对棘手查询的 DSPy 深化:

Use ragflow_retrieval_by_name with dataset_names ["Quant Literature"], query "what is a volatility clock", deepening_level 2.

多页:

Use ragflow_retrieval_by_name with dataset_names ["BASF"], query "BASF business segments", page_size 10, page 2.

列出可用内容:

Use ragflow_list_datasets.
Use ragflow_list_documents_by_name with dataset_name "BASF".

提取特定块:

Use ragflow_get_chunks with dataset_id "43066ee0599411f089787a39c10de57b" and document_id "d74a1c105a3311f09fc94a0fcd8b7722".

更大的提示词

一些关于我如何从 Claude Desktop 驱动它的示例。

财务深度分析:

Help me analyse BASF's recent financials.

1. Use ragflow_retrieval_by_name to search ["BASF"] for the latest income statement
   (revenue, operating income, net income). Use page_size 15,
   similarity_threshold 0.15, deepening_level 1.

2. Then run ragflow_retrieval_by_name again for the cash flow statement,
   page_size 10, similarity_threshold 0.2.

3. Finally look for year-over-year changes with page_size 12,
   similarity_threshold 0.18.

多语言研究:

Use ragflow_retrieval_by_name with dataset_names ["BASF"],
query "Was sind die wichtigsten Geschäftsbereiche von BASF?",
deepening_level 2.

DSPy 会检测查询语言并相应地进行优化。我已将其用于德语、英语和混合语言查询。只要底层文档包含这些语言的内容,它就能正常工作。

文档过滤研究:

1. Use ragflow_list_documents_by_name with dataset_name "BASF" to see what's in there.
2. Use ragflow_retrieval_by_name with dataset_names ["BASF"],
   document_name "sustainability_report", query "carbon neutrality goals",
   page_size 15, deepening_level 1.
3. Follow up with document_name "annual_report_2023" and
   query "environmental investments".

跨知识库查询:

Use ragflow_retrieval_by_name with dataset_names ["BASF", "Industry Reports"],
query "chemical industry sustainability benchmarks",
page_size 12, deepening_level 1.

DSPy 深化是如何工作的

deepening_level 在检索之上运行一个 LLM 驱动的优化循环:

  • 0:无深化(默认)。

  • 1:一次优化传递。

  • 2:两次带有差距分析的传递。

  • 3:三次以上传递加上结果合并。

每次传递:执行搜索,总结前几个结果,询问 LLM 缺少什么,生成新查询,运行该查询。响应元数据包括原始查询、每个优化后的查询以及每一步的推理。

DSPy 需要:

  • DSPY_MODEL — openai/gpt-4o-mini 效果很好

  • OPENAI_API_KEY

重排序(目前已损坏)

当工作正常时,重排序会用重排序模型的分数替换向量余弦分数(根据我的经验,相关性通常提高 10-30%)。RAGFlow 目前有一个已知 bug,即 use_rerank=true 会产生:

UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol

因此,在上游问题修复之前,请保持 use_rerank=false。标准向量检索可以正常工作。

数据集查找是如何工作的

  • 不区分大小写的名称匹配。

  • 部分名称的模糊匹配。

  • 数据集会被缓存以进行名称查找;缓存未命中会触发刷新。

  • 如果查找失败,错误信息会包含可用的数据集名称,以便您知道实际存在的内容。

文档匹配

当您传递 document_name 时:

  • 精确匹配优先,然后是“以...开头”,然后是“包含”,最后是部分匹配。

  • 在平局的情况下,最近更新的文档获胜。

  • 名称中包含 2024、2023、latest、current 或 new 的文档会获得少量分数奖励。

  • 所有匹配项都会返回在响应元数据中,以便您可以使用更具体的名称重新发出请求。

错误处理

针对以下情况提供合理的错误消息:API 错误、缺少数据集、无法访问 RAGFlow、会话中断、输入无效和配置问题。敏感值在日志中会被屏蔽。

环境变量

  • RAGFLOW_BASE_URL — 覆盖配置文件。代码中的默认值:http://192.168.122.93:9380(这是我的本地实例)。

  • RAGFLOW_API_KEY — 必需。

开发

直接运行服务器:

uv run ragflow-claude-mcp

它像 MCP 服务器一样在 stdio 上监听。

开发依赖:

uv install --extra dev

这会安装 pytest + asyncio/mock/cov 插件。

测试:

uv run pytest
uv run pytest --cov=src --cov-report=html --cov-report=term
uv run pytest tests/test_server.py
uv run pytest -v

覆盖率约为 44%,22/23 个测试通过(一个因间歇性 CI 故障被跳过)。测试涵盖服务器初始化、RAGFlow API 集成、DSPy 深化、OpenAI/OpenRouter 配置分支和配置加载。

实现说明

检索 API 是服务器实际依赖的唯一 RAGFlow 表面。没有助手/聊天依赖,没有服务器端提示词配置 — 只是返回块。更容易推理,更容易调试。

故障排除

  • “Dataset not found”:运行 ragflow_list_datasets 查看实际存在的内容。

  • 连接错误:仔细检查 RAGFLOW_BASE_URL 和 RAGFLOW_API_KEY。

  • 服务器无法启动:uv install 是否真的完成了?

  • 需要原始块:使用 ragflow_retrieval_by_name / ragflow_retrieval。

  • 会话卡住:运行 ragflow_list_sessions 然后 ragflow_reset_session。

  • Cloudflare 403:确认 CF_ACCESS_CLIENT_ID / CF_ACCESS_CLIENT_SECRET 与 Zero Trust 应用上的活动服务令牌匹配。

已知问题

重排序在上游已损坏

use_rerank=true 会报错 UnsupportedProtocol: Request URL is missing an 'http://' or 'https://' protocol。这是 RAGFlow 端的缺陷。解决方法:保持关闭。我正在关注 RAGFlow 仓库以获取修复。

贡献

仅限 PR — main 分支受保护。提交必须经过 SSH 签名。

  1. Fork。

  2. git checkout -b feature/your-thing。

  3. 进行更改,编写清晰的提交信息。

  4. 推送到您的 Fork。

  5. 针对 main 打开 PR。

PR 会自动运行 TruffleHog — 不要包含密钥、令牌或机密。有关详细信息,请参阅 CONTRIBUTING.md。

Available Tools

8 tools
ragflow_get_chunksC

Get chunks with references from a specific document

Inputs: dataset_id (required), document_id (required).

ragflow_list_datasetsA

List all available datasets/knowledge bases in RAGFlow

Inputs: no named parameters.

ragflow_list_documentsC

List documents in a specific dataset

Inputs: dataset_id (required).

ragflow_list_documents_by_nameC

List documents in a dataset by dataset name

Inputs: dataset_name (required).

ragflow_list_sessionsB

List active chat sessions for all datasets

Inputs: no named parameters.

ragflow_reset_sessionB

Reset/clear the chat session for a specific dataset

Inputs: dataset_id (required).

ragflow_retrievalB

Retrieve document chunks directly from RAGFlow datasets using the retrieval API. Returns raw chunks with similarity scores.

Inputs: query (required), dataset_ids (required), page, top_k, +5 more.

ragflow_retrieval_by_nameB

Retrieve document chunks by dataset names using the retrieval API. Returns raw chunks with similarity scores.

Inputs: query (required), dataset_names (required), page, top_k, +5 more.

Tool Schema Changelog

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

Loading tool schema changelog…

    TDQS

    B3.4/5.0

    Scored across 8 tools

    Disambiguation4/5

    Most tools have distinct purposes, but ragflow_list_documents and ragflow_retrieval each have an alternative by-name variant, which could cause confusion if descriptions are not heeded. However, descriptions clarify the difference between ID-based and name-based operations, keeping overlap minimal.

    Naming Consistency5/5

    All tools follow a consistent verb_noun pattern with snake_case and the 'ragflow_' prefix. Variations like '_by_name' are systematic and predictable, enhancing readability for agents.

    Tool Count5/5

    With 8 tools, the set is well-scoped for a knowledge base retrieval server. Each tool serves a clear function, and the count is neither too sparse nor overwhelming for the intended purpose.

    Completeness4/5

    The tool surface covers listing datasets, listing documents, retrieving chunks, and managing chat sessions. It lacks create/update/delete operations, but given the likely read-heavy focus of the server, these gaps are acceptable and do not impede the primary retrieval workflow.

    Maintenance

    ActivityMaintained
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      A
      quality
      D
      maintenance
      Integrates R2R (Retrieval-Augmented Generation) with Claude Desktop, enabling semantic search across knowledge bases and RAG-based question answering with support for vector, graph, web, and document search.
      2
      -
    • A
      license
      A
      quality
      F
      maintenance
      Connects Claude Desktop to a RAGBrain knowledge base to enable semantic search, document retrieval, and namespace management. It allows users to browse collections, discover documents by topic, and access full text content through natural language.
      5
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      Provides semantic search capabilities by connecting Claude Desktop to a Cloudflare Workers backend powered by Vectorize. It enables natural language querying of knowledge bases using vector similarity and edge-based embedding generation.
      2
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables semantic retrieval and knowledge base management through the RAGFlow API, including dataset, document, chunk, chat, and graph operations.
      7
      MIT