list_python_environments
Discover available Python environments including system Python and conda environments to manage and select appropriate interpreters for code execution.
Instructions
List all available Python environments (system Python and conda environments).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- mcp_python_interpreter/server.py:467-481 (handler)The primary handler function for the 'list_python_environments' tool. Decorated with @mcp.tool(), it calls get_python_environments() to retrieve the list of environments and formats them into a human-readable string output.@mcp.tool() def list_python_environments() -> str: """List all available Python environments (system Python and conda environments).""" environments = get_python_environments() if not environments: return "No Python environments found." result = "Available Python Environments:\n\n" for env in environments: result += f"- Name: {env['name']}\n" result += f" Path: {env['path']}\n" result += f" Version: Python {env['version']}\n\n" return result
- Supporting helper function get_python_environments() that discovers available Python environments (system, default, and conda) by running subprocess commands to gather paths and versions.def get_python_environments() -> List[Dict[str, str]]: """Get all available Python environments.""" environments = [] if DEFAULT_PYTHON_PATH != sys.executable: try: result = subprocess.run( [DEFAULT_PYTHON_PATH, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')"], capture_output=True, text=True, check=True, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) version = result.stdout.strip() environments.append({ "name": "default", "path": DEFAULT_PYTHON_PATH, "version": version }) except Exception as e: print(f"Error getting version for custom Python path: {e}", file=sys.stderr) environments.append({ "name": "system", "path": sys.executable, "version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}" }) # Try conda environments try: result = subprocess.run( ["conda", "info", "--envs", "--json"], capture_output=True, text=True, check=False, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) if result.returncode == 0: conda_info = json.loads(result.stdout) for env in conda_info.get("envs", []): env_name = os.path.basename(env) if env_name == "base": env_name = "conda-base" python_path = os.path.join(env, "bin", "python") if not os.path.exists(python_path): python_path = os.path.join(env, "python.exe") if os.path.exists(python_path): try: version_result = subprocess.run( [python_path, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')"], capture_output=True, text=True, check=True, timeout=10, stdin=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 ) version = version_result.stdout.strip() environments.append({ "name": env_name, "path": python_path, "version": version }) except Exception: pass except Exception as e: print(f"Error getting conda environments: {e}", file=sys.stderr) return environments
- mcp_python_interpreter/server.py:467-467 (registration)The @mcp.tool() decorator on the handler function serves as the tool registration in the MCP framework.@mcp.tool()