From a8d750b21b4139112b61d7ed2e955bc432ccf350 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <209825114+claude[bot]@users.noreply.github.com> Date: Tue, 8 Jul 2025 22:10:06 +0000 Subject: [PATCH] fix: handle MCP tools returning lists in agent.py - Fixed issue where MCP's to_openai_tool() returns a list of tools but agent.py was expecting a single tool - Now properly extends the formatted_tools list when MCP returns multiple tools - Maintains backward compatibility for single tool returns - Added test script to verify the fix Fixes #767 Co-authored-by: Mervin Praison --- .../praisonaiagents/agent/agent.py | 7 ++- test_mcp_fix.py | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test_mcp_fix.py diff --git a/src/praisonai-agents/praisonaiagents/agent/agent.py b/src/praisonai-agents/praisonaiagents/agent/agent.py index 15df85714..1b9a41d97 100644 --- a/src/praisonai-agents/praisonaiagents/agent/agent.py +++ b/src/praisonai-agents/praisonaiagents/agent/agent.py @@ -934,7 +934,12 @@ def _format_tools_for_completion(self, tools=None): logging.warning(f"Could not generate definition for tool: {tool}") # Handle objects with to_openai_tool method (MCP tools) elif hasattr(tool, "to_openai_tool"): - formatted_tools.append(tool.to_openai_tool()) + openai_tools = tool.to_openai_tool() + # MCP tools can return either a single tool or a list of tools + if isinstance(openai_tools, list): + formatted_tools.extend(openai_tools) + elif openai_tools is not None: + formatted_tools.append(openai_tools) # Handle callable functions elif callable(tool): tool_def = self._generate_tool_definition(tool.__name__) diff --git a/test_mcp_fix.py b/test_mcp_fix.py new file mode 100644 index 000000000..209e78a56 --- /dev/null +++ b/test_mcp_fix.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +"""Test script to verify MCP schema fix""" + +import os +import sys + +# Add the src directory to Python path +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents')) + +from dotenv import load_dotenv +from praisonaiagents import Agent, MCP + +# Load .env before importing anything else +load_dotenv() + +# Define allowed directories for filesystem access +allowed_dirs = [ + "/Users/praison/praisonai-package/src/praisonai-agents", +] + +# Test with minimal example +print("Testing MCP filesystem agent...") + +try: + # Use the correct pattern from filesystem MCP documentation + filesystem_agent = Agent( + instructions="""You are a helpful assistant that can interact with the filesystem. + Use the available tools when relevant to manage files and directories.""", + llm="gpt-4o-mini", + tools=MCP( + command="npx", + args=["-y", "@modelcontextprotocol/server-filesystem"] + allowed_dirs + ) + ) + + print("✓ Agent created successfully") + print("✓ MCP tools loaded without schema errors") + + # Try to start the agent (this will validate the tool schemas) + result = filesystem_agent.start("List files in /Users/praison/praisonai-package/src/praisonai-agents directory using MCP list_files") + print("✓ Agent executed successfully") + print(f"Result: {result}") + +except Exception as e: + print(f"✗ Error: {e}") + import traceback + traceback.print_exc() + sys.exit(1) + +print("\nTest passed! The MCP schema fix is working correctly.") \ No newline at end of file