Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): support plain return values in FunctionTool (#1703)
Zheng Xiao677bb77d0dd2
diff --git a/src/agentscope/tool/_adapters.py b/src/agentscope/tool/_adapters.pyindex 1c5664c0f..cbd23a130 100644--- a/src/agentscope/tool/_adapters.py+++ b/src/agentscope/tool/_adapters.py@@ -1,9 +1,10 @@# -*- coding: utf-8 -*-"""Adapters to convert functions and MCP tools to ToolProtocol."""import inspect+import jsonfrom contextlib import _AsyncGeneratorContextManagerfrom datetime import timedelta-from typing import Callable, Any, AsyncGenerator+from typing import Callable, Any, AsyncGenerator, Generatorfrom mcp import ClientSessionimport mcp@@ -32,7 +33,7 @@ class FunctionTool(ToolBase):This class wraps a regular Python function and makes it compatible withthe ToolProtocol interface. It automatically extracts metadata from thefunction's signature and docstring, and normalizes the return value to- AsyncGenerator[ToolChunk, None].+ ToolChunk or AsyncGenerator[ToolChunk, None]."""is_external_tool: bool = False@@ -115,7 +116,47 @@ class FunctionTool(ToolBase):else:result = self._func(**kwargs)- return result+ if isinstance(result, AsyncGenerator):++ async def _stream() -> AsyncGenerator[ToolChunk, None]:+ async for chunk in result:+ if isinstance(chunk, ToolChunk):+ yield chunk+ else:+ yield self._convert_func_result_to_chunk(chunk)++ return _stream()++ if isinstance(result, Generator):++ async def _stream() -> AsyncGenerator[ToolChunk, None]:+ for chunk in result:+ if isinstance(chunk, ToolChunk):+ yield chunk+ else:+ yield self._convert_func_result_to_chunk(chunk)++ return _stream()++ return self._convert_func_result_to_chunk(result)++ @staticmethod+ def _convert_func_result_to_chunk(+ result: Any,+ ) -> ToolChunk:+ if isinstance(result, ToolChunk):+ return result+ if isinstance(result, str):+ text = result+ else:+ try:+ text = json.dumps(result, ensure_ascii=False)+ except (TypeError, ValueError):+ text = str(result)+ return ToolChunk(+ content=[TextBlock(text=text)],+ state=ToolResultState.RUNNING,+ )class MCPTool(ToolBase):diff --git a/tests/toolkit_test.py b/tests/toolkit_test.pyindex e5dd36ca7..e7c17499c 100644--- a/tests/toolkit_test.py+++ b/tests/toolkit_test.py@@ -523,6 +523,125 @@ class RegisterFunctionTest(IsolatedAsyncioTestCase):},)+ async def test_sync_function_returning_plain_string(self) -> None:+ """Test wrapping a sync function that returns a plain string."""++ def get_weather(location: str) -> str:+ """Get weather information.++ Args:+ location: The location to get weather for+ """+ return f"The weather in {location} is sunny."++ toolkit = Toolkit(+ tools=[FunctionTool(get_weather)],+ )++ state = AgentState()+ tool_call = ToolCallBlock(+ id="test_weather",+ name="get_weather",+ input=json.dumps({"location": "Chengdu"}),+ )++ chunks = []+ response = None+ async for result in toolkit.call_tool(tool_call, state):+ if isinstance(result, ToolChunk):+ chunks.append(result)+ elif isinstance(result, ToolResponse):+ response = result++ self.assertEqual(len(chunks), 1)+ self.assertDictEqual(+ chunks[0].model_dump(),+ {+ "content": [+ {+ "type": "text",+ "id": AnyString(),+ "text": "The weather in Chengdu is sunny.",+ },+ ],+ "state": "running",+ "is_last": True,+ "metadata": {},+ "id": AnyString(),+ },+ )++ self.assertIsNotNone(response)+ self.assertDictEqual(+ response.model_dump(),+ {+ "content": [+ {+ "type": "text",+ "id": AnyString(),+ "text": "The weather in Chengdu is sunny.",+ },+ ],+ "state": "success",+ "metadata": {},+ "id": "test_weather",+ },+ )++ async def test_async_function_returning_json_serializable_value(+ self,+ ) -> None:+ """Test wrapping an async function that returns a JSON value."""++ async def get_weather(location: str) -> dict:+ """Get weather information.++ Args:+ location: The location to get weather for+ """+ return {+ "location": location,+ "weather": {+ "condition": "sunny",+ "temperature": 22,+ },+ }++ toolkit = Toolkit(+ tools=[FunctionTool(get_weather)],+ )++ state = AgentState()+ tool_call = ToolCallBlock(+ id="test_weather_json",+ name="get_weather",+ input=json.dumps({"location": "成都"}),+ )+ expected_text = json.dumps(+ {+ "location": "成都",+ "weather": {+ "condition": "sunny",+ "temperature": 22,+ },+ },+ ensure_ascii=False,+ )++ chunks = []+ response = None+ async for result in toolkit.call_tool(tool_call, state):+ if isinstance(result, ToolChunk):+ chunks.append(result)+ elif isinstance(result, ToolResponse):+ response = result++ self.assertEqual(len(chunks), 1)+ self.assertEqual(chunks[0].content[0].text, expected_text)++ self.assertIsNotNone(response)+ self.assertEqual(response.content[0].text, expected_text)+async def test_sync_streaming_function(self) -> None:"""Test registering a synchronous streaming function."""@@ -623,6 +742,56 @@ class RegisterFunctionTest(IsolatedAsyncioTestCase):},)+ async def test_sync_streaming_function_returning_plain_values(+ self,+ ) -> None:+ """Test wrapping a sync generator that yields plain values."""++ def stream_weather() -> Generator[Any, None, None]:+ """Stream weather information."""+ yield "checking"+ yield {+ "condition": "sunny",+ "temperature": 22,+ }++ toolkit = Toolkit(+ tools=[FunctionTool(stream_weather)],+ )++ state = AgentState()+ tool_call = ToolCallBlock(+ id="test_weather_stream",+ name="stream_weather",+ input=json.dumps({}),+ )+ expected_dict_text = json.dumps(+ {+ "condition": "sunny",+ "temperature": 22,+ },+ ensure_ascii=False,+ )++ chunks = []+ response = None+ async for result in toolkit.call_tool(tool_call, state):+ if isinstance(result, ToolChunk):+ chunks.append(result)+ elif isinstance(result, ToolResponse):+ response = result++ self.assertEqual(+ [chunk.content[0].text for chunk in chunks],+ ["checking", expected_dict_text],+ )++ self.assertIsNotNone(response)+ self.assertEqual(+ response.content[0].text,+ f"checking{expected_dict_text}",+ )+async def test_async_non_streaming_function(self) -> None:"""Test registering an asynchronous non-streaming function."""@@ -828,6 +997,54 @@ class RegisterFunctionTest(IsolatedAsyncioTestCase):},)+ async def test_async_streaming_function_returning_plain_values(+ self,+ ) -> None:+ """Test wrapping an async generator that yields plain values."""++ async def stream_status() -> AsyncGenerator[Any, None]:+ """Stream status information."""+ yield "started"+ yield {+ "done": True,+ }++ toolkit = Toolkit(+ tools=[FunctionTool(stream_status)],+ )++ state = AgentState()+ tool_call = ToolCallBlock(+ id="test_status_stream",+ name="stream_status",+ input=json.dumps({}),+ )+ expected_dict_text = json.dumps(+ {+ "done": True,+ },+ ensure_ascii=False,+ )++ chunks = []+ response = None+ async for result in toolkit.call_tool(tool_call, state):+ if isinstance(result, ToolChunk):+ chunks.append(result)+ elif isinstance(result, ToolResponse):+ response = result++ self.assertEqual(+ [chunk.content[0].text for chunk in chunks],+ ["started", expected_dict_text],+ )++ self.assertIsNotNone(response)+ self.assertEqual(+ response.content[0].text,+ f"started{expected_dict_text}",+ )+class ToolGroupTest(IsolatedAsyncioTestCase):"""The tool group test case."""