install_package
Install Python packages in a persistent REPL environment using the MCP protocol. Specify the package name to enable Python runtime customization and execution.
Instructions
Install a Python package using uv
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| package | Yes | Package name to install (e.g., 'pandas') |
Implementation Reference
- src/mcp_python/server.py:143-199 (handler)Handler logic for the 'install_package' tool. Validates the package name, installs it using 'uv pip install', imports it into the global namespace, and returns success or error messages.elif name == "install_package": package = arguments.get("package") if not package: raise ValueError("Missing package name") # Basic package name validation if not re.match("^[A-Za-z0-9][A-Za-z0-9._-]*$", package): return [ types.TextContent( type="text", text=f"Invalid package name: {package}" ) ] try: # Install package using uv process = subprocess.run( ["uv", "pip", "install", package], capture_output=True, text=True, check=True ) if process.returncode != 0: return [ types.TextContent( type="text", text=f"Failed to install package: {process.stderr}" ) ] # Import the package to make it available in the REPL try: exec(f"import {package.split('[')[0]}", self.global_namespace) return [ types.TextContent( type="text", text=f"Successfully installed and imported {package}" ) ] except ImportError as e: return [ types.TextContent( type="text", text=f"Package installed but import failed: {str(e)}" ) ] except subprocess.CalledProcessError as e: return [ types.TextContent( type="text", text=f"Failed to install package:\n{e.stderr}" ) ] elif name == "list_variables":
- src/mcp_python/server.py:59-72 (registration)Registers the 'install_package' tool in the list_tools handler, including its name, description, and input schema requiring a 'package' string.types.Tool( name="install_package", description="Install a Python package using uv", inputSchema={ "type": "object", "properties": { "package": { "type": "string", "description": "Package name to install (e.g., 'pandas')", } }, "required": ["package"], }, )
- src/mcp_python/server.py:62-71 (schema)Input schema definition for the 'install_package' tool, specifying an object with a required 'package' property of type string.inputSchema={ "type": "object", "properties": { "package": { "type": "string", "description": "Package name to install (e.g., 'pandas')", } }, "required": ["package"], },