install_package
Install or upgrade Python packages in specified environments using the MCP Python Interpreter. Manage dependencies efficiently by specifying the environment and upgrade options for each package.
Instructions
Install a Python package in the specified environment.
Args:
package_name: Name of the package to install
environment: Name of the Python environment (default if custom path provided, otherwise system)
upgrade: Whether to upgrade the package if already installed (default: False)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| environment | No | default | |
| package_name | Yes | ||
| upgrade | No |
Implementation Reference
- mcp_python_interpreter/server.py:684-720 (handler)The handler function for the 'install_package' MCP tool. It resolves the target Python environment, constructs a pip install command, executes it via subprocess, and returns success/error messages.@mcp.tool() async def install_package( package_name: str, environment: str = "default", upgrade: bool = False, timeout: int = 300 ) -> str: """ Install a Python package in the specified environment. Args: package_name: Name of the package to install environment: Name of the Python environment upgrade: Whether to upgrade if already installed timeout: Maximum execution time in seconds """ environments = get_python_environments() if environment == "default" and not any(e["name"] == "default" for e in environments): environment = "system" env = next((e for e in environments if e["name"] == environment), None) if not env: return f"Environment '{environment}' not found. Available: {', '.join(e['name'] for e in environments)}" cmd = [env["path"], "-m", "pip", "install"] if upgrade: cmd.append("--upgrade") cmd.append(package_name) result = await run_subprocess_async(cmd, timeout=timeout) if result["status"] == 0: return f"Successfully {'upgraded' if upgrade else 'installed'} {package_name} in {environment}." else: return f"Error installing {package_name}:\n{result['stderr']}"