check_installed_packages
Verify package installation in a Conda environment to ensure dependencies are available for Python code execution.
Instructions
Check if packages are installed in the conda environment
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| packages | Yes | List of packages to check |
Implementation Reference
- src/index.ts:392-511 (handler)Core handler function that executes the tool logic: creates a temporary Python script to attempt importing each package, collects installation status, version, and location, executes it in the configured environment, and returns formatted results.async function checkPackageInstallation(packages: string[]) { try { if (!packages || packages.length === 0) { return { type: 'text', text: JSON.stringify({ status: 'error', error: 'No packages specified' }), isError: true }; } // Create a temporary Python script to check packages const tempId = randomBytes(4).toString('hex'); // CODE_STORAGE_DIR is validated at the start of the program, so it's safe to use here const checkScriptPath = join(CODE_STORAGE_DIR, `check_packages_${tempId}.py`); // This script will attempt to import each package and return the results const checkScript = ` import importlib.util import json import sys results = {} for package in ${JSON.stringify(packages)}: try: # Try to find the spec spec = importlib.util.find_spec(package) if spec is None: # Package not found results[package] = { "installed": False, "error": "Package not found" } continue # Try to import the package module = importlib.import_module(package) # Get version if available version = getattr(module, "__version__", None) if version is None: version = getattr(module, "version", None) results[package] = { "installed": True, "version": version, "location": getattr(module, "__file__", None) } except ImportError as e: results[package] = { "installed": False, "error": str(e) } except Exception as e: results[package] = { "installed": False, "error": f"Unexpected error: {str(e)}" } print(json.dumps(results)) `; await writeFile(checkScriptPath, checkScript, 'utf-8'); // Execute the check script with unbuffered output const pythonCmd = platform() === 'win32' ? `python -u "${checkScriptPath}"` : `python3 -u "${checkScriptPath}"`; const { command, options } = getPlatformSpecificCommand(pythonCmd); const { stdout, stderr } = await execAsync(command, { cwd: CODE_STORAGE_DIR, env: { ...process.env, PYTHONUNBUFFERED: '1' }, ...options }); if (stderr) { return { type: 'text', text: JSON.stringify({ status: 'error', error: stderr }), isError: true }; } // Parse the package information const packageInfo = JSON.parse(stdout.trim()); // Add summary information to make it easier to use const allInstalled = Object.values(packageInfo).every((info: any) => info.installed); const notInstalled = Object.entries(packageInfo) .filter(([_, info]: [string, any]) => !info.installed) .map(([name, _]: [string, any]) => name); return { type: 'text', text: JSON.stringify({ status: 'success', env_type: ENV_CONFIG.type, all_installed: allInstalled, not_installed: notInstalled, package_details: packageInfo }), isError: false }; } catch (error) { return { type: 'text', text: JSON.stringify({ status: 'error', env_type: ENV_CONFIG.type, error: error instanceof Error ? error.message : String(error) }), isError: true }; } }
- src/index.ts:634-650 (registration)Registers the 'check_installed_packages' tool in the MCP server's listTools response, specifying name, description, and input schema.{ name: "check_installed_packages", description: `Check if packages are installed in the ${ENV_CONFIG.type} environment`, inputSchema: { type: "object", properties: { packages: { type: "array", items: { type: "string" }, description: "List of packages to check" } }, required: ["packages"] } },
- src/index.ts:717-719 (schema)TypeScript interface defining the input arguments for the check_installed_packages tool.interface CheckInstalledPackagesArgs { packages?: string[]; }
- src/index.ts:877-892 (handler)Dispatcher case in the main CallToolRequest handler that validates input and invokes the checkPackageInstallation function.case "check_installed_packages": { const args = request.params.arguments as CheckInstalledPackagesArgs; if (!args?.packages || !Array.isArray(args.packages)) { throw new Error("Valid packages array is required"); } const result = await checkPackageInstallation(args.packages); return { content: [{ type: "text", text: result.text, isError: result.isError }] }; }