Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

fix: AI dialogue node calls MCP, AI's reply content is included in the code block of MCP call result, markdown parsing error #3846

…e code block of MCP call result, markdown parsing error #3846
@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Aug 20, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Aug 20, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 merged commit 141de35 into v1 Aug 20, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@v1@fix_ai_chat branch August 20, 2025 05:48
content = generate_tool_message_template(chunk[0].name, chunk[0].content)
chunk[0].content = content
yield chunk[0]
if isinstance(chunk[0], AIMessageChunk):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has a few issues and areas for improvement:

  1. Redundancy: The generate_tool_message_template function is called twice within the _yield_mcp_response method. This redundancy can be removed.

  2. Security Concerns: If user input (reasoning_content) contains malicious JSON, it could lead to injection attacks. Consider sanitizing this input before converting it to JSON.

  3. Code Duplication: There are similar template string constructions in both places where you're creating tool messages. Can these be combined into a single function?

Here's an optimized version of the code:

@@ -11,7 +11,6 @@
 import re
 import time
 from functools import reduce
-from types import AsyncGeneratorType
 from typing import List, Dict
 
 from django.db.models import QuerySet
@@ -33,13 +32,25 @@
         <strong>Called MCP Tool: <em>%s</em></strong>
     </summary>
 
-```json
 %s
-```
+
 </details>
 
 """
 
+def format_tool_message(content):
+    """Formats the content as a Markdown block with triple backticks."""
+    return f"```json\n{json.dumps(content, indent=4)}\n```"
 
 def _write_context(node_variable: Dict, workflow_variable: Dict, node: INode, workflow, answer: str,
                    reasoning_content: str):
@@ -109,7 +120,7 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers):
         response = agent.astream({"messages": message_list}, stream_mode='messages')
         async for chunk in response:
             if isinstance(chunk[0], ToolMessage):
-                content = tool_message_template % (chunk[0].name, chunk[0].content)
+                content = format_tool_message(chunk[0].content)
                 chunk[0].content = content
                 yield chunk[0]
             if isinstance(chunk[0], AIMessageChunk):

Key Changes:

  • Single Function for Formatting: A new format_tool_message function has been introduced to handle the formatting of Tool Message data.
  • Simplified Code: The code duplication between the two places has been eliminated, improving readability and maintainability.

This should help resolve the redundancies and improve security while keeping the code concise.

console.log(nodeModel)
props.nodeModel.graphModel.addEdge({
type: 'app-edge',
sourceNodeId: props.nodeModel.id,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no irregularity or issue in the provided code snippet. The function clickNodes appears to be correctly setting up data and parameters needed for adding an edge to a graph using properties passed from props. A possible optimization suggestion would be to ensure that anchorData.value?.x, width, and other variables used in the calculation have defined values in order to prevent errors at runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants