RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Refactor: move mcp connection utilities to common (#11304)
Jin Haibd4bc57009fe
diff --git a/agent/component/agent_with_tools.py b/agent/component/agent_with_tools.pyindex a27504139e..906a9eca3c 100644--- a/agent/component/agent_with_tools.py+++ b/agent/component/agent_with_tools.py@@ -30,7 +30,7 @@ from api.db.services.mcp_server_service import MCPServerServicefrom common.connection_utils import timeoutfrom rag.prompts.generator import next_step, COMPLETE_TASK, analyze_task, \citation_prompt, reflect, rank_memories, kb_prompt, citation_plus, full_question, message_fit_in-from rag.utils.mcp_tool_call_conn import MCPToolCallSession, mcp_tool_metadata_to_openai_tool+from common.mcp_tool_call_conn import MCPToolCallSession, mcp_tool_metadata_to_openai_toolfrom agent.component.llm import LLMParam, LLMdiff --git a/agent/tools/base.py b/agent/tools/base.pyindex a3d569694a..791242d595 100644--- a/agent/tools/base.py+++ b/agent/tools/base.py@@ -21,9 +21,8 @@ from functools import partialfrom typing import TypedDict, List, Anyfrom agent.component.base import ComponentParamBase, ComponentBasefrom common.misc_utils import hash_str2int-from rag.llm.chat_model import ToolCallSessionfrom rag.prompts.generator import kb_prompt-from rag.utils.mcp_tool_call_conn import MCPToolCallSession+from common.mcp_tool_call_conn import MCPToolCallSession, ToolCallSessionfrom timeit import default_timer as timerdiff --git a/api/apps/mcp_server_app.py b/api/apps/mcp_server_app.pyindex 66d4474915..a8ac2aef1d 100644--- a/api/apps/mcp_server_app.py+++ b/api/apps/mcp_server_app.py@@ -25,7 +25,7 @@ from common.misc_utils import get_uuidfrom api.utils.api_utils import get_data_error_result, get_json_result, server_error_response, validate_request, \get_mcp_toolsfrom api.utils.web_utils import get_float, safe_json_parse-from rag.utils.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessions+from common.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessions@manager.route("/list", methods=["POST"]) # noqa: F821diff --git a/api/apps/sdk/agents.py b/api/apps/sdk/agents.pyindex 208b7a1bef..14ea97fb63 100644--- a/api/apps/sdk/agents.py+++ b/api/apps/sdk/agents.py@@ -41,12 +41,12 @@ def list_agents(tenant_id):return get_error_data_result("The agent doesn't exist.")page_number = int(request.args.get("page", 1))items_per_page = int(request.args.get("page_size", 30))- orderby = request.args.get("orderby", "update_time")+ order_by = request.args.get("orderby", "update_time")if request.args.get("desc") == "False" or request.args.get("desc") == "false":desc = Falseelse:desc = True- canvas = UserCanvasService.get_list(tenant_id, page_number, items_per_page, orderby, desc, id, title)+ canvas = UserCanvasService.get_list(tenant_id, page_number, items_per_page, order_by, desc, id, title)return get_result(data=canvas)diff --git a/api/ragflow_server.py b/api/ragflow_server.pyindex 868e054aeb..c340255e7e 100644--- a/api/ragflow_server.py+++ b/api/ragflow_server.py@@ -41,7 +41,7 @@ from api.db.db_models import init_database_tables as init_web_dbfrom api.db.init_data import init_web_datafrom common.versions import get_ragflow_versionfrom common.config_utils import show_configs-from rag.utils.mcp_tool_call_conn import shutdown_all_mcp_sessions+from common.mcp_tool_call_conn import shutdown_all_mcp_sessionsfrom rag.utils.redis_conn import RedisDistributedLockstop_event = threading.Event()diff --git a/api/utils/api_utils.py b/api/utils/api_utils.pyindex 4cace9eca0..1bd3f3e3cc 100644--- a/api/utils/api_utils.py+++ b/api/utils/api_utils.py@@ -37,7 +37,7 @@ from peewee import OperationalErrorfrom common.constants import ActiveEnumfrom api.db.db_models import APITokenfrom api.utils.json_encode import CustomJSONEncoder-from rag.utils.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessions+from common.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessionsfrom api.db.services.tenant_llm_service import LLMFactoriesServicefrom common.connection_utils import timeoutfrom common.constants import RetCodediff --git a/common/data_source/interfaces.py b/common/data_source/interfaces.pyindex 9c5f00141f..5e5d3aa2e2 100644--- a/common/data_source/interfaces.py+++ b/common/data_source/interfaces.py@@ -69,7 +69,7 @@ class SlimConnectorWithPermSync(ABC):class CheckpointedConnectorWithPermSync(ABC):- """Checkpointed connector interface (with permission sync)"""+ """Checkpoint connector interface (with permission sync)"""@abstractmethoddef load_from_checkpoint(@@ -143,7 +143,7 @@ class CredentialsProviderInterface(abc.ABC, Generic[T]):@abc.abstractmethoddef is_dynamic(self) -> bool:- """If dynamic, the credentials may change during usage ... maening the client+ """If dynamic, the credentials may change during usage ... meaning the clientneeds to use the locking features of the credentials provider to operatecorrectly.diff --git a/rag/utils/mcp_tool_call_conn.py b/common/mcp_tool_call_conn.pysimilarity index 94%rename from rag/utils/mcp_tool_call_conn.pyrename to common/mcp_tool_call_conn.pyindex 2093f7bc86..b19f063e1a 100644--- a/rag/utils/mcp_tool_call_conn.py+++ b/common/mcp_tool_call_conn.py@@ -21,7 +21,7 @@ import weakreffrom concurrent.futures import ThreadPoolExecutorfrom concurrent.futures import TimeoutError as FuturesTimeoutErrorfrom string import Template-from typing import Any, Literal+from typing import Any, Literal, Protocolfrom typing_extensions import override@@ -30,12 +30,15 @@ from mcp.client.session import ClientSessionfrom mcp.client.sse import sse_clientfrom mcp.client.streamable_http import streamablehttp_clientfrom mcp.types import CallToolResult, ListToolsResult, TextContent, Tool-from rag.llm.chat_model import ToolCallSessionMCPTaskType = Literal["list_tools", "tool_call"]MCPTask = tuple[MCPTaskType, dict[str, Any], asyncio.Queue[Any]]+class ToolCallSession(Protocol):+ def tool_call(self, name: str, arguments: dict[str, Any]) -> str: ...++class MCPToolCallSession(ToolCallSession):_ALL_INSTANCES: weakref.WeakSet["MCPToolCallSession"] = weakref.WeakSet()@@ -106,7 +109,8 @@ class MCPToolCallSession(ToolCallSession):await self._process_mcp_tasks(None, msg)else:- await self._process_mcp_tasks(None, f"Unsupported MCP server type: {self._mcp_server.server_type}, id: {self._mcp_server.id}")+ await self._process_mcp_tasks(None,+ f"Unsupported MCP server type: {self._mcp_server.server_type}, id: {self._mcp_server.id}")async def _process_mcp_tasks(self, client_session: ClientSession | None, error_message: str | None = None) -> None:while not self._close:@@ -164,7 +168,8 @@ class MCPToolCallSession(ToolCallSession):raiseasync def _call_mcp_tool(self, name: str, arguments: dict[str, Any], timeout: float | int = 10) -> str:- result: CallToolResult = await self._call_mcp_server("tool_call", name=name, arguments=arguments, timeout=timeout)+ result: CallToolResult = await self._call_mcp_server("tool_call", name=name, arguments=arguments,+ timeout=timeout)if result.isError:return f"MCP server error: {result.content}"@@ -283,7 +288,8 @@ def close_multiple_mcp_toolcall_sessions(sessions: list[MCPToolCallSession]) ->except Exception:logging.exception("Exception during MCP session cleanup thread management")- logging.info(f"{len(sessions)} MCP sessions has been cleaned up. {len(list(MCPToolCallSession._ALL_INSTANCES))} in global context.")+ logging.info(+ f"{len(sessions)} MCP sessions has been cleaned up. {len(list(MCPToolCallSession._ALL_INSTANCES))} in global context.")def shutdown_all_mcp_sessions():@@ -298,7 +304,7 @@ def shutdown_all_mcp_sessions():logging.info("All MCPToolCallSession instances have been closed.")-def mcp_tool_metadata_to_openai_tool(mcp_tool: Tool|dict) -> dict[str, Any]:+def mcp_tool_metadata_to_openai_tool(mcp_tool: Tool | dict) -> dict[str, Any]:if isinstance(mcp_tool, dict):return {"type": "function",diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.pyindex 17ddbc1388..c9e3b29f79 100644--- a/rag/llm/chat_model.py+++ b/rag/llm/chat_model.py@@ -22,7 +22,6 @@ import reimport timefrom abc import ABCfrom copy import deepcopy-from typing import Any, Protocolfrom urllib.parse import urljoinimport json_repair@@ -65,10 +64,6 @@ LENGTH_NOTIFICATION_CN = "······\n由于大模型的上下文窗口大小LENGTH_NOTIFICATION_EN = "...\nThe answer is truncated by your chosen LLM due to its limitation on context length."-class ToolCallSession(Protocol):- def tool_call(self, name: str, arguments: dict[str, Any]) -> str: ...--class Base(ABC):def __init__(self, key, model_name, base_url, **kwargs):timeout = int(os.environ.get("LM_TIMEOUT_SECONDS", 600))diff --git a/rag/nlp/__init__.py b/rag/nlp/__init__.pyindex 80acf1d8fb..de7c2ce608 100644--- a/rag/nlp/__init__.py+++ b/rag/nlp/__init__.py@@ -155,13 +155,13 @@ def qbullets_category(sections):if re.match(pro, sec) and not not_bullet(sec):hits[i] += 1break- maxium = 0+ maximum = 0res = -1for i, h in enumerate(hits):- if h <= maxium:+ if h <= maximum:continueres = i- maxium = h+ maximum = hreturn res, QUESTION_PATTERN[res]@@ -222,13 +222,13 @@ def bullets_category(sections):if re.match(p, sec) and not not_bullet(sec):hits[i] += 1break- maxium = 0+ maximum = 0res = -1for i, h in enumerate(hits):- if h <= maxium:+ if h <= maximum:continueres = i- maxium = h+ maximum = hreturn res