Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ---
  2. sidebar_position: 3
  3. slug: /mcp_client
  4. ---
  5. # RAGFlow MCP client examples
  6. Python and curl MCP client examples.
  7. ------
  8. ## Example MCP Python client
  9. We provide a *prototype* MCP client example for testing [here](https://github.com/infiniflow/ragflow/blob/main/mcp/client/client.py).
  10. :::info IMPORTANT
  11. If your MCP server is running in host mode, include your acquired API key in your client's `headers` when connecting asynchronously to it:
  12. ```python
  13. async with sse_client("http://localhost:9382/sse", headers={"api_key": "YOUR_KEY_HERE"}) as streams:
  14. # Rest of your code...
  15. ```
  16. Alternatively, to comply with [OAuth 2.1 Section 5](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12#section-5), you can run the following code *instead* to connect to your MCP server:
  17. ```python
  18. async with sse_client("http://localhost:9382/sse", headers={"Authorization": "YOUR_KEY_HERE"}) as streams:
  19. # Rest of your code...
  20. ```
  21. :::
  22. ## Use curl to interact with the RAGFlow MCP server
  23. When interacting with the MCP server via HTTP requests, follow this initialization sequence:
  24. 1. **The client sends an `initialize` request** with protocol version and capabilities.
  25. 2. **The server replies with an `initialize` response**, including the supported protocol and capabilities.
  26. 3. **The client confirms readiness with an `initialized` notification**.
  27. _The connection is established between the client and the server, and further operations (such as tool listing) may proceed._
  28. :::tip NOTE
  29. For more information about this initialization process, see [here](https://modelcontextprotocol.io/docs/concepts/architecture#1-initialization).
  30. :::
  31. In the following sections, we will walk you through a complete tool calling process.
  32. ### 1. Obtain a session ID
  33. Each curl request with the MCP server must include a session ID:
  34. ```bash
  35. $ curl -N -H "api_key: YOUR_API_KEY" http://127.0.0.1:9382/sse
  36. ```
  37. :::tip NOTE
  38. See [here](../acquire_ragflow_api_key.md) for information about acquiring an API key.
  39. :::
  40. #### Transport
  41. The transport will stream messages such as tool results, server responses, and keep-alive pings.
  42. _The server returns the session ID:_
  43. ```bash
  44. event: endpoint
  45. data: /messages/?session_id=5c6600ef61b845a788ddf30dceb25c54
  46. ```
  47. ### 2. Send an `Initialize` request
  48. The client sends an `initialize` request with protocol version and capabilities:
  49. ```bash
  50. session_id="5c6600ef61b845a788ddf30dceb25c54" && \
  51. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  52. -H "api_key: YOUR_API_KEY" \
  53. -H "Content-Type: application/json" \
  54. -d '{
  55. "jsonrpc": "2.0",
  56. "id": 1,
  57. "method": "initialize",
  58. "params": {
  59. "protocolVersion": "1.0",
  60. "capabilities": {},
  61. "clientInfo": {
  62. "name": "ragflow-mcp-client",
  63. "version": "0.1"
  64. }
  65. }
  66. }' && \
  67. ```
  68. #### Transport
  69. _The server replies with an `initialize` response, including the supported protocol and capabilities:_
  70. ```bash
  71. event: message
  72. data: {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-03-26","capabilities":{"experimental":{"headers":{"host":"127.0.0.1:9382","user-agent":"curl/8.7.1","accept":"*/*","api_key":"ragflow-xxxxxxxxxxxx","accept-encoding":"gzip"}},"tools":{"listChanged":false}},"serverInfo":{"name":"ragflow-server","version":"1.9.4"}}}
  73. ```
  74. ### 3. Acknowledge readiness
  75. The client confirms readiness with an `initialized` notification:
  76. ```bash
  77. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  78. -H "api_key: YOUR_API_KEY" \
  79. -H "Content-Type: application/json" \
  80. -d '{
  81. "jsonrpc": "2.0",
  82. "method": "notifications/initialized",
  83. "params": {}
  84. }' && \
  85. ```
  86. _The connection is established between the client and the server, and further operations (such as tool listing) may proceed._
  87. ### 4. Tool listing
  88. ```bash
  89. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  90. -H "api_key: YOUR_API_KEY" \
  91. -H "Content-Type: application/json" \
  92. -d '{
  93. "jsonrpc": "2.0",
  94. "id": 3,
  95. "method": "tools/list",
  96. "params": {}
  97. }' && \
  98. ```
  99. #### Transport
  100. ```bash
  101. event: message
  102. data: {"jsonrpc":"2.0","id":3,"result":{"tools":[{"name":"ragflow_retrieval","description":"Retrieve relevant chunks from the RAGFlow retrieve interface based on the question, using the specified dataset_ids and optionally document_ids. Below is the list of all available datasets, including their descriptions and IDs. If you're unsure which datasets are relevant to the question, simply pass all dataset IDs to the function.","inputSchema":{"type":"object","properties":{"dataset_ids":{"type":"array","items":{"type":"string"}},"document_ids":{"type":"array","items":{"type":"string"}},"question":{"type":"string"}},"required":["dataset_ids","question"]}}]}}
  103. ```
  104. ### 5. Tool calling
  105. ```bash
  106. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  107. -H "api_key: YOUR_API_KEY" \
  108. -H "Content-Type: application/json" \
  109. -d '{
  110. "jsonrpc": "2.0",
  111. "id": 4,
  112. "method": "tools/call",
  113. "params": {
  114. "name": "ragflow_retrieval",
  115. "arguments": {
  116. "question": "How to install neovim?",
  117. "dataset_ids": ["DATASET_ID_HERE"],
  118. "document_ids": []
  119. }
  120. ```
  121. #### Transport
  122. ```bash
  123. event: message
  124. data: {"jsonrpc":"2.0","id":4,"result":{...}}
  125. ```
  126. ### A complete curl example
  127. ```bash
  128. session_id="YOUR_SESSION_ID" && \
  129. # Step 1: Initialize request
  130. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  131. -H "api_key: YOUR_API_KEY" \
  132. -H "Content-Type: application/json" \
  133. -d '{
  134. "jsonrpc": "2.0",
  135. "id": 1,
  136. "method": "initialize",
  137. "params": {
  138. "protocolVersion": "1.0",
  139. "capabilities": {},
  140. "clientInfo": {
  141. "name": "ragflow-mcp-client",
  142. "version": "0.1"
  143. }
  144. }
  145. }' && \
  146. sleep 2 && \
  147. # Step 2: Initialized notification
  148. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  149. -H "api_key: YOUR_API_KEY" \
  150. -H "Content-Type: application/json" \
  151. -d '{
  152. "jsonrpc": "2.0",
  153. "method": "notifications/initialized",
  154. "params": {}
  155. }' && \
  156. sleep 2 && \
  157. # Step 3: Tool listing
  158. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  159. -H "api_key: YOUR_API_KEY" \
  160. -H "Content-Type: application/json" \
  161. -d '{
  162. "jsonrpc": "2.0",
  163. "id": 3,
  164. "method": "tools/list",
  165. "params": {}
  166. }' && \
  167. sleep 2 && \
  168. # Step 4: Tool call
  169. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  170. -H "api_key: YOUR_API_KEY" \
  171. -H "Content-Type: application/json" \
  172. -d '{
  173. "jsonrpc": "2.0",
  174. "id": 4,
  175. "method": "tools/call",
  176. "params": {
  177. "name": "ragflow_retrieval",
  178. "arguments": {
  179. "question": "How to install neovim?",
  180. "dataset_ids": ["DATASET_ID_HERE"],
  181. "document_ids": []
  182. }
  183. }
  184. }'
  185. ```