Skip to main content
Glama
milkymap

MCP4Modal Sandbox

by milkymap

launch_sandbox

Launch isolated Python sandboxes with configurable dependencies, GPU support, and environment variables for testing, ML workloads, and secure code execution.

Instructions

        Launches a new Modal sandbox with specified configuration.
        
        Parameters:
        - python_version: Python version to use (default: "3.12")
        - pip_packages: List of pip packages to install
        - apt_packages: List of apt packages to install
        - timeout_seconds: Maximum runtime in seconds (default: 3600)
        - cpu: CPU cores allocated (default: 1.0)
        - memory: Memory in MB allocated (default: 1024)
        - secrets: Dictionary of environment variables to inject (creates new secret)
        - volumes: Dictionary of volumes to mount in sandbox, where the key is the path in the sandbox and the value is the name of the volume
        - workdir: Working directory in sandbox (default: "/")
        - gpu_type: Type of GPU to use (optional). Supported types:
          * T4: Entry-level GPU, good for inference
          * L4: Mid-range GPU, good for general ML tasks
          * A10G: High-performance GPU, good for training
          * A100-40GB: High-end GPU with 40GB memory
          * A100-80GB: High-end GPU with 80GB memory
          * L40S: Latest generation GPU, good for ML workloads
          * H100: Latest generation high-end GPU
          * H200: Latest generation flagship GPU
          * B200: Latest generation enterprise GPU
        - gpu_count: Number of GPUs to use (optional, default: 1)
          * A10G supports up to 4 GPUs
          * Other types support up to 8 GPUs
        
        Returns a SandboxLaunchResponse containing:
        - sandbox_id: Unique identifier for the sandbox
        - status: Current status of the sandbox
        - python_version: Python version installed
        - pip_packages: List of pip packages installed
        - apt_packages: List of apt packages installed
        - preloaded_secrets: List of predefined secrets injected from Modal dashboard
        
        This tool is useful for:
        - Creating isolated Python environments
        - Running code with specific dependencies
        - Testing in clean environments
        - Executing long-running tasks
        - Running GPU-accelerated workloads
        - Training machine learning models
        - Running inference on large models
        
        Secrets Management:
        - Use 'secrets' parameter to create new secrets with key-value pairs
        - Use 'inject_predefined_secrets' to reference existing secrets from Modal dashboard
        - Predefined secrets are applied after custom secrets, so they can override values
        - Access secrets as environment variables in your sandbox code using os.environ
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
python_versionNo3.12
pip_packagesNo
apt_packagesNo
timeout_secondsNo
cpuNo
memoryNo
secretsNo
volumesNo
workdirNo/home/solver
gpu_typeNo
gpu_countNo

Implementation Reference

  • The handler function that launches a new Modal sandbox. Builds a custom image with specified Python version, pip/apt packages, handles secrets and volumes, configures GPU if provided, creates the sandbox using modal.Sandbox.create.aio, and returns a SandboxLaunchResponse with sandbox_id and other details.
    async def launch_sandbox(
        self, 
        python_version: str = "3.12",
        pip_packages: List[str] = None,
        apt_packages: List[str] = None,
        timeout_seconds: int = 600,
        cpu: float = 2.0,
        memory: int = 4096,
        secrets: Dict[str, str] = None,
        volumes: Dict[str, str] = None,
        workdir: str = "/home/solver",
        gpu_type: Optional[GPUType] = None,
        gpu_count: Optional[int] = None,
    ) -> SandboxLaunchResponse:
        pip_packages = pip_packages or []
        apt_packages = apt_packages or []
        secrets = secrets or {}
        inject_predefined_secrets = self.preloaded_secrets or []
    
        # Build the image with Python version and dependencies
        image = modal.Image.debian_slim(python_version=python_version)
        
        # Install system dependencies
        if apt_packages:
            image = image.apt_install(*apt_packages)
        
        # Install Python packages
        if pip_packages:
            image = image.pip_install(*pip_packages)
        
        # Create secrets for environment variables (the proper Modal way)
        modal_secrets = []
        if secrets:
            secret = modal.Secret.from_dict(secrets)
            modal_secrets.append(secret)
        
        if inject_predefined_secrets:
            for secret_name in inject_predefined_secrets:
                secret = modal.Secret.from_name(secret_name)
                modal_secrets.append(secret)
        
        modal_volumes = {}
        if volumes:
            for volume_path, volume_name in volumes.items():
                modal_volumes[volume_path] = modal.Volume.from_name(volume_name, create_if_missing=True)
        
        # Configure GPU if specified
        gpu = None
        if gpu_type:
            if gpu_count:
                gpu = f"{gpu_type.value}:{gpu_count}"
            else:
                gpu = gpu_type.value
        
        # Get or create Modal app for the specified namespace
        app = modal.App.lookup(self.app_name, create_if_missing=True)
        
        # Create sandbox with Modal
        with modal.enable_output():
            logger.info(f"Creating sandbox with Python {python_version} in app '{self.app_name}'" + (f" and GPU {gpu}" if gpu else ""))
            sandbox = await modal.Sandbox.create.aio(
                "/bin/bash",
                image=image,
                app=app,
                timeout=timeout_seconds,
                cpu=cpu,
                memory=memory,
                secrets=modal_secrets,
                volumes=modal_volumes,
                workdir=workdir,
                gpu=gpu
            )
            
        # Get the Modal-assigned ID
        sandbox_id = sandbox.object_id
        
        logger.info(f"Launched sandbox {sandbox_id} with Python {python_version}")
        
        return SandboxLaunchResponse(
            sandbox_id=sandbox_id,
            status="running",
            python_version=python_version,
            pip_packages=pip_packages,
            apt_packages=apt_packages,
            preloaded_secrets=inject_predefined_secrets,
        )
  • Multiline string defining the tool description, parameters, return values, and usage instructions for the launch_sandbox tool schema.
    LAUNCH_SANDBOX = """
            Launches a new Modal sandbox with specified configuration.
            
            Parameters:
            - python_version: Python version to use (default: "3.12")
            - pip_packages: List of pip packages to install
            - apt_packages: List of apt packages to install
            - timeout_seconds: Maximum runtime in seconds (default: 3600)
            - cpu: CPU cores allocated (default: 1.0)
            - memory: Memory in MB allocated (default: 1024)
            - secrets: Dictionary of environment variables to inject (creates new secret)
            - volumes: Dictionary of volumes to mount in sandbox, where the key is the path in the sandbox and the value is the name of the volume
            - workdir: Working directory in sandbox (default: "/")
            - gpu_type: Type of GPU to use (optional). Supported types:
              * T4: Entry-level GPU, good for inference
              * L4: Mid-range GPU, good for general ML tasks
              * A10G: High-performance GPU, good for training
              * A100-40GB: High-end GPU with 40GB memory
              * A100-80GB: High-end GPU with 80GB memory
              * L40S: Latest generation GPU, good for ML workloads
              * H100: Latest generation high-end GPU
              * H200: Latest generation flagship GPU
              * B200: Latest generation enterprise GPU
            - gpu_count: Number of GPUs to use (optional, default: 1)
              * A10G supports up to 4 GPUs
              * Other types support up to 8 GPUs
            
            Returns a SandboxLaunchResponse containing:
            - sandbox_id: Unique identifier for the sandbox
            - status: Current status of the sandbox
            - python_version: Python version installed
            - pip_packages: List of pip packages installed
            - apt_packages: List of apt packages installed
            - preloaded_secrets: List of predefined secrets injected from Modal dashboard
            
            This tool is useful for:
            - Creating isolated Python environments
            - Running code with specific dependencies
            - Testing in clean environments
            - Executing long-running tasks
            - Running GPU-accelerated workloads
            - Training machine learning models
            - Running inference on large models
            
            Secrets Management:
            - Use 'secrets' parameter to create new secrets with key-value pairs
            - Use 'inject_predefined_secrets' to reference existing secrets from Modal dashboard
            - Predefined secrets are applied after custom secrets, so they can override values
            - Access secrets as environment variables in your sandbox code using os.environ
            """
  • Tool registration using fastmcp.tool decorator, specifying name and description, binding to the launch_sandbox handler method.
    mcp_app.tool(
        name="launch_sandbox",
        description=ToolDescriptions.LAUNCH_SANDBOX,
    )(self.launch_sandbox)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden and does well by disclosing behavioral traits: it creates new secrets, mentions runtime limits via timeout, describes GPU constraints, and explains secrets management. It doesn't cover rate limits or error conditions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately front-loaded with core purpose, but the 'This tool is useful for' section contains redundant items (e.g., 'Running GPU-accelerated workloads' and 'Training machine learning models' overlap). Some sentences could be consolidated for better efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (11 parameters, no annotations, no output schema), the description is quite complete. It explains parameters thoroughly, describes return values, and covers behavioral aspects. Minor gaps include lack of error handling or cost implications.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description fully compensates by providing detailed parameter explanations beyond just names. It adds meaning for all 11 parameters including defaults, GPU type descriptions with use cases, and constraints like GPU count limits.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('launches') and resource ('new Modal sandbox') with specific configuration. It distinguishes from siblings like 'list_sandboxes' (reads) and 'terminate_sandbox' (destroys) by focusing on creation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The 'This tool is useful for' section provides clear context about when to use it (isolated environments, specific dependencies, GPU workloads). However, it doesn't explicitly state when NOT to use it or mention alternatives like 'execute_command' for simpler tasks.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/milkymap/mcp4modal_sandbox'

If you have feedback or need assistance with the MCP directory API, please join our Discord server