Skip to content

armgit / dify

Public Imported from GitHub
0

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.

fix(mcp): properly handle arrays containing both numbers and strings (#25430)

Novice240b65b980cb
diff --git a/api/core/tools/mcp_tool/tool.py b/api/core/tools/mcp_tool/tool.py
index 6810ac683d5..21d256ae039 100644
--- a/api/core/tools/mcp_tool/tool.py
+++ b/api/core/tools/mcp_tool/tool.py
@@ -67,22 +67,42 @@ class MCPTool(Tool):
for content in result.content:
if isinstance(content, TextContent):
- try:
- content_json = json.loads(content.text)
- if isinstance(content_json, dict):
- yield self.create_json_message(content_json)
- elif isinstance(content_json, list):
- for item in content_json:
- yield self.create_json_message(item)
- else:
- yield self.create_text_message(content.text)
- except json.JSONDecodeError:
- yield self.create_text_message(content.text)
-
+ yield from self._process_text_content(content)
elif isinstance(content, ImageContent):
- yield self.create_blob_message(
- blob=base64.b64decode(content.data), meta={"mime_type": content.mimeType}
- )
+ yield self._process_image_content(content)
+
+ def _process_text_content(self, content: TextContent) -> Generator[ToolInvokeMessage, None, None]:
+ """Process text content and yield appropriate messages."""
+ try:
+ content_json = json.loads(content.text)
+ yield from self._process_json_content(content_json)
+ except json.JSONDecodeError:
+ yield self.create_text_message(content.text)
+
+ def _process_json_content(self, content_json: Any) -> Generator[ToolInvokeMessage, None, None]:
+ """Process JSON content based on its type."""
+ if isinstance(content_json, dict):
+ yield self.create_json_message(content_json)
+ elif isinstance(content_json, list):
+ yield from self._process_json_list(content_json)
+ else:
+ # For primitive types (str, int, bool, etc.), convert to string
+ yield self.create_text_message(str(content_json))
+
+ def _process_json_list(self, json_list: list) -> Generator[ToolInvokeMessage, None, None]:
+ """Process a list of JSON items."""
+ if any(not isinstance(item, dict) for item in json_list):
+ # If the list contains any non-dict item, treat the entire list as a text message.
+ yield self.create_text_message(str(json_list))
+ return
+
+ # Otherwise, process each dictionary as a separate JSON message.
+ for item in json_list:
+ yield self.create_json_message(item)
+
+ def _process_image_content(self, content: ImageContent) -> ToolInvokeMessage:
+ """Process image content and return a blob message."""
+ return self.create_blob_message(blob=base64.b64decode(content.data), meta={"mime_type": content.mimeType})
def fork_tool_runtime(self, runtime: ToolRuntime) -> "MCPTool":
return MCPTool(