You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ---
  2. sidebar_position: 4
  3. slug: /mcp_server
  4. ---
  5. # RAGFlow MCP server overview
  6. The RAGFlow Model Context Protocol (MCP) server operates as an independent component that complements the RAGFlow server. However, it requires a RAGFlow server to work functionally well, meaning, the MCP client and server communicate with each other in MCP HTTP+SSE mode (once the connection is established, server pushes messages to client only), and responses are expected from RAGFlow server.
  7. The MCP server currently offers a specific tool to assist users in searching for relevant information powered by RAGFlow DeepDoc technology:
  8. - **retrieve**: Fetches relevant chunks from specified `dataset_ids` and optional `document_ids` using the RAGFlow retrieve interface, based on a given question. Details of all available datasets, namely, `id` and `description`, are provided within the tool description for each individual dataset.
  9. ## Launching the MCP Server
  10. Similar to launching the RAGFlow server, the MCP server can be started either from source code or via Docker.
  11. ### Launch Modes
  12. The MCP server supports two launch modes:
  13. 1. **Self-Host Mode**:
  14. - In this mode, the MCP server is launched to access a specific tenant's datasets.
  15. - This is the default mode.
  16. - The `--api_key` argument is **required** to authenticate the server with the RAGFlow server.
  17. - Example:
  18. ```bash
  19. 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
  20. ```
  21. 1. **Host Mode**:
  22. - In this mode, the MCP server allows each user to access their own datasets.
  23. - To ensure secure access, a valid API key must be included in the request headers to identify the user.
  24. - The `--api_key` argument is **not required** during server launch but must be provided in the headers on each client request for user authentication.
  25. - Example:
  26. ```bash
  27. uv run mcp/server/server.py --host=127.0.0.1 --port=9382 --base_url=http://127.0.0.1:9380 --mode=host
  28. ```
  29. ### Launching from Source Code
  30. All you need to do is stand on the right place and strike out command, assuming you are on the project working directory.
  31. ```bash
  32. 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
  33. ```
  34. For testing purposes, there is an [MCP client example](#example_mcp_client) provided, free to take!
  35. #### Required Arguments
  36. - **`host`**: Specifies the server's host address.
  37. - **`port`**: Defines the server's listening port.
  38. - **`base_url`**: The address of the RAGFlow server that is already running and ready to handle tasks.
  39. - **`mode`**: Launch mode, only accept `self-host` or `host`.
  40. - **`api_key`**: Required when `mode` is `self-host` to authenticate the MCP server with the RAGFlow server.
  41. Here are three augments required, the first two,`host` and `port`, are self-explained. The`base_url` is the address of the ready-to-serve RAGFlow server to actually perform the task.
  42. ### Launching from Docker
  43. Building a standalone MCP server image is straightforward and easy, so we just proposed a way to launch it with RAGFlow server here.
  44. #### Alongside RAGFlow {#alongside_ragflow}
  45. As MCP server is an extra and optional component of RAGFlow server, we consume that not everybody going to use it. Thus, it is disable by default.
  46. To enable it, simply find `docker/docker-compose.yml` to uncomment `services.ragflow.command` section.
  47. ```yaml
  48. services:
  49. ragflow:
  50. ...
  51. image: ${RAGFLOW_IMAGE}
  52. # example to setup MCP server
  53. command:
  54. - --enable-mcpserver
  55. - --mcp-host=0.0.0.0
  56. - --mcp-port=9382
  57. - --mcp-base-url=http://127.0.0.1:9380
  58. - --mcp-script-path=/ragflow/mcp/server/server.py
  59. - --mcp-mode=self-host # `self-host` or `host`
  60. - --mcp-host-api-key=ragflow-xxxxxxx # only need to privide when mode is `self-host` and use bare string without quotation marks here.
  61. ```
  62. To troubleshoot, launch the service in the foreground using `docker compose -f docker-compose.yml`.
  63. ### For those upgrading from versions before v0.18.0
  64. 1. Get all MCP related files ready.
  65. 1. copy `mcp/` directory to local.
  66. 1. copy `docker/docker-compose.yml` to local.
  67. 1. copy `docker/entrypoint.sh` to local.
  68. 1. resolve necessary dependencies via `uv`.
  69. - simply run `uv add mcp` if it works for you. Or:
  70. - copy `pyproject.toml` and run `uv sync --python 3.10 --all-extras`.
  71. 1. Change `docker-compose.yml` to enable MCP as it is disable by default, [see last section](#alongside_ragflow).
  72. 1. Launch the service with `docker compose -f docker-compose.yml up -d`
  73. ### Check the MCP server status
  74. Checking logs of RAGFlow server with `docker logs ragflow-server`. If you see the MCP server ASCII art there, it means all is OK!
  75. ```bash
  76. ragflow-server | Starting MCP Server on 0.0.0.0:9382 with base URL http://127.0.0.1:9380...
  77. ragflow-server | Starting 1 task executor(s) on host 'dd0b5e07e76f'...
  78. 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'}
  79. ragflow-server |
  80. ragflow-server | __ __ ____ ____ ____ _____ ______ _______ ____
  81. ragflow-server | | \/ |/ ___| _ \ / ___|| ____| _ \ \ / / ____| _ \
  82. ragflow-server | | |\/| | | | |_) | \___ \| _| | |_) \ \ / /| _| | |_) |
  83. ragflow-server | | | | | |___| __/ ___) | |___| _ < \ V / | |___| _ <
  84. ragflow-server | |_| |_|\____|_| |____/|_____|_| \_\ \_/ |_____|_| \_\
  85. ragflow-server |
  86. ragflow-server | MCP launch mode: self-host
  87. ragflow-server | MCP host: 0.0.0.0
  88. ragflow-server | MCP port: 9382
  89. ragflow-server | MCP base_url: http://127.0.0.1:9380
  90. ragflow-server | INFO: Started server process [26]
  91. ragflow-server | INFO: Waiting for application startup.
  92. ragflow-server | INFO: Application startup complete.
  93. ragflow-server | INFO: Uvicorn running on http://0.0.0.0:9382 (Press CTRL+C to quit)
  94. ragflow-server | 2025-04-18 15:41:20,469 INFO 27 found 0 gpus
  95. ragflow-server | 2025-04-18 15:41:23,263 INFO 27 init database on cluster mode successfully
  96. ragflow-server | 2025-04-18 15:41:25,318 INFO 27 load_model /ragflow/rag/res/deepdoc/det.onnx uses CPU
  97. ragflow-server | 2025-04-18 15:41:25,367 INFO 27 load_model /ragflow/rag/res/deepdoc/rec.onnx uses CPU
  98. ragflow-server | ____ ___ ______ ______ __
  99. ragflow-server | / __ \ / | / ____// ____// /____ _ __
  100. ragflow-server | / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  101. ragflow-server | / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  102. ragflow-server | /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  103. ragflow-server |
  104. ragflow-server |
  105. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 RAGFlow version: v0.18.0-285-gb2c299fa full
  106. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 project base: /ragflow
  107. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 Current configs, from /ragflow/conf/service_conf.yaml:
  108. ragflow-server | ragflow: {'host': '0.0.0.0', 'http_port': 9380}
  109. ...
  110. ragflow-server | * Running on all addresses (0.0.0.0)
  111. ragflow-server | * Running on http://127.0.0.1:9380
  112. ragflow-server | * Running on http://172.19.0.6:9380
  113. ragflow-server | ______ __ ______ __
  114. ragflow-server | /_ __/___ ______/ /__ / ____/ _____ _______ __/ /_____ _____
  115. ragflow-server | / / / __ `/ ___/ //_/ / __/ | |/_/ _ \/ ___/ / / / __/ __ \/ ___/
  116. ragflow-server | / / / /_/ (__ ) ,< / /____> </ __/ /__/ /_/ / /_/ /_/ / /
  117. ragflow-server | /_/ \__,_/____/_/|_| /_____/_/|_|\___/\___/\__,_/\__/\____/_/
  118. ragflow-server |
  119. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 TaskExecutor: RAGFlow version: v0.18.0-285-gb2c299fa full
  120. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 Use Elasticsearch http://es01:9200 as the doc engine.
  121. ...
  122. ```
  123. You are ready to brew🍺!
  124. ## Testing and Usage
  125. Typically, there are various ways to utilize an MCP server. You can integrate it with LLMs or use it as a standalone tool. You find the way.
  126. ### Example MCP Client {#example_mcp_client}
  127. ```python
  128. #
  129. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  130. #
  131. # Licensed under the Apache License, Version 2.0 (the "License");
  132. # you may not use this file except in compliance with the License.
  133. # You may obtain a copy of the License at
  134. #
  135. # http://www.apache.org/licenses/LICENSE-2.0
  136. #
  137. # Unless required by applicable law or agreed to in writing, software
  138. # distributed under the License is distributed on an "AS IS" BASIS,
  139. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  140. # See the License for the specific language governing permissions and
  141. # limitations under the License.
  142. #
  143. from mcp.client.session import ClientSession
  144. from mcp.client.sse import sse_client
  145. async def main():
  146. try:
  147. # To access RAGFlow server in `host` mode, you need to attach `api_key` for each request to indicate identification.
  148. # async with sse_client("http://localhost:9382/sse", headers={"api_key": "ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:
  149. async with sse_client("http://localhost:9382/sse") as streams:
  150. async with ClientSession(
  151. streams[0],
  152. streams[1],
  153. ) as session:
  154. await session.initialize()
  155. tools = await session.list_tools()
  156. print(f"{tools.tools=}")
  157. response = await session.call_tool(name="ragflow_retrieval", arguments={"dataset_ids": ["ce3bb17cf27a11efa69751e139332ced"], "document_ids": [], "question": "How to install neovim?"})
  158. print(f"Tool response: {response.model_dump()}")
  159. except Exception as e:
  160. print(e)
  161. if __name__ == "__main__":
  162. from anyio import run
  163. run(main)
  164. ```
  165. ## Security and Concerns
  166. Since MCP technology is still in booming age and there are still no official Authentication and Authorization best practices to follow, RAGFlow uses `api_key` to validate the identification, and it is required to perform any operations mentioned in the preview section. Obviously, this is not a premium solution to do so, thus this RAGFlow MCP server is not expected to exposed to public use as it could be highly venerable to be attacked. For local SSE server, bind only to localhost (127.0.0.1) instead of all interfaces (0.0.0.0). For additional guidance, you can refer to [MCP official website](https://modelcontextprotocol.io/docs/concepts/transports#security-considerations).