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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ---
  2. sidebar_position: 7
  3. slug: /MCP server
  4. ---
  5. # Overview of RAGFlow Model Context Protocol (MCP) Server
  6. The RAGFlow 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. ### Launching from Source Code
  12. All you need to do is stand on the right place and strike out command, assuming you are on the project working directory.
  13. ```bash
  14. uv run mcp/server/server.py --host=127.0.0.1 --port=9382 --base_url=http://127.0.0.1:9380
  15. ```
  16. For testing purposes, there is an [MCP client example](#example_mcp_client) provided, free to take!
  17. #### Required Arguments
  18. • **`host`**: Specifies the server's host address.
  19. • **`port`**: Defines the server's listening port.
  20. • **`base_url`**: The address of the RAGFlow server that is already running and ready to handle tasks.
  21. 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.
  22. ### Launching from Docker
  23. Building a standalone MCP server image is straightforward and easy, so we just proposed a way to launch it with RAGFlow server here.
  24. #### Alongside RAGFlow
  25. 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.
  26. To enable it, simply find `docker/docker-compose.yml` to uncomment `services.ragflow.command` section.
  27. ```yaml
  28. services:
  29. ragflow:
  30. ...
  31. image: ${RAGFLOW_IMAGE}
  32. # example to setup MCP server
  33. command:
  34. - --enable-mcpserver
  35. - --mcp-host=0.0.0.0
  36. - --mcp-port=9382
  37. - --mcp-base-url=http://127.0.0.1:9380
  38. - --mcp-script-path=/ragflow/mcp/server/server.py
  39. ```
  40. Then launch it normally `docker compose -f docker-compose.yml`.
  41. ```bash
  42. ragflow-server | Starting MCP Server on 0.0.0.0:9382 with base URL http://127.0.0.1:9380...
  43. ragflow-server | Starting 1 task executor(s) on host 'dd0b5e07e76f'...
  44. 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'}
  45. ragflow-server |
  46. ragflow-server | __ __ ____ ____ ____ _____ ______ _______ ____
  47. ragflow-server | | \/ |/ ___| _ \ / ___|| ____| _ \ \ / / ____| _ \
  48. ragflow-server | | |\/| | | | |_) | \___ \| _| | |_) \ \ / /| _| | |_) |
  49. ragflow-server | | | | | |___| __/ ___) | |___| _ < \ V / | |___| _ <
  50. ragflow-server | |_| |_|\____|_| |____/|_____|_| \_\ \_/ |_____|_| \_\
  51. ragflow-server |
  52. ragflow-server | MCP host: 0.0.0.0
  53. ragflow-server | MCP port: 9382
  54. ragflow-server | MCP base_url: http://127.0.0.1:9380
  55. ragflow-server | INFO: Started server process [26]
  56. ragflow-server | INFO: Waiting for application startup.
  57. ragflow-server | INFO: Application startup complete.
  58. ragflow-server | INFO: Uvicorn running on http://0.0.0.0:9382 (Press CTRL+C to quit)
  59. ragflow-server | 2025-04-18 15:41:20,469 INFO 27 found 0 gpus
  60. ragflow-server | 2025-04-18 15:41:23,263 INFO 27 init database on cluster mode successfully
  61. ragflow-server | 2025-04-18 15:41:25,318 INFO 27 load_model /ragflow/rag/res/deepdoc/det.onnx uses CPU
  62. ragflow-server | 2025-04-18 15:41:25,367 INFO 27 load_model /ragflow/rag/res/deepdoc/rec.onnx uses CPU
  63. ragflow-server | ____ ___ ______ ______ __
  64. ragflow-server | / __ \ / | / ____// ____// /____ _ __
  65. ragflow-server | / /_/ // /| | / / __ / /_ / // __ \| | /| / /
  66. ragflow-server | / _, _// ___ |/ /_/ // __/ / // /_/ /| |/ |/ /
  67. ragflow-server | /_/ |_|/_/ |_|\____//_/ /_/ \____/ |__/|__/
  68. ragflow-server |
  69. ragflow-server |
  70. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 RAGFlow version: v0.17.2-285-gb2c299fa full
  71. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 project base: /ragflow
  72. ragflow-server | 2025-04-18 15:41:29,088 INFO 27 Current configs, from /ragflow/conf/service_conf.yaml:
  73. ragflow-server | ragflow: {'host': '0.0.0.0', 'http_port': 9380}
  74. ...
  75. ragflow-server | * Running on all addresses (0.0.0.0)
  76. ragflow-server | * Running on http://127.0.0.1:9380
  77. ragflow-server | * Running on http://172.19.0.6:9380
  78. ragflow-server | ______ __ ______ __
  79. ragflow-server | /_ __/___ ______/ /__ / ____/ _____ _______ __/ /_____ _____
  80. ragflow-server | / / / __ `/ ___/ //_/ / __/ | |/_/ _ \/ ___/ / / / __/ __ \/ ___/
  81. ragflow-server | / / / /_/ (__ ) ,< / /____> </ __/ /__/ /_/ / /_/ /_/ / /
  82. ragflow-server | /_/ \__,_/____/_/|_| /_____/_/|_|\___/\___/\__,_/\__/\____/_/
  83. ragflow-server |
  84. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 TaskExecutor: RAGFlow version: v0.17.2-285-gb2c299fa full
  85. ragflow-server | 2025-04-18 15:41:34,501 INFO 32 Use Elasticsearch http://es01:9200 as the doc engine.
  86. ...
  87. ```
  88. You are ready to brew🍺!
  89. ## Testing and Usage
  90. 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.
  91. ### Example MCP Client {#example_mcp_client}
  92. ```python
  93. #
  94. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  95. #
  96. # Licensed under the Apache License, Version 2.0 (the "License");
  97. # you may not use this file except in compliance with the License.
  98. # You may obtain a copy of the License at
  99. #
  100. # http://www.apache.org/licenses/LICENSE-2.0
  101. #
  102. # Unless required by applicable law or agreed to in writing, software
  103. # distributed under the License is distributed on an "AS IS" BASIS,
  104. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  105. # See the License for the specific language governing permissions and
  106. # limitations under the License.
  107. #
  108. from mcp.client.session import ClientSession
  109. from mcp.client.sse import sse_client
  110. async def main():
  111. try:
  112. async with sse_client("http://localhost:9382/sse", headers={"api_key": "ragflow-IyMGI1ZDhjMTA2ZTExZjBiYTMyMGQ4Zm"}) as streams:
  113. async with ClientSession(
  114. streams[0],
  115. streams[1],
  116. ) as session:
  117. await session.initialize()
  118. tools = await session.list_tools()
  119. print(f"{tools.tools=}")
  120. response = await session.call_tool(name="ragflow_retrival", arguments={"dataset_ids": ["ce3bb17cf27a11efa69751e139332ced"], "document_ids": [], "question": "How to install neovim?"})
  121. print(f"Tool response: {response.model_dump()}")
  122. except Exception as e:
  123. print(e)
  124. if __name__ == "__main__":
  125. from anyio import run
  126. run(main)
  127. ```
  128. ## Security and Concerns
  129. 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).