Skip to main content
Glama
milkymap

MCP4Modal Sandbox

by milkymap

execute_command

Execute shell commands in isolated Modal sandbox environments to run scripts, test programs, and debug with captured output and timing results.

Instructions

        Executes a command in a specified Modal sandbox environment.
        
        Parameters:
        - sandbox_id: The unique identifier of the sandbox to run the command in
        - command: The shell command to execute (e.g. "python script.py", "ls -la", etc.)
        - working_dir: Optional working directory to execute the command from
        - timeout: Optional timeout in seconds for command execution
        
        Returns a SandboxExecuteResponse containing:
        - stdout: Standard output from the command execution
        - stderr: Standard error output from the command execution  
        - returncode: Exit code of the command (0 typically indicates success)
        - execution_time: Time taken to execute the command in seconds
        
        This tool is useful for:
        - Running arbitrary commands in isolated sandbox environments
        - Testing scripts and programs in clean environments
        - Executing programs with specific dependencies
        - Debugging environment-specific issues
        - Running automated tests in isolation
        
        The tool will:
        1. Verify the sandbox exists and is running
        2. Execute the specified command in that sandbox
        3. Capture all output and timing information
        4. Return detailed execution results
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sandbox_idYes
commandYes
timeout_secondsNo

Implementation Reference

  • The handler function for the 'execute_command' tool. It retrieves the Modal sandbox by ID, checks if it's running, executes the provided command using modal_sandbox.exec.aio, waits for completion, captures stdout, stderr, returncode, and execution time, then returns a SandboxExecuteResponse.
    async def execute_command(
        self,
        sandbox_id: str, 
        command: List[str],
        timeout_seconds: int = 30
    ) -> SandboxExecuteResponse:
        # Get sandbox from Modal using from_id
        modal_sandbox = await modal.Sandbox.from_id.aio(sandbox_id)
        
        # Check if sandbox is running before executing command
        sandbox_status = await modal_sandbox.poll.aio()
        if sandbox_status is not None:
            raise ToolError(f"Sandbox {sandbox_id} is not running")
        
        start_time = time()
        
        result = await modal_sandbox.exec.aio(*command, timeout=timeout_seconds)
        await result.wait.aio()
        
        execution_time = time() - start_time
        # Get output from the sandbox
        stdout = result.stdout.read() if result.stdout else ""
        stderr = result.stderr.read() if result.stderr else ""
        returncode = result.returncode
        
        logger.info(f"Executed command in sandbox {sandbox_id}: {' '.join(command)}")
        
        return SandboxExecuteResponse(
            stdout=stdout,
            stderr=stderr,
            returncode=returncode,
            execution_time=execution_time
        )
  • The registration of the 'execute_command' tool in the FastMCP app using mcp_app.tool, linking the name, description from ToolDescriptions, and the handler self.execute_command.
    mcp_app.tool(
        name="execute_command",
        description=ToolDescriptions.EXECUTE_COMMAND,
    )(self.execute_command)
  • The tool description string for 'execute_command', used in registration, which serves as the schema documentation for inputs and outputs.
    EXECUTE_COMMAND = """
            Executes a command in a specified Modal sandbox environment.
            
            Parameters:
            - sandbox_id: The unique identifier of the sandbox to run the command in
            - command: The shell command to execute (e.g. "python script.py", "ls -la", etc.)
            - working_dir: Optional working directory to execute the command from
            - timeout: Optional timeout in seconds for command execution
            
            Returns a SandboxExecuteResponse containing:
            - stdout: Standard output from the command execution
            - stderr: Standard error output from the command execution  
            - returncode: Exit code of the command (0 typically indicates success)
            - execution_time: Time taken to execute the command in seconds
            
            This tool is useful for:
            - Running arbitrary commands in isolated sandbox environments
            - Testing scripts and programs in clean environments
            - Executing programs with specific dependencies
            - Debugging environment-specific issues
            - Running automated tests in isolation
            
            The tool will:
            1. Verify the sandbox exists and is running
            2. Execute the specified command in that sandbox
            3. Capture all output and timing information
            4. Return detailed execution results
            """
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 detailing behavioral traits: it verifies sandbox existence/running status, executes commands, captures output/timing, and returns specific results (stdout, stderr, etc.). It also implies isolation and potential timeouts, though it could mention error handling or security constraints more explicitly.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded, starting with the core purpose. However, it includes some redundancy (e.g., listing return values in detail after stating 'Returns a SandboxExecuteResponse') and could be more streamlined by merging the 'useful for' and 'The tool will' sections into a single usage overview.

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 tool's complexity (command execution in sandboxes), no annotations, and no output schema, the description is mostly complete: it explains purpose, parameters, return values, and usage scenarios. It could improve by addressing potential errors (e.g., what if sandbox isn't running) or linking to sibling tools for sandbox management, but overall it provides sufficient context for an agent to use the tool correctly.

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?

Schema description coverage is 0%, so the description must compensate fully. It adds significant meaning beyond the schema: explains sandbox_id as 'unique identifier of the sandbox,' command as 'shell command to execute' with examples, working_dir as 'optional working directory' (though not in schema, adding clarification), and timeout as 'optional timeout in seconds.' This covers all parameters effectively.

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 tool 'executes a command in a specified Modal sandbox environment,' using specific verbs ('executes,' 'run') and resources ('command,' 'sandbox'). It distinguishes from siblings like launch_sandbox (creates sandbox) or list_directory_contents (reads files) by focusing on command execution within an existing sandbox.

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 description provides clear context for when to use this tool ('useful for running arbitrary commands in isolated sandbox environments, testing scripts, debugging, etc.'), but does not explicitly state when NOT to use it or name alternatives among siblings (e.g., use list_directory_contents for file listing instead of 'ls' via this tool).

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