diff --git a/src/praisonai-agents/praisonaiagents/mcp/mcp.py b/src/praisonai-agents/praisonaiagents/mcp/mcp.py index 5ec01bbd9..6b8df073e 100644 --- a/src/praisonai-agents/praisonaiagents/mcp/mcp.py +++ b/src/praisonai-agents/praisonaiagents/mcp/mcp.py @@ -11,15 +11,8 @@ from typing import Any, List, Optional, Callable, Iterable, Union from functools import wraps, partial -try: - from mcp import ClientSession, StdioServerParameters - from mcp.client.stdio import stdio_client - MCP_AVAILABLE = True -except ImportError: - MCP_AVAILABLE = False - ClientSession = None - StdioServerParameters = None - stdio_client = None +from mcp import ClientSession, StdioServerParameters +from mcp.client.stdio import stdio_client class MCPToolRunner(threading.Thread): """A dedicated thread for running MCP operations.""" @@ -203,17 +196,10 @@ def __init__(self, command_or_string=None, args=None, *, command=None, timeout=6 debug: Enable debug logging for MCP operations (default: False) **kwargs: Additional parameters for StdioServerParameters """ - # Check if MCP is available - if not MCP_AVAILABLE: - raise ImportError( - "MCP (Model Context Protocol) package is not installed. " - "Install it with: pip install praisonaiagents[mcp]" - ) - # Handle backward compatibility with named parameter 'command' if command_or_string is None and command is not None: command_or_string = command - + # Set up logging - default to WARNING level to hide INFO messages if debug: logging.getLogger("mcp-wrapper").setLevel(logging.DEBUG) diff --git a/src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py b/src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py index c2a85b0fd..5137c0800 100644 --- a/src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py +++ b/src/praisonai-agents/praisonaiagents/mcp/mcp_http_stream.py @@ -16,17 +16,11 @@ from typing import List, Dict, Any, Optional, Callable, Iterable, Union from urllib.parse import urlparse, urljoin -try: - from mcp import ClientSession - MCP_AVAILABLE = True -except ImportError: - MCP_AVAILABLE = False - ClientSession = None - +from mcp import ClientSession try: import aiohttp except ImportError: - aiohttp = None + raise ImportError("aiohttp is required for HTTP Stream transport. Install with: pip install praisonaiagents[mcp]") logger = logging.getLogger("mcp-http-stream") @@ -513,23 +507,9 @@ def __init__(self, server_url: str, debug: bool = False, timeout: int = 60, opti timeout: Timeout in seconds for operations (default: 60) options: Additional configuration options for the transport """ - # Check if MCP is available - if not MCP_AVAILABLE: - raise ImportError( - "MCP (Model Context Protocol) package is not installed. " - "Install it with: pip install praisonaiagents[mcp]" - ) - - # Check if aiohttp is available - if aiohttp is None: - raise ImportError( - "aiohttp is required for HTTP Stream transport. " - "Install it with: pip install praisonaiagents[mcp]" - ) - # Parse URL to extract base URL and endpoint parsed = urlparse(server_url) - + # If the URL already has a path, use it; otherwise use default /mcp endpoint if parsed.path and parsed.path != '/': self.base_url = server_url diff --git a/src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py b/src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py index 143a7bc11..f6d1f080d 100644 --- a/src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py +++ b/src/praisonai-agents/praisonaiagents/mcp/mcp_sse.py @@ -11,14 +11,8 @@ import json from typing import List, Dict, Any, Optional, Callable, Iterable -try: - from mcp import ClientSession - from mcp.client.sse import sse_client - MCP_AVAILABLE = True -except ImportError: - MCP_AVAILABLE = False - ClientSession = None - sse_client = None +from mcp import ClientSession +from mcp.client.sse import sse_client logger = logging.getLogger("mcp-sse") @@ -175,19 +169,12 @@ def __init__(self, server_url: str, debug: bool = False, timeout: int = 60): debug: Whether to enable debug logging timeout: Timeout in seconds for operations (default: 60) """ - # Check if MCP is available - if not MCP_AVAILABLE: - raise ImportError( - "MCP (Model Context Protocol) package is not installed. " - "Install it with: pip install praisonaiagents[mcp]" - ) - self.server_url = server_url self.debug = debug self.timeout = timeout self.session = None self.tools = [] - + # Set up logging if debug: logger.setLevel(logging.DEBUG) diff --git a/src/praisonai-agents/tests/test_mcp_optional_import.py b/src/praisonai-agents/tests/test_mcp_optional_import.py deleted file mode 100644 index 536111d80..000000000 --- a/src/praisonai-agents/tests/test_mcp_optional_import.py +++ /dev/null @@ -1,71 +0,0 @@ -""" -Test that MCP module imports are optional and don't break basic functionality. -This test verifies the fix for issue #1091. -""" -import sys -import pytest - - -class TestMCPOptionalImport: - """Test that MCP is properly optional.""" - - def test_praisonaiagents_import_without_mcp(self): - """Test that praisonaiagents can be imported without mcp installed.""" - # This should not raise any ImportError - import praisonaiagents - assert praisonaiagents is not None - - def test_agent_import_without_mcp(self): - """Test that Agent can be imported without mcp installed.""" - from praisonaiagents import Agent - assert Agent is not None - - def test_mcp_lazy_loading(self): - """Test that MCP is lazily loaded.""" - from praisonaiagents import MCP - # MCP should either be available or None (not raise ImportError on access) - # The actual ImportError should only be raised when trying to instantiate - # if mcp package is not installed - - def test_mcp_module_import_no_crash(self): - """Test that mcp module can be imported without crashing.""" - # This tests that the try/except in mcp.py works correctly - try: - from praisonaiagents.mcp import mcp as mcp_module - assert hasattr(mcp_module, 'MCP_AVAILABLE') - except ImportError: - # If import fails, it should be because of missing mcp, - # not because of a syntax error - pass - - def test_mcp_sse_module_import_no_crash(self): - """Test that mcp_sse module can be imported without crashing.""" - try: - from praisonaiagents.mcp import mcp_sse - assert hasattr(mcp_sse, 'MCP_AVAILABLE') - except ImportError: - pass - - def test_mcp_http_stream_module_import_no_crash(self): - """Test that mcp_http_stream module can be imported without crashing.""" - try: - from praisonaiagents.mcp import mcp_http_stream - assert hasattr(mcp_http_stream, 'MCP_AVAILABLE') - except ImportError: - pass - - def test_basic_agent_creation_without_mcp_tools(self): - """Test that agents can be created without MCP tools.""" - from praisonaiagents import Agent - - # Creating an agent without MCP tools should work - agent = Agent( - name="Test Agent", - instructions="You are a test agent.", - llm="gpt-4o-mini" - ) - assert agent is not None - - -if __name__ == "__main__": - pytest.main([__file__, "-v"])