run_python
Execute Python code or run scripts with command line arguments and virtual environment support, enabling code execution and testing within VS Code.
Instructions
Execute Python code or script
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | No | Python code to execute | |
| script_path | No | Path to Python script file | |
| args | No | Command line arguments | |
| venv | No | Path to virtual environment |
Implementation Reference
- The main handler function that executes Python code or scripts. Accepts code (inline string), script_path (file path), args (command-line arguments), and venv (virtual environment). Builds the command string using python3/python/py and delegates to executeWithStreaming.
async runPython(args: PythonExecutionArgs): Promise<ToolResult> { const { code, script_path, args: scriptArgs = [], venv } = args; if (!code && !script_path) { throw new Error('Either code or script_path must be provided'); } let command: string = ''; if (code) { // Check for Python availability const pythonCmd = venv ? `${venv}/bin/python3` : await this.getPythonCommand(); const escapedCode = code.replace(/"/g, '\\"').replace(/\$/g, '\\$'); command = `${pythonCmd} -c "${escapedCode}"`; if (scriptArgs.length > 0) { command += ` ${scriptArgs.join(' ')}`; } } else if (script_path) { const pythonCmd = venv ? `${venv}/bin/python3` : await this.getPythonCommand(); const fullPath = this.workspaceService.resolvePath(script_path); command = `${pythonCmd} ${this.quotePath(fullPath)} ${scriptArgs.join(' ')}`; } return this.executeWithStreaming(command, { cwd: this.getCurrentWorkspace() }); } - TypeScript interface for PythonExecutionArgs input schema with optional fields: code, script_path, args (string array), and venv.
export interface PythonExecutionArgs { code?: string; script_path?: string; args?: string[]; venv?: string; } - src/toolDefinitions.ts:143-156 (schema)Tool registration schema with name 'run_python', description, and inputSchema (object with code, script_path, args, venv properties) used for MCP tool listing.
// Code Execution { name: 'run_python', description: 'Execute Python code or script', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Python code to execute' }, script_path: { type: 'string', description: 'Path to Python script file' }, args: { type: 'array', items: { type: 'string' }, description: 'Command line arguments' }, venv: { type: 'string', description: 'Path to virtual environment' }, }, }, }, - src/index.ts:170-172 (registration)Case statement in executeToolCommand switch that routes 'run_python' tool calls to CodeExecutionService.runPython(args).
// Code Execution case 'run_python': return await this.codeExecutionService.runPython(args); - Helper that auto-detects which Python command (python3, python, or py) is available on the system by running --version.
private async getPythonCommand(): Promise<string> { const commands = ['python3', 'python', 'py']; for (const cmd of commands) { try { await this.executeWithStreaming(`${cmd} --version`, {}, 5000); return cmd; } catch { // Try next command } } throw new Error('Python not found. Please install Python 3.x'); }