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_client_example.md 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. }'
  122. ```
  123. #### Transport
  124. ```bash
  125. event: message
  126. data: {"jsonrpc":"2.0","id":4,"result":{...}}
  127. ```
  128. ### A complete curl example
  129. ```bash
  130. session_id="YOUR_SESSION_ID" && \
  131. # Step 1: Initialize request
  132. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  133. -H "api_key: YOUR_API_KEY" \
  134. -H "Content-Type: application/json" \
  135. -d '{
  136. "jsonrpc": "2.0",
  137. "id": 1,
  138. "method": "initialize",
  139. "params": {
  140. "protocolVersion": "1.0",
  141. "capabilities": {},
  142. "clientInfo": {
  143. "name": "ragflow-mcp-client",
  144. "version": "0.1"
  145. }
  146. }
  147. }' && \
  148. sleep 2 && \
  149. # Step 2: Initialized notification
  150. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  151. -H "api_key: YOUR_API_KEY" \
  152. -H "Content-Type: application/json" \
  153. -d '{
  154. "jsonrpc": "2.0",
  155. "method": "notifications/initialized",
  156. "params": {}
  157. }' && \
  158. sleep 2 && \
  159. # Step 3: Tool listing
  160. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  161. -H "api_key: YOUR_API_KEY" \
  162. -H "Content-Type: application/json" \
  163. -d '{
  164. "jsonrpc": "2.0",
  165. "id": 3,
  166. "method": "tools/list",
  167. "params": {}
  168. }' && \
  169. sleep 2 && \
  170. # Step 4: Tool call
  171. curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
  172. -H "api_key: YOUR_API_KEY" \
  173. -H "Content-Type: application/json" \
  174. -d '{
  175. "jsonrpc": "2.0",
  176. "id": 4,
  177. "method": "tools/call",
  178. "params": {
  179. "name": "ragflow_retrieval",
  180. "arguments": {
  181. "question": "How to install neovim?",
  182. "dataset_ids": ["DATASET_ID_HERE"],
  183. "document_ids": []
  184. }
  185. }
  186. }'
  187. ```