启动 RAGFlow MCP 服务器
通过源码或 Docker 启动 MCP 服务器。
RAGFlow 模型上下文协议 (MCP) 服务器是一个独立的组件,旨在补充 RAGFlow 服务器。请注意,MCP 服务器必须与正常运行的 RAGFlow 服务器一同工作。
MCP 服务器可以以自托管模式(默认)或托管模式启动。
- 自托管模式:
在自托管模式下启动 MCP 服务器时,您必须提供一个 API 密钥,用于向 RAGFlow 服务器验证 MCP 服务器的身份。在此模式下,MCP 服务器*只能*访问 RAGFlow 服务器上指定租户的数据集(知识库)。 - 托管模式:
在托管模式下,每个 MCP 客户端都可以访问其在 RAGFlow 服务器上的知识库。但是,每个客户端请求都必须包含一个有效的 API 密钥,以向 RAGFlow 服务器验证客户端的身份。
一旦建立连接,MCP 服务器会以 MCP HTTP+SSE(服务器发送事件)模式与客户端通信,将 RAGFlow 服务器的响应实时单向推送给客户端。
前提条件
- 请确保 RAGFlow 已升级至 v0.18.0 或更高版本。
- 请准备好您的 RAGFlow API 密钥。请参阅获取 RAGFlow API 密钥。
如果您希望在不升级 RAGFlow 的情况下试用我们的 MCP 服务器,社区贡献者 yiminghub2024 👏 在这里分享了他们推荐的步骤。
启动 MCP 服务器
您可以通过源代码或 Docker 启动 MCP 服务器。
从源代码启动
- 确保 RAGFlow 服务器 v0.18.0+ 已正确运行。
- 启动 MCP 服务器
# Launch the MCP server to work in self-host mode, run either of the following
uv run mcp/server/server.py --host=127.0.0.1 --port=9382 --base-url=http://127.0.0.1:9380 --api-key=ragflow-xxxxx
# uv run mcp/server/server.py --host=127.0.0.1 --port=9382 --base-url=http://127.0.0.1:9380 --mode=self-host --api-key=ragflow-xxxxx
# To launch the MCP server to work in host mode, run the following instead:
# uv run mcp/server/server.py --host=127.0.0.1 --port=9382 --base-url=http://127.0.0.1:9380 --mode=host
其中
host
:MCP 服务器的主机地址。port
:MCP 服务器的监听端口。base_url
:正在运行的 RAGFlow 服务器的地址。mode
:启动模式。self-host
:(默认)自托管模式。host
:托管模式。
api_key
:在自托管模式下为必需项,用于向 RAGFlow 服务器验证 MCP 服务器的身份。请参阅此处获取有关如何获取 API 密钥的说明。
传输
RAGFlow MCP 服务器支持两种传输方式:旧版的 SSE 传输(在 /sse
提供服务),于 2024 年 11 月 5 日引入,并于 2025 年 3 月 26 日弃用;以及可流式传输的 HTTP 传输(在 /mcp
提供服务)。默认情况下,旧版 SSE 传输和带 JSON 响应的可流式 HTTP 传输均已启用。要禁用任一传输,请使用 --no-transport-sse-enabled
或 --no-transport-streamable-http-enabled
标志。要禁用可流式 HTTP 传输的 JSON 响应,请使用 --no-json-response
标志。
从 Docker 启动
1. 启用 MCP 服务器
MCP 服务器被设计为 RAGFlow 服务器的可选补充组件,默认情况下是禁用的。要启用 MCP 服务器:
- 导航到 docker/docker-compose.yml。
- 取消注释
services.ragflow.command
部分,如下所示:
services:
ragflow:
...
image: ${RAGFLOW_IMAGE}
# Example configuration to set up an MCP server:
command:
- --enable-mcpserver
- --mcp-host=0.0.0.0
- --mcp-port=9382
- --mcp-base-url=http://127.0.0.1:9380
- --mcp-script-path=/ragflow/mcp/server/server.py
- --mcp-mode=self-host
- --mcp-host-api-key=ragflow-xxxxxxx
# Optional transport flags for the RAGFlow MCP server.
# If you set `mcp-mode` to `host`, you must add the --no-transport-streamable-http-enabled flag, because the streamable-HTTP transport is not yet supported in host mode.
# The legacy SSE transport and the streamable-HTTP transport with JSON responses are enabled by default.
# To disable a specific transport or JSON responses for the streamable-HTTP transport, use the corresponding flag(s):
# - --no-transport-sse-enabled # Disables the legacy SSE endpoint (/sse)
# - --no-transport-streamable-http-enabled # Disables the streamable-HTTP transport (served at the /mcp endpoint)
# - --no-json-response # Disables JSON responses for the streamable-HTTP transport
其中
mcp-host
:MCP 服务器的主机地址。mcp-port
:MCP 服务器的监听端口。mcp-base_url
:正在运行的 RAGFlow 服务器的地址。mcp-script-path
:MCP 服务器主脚本的文件路径。mcp-mode
:启动模式。self-host
:(默认)自托管模式。host
:托管模式。
mcp-host-api_key
:在自托管模式下为必需项,用于向 RAGFlow 服务器验证 MCP 服务器的身份。请参阅此处获取有关如何获取 API 密钥的说明。
如果将 mcp-mode
设置为 host
,则必须添加 --no-transport-streamable-http-enabled
标志,因为可流式传输的 HTTP 传输在托管模式下尚不支持。
2. 启动带有 MCP 服务器的 RAGFlow 服务器
运行 docker compose -f docker-compose.yml up
以同时启动 RAGFlow 服务器和 MCP 服务器。
以下 ASCII 艺术图确认启动成功:
ragflow-server | Starting MCP Server on 0.0.0.0:9382 with base URL http://127.0.0.1:9380...
ragflow-server | Starting 1 task executor(s) on host 'dd0b5e07e76f'...
ragflow-server | 2025-04-18 15:41:18,816 INFO 27 ragflow_server log path: /ragflow/logs/ragflow_server.log, log levels: {'peewee': 'WARNING', 'pdfminer': 'WARNING', 'root': 'INFO'}
ragflow-server |
ragflow-server | __ __ ____ ____ ____ _____ ______ _______ ____
ragflow-server | | \/ |/ ___| _ \ / ___|| ____| _ \ \ / / ____| _ \
ragflow-server | | |\/| | | | |_) | \___ \| _| | |_) \ \ / /| _| | |_) |
ragflow-server | | | | | |___| __/ ___) | |___| _ < \ V / | |___| _ <
ragflow-server | |_| |_|\____|_| |____/|_____|_| \_\ \_/ |_____|_| \_\
ragflow-server |
ragflow-server | MCP launch mode: self-host
ragflow-server | MCP host: 0.0.0.0
ragflow-server | MCP port: 9382
ragflow-server | MCP base_url: http://127.0.0.1:9380
ragflow-server | INFO: Started server process [26]
ragflow-server | INFO: Waiting for application startup.
ragflow-server | INFO: Application startup complete.
ragflow-server | INFO: Uvicorn running on http://0.0.0.0:9382 (Press CTRL+C to quit)
ragflow-server | 2025-04-18 15:41:20,469 INFO 27 found 0 gpus
ragflow-server | 2025-04-18 15:41:23,263 INFO 27 init database on cluster mode successfully
ragflow-server | 2025-04-18 15:41:25,318 INFO 27 load_model /ragflow/rag/res/deepdoc/det.onnx uses CPU
ragflow-server | 2025-04-18 15:41:25,367 INFO 27 load_model /ragflow/rag/res/deepdoc/rec.onnx uses CPU
ragflow-server | ____ ___ ______ ______ __
ragflow-server | / __ \ / | / ____// ____// /____ _ __
ragflow-server | / /_/ // /| | / / __ / /_ / // __ \| | /| / /
ragflow-server | / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
ragflow-server | /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
ragflow-server |
ragflow-server |
ragflow-server | 2025-04-18 15:41:29,088 INFO 27 RAGFlow version: v0.18.0-285-gb2c299fa full
ragflow-server | 2025-04-18 15:41:29,088 INFO 27 project base: /ragflow
ragflow-server | 2025-04-18 15:41:29,088 INFO 27 Current configs, from /ragflow/conf/service_conf.yaml:
ragflow-server | ragflow: {'host': '0.0.0.0', 'http_port': 9380}
...
ragflow-server | * Running on all addresses (0.0.0.0)
ragflow-server | * Running on http://127.0.0.1:9380
ragflow-server | * Running on http://172.19.0.6:9380
ragflow-server | ______ __ ______ __
ragflow-server | /_ __/___ ______/ /__ / ____/ _____ _______ __/ /_____ _____
ragflow-server | / / / __ `/ ___/ //_/ / __/ | |/_/ _ \/ ___/ / / / __/ __ \/ ___/
ragflow-server | / / / /_/ (__ ) ,< / /____> </ __/ /__/ /_/ / /_/ /_/ / /
ragflow-server | /_/ \__,_/____/_/|_| /_____/_/|_|\___/\___/\__,_/\__/\____/_/
ragflow-server |
ragflow-server | 2025-04-18 15:41:34,501 INFO 32 TaskExecutor: RAGFlow version: v0.18.0-285-gb2c299fa full
ragflow-server | 2025-04-18 15:41:34,501 INFO 32 Use Elasticsearch http://es01:9200 as the doc engine.
...
在不升级 RAGFlow 的情况下启动 MCP 服务器
本节由我们的社区贡献者 yiminghub2024 贡献。👏
- 准备所有 MCP 特定的文件和目录。
i. 将 mcp/ 目录复制到您的本地工作目录。
ii. 将 docker/docker-compose.yml 复制到本地。
iii. 将 docker/entrypoint.sh 复制到本地。
iv. 使用uv
安装所需的依赖项- 运行
uv add mcp
或 - 将 pyproject.toml 复制到本地并运行
uv sync --python 3.10 --all-extras
。
- 运行
- 编辑 docker-compose.yml 以启用 MCP(默认禁用)。
- 启动 MCP 服务器
docker compose -f docker-compose.yml up -d
检查 MCP 服务器状态
运行以下命令以检查 RAGFlow 服务器和 MCP 服务器的日志:
docker logs ragflow-server
安全注意事项
由于 MCP 技术仍处于早期阶段,尚未建立官方的身份验证或授权最佳实践,RAGFlow 目前使用API 密钥来验证上述操作的身份。然而,在公共环境中,这种临时解决方案可能会使您的 MCP 服务器面临潜在的网络攻击。因此,在运行本地 SSE 服务器时,建议仅绑定到 localhost (127.0.0.1
) 而不是所有接口 (0.0.0.0
)。
如需进一步指导,请参阅官方 MCP 文档。
常见问题
何时使用 API 密钥进行身份验证?
API 密钥的使用取决于您的 MCP 服务器的运行模式。
- 自托管模式(默认)
在自托管模式下启动 MCP 服务器时,您应在启动时提供一个 API 密钥,以向 RAGFlow 服务器验证其身份。- 如果从源代码启动,请在命令中包含 API 密钥。
- 如果从 Docker 启动,请在 docker/docker-compose.yml 中更新 API 密钥。
- 托管模式:
如果您的 RAGFlow MCP 服务器在托管模式下工作,请在客户端请求的headers
中包含 API 密钥,以向 RAGFlow 服务器验证您的客户端身份。可在此处找到一个示例:此处。