install_dependencies
Install Python packages in the Conda environment to enable code execution with required libraries and dependencies.
Instructions
Install Python dependencies in the conda environment
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"packages": "<packages>"
},
"name": "install_dependencies"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| packages | Yes | List of packages to install |
Implementation Reference
- src/index.ts:314-387 (handler)The core handler function that executes the installation of specified Python packages using the configured environment (conda, venv, or uv-venv). It constructs platform-specific shell commands, executes them, and returns success/error JSON responses.
async function installDependencies(packages: string[]) { try { if (!packages || packages.length === 0) { return { type: 'text', text: JSON.stringify({ status: 'error', error: 'No packages specified' }), isError: true }; } // Build the install command based on environment type let installCmd = ''; const packageList = packages.join(' '); switch (ENV_CONFIG.type) { case 'conda': if (!ENV_CONFIG.conda_name) { throw new Error("conda_name is required for conda environment"); } installCmd = `conda install -y -n ${ENV_CONFIG.conda_name} ${packageList}`; break; case 'venv': installCmd = `pip install ${packageList}`; break; case 'venv-uv': installCmd = `uv pip install ${packageList}`; break; default: throw new Error(`Unsupported environment type: ${ENV_CONFIG.type}`); } // Get platform-specific command const { command, options } = getPlatformSpecificCommand(installCmd); // Execute installation with unbuffered Python const { stdout, stderr } = await execAsync(command, { cwd: CODE_STORAGE_DIR, env: { ...process.env, PYTHONUNBUFFERED: '1' }, ...options }); const response = { status: 'success', env_type: ENV_CONFIG.type, installed_packages: packages, output: stdout, warnings: stderr || undefined }; return { type: 'text', text: JSON.stringify(response), isError: false }; } catch (error) { const response = { status: 'error', env_type: ENV_CONFIG.type, error: error instanceof Error ? error.message : String(error) }; return { type: 'text', text: JSON.stringify(response), isError: true }; } } - src/index.ts:618-633 (registration)Tool registration in the ListTools response, defining name, description, and input schema for the install_dependencies tool.
name: "install_dependencies", description: `Install Python dependencies in the ${ENV_CONFIG.type} environment`, inputSchema: { type: "object", properties: { packages: { type: "array", items: { type: "string" }, description: "List of packages to install" } }, required: ["packages"] } }, - src/index.ts:713-715 (schema)TypeScript interface defining the expected arguments for the install_dependencies tool handler.
interface InstallDependenciesArgs { packages?: string[]; } - src/index.ts:860-875 (registration)Dispatcher case in the central CallToolRequest handler that validates args and invokes the installDependencies function.
case "install_dependencies": { const args = request.params.arguments as InstallDependenciesArgs; if (!args?.packages || !Array.isArray(args.packages)) { throw new Error("Valid packages array is required"); } const result = await installDependencies(args.packages); return { content: [{ type: "text", text: result.text, isError: result.isError }] }; } - src/index.ts:59-108 (helper)Helper function used by installDependencies (and others) to generate platform-specific shell commands for environment activation.
function getPlatformSpecificCommand(pythonCommand: string): { command: string, options: ExecOptions } { const isWindows = platform() === 'win32'; let command = ''; let options: ExecOptions = {}; switch (ENV_CONFIG.type) { case 'conda': if (!ENV_CONFIG.conda_name) { throw new Error("conda_name is required for conda environment"); } if (isWindows) { command = `conda run -n ${ENV_CONFIG.conda_name} ${pythonCommand}`; options = { shell: 'cmd.exe' }; } else { command = `source $(conda info --base)/etc/profile.d/conda.sh && conda activate ${ENV_CONFIG.conda_name} && ${pythonCommand}`; options = { shell: '/bin/bash' }; } break; case 'venv': if (!ENV_CONFIG.venv_path) { throw new Error("venv_path is required for virtualenv"); } if (isWindows) { command = `${join(ENV_CONFIG.venv_path, 'Scripts', 'activate')} && ${pythonCommand}`; options = { shell: 'cmd.exe' }; } else { command = `source ${join(ENV_CONFIG.venv_path, 'bin', 'activate')} && ${pythonCommand}`; options = { shell: '/bin/bash' }; } break; case 'venv-uv': if (!ENV_CONFIG.uv_venv_path) { throw new Error("uv_venv_path is required for uv virtualenv"); } if (isWindows) { command = `${join(ENV_CONFIG.uv_venv_path, 'Scripts', 'activate')} && ${pythonCommand}`; options = { shell: 'cmd.exe' }; } else { command = `source ${join(ENV_CONFIG.uv_venv_path, 'bin', 'activate')} && ${pythonCommand}`; options = { shell: '/bin/bash' }; } break; default: throw new Error(`Unsupported environment type: ${ENV_CONFIG.type}`); } return { command, options };