terminate_sandbox
Stop a running Modal sandbox by its ID to free up resources and manage environment lifecycle.
Instructions
Terminates a Modal sandbox by its ID.
Parameters:
- sandbox_id: The unique identifier of the sandbox to terminate
Returns a SandboxTerminateResponse containing:
- success: Boolean indicating if termination was successful
- message: Detailed message about the termination result
This tool is useful for:
- Stopping running sandboxes that are no longer needed
- Cleaning up resources
- Forcefully ending long-running or stuck sandboxes
- Managing sandbox lifecycle
The tool will:
1. Check if the sandbox exists and is running
2. Send termination signal if running
3. Wait for confirmation of termination
4. Return status of the operation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sandbox_id | Yes |
Implementation Reference
- The core handler function that terminates a Modal sandbox by ID. It checks status, terminates, waits for completion, and returns a response.async def terminate_sandbox(self, sandbox_id: str) -> SandboxTerminateResponse: # Get sandbox from Modal using from_id modal_sandbox = await modal.Sandbox.from_id.aio(sandbox_id) # Check if sandbox is running before terminating sandbox_status = await modal_sandbox.poll.aio() # Terminate the Modal sandbox if sandbox_status is not None: return SandboxTerminateResponse( success=False, message=f"Sandbox {sandbox_id} is not running" ) await modal_sandbox.terminate.aio() # Wait for termination await modal_sandbox.wait.aio(raise_on_termination=False) logger.info(f"Terminated sandbox {sandbox_id}") return SandboxTerminateResponse( success=True, message=f"Sandbox {sandbox_id} terminated successfully" )
- src/mcp4modal_sandbox/backend/mcp_server.py:106-109 (registration)Registers the 'terminate_sandbox' tool with the FastMCP server instance.mcp_app.tool( name="terminate_sandbox", description=ToolDescriptions.TERMINATE_SANDBOX, )(self.terminate_sandbox)
- Pydantic BaseModel defining the output schema for the terminate_sandbox tool response.class SandboxTerminateResponse(BaseModel): success: bool message: str
- Detailed tool description string including parameters and usage, used during registration.TERMINATE_SANDBOX = """ Terminates a Modal sandbox by its ID. Parameters: - sandbox_id: The unique identifier of the sandbox to terminate Returns a SandboxTerminateResponse containing: - success: Boolean indicating if termination was successful - message: Detailed message about the termination result This tool is useful for: - Stopping running sandboxes that are no longer needed - Cleaning up resources - Forcefully ending long-running or stuck sandboxes - Managing sandbox lifecycle The tool will: 1. Check if the sandbox exists and is running 2. Send termination signal if running 3. Wait for confirmation of termination 4. Return status of the operation """