fix: avoid fake MCP completion text for empty results
This commit is contained in:
parent
9dfb1567d2
commit
afd75b4ca9
@ -940,7 +940,7 @@ class MCPClientManager:
|
||||
return json.dumps(structured, ensure_ascii=False)[:500]
|
||||
except Exception:
|
||||
return str(structured)[:500]
|
||||
return "MCP 工具调用完成"
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _content_preview(content_items: Any) -> str:
|
||||
|
||||
@ -504,6 +504,19 @@ class MCPIntegrationTest(unittest.TestCase):
|
||||
self.assertTrue(result.get("success"), result)
|
||||
self.assertTrue((result.get("structured_content") or {}).get("ping_ok"), result)
|
||||
|
||||
def test_build_call_message_empty_content_returns_empty_string(self):
|
||||
message = MCPClientManager._build_call_message(
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
self.assertEqual(message, "")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -124,7 +124,39 @@ class MediaStoreAndMCPContentTest(unittest.TestCase):
|
||||
self.assertNotIn("data", content)
|
||||
self.assertTrue(content.get("data_omitted"))
|
||||
|
||||
def test_mcp_formatter_returns_empty_when_content_is_empty(self):
|
||||
result_data = {
|
||||
"success": True,
|
||||
"server_id": "browsermcp",
|
||||
"tool_alias": "mcp__browsermcp__browser_get_console_logs",
|
||||
"tool_name": "browser_get_console_logs",
|
||||
"message": "MCP 工具调用完成",
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "",
|
||||
}
|
||||
],
|
||||
"raw_result": {
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "",
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
parsed = extract_mcp_content_for_context(result_data)
|
||||
self.assertEqual(parsed.get("text"), "")
|
||||
|
||||
text = format_tool_result_for_context(
|
||||
"mcp__browsermcp__browser_get_console_logs",
|
||||
result_data,
|
||||
raw_text='{"success": true}',
|
||||
)
|
||||
self.assertEqual(text, "")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
@ -52,6 +52,10 @@ def _sanitize_mcp_content_item(item: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return sanitized
|
||||
|
||||
|
||||
def _is_default_mcp_message(message: str) -> bool:
|
||||
return message.strip() == "MCP 工具调用完成"
|
||||
|
||||
|
||||
def extract_mcp_content_for_context(result_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""提取 MCP content,返回可读文本 + 可落盘媒体列表 + 去二进制后的 payload。"""
|
||||
if not isinstance(result_data, dict):
|
||||
@ -144,7 +148,7 @@ def extract_mcp_content_for_context(result_data: Dict[str, Any]) -> Dict[str, An
|
||||
sanitized_content.append(_sanitize_mcp_content_item(item))
|
||||
|
||||
message = str(result_data.get("message") or "").strip()
|
||||
if message and message != "MCP 工具调用完成":
|
||||
if message and not _is_default_mcp_message(message):
|
||||
if message not in text_lines:
|
||||
text_lines.insert(0, message)
|
||||
|
||||
@ -161,10 +165,8 @@ def extract_mcp_content_for_context(result_data: Dict[str, Any]) -> Dict[str, An
|
||||
except Exception:
|
||||
text_lines.append(str(structured))
|
||||
|
||||
if not text_lines and message:
|
||||
if not text_lines and message and not _is_default_mcp_message(message):
|
||||
text_lines.append(message)
|
||||
if not text_lines:
|
||||
text_lines.append("MCP 工具调用完成")
|
||||
|
||||
sanitized_payload: Dict[str, Any] = dict(result_data)
|
||||
if isinstance(result_data.get("content"), list):
|
||||
@ -270,6 +272,7 @@ def format_tool_result_for_context(function_name: str, result_data: Any, raw_tex
|
||||
text = str(parsed.get("text") or "").strip()
|
||||
if text:
|
||||
return text
|
||||
return ""
|
||||
|
||||
if function_name == "read_file" and isinstance(result_data, dict):
|
||||
return format_read_file_result(result_data)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user