#!/usr/bin/env python3 """ MCP Import Debugging Utility. This script checks and debugs MCP package imports to help troubleshoot import-related issues with the MCP client libraries. Purpose: Troubleshooting import issues Usage: Diagnostic utility To run this script: pdm run python usage/debug_imports.py Dependencies: - mcp (install with: pdm add mcp) This utility helps identify which MCP components are available and which import paths work correctly. """ def check_mcp_imports(): """Check various import paths for MCP components.""" print("šŸ” MCP Package Import Debugging") print("=" * 40) # Try different import paths import_attempts = [ # Basic imports ("mcp", "import mcp"), ("mcp.ClientSession", "from mcp import ClientSession"), ("mcp.StdioServerParameters", "from mcp import StdioServerParameters"), # Try mcp.types ("mcp.types", "import mcp.types"), ( "mcp.types.StdioServerParameters", "from mcp.types import StdioServerParameters", ), # Try mcp.client ("mcp.client.stdio", "from mcp.client.stdio import stdio_client"), # Try other possible locations ("mcp.server", "from mcp.server import Server"), ("mcp.client", "from mcp.client import ClientSession"), # Try smolagents ("smolagents", "import smolagents"), ("smolagents.mcp_client", "from smolagents.mcp_client import MCPClient"), ] successful_imports = [] failed_imports = [] for module_name, import_statement in import_attempts: try: exec(import_statement) successful_imports.append((module_name, import_statement)) print(f"āœ… {import_statement}") except ImportError as e: failed_imports.append((module_name, import_statement, str(e))) print(f"āŒ {import_statement}") print(f" Error: {e}") print("\nšŸ“Š Results:") print(f" āœ… Successful: {len(successful_imports)}") print(f" āŒ Failed: {len(failed_imports)}") # Try to inspect what's actually in the mcp module print("\nšŸ” Exploring mcp module contents:") try: import mcp print(f" mcp module location: {mcp.__file__}") print(f" mcp module attributes: {dir(mcp)}") # Check for StdioServerParameters in different locations if hasattr(mcp, "StdioServerParameters"): print(" āœ… Found StdioServerParameters in mcp") else: print(" āŒ StdioServerParameters not found in mcp") # Check mcp.types if it exists try: import mcp.types print(f" mcp.types attributes: {dir(mcp.types)}") except ImportError: print(" āŒ mcp.types not available") # Check mcp.client if it exists try: import mcp.client print(f" mcp.client attributes: {dir(mcp.client)}") except ImportError: print(" āŒ mcp.client not available") except ImportError as e: print(f" āŒ Can't import mcp at all: {e}") # Check smolagents if available print("\nšŸ” Exploring smolagents module:") try: import smolagents print(f" smolagents module location: {smolagents.__file__}") print(f" smolagents version: {getattr(smolagents, '__version__', 'unknown')}") try: import smolagents.mcp_client print(" āœ… smolagents.mcp_client is available") except ImportError: print(" āŒ smolagents.mcp_client not available") except ImportError as e: print(f" āŒ Can't import smolagents: {e}") print("\nšŸ’” Recommendations:") if len(successful_imports) > 0: print(" Some MCP imports work - check successful ones above") else: print(" No MCP imports working - install with: pdm add mcp") # Check for smolagents specifically smolagents_works = any("smolagents" in imp[0] for imp in successful_imports) if smolagents_works: print(" āœ… smolagents is working - use sentiment_mcp.py") else: print(" Install smolagents with: pdm add 'smolagents[mcp]'") return successful_imports, failed_imports if __name__ == "__main__": print("šŸš€ MCP Import Debugging Utility") print("This tool helps diagnose MCP package import issues.") print("=" * 50) check_mcp_imports() print("\nšŸ“‹ Next Steps:") print("1. If MCP imports fail: pdm add mcp") print("2. If smolagents imports fail: pdm add 'smolagents[mcp]'") print("3. Try running: pdm run python usage/sentiment_mcp.py") print("4. Backup option: pdm run python usage/sentiment_gradio.py")