Skip to main content
Glama
yzfly

MCP Python Interpreter

by yzfly

run_python_file

Execute Python files with specified environments, arguments, and timeout settings using subprocess execution.

Instructions

Execute a Python file (always uses subprocess for file execution).

Args:
    file_path: Path to the Python file to execute
    environment: Name of the Python environment to use
    arguments: List of command-line arguments to pass to the script
    timeout: Maximum execution time in seconds (default: 300)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
environmentNodefault
argumentsNo
timeoutNo

Implementation Reference

  • The @mcp.tool() decorated function that implements and registers the 'run_python_file' tool. It validates the file path, selects the Python environment, constructs the command with optional arguments, executes the file via subprocess, and formats the output including stdout/stderr and status.
    @mcp.tool()
    async def run_python_file(
        file_path: str,
        environment: str = "default",
        arguments: Optional[List[str]] = None,
        timeout: int = 300
    ) -> str:
        """
        Execute a Python file (always uses subprocess for file execution).
        
        Args:
            file_path: Path to the Python file to execute
            environment: Name of the Python environment to use
            arguments: List of command-line arguments to pass to the script
            timeout: Maximum execution time in seconds (default: 300)
        """
        path = Path(file_path)
        if path.is_absolute():
            if not is_path_allowed(path):
                return f"Access denied: Can only run files in working directory: {WORKING_DIR}"
        else:
            path = WORKING_DIR / path
        
        if not path.exists():
            return f"File '{path}' not found."
        
        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"], str(path)]
        if arguments:
            cmd.extend(arguments)
        
        result = await run_subprocess_async(cmd, cwd=str(WORKING_DIR), timeout=timeout)
        
        output = f"Execution of '{path}' in '{environment}' environment:\n\n"
        
        if result["status"] == 0:
            output += "--- Output ---\n"
            output += result["stdout"] if result["stdout"] else "(No output)\n"
        else:
            output += f"--- Error (status code: {result['status']}) ---\n"
            output += result["stderr"] if result["stderr"] else "(No error message)\n"
            
            if result["stdout"]:
                output += "\n--- Output ---\n"
                output += result["stdout"]
        
        return output

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