From 60dc0d5713d9177cca3fe78e7213ab43ff07bc6c Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Mon, 28 Apr 2025 13:20:13 -0400 Subject: [PATCH] fix: handle empty tool response --- langchain_mcp_adapters/tools.py | 4 +++- tests/test_tools.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/langchain_mcp_adapters/tools.py b/langchain_mcp_adapters/tools.py index 04106ab2..845bfe96 100644 --- a/langchain_mcp_adapters/tools.py +++ b/langchain_mcp_adapters/tools.py @@ -27,7 +27,9 @@ def _convert_call_tool_result( non_text_contents.append(content) tool_content: str | list[str] = [content.text for content in text_contents] - if len(text_contents) == 1: + if not text_contents: + tool_content = "" + elif len(text_contents) == 1: tool_content = tool_content[0] if call_tool_result.isError: diff --git a/tests/test_tools.py b/tests/test_tools.py index 1bb7b21c..3e23f84b 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -19,6 +19,19 @@ ) +def test_convert_empty_text_content(): + # Test with a single text content + result = CallToolResult( + content=[], + isError=False, + ) + + text_content, non_text_content = _convert_call_tool_result(result) + + assert text_content == "" + assert non_text_content is None + + def test_convert_single_text_content(): # Test with a single text content result = CallToolResult(