Skip to main content
Glama
milkymap

MCP4Modal Sandbox

by milkymap

read_file_content_from_sandbox

Read file content from Modal sandboxes 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
NameRequiredDescriptionDefault
sandbox_idYes
pathYes

Implementation Reference

  • Main handler function: gets sandbox by ID, checks if running, reads file content using helper in thread executor, returns response with content.
    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
        )
  • Pydantic response schema defining the output: file content as string.
    class SandboxReadFileContentResponse(BaseModel):
        content: str
  • Tool registration in MCP app with name and description, 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)
  • Supporting helper: reads file synchronously in thread pool executor to handle Modal sandbox file I/O without blocking the async loop.
    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)

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