fix: change the mcp tool node error type

pull/20716/head
Novice 11 months ago
parent cca3cff759
commit 87e8e0f69a

@ -284,7 +284,7 @@ def sse_client(
try: try:
with create_ssrf_proxy_mcp_http_client(headers=transport.headers) as client: with create_ssrf_proxy_mcp_http_client(headers=transport.headers) as client:
with ssrf_proxy_sse_connect( with ssrf_proxy_sse_connect(
url, 2, timeout=httpx.Timeout(timeout, read=sse_read_timeout), client=client url, timeout=httpx.Timeout(timeout, read=sse_read_timeout), client=client
) as event_source: ) as event_source:
event_source.response.raise_for_status() event_source.response.raise_for_status()

@ -185,7 +185,6 @@ class StreamableHTTPTransport:
with ssrf_proxy_sse_connect( with ssrf_proxy_sse_connect(
self.url, self.url,
2,
headers=headers, headers=headers,
timeout=httpx.Timeout(self.timeout.seconds, read=self.sse_read_timeout.seconds), timeout=httpx.Timeout(self.timeout.seconds, read=self.sse_read_timeout.seconds),
client=client, client=client,
@ -215,7 +214,6 @@ class StreamableHTTPTransport:
with ssrf_proxy_sse_connect( with ssrf_proxy_sse_connect(
self.url, self.url,
2,
headers=headers, headers=headers,
timeout=httpx.Timeout(self.timeout.seconds, read=ctx.sse_read_timeout.seconds), timeout=httpx.Timeout(self.timeout.seconds, read=ctx.sse_read_timeout.seconds),
client=ctx.client, client=ctx.client,

@ -179,10 +179,7 @@ class BaseSession(
def check_receiver_status(self) -> None: def check_receiver_status(self) -> None:
if self._receiver_future.done(): if self._receiver_future.done():
try: self._receiver_future.result()
self._receiver_future.result()
except Exception as e:
raise e
def __exit__( def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None

@ -6,8 +6,6 @@ from configs import dify_config
from core.mcp.types import ErrorData, JSONRPCError from core.mcp.types import ErrorData, JSONRPCError
from core.model_runtime.utils.encoders import jsonable_encoder from core.model_runtime.utils.encoders import jsonable_encoder
SSRF_DEFAULT_MAX_RETRIES = dify_config.SSRF_DEFAULT_MAX_RETRIES
HTTP_REQUEST_NODE_SSL_VERIFY = dify_config.HTTP_REQUEST_NODE_SSL_VERIFY HTTP_REQUEST_NODE_SSL_VERIFY = dify_config.HTTP_REQUEST_NODE_SSL_VERIFY
STATUS_FORCELIST = [429, 500, 502, 503, 504] STATUS_FORCELIST = [429, 500, 502, 503, 504]
@ -57,7 +55,7 @@ def create_ssrf_proxy_mcp_http_client(
) )
def ssrf_proxy_sse_connect(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs): def ssrf_proxy_sse_connect(url, **kwargs):
"""Connect to SSE endpoint with SSRF proxy protection. """Connect to SSE endpoint with SSRF proxy protection.
This function creates an SSE connection using the configured proxy settings This function creates an SSE connection using the configured proxy settings
@ -65,7 +63,6 @@ def ssrf_proxy_sse_connect(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
Args: Args:
url: The SSE endpoint URL url: The SSE endpoint URL
max_retries: Maximum number of retry attempts
**kwargs: Additional arguments passed to the SSE connection **kwargs: Additional arguments passed to the SSE connection
Returns: Returns:

@ -39,14 +39,19 @@ class MCPTool(Tool):
app_id: Optional[str] = None, app_id: Optional[str] = None,
message_id: Optional[str] = None, message_id: Optional[str] = None,
) -> Generator[ToolInvokeMessage, None, None]: ) -> Generator[ToolInvokeMessage, None, None]:
from core.tools.errors import ToolInvokeError
try: try:
with MCPClient(self.server_url, self.provider_id, self.tenant_id, authed=True) as mcp_client: with MCPClient(self.server_url, self.provider_id, self.tenant_id, authed=True) as mcp_client:
tool_parameters = self._handle_none_parameter(tool_parameters) tool_parameters = self._handle_none_parameter(tool_parameters)
result = mcp_client.invoke_tool(tool_name=self.entity.identity.name, tool_args=tool_parameters) result = mcp_client.invoke_tool(tool_name=self.entity.identity.name, tool_args=tool_parameters)
except MCPAuthError as e: except MCPAuthError as e:
raise ValueError("Please auth the tool first") raise ToolInvokeError("Please auth the tool first") from e
except MCPConnectionError as e: except MCPConnectionError as e:
raise ValueError(f"Failed to connect to MCP server: {e}") raise ToolInvokeError(f"Failed to connect to MCP server: {e}") from e
except Exception as e:
raise ToolInvokeError(f"Failed to invoke tool: {e}") from e
for content in result.content: for content in result.content:
if isinstance(content, TextContent): if isinstance(content, TextContent):
yield self.create_text_message(content.text) yield self.create_text_message(content.text)

Loading…
Cancel
Save