RAGFlow MCP 客户端示例
Python 和 curl MCP 客户端示例。
MCP Python 客户端示例
我们提供了一个用于测试的原型 MCP 客户端示例此处。
重要
如果您的 MCP 服务器在主机模式下运行,请在异步连接到它时在客户端的 headers 中包含您获取的 API 密钥:
async with sse_client("http://localhost:9382/sse", headers={"api_key": "YOUR_KEY_HERE"}) as streams:
# 其余代码...
或者,为了符合 OAuth 2.1 第 5 节,您可以改为运行以下代码来连接到您的 MCP 服务器:
async with sse_client("http://localhost:9382/sse", headers={"Authorization": "YOUR_KEY_HERE"}) as streams:
# 其余代码...
使用 curl 与 RAGFlow MCP 服务器交互
通过 HTTP 请求与 MCP 服务器交互时,请遵循以下初始化序列:
- 客户端发送
initialize请求,包含协议版本和功能。 - 服务器回复
initialize响应,包括支持的协议和功能。 - 客户端通过
initialized通知确认就绪。 客户端和服务器之间建立连接,可以继续进行进一步的操作(如工具列表)。
注意
有关此初始化过程的更多信息,请参阅此处。
在以下部分中,我们将引导您完成完整的工具调用过程。
1. 获取会话 ID
每个与 MCP 服务器的 curl 请求都必须包含会话 ID:
$ curl -N -H "api_key: YOUR_API_KEY" http://127.0.0.1:9382/sse
注意
有关获取 API 密钥的信息,请参阅此处。
传输
传输将流式传输消息,如工具结果、服务器响应和保活 ping。
服务器返回会话 ID:
event: endpoint
data: /messages/?session_id=5c6600ef61b845a788ddf30dceb25c54
2. 发送 Initialize 请求
客户端发送包含协议版本和功能的 initialize 请求:
session_id="5c6600ef61b845a788ddf30dceb25c54" && \
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "1.0",
"capabilities": {},
"clientInfo": {
"name": "ragflow-mcp-client",
"version": "0.1"
}
}
}' && \
传输
服务器回复 initialize 响应,包括支持的协议和功能:
event: message
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"}}}
3. 确认就绪
客户端通过 initialized 通知确认就绪:
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "notifications/initialized",
"params": {}
}' && \
客户端和服务器之间建立连接,可以继续进行进一步的操作(如工具列表)。
4. 工具列表
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/list",
"params": {}
}' && \
传输
event: message
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"]}}]}}
5. 工具调用
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "ragflow_retrieval",
"arguments": {
"question": "How to install neovim?",
"dataset_ids": ["DATASET_ID_HERE"],
"document_ids": []
}
}
}'
传输
event: message
data: {"jsonrpc":"2.0","id":4,"result":{...}}
完整的 curl 示例
session_id="YOUR_SESSION_ID" && \
# 步骤 1:初始化请求
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "1.0",
"capabilities": {},
"clientInfo": {
"name": "ragflow-mcp-client",
"version": "0.1"
}
}
}' && \
sleep 2 && \
# 步骤 2:已初始化通知
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "notifications/initialized",
"params": {}
}' && \
sleep 2 && \
# 步骤 3:工具列表
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/list",
"params": {}
}' && \
sleep 2 && \
# 步骤 4:工具调用
curl -X POST "http://127.0.0.1:9382/messages/?session_id=$session_id" \
-H "api_key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "ragflow_retrieval",
"arguments": {
"question": "How to install neovim?",
"dataset_ids": ["DATASET_ID_HERE"],
"document_ids": []
}
}
}'