DSPy: The framework for programming—not prompting—language models
Python
fix(dspy): Fix MCP tool conversion when schema has no input params (#8566)
tvdaptiblecd9ae6e6334c
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex 843eceed1..0eb8c90fd 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -111,8 +111,8 @@ class Tool(Type):self.name = self.name or nameself.desc = self.desc or desc- self.args = self.args or args- self.arg_types = self.arg_types or arg_types+ self.args = self.args if self.args is not None else args+ self.arg_types = self.arg_types if self.arg_types is not None else arg_typesself.has_kwargs = any(param.kind == param.VAR_KEYWORD for param in sig.parameters.values())def _validate_and_parse_args(self, **kwargs):diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.pyindex 185714539..90e23b331 100644--- a/tests/adapters/test_tool.py+++ b/tests/adapters/test_tool.py@@ -5,7 +5,7 @@ import pytestfrom pydantic import BaseModelimport dspy-from dspy.adapters.types.tool import Tool, ToolCalls+from dspy.adapters.types.tool import Tool, ToolCalls, convert_input_schema_to_tool_args# Test fixtures@@ -457,3 +457,46 @@ def test_tool_calls_format_from_dict_list():assert len(result[0]["tool_calls"]) == 2assert result[0]["tool_calls"][0]["function"]["name"] == "search"assert result[0]["tool_calls"][1]["function"]["name"] == "translate"+++def test_tool_convert_input_schema_to_tool_args__no_input_params():+ args, arg_types, arg_desc = convert_input_schema_to_tool_args(schema={"properties": {}})+ assert args == {}+ assert arg_types == {}+ assert arg_desc == {}+++def test_tool_convert_input_schema_to_tool_args__lang_chain():+ # Example from langchain docs:+ # https://web.archive.org/web/20250723101359/https://api.python.langchain.com/en/latest/tools/langchain_core.tools.tool.html+ args, arg_types, arg_desc = convert_input_schema_to_tool_args(schema={+ "title": "fooSchema",+ "description": "The foo.",+ "type": "object",+ "properties": {+ "bar": {+ "title": "Bar",+ "description": "The bar.",+ "type": "string",+ },+ "baz": {+ "title": "Baz",+ "type": "integer",+ }+ },+ "required": [+ "baz",+ ],+ })+ assert args == {+ "bar": {"title": "Bar", "description": "The bar.", "type": "string"},+ "baz": {"title": "Baz", "type": "integer"},+ }+ assert arg_types == {+ "bar": str,+ "baz": int,+ }+ assert arg_desc == {+ "bar": "The bar.",+ "baz": "No description provided. (Required)",+ }diff --git a/tests/utils/resources/mcp_server.py b/tests/utils/resources/mcp_server.pyindex 6208ac720..036126211 100644--- a/tests/utils/resources/mcp_server.py+++ b/tests/utils/resources/mcp_server.py@@ -38,5 +38,11 @@ def get_account_name(account: Account):return account.profile.name+def current_datetime() -> str:+ """Get the current datetime"""+ return "2025-07-23T09:10:10.0+00:00"++if __name__ == "__main__":mcp.run()diff --git a/tests/utils/test_mcp.py b/tests/utils/test_mcp.pyindex 296db0e2c..9bcdc1e48 100644--- a/tests/utils/test_mcp.py+++ b/tests/utils/test_mcp.py@@ -87,3 +87,12 @@ async def test_convert_mcp_tool():}result = await nested_pydantic_tool.acall(account=account_in_json)assert result == "Bob"++ # Check no input parameter current_datetime tool+ current_datetime_tool = convert_mcp_tool(session, response.tools[4])+ assert current_datetime_tool.name == "current_datetime"+ assert current_datetime_tool.desc == "Get the current datetime"+ assert current_datetime_tool.args == {}+ assert current_datetime_tool.arg_types == {}+ assert current_datetime_tool.arg_desc == {}+ assert await current_datetime_tool.acall() == "2025-07-23T09:10:10.0+00:00"