Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
chore: code improvement for mcp_client and mcp_tools_manage_service (#22645)
Bowen Liang74940ad3f29f
diff --git a/api/controllers/console/workspace/tool_providers.py b/api/controllers/console/workspace/tool_providers.pyindex e41375e52b1..c70bf84d2a6 100644--- a/api/controllers/console/workspace/tool_providers.py+++ b/api/controllers/console/workspace/tool_providers.py@@ -29,7 +29,7 @@ from libs.login import login_requiredfrom services.plugin.oauth_service import OAuthProxyServicefrom services.tools.api_tools_manage_service import ApiToolManageServicefrom services.tools.builtin_tools_manage_service import BuiltinToolManageService-from services.tools.mcp_tools_mange_service import MCPToolManageService+from services.tools.mcp_tools_manage_service import MCPToolManageServicefrom services.tools.tool_labels_service import ToolLabelsServicefrom services.tools.tools_manage_service import ToolCommonServicefrom services.tools.tools_transform_service import ToolTransformServicediff --git a/api/core/mcp/auth/auth_provider.py b/api/core/mcp/auth/auth_provider.pyindex cd55dbf64f1..00d5a259564 100644--- a/api/core/mcp/auth/auth_provider.py+++ b/api/core/mcp/auth/auth_provider.py@@ -8,7 +8,7 @@ from core.mcp.types import (OAuthTokens,)from models.tools import MCPToolProvider-from services.tools.mcp_tools_mange_service import MCPToolManageService+from services.tools.mcp_tools_manage_service import MCPToolManageServiceLATEST_PROTOCOL_VERSION = "1.0"diff --git a/api/core/mcp/mcp_client.py b/api/core/mcp/mcp_client.pyindex e9036de8c60..f7aa7bbd7b7 100644--- a/api/core/mcp/mcp_client.py+++ b/api/core/mcp/mcp_client.py@@ -68,15 +68,17 @@ class MCPClient:}parsed_url = urlparse(self.server_url)- path = parsed_url.path- method_name = path.rstrip("/").split("/")[-1] if path else ""- try:+ path = parsed_url.path or ""+ method_name = path.removesuffix("/").lower()+ if method_name in connection_methods:client_factory = connection_methods[method_name]self.connect_server(client_factory, method_name)- except KeyError:+ else:try:+ logger.debug(f"Not supported method {method_name} found in URL path, trying default 'mcp' method.")self.connect_server(sse_client, "sse")except MCPConnectionError:+ logger.debug("MCP connection failed with 'sse', falling back to 'mcp' method.")self.connect_server(streamablehttp_client, "mcp")def connect_server(@@ -91,7 +93,7 @@ class MCPClient:else {})self._streams_context = client_factory(url=self.server_url, headers=headers)- if self._streams_context is None:+ if not self._streams_context:raise MCPConnectionError("Failed to create connection context")# Use exit_stack to manage context managers properly@@ -141,10 +143,11 @@ class MCPClient:try:# ExitStack will handle proper cleanup of all managed context managersself.exit_stack.close()+ except Exception as e:+ logging.exception("Error during cleanup")+ raise ValueError(f"Error during cleanup: {e}")+ finally:self._session = Noneself._session_context = Noneself._streams_context = Noneself._initialized = False- except Exception as e:- logging.exception("Error during cleanup")- raise ValueError(f"Error during cleanup: {e}")diff --git a/api/core/tools/tool_manager.py b/api/core/tools/tool_manager.pyindex d61856a8f58..7822bc389c8 100644--- a/api/core/tools/tool_manager.py+++ b/api/core/tools/tool_manager.py@@ -21,7 +21,7 @@ from core.tools.plugin_tool.tool import PluginToolfrom core.tools.utils.uuid_utils import is_valid_uuidfrom core.tools.workflow_as_tool.provider import WorkflowToolProviderControllerfrom core.workflow.entities.variable_pool import VariablePool-from services.tools.mcp_tools_mange_service import MCPToolManageService+from services.tools.mcp_tools_manage_service import MCPToolManageServiceif TYPE_CHECKING:from core.workflow.nodes.tool.entities import ToolEntitydiff --git a/api/services/tools/mcp_tools_mange_service.py b/api/services/tools/mcp_tools_manage_service.pysimilarity index 95%rename from api/services/tools/mcp_tools_mange_service.pyrename to api/services/tools/mcp_tools_manage_service.pyindex fda6da59835..e0e256912e3 100644--- a/api/services/tools/mcp_tools_mange_service.py+++ b/api/services/tools/mcp_tools_manage_service.py@@ -70,16 +70,15 @@ class MCPToolManageService:MCPToolProvider.server_url_hash == server_url_hash,MCPToolProvider.server_identifier == server_identifier,),- MCPToolProvider.tenant_id == tenant_id,).first())if existing_provider:if existing_provider.name == name:raise ValueError(f"MCP tool {name} already exists")- elif existing_provider.server_url_hash == server_url_hash:+ if existing_provider.server_url_hash == server_url_hash:raise ValueError(f"MCP tool {server_url} already exists")- elif existing_provider.server_identifier == server_identifier:+ if existing_provider.server_identifier == server_identifier:raise ValueError(f"MCP tool {server_identifier} already exists")encrypted_server_url = encrypter.encrypt_token(tenant_id, server_url)mcp_tool = MCPToolProvider(@@ -111,15 +110,14 @@ class MCPToolManageService:]@classmethod- def list_mcp_tool_from_remote_server(cls, tenant_id: str, provider_id: str):+ def list_mcp_tool_from_remote_server(cls, tenant_id: str, provider_id: str) -> ToolProviderApiEntity:mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)-try:with MCPClient(mcp_provider.decrypted_server_url, provider_id, tenant_id, authed=mcp_provider.authed, for_list=True) as mcp_client:tools = mcp_client.list_tools()- except MCPAuthError as e:+ except MCPAuthError:raise ValueError("Please auth the tool first")except MCPError as e:raise ValueError(f"Failed to connect to MCP server: {e}")@@ -184,12 +182,11 @@ class MCPToolManageService:error_msg = str(e.orig)if "unique_mcp_provider_name" in error_msg:raise ValueError(f"MCP tool {name} already exists")- elif "unique_mcp_provider_server_url" in error_msg:+ if "unique_mcp_provider_server_url" in error_msg:raise ValueError(f"MCP tool {server_url} already exists")- elif "unique_mcp_provider_server_identifier" in error_msg:+ if "unique_mcp_provider_server_identifier" in error_msg:raise ValueError(f"MCP tool {server_identifier} already exists")- else:- raise+ raise@classmethoddef update_mcp_provider_credentials(