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.

mcp.md 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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
  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`
  61. ```
  62. Then launch it normally `docker compose -f docker-compose.yml`.
  63. ```bash
  64. ragflow-server | Starting MCP Server on 0.0.0.0:9382 with base URL http://127.0.0.1:9380...
  65. ragflow-server | Starting 1 task executor(s) on host 'dd0b5e07e76f'...
  66. 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'}
  67. ragflow-server |
  68. ragflow-server | __ __ ____ ____ ____ _____ ______ _______ ____
  69. ragflow-server | | \/ |/ ___| _ \ / ___|| ____| _ \ \ / / ____| _ \
  70. ragflow-server | | |\/| | | | |_) | \___ \| _| | |_) \ \ / /| _| | |_) |
  71. ragflow-server | | | | | |___| __/ ___) | |___| _ < \ V / | |___| _ <
  72. ragflow-server | |_| |_|\____|_| |____/|_____|_| \_\ \_/ |_____|_| \_\
  73. ragflow-server |
  74. ragflow-server | MCP launch mode: self-host
  75. ragflow-server | MCP host: 0.0.0.0
  76. ragflow-server | MCP port: 9382
  77. ragflow-server | MCP base_url: http://127.0.0.1:9380
  78. ragflow-server | INFO: Started server process [26]
  79. ragflow-server | INFO: Waiting for application startup.
  80. ragflow-server | INFO: Application startup complete.
  81. ragflow-server | INFO: Uvicorn running on http://0.0.0.0:9382 (Press CTRL+C to quit)
  82. ragflow-server | 2025-04-18 15:41:20,469 INFO 27 found 0 gpus
  83. ragflow-server | 2025-04-18 15:41:23,263 INFO 27 init database on cluster mode successfully
  84. ragflow-server | 2025-04-18 15:41:25,318 INFO 27 load_model /ragflow/rag/res/deepdoc/det.onnx uses CPU
  85. ragflow-server | 2025-04-18 15:41:25,367 INFO 27 load_model /ragflow/rag/res/deepdoc/rec.onnx uses CPU
  86. ragflow-server | ____ ___ ______ ______ __
  87. ragflow-server | / __ \ / | / ____// ____// /____ _ __
  88. ragflow-server | / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  89. ragflow-server | / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  90. ragflow-server | /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  91. ragflow-server |
  92. ragflow-server |
  93. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 RAGFlow version: v0.18.0-285-gb2c299fa full
  94. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 project base: /ragflow
  95. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 Current configs, from /ragflow/conf/service_conf.yaml:
  96. ragflow-server | ragflow: {'host': '0.0.0.0', 'http_port': 9380}
  97. ...
  98. ragflow-server | * Running on all addresses (0.0.0.0)
  99. ragflow-server | * Running on http://127.0.0.1:9380
  100. ragflow-server | * Running on http://172.19.0.6:9380
  101. ragflow-server | ______ __ ______ __
  102. ragflow-server | /_ __/___ ______/ /__ / ____/ _____ _______ __/ /_____ _____
  103. ragflow-server | / / / __ `/ ___/ //_/ / __/ | |/_/ _ \/ ___/ / / / __/ __ \/ ___/
  104. ragflow-server | / / / /_/ (__ ) ,< / /____> </ __/ /__/ /_/ / /_/ /_/ / /
  105. ragflow-server | /_/ \__,_/____/_/|_| /_____/_/|_|\___/\___/\__,_/\__/\____/_/
  106. ragflow-server |
  107. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 TaskExecutor: RAGFlow version: v0.18.0-285-gb2c299fa full
  108. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 Use Elasticsearch http://es01:9200 as the doc engine.
  109. ...
  110. ```
  111. You are ready to brew🍺!
  112. ## Testing and Usage
  113. 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.
  114. ### Example MCP Client {#example_mcp_client}
  115. ```python
  116. #
  117. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  118. #
  119. # Licensed under the Apache License, Version 2.0 (the "License");
  120. # you may not use this file except in compliance with the License.
  121. # You may obtain a copy of the License at
  122. #
  123. # http://www.apache.org/licenses/LICENSE-2.0
  124. #
  125. # Unless required by applicable law or agreed to in writing, software
  126. # distributed under the License is distributed on an "AS IS" BASIS,
  127. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  128. # See the License for the specific language governing permissions and
  129. # limitations under the License.
  130. #
  131. from mcp.client.session import ClientSession
  132. from mcp.client.sse import sse_client
  133. async def main():
  134. try:
  135. # To access RAGFlow server in `host` mode, you need to attach `api_key` for each request to indicate identification.
  136. # async with sse_client("http://localhost:9382/sse", headers={"api_key": "ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:
  137. async with sse_client("http://localhost:9382/sse") as streams:
  138. async with ClientSession(
  139. streams[0],
  140. streams[1],
  141. ) as session:
  142. await session.initialize()
  143. tools = await session.list_tools()
  144. print(f"{tools.tools=}")
  145. response = await session.call_tool(name="ragflow_retrieval", arguments={"dataset_ids": ["ce3bb17cf27a11efa69751e139332ced"], "document_ids": [], "question": "How to install neovim?"})
  146. print(f"Tool response: {response.model_dump()}")
  147. except Exception as e:
  148. print(e)
  149. if __name__ == "__main__":
  150. from anyio import run
  151. run(main)
  152. ```
  153. ## Security and Concerns
  154. 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).