Skip to main content
Glama
yzfly

MCP Python Interpreter

by yzfly

install_package

Install Python packages in specified environments to add functionality, with options to upgrade existing packages and set execution time limits.

Instructions

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
package_nameYes
environmentNodefault
upgradeNo
timeoutNo

Implementation Reference

  • The handler function implementing the 'install_package' MCP tool. It uses pip to install or upgrade the specified package in the chosen Python environment via subprocess execution. Includes input validation for environments and error handling.
    @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']}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yzfly/mcp-python-interpreter'

If you have feedback or need assistance with the MCP directory API, please join our Discord server