read_file_content_from_sandbox
Read file contents from an isolated Modal.com sandbox environment to view, debug, or inspect files without downloading them.
Instructions
Reads the content of a file in the sandbox.
Parameters:
- sandbox_id: The unique identifier of the sandbox
- path: Path to the file to read
Returns a SandboxReadFileContentResponse containing:
- content: String content of the file
This tool is useful for:
- Viewing file contents without downloading
- Debugging sandbox operations
- Checking operation results
- Quick file inspection
The tool will:
1. Verify sandbox and file exist
2. Read file contents
3. Return file content as string
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sandbox_id | Yes | ||
| path | Yes |
Implementation Reference
- The main handler function that implements the tool logic: retrieves the Modal sandbox by ID, checks if running, reads the file content using a thread helper, logs the action, and returns the content in a response object.async def read_file_content_from_sandbox(self, sandbox_id: str, path: str) -> SandboxReadFileContentResponse: # Get sandbox from Modal using from_id modal_sandbox = await modal.Sandbox.from_id.aio(sandbox_id) # Check if sandbox is running before reading file sandbox_status = await modal_sandbox.poll.aio() if sandbox_status is not None: raise ToolError(f"Sandbox {sandbox_id} is not running") # Read from sandbox using thread executor content = await self._read_sandbox_file_in_thread(modal_sandbox, path, 'rb') logger.info(f"Read file content from {path} in sandbox {sandbox_id}") return SandboxReadFileContentResponse( content=content )
- src/mcp4modal_sandbox/backend/mcp_server.py:146-150 (registration)Registers the tool with the FastMCP app, specifying the name, description from ToolDescriptions, and binding the handler method.mcp_app.tool( name="read_file_content_from_sandbox", description=ToolDescriptions.READ_FILE_CONTENT_FROM_SANDBOX, )(self.read_file_content_from_sandbox)
- Pydantic model defining the output schema for the tool response, containing the file content as a string.class SandboxReadFileContentResponse(BaseModel): content: str
- Tool description string that defines the input parameters, output, and usage instructions for the MCP tool schema.READ_FILE_CONTENT_FROM_SANDBOX = """ Reads the content of a file in the sandbox. Parameters: - sandbox_id: The unique identifier of the sandbox - path: Path to the file to read Returns a SandboxReadFileContentResponse containing: - content: String content of the file This tool is useful for: - Viewing file contents without downloading - Debugging sandbox operations - Checking operation results - Quick file inspection The tool will: 1. Verify sandbox and file exist 2. Read file contents 3. Return file content as string """
- Helper method that runs file reading synchronously in a thread pool executor to avoid blocking the async event loop when using Modal Sandbox.open().async def _read_sandbox_file_in_thread(self, modal_sandbox, file_path: str, mode: str = 'rb'): def _sync_read(): with modal_sandbox.open(file_path, mode) as f: return f.read() loop = asyncio.get_event_loop() return await loop.run_in_executor(self.thread_pool_executor, _sync_read)