Skip to content

Commit bc5e89c

Browse files
Merge pull request #768 from MervinPraison/claude/issue-767-20250708_220427
fix: handle MCP tools returning lists in agent.py
2 parents 5241bab + a8d750b commit bc5e89c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/praisonai-agents/praisonaiagents/agent/agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,12 @@ def _format_tools_for_completion(self, tools=None):
942942
logging.warning(f"Could not generate definition for tool: {tool}")
943943
# Handle objects with to_openai_tool method (MCP tools)
944944
elif hasattr(tool, "to_openai_tool"):
945-
formatted_tools.append(tool.to_openai_tool())
945+
openai_tools = tool.to_openai_tool()
946+
# MCP tools can return either a single tool or a list of tools
947+
if isinstance(openai_tools, list):
948+
formatted_tools.extend(openai_tools)
949+
elif openai_tools is not None:
950+
formatted_tools.append(openai_tools)
946951
# Handle callable functions
947952
elif callable(tool):
948953
tool_def = self._generate_tool_definition(tool.__name__)

test_mcp_fix.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
"""Test script to verify MCP schema fix"""
3+
4+
import os
5+
import sys
6+
7+
# Add the src directory to Python path
8+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents'))
9+
10+
from dotenv import load_dotenv
11+
from praisonaiagents import Agent, MCP
12+
13+
# Load .env before importing anything else
14+
load_dotenv()
15+
16+
# Define allowed directories for filesystem access
17+
allowed_dirs = [
18+
"/Users/praison/praisonai-package/src/praisonai-agents",
19+
]
20+
21+
# Test with minimal example
22+
print("Testing MCP filesystem agent...")
23+
24+
try:
25+
# Use the correct pattern from filesystem MCP documentation
26+
filesystem_agent = Agent(
27+
instructions="""You are a helpful assistant that can interact with the filesystem.
28+
Use the available tools when relevant to manage files and directories.""",
29+
llm="gpt-4o-mini",
30+
tools=MCP(
31+
command="npx",
32+
args=["-y", "@modelcontextprotocol/server-filesystem"] + allowed_dirs
33+
)
34+
)
35+
36+
print("✓ Agent created successfully")
37+
print("✓ MCP tools loaded without schema errors")
38+
39+
# Try to start the agent (this will validate the tool schemas)
40+
result = filesystem_agent.start("List files in /Users/praison/praisonai-package/src/praisonai-agents directory using MCP list_files")
41+
print("✓ Agent executed successfully")
42+
print(f"Result: {result}")
43+
44+
except Exception as e:
45+
print(f"✗ Error: {e}")
46+
import traceback
47+
traceback.print_exc()
48+
sys.exit(1)
49+
50+
print("\nTest passed! The MCP schema fix is working correctly.")

0 commit comments

Comments
 (0)