Spaces:
Runtime error
Runtime error
| #!/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") | |