write_file_content_to_sandbox
Writes content to files in Modal sandbox environments for prototyping, code generation, and file management in isolated Python workspaces.
Instructions
Writes content to a file in a Modal sandbox.
This is useful for writing code, text, or any other content to a file in the sandbox.
Parameters:
- sandbox_id: ID of the target sandbox where code will be written
- sandbox_path: Path where the code file should be created/written in the sandbox
- content: Content to write to the file
Returns a SandboxWriteCodeResponse containing:
- success: Boolean indicating if code was written successfully
- message: Descriptive message about the operation
- file_path: Path where code was written in sandbox
This tool is powerful for:
- Rapid prototyping and code generation
- Creating boilerplate code
- Implementing algorithms from descriptions
- Converting pseudocode to actual code
- Generating test cases
- Creating utility functions and helper code
The tool will:
1. Verify the sandbox is running
2. Write content to specified path in sandbox
3. Handle errors and provide detailed feedback
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sandbox_id | Yes | ||
| sandbox_path | Yes | ||
| content | Yes |
Implementation Reference
- The main handler function that implements the write_file_content_to_sandbox tool. It fetches the sandbox, checks if it's running, writes the content using a helper method, logs the action, and returns a success response.async def write_file_content_to_sandbox( self, sandbox_id:str, sandbox_path:str, content:str, ) -> SandboxWriteFileResponse: sandbox = await modal.Sandbox.from_id.aio(sandbox_id) sandbox_status = await sandbox.poll.aio() if sandbox_status is not None: raise ToolError(f"Sandbox {sandbox_id} is not running") # Write to sandbox using thread executor await self._write_sandbox_file_in_thread(sandbox, sandbox_path, content, "w") logger.info(f"Content written successfully to {sandbox_path}") return SandboxWriteFileResponse( success=True, message=f"Content written successfully to {sandbox_path}", file_path=sandbox_path, )
- src/mcp4modal_sandbox/backend/mcp_server.py:151-154 (registration)Registration of the write_file_content_to_sandbox tool in the FastMCP app, linking the name, description, and handler method.mcp_app.tool( name="write_file_content_to_sandbox", description=ToolDescriptions.WRITE_FILE_CONTENT_TO_SANDBOX, )(self.write_file_content_to_sandbox)
- Tool description string used for the write_file_content_to_sandbox tool, including parameter explanations and usage notes. Serves as the schema/documentation.WRITE_FILE_CONTENT_TO_SANDBOX = """ Writes content to a file in a Modal sandbox. This is useful for writing code, text, or any other content to a file in the sandbox. Parameters: - sandbox_id: ID of the target sandbox where code will be written - sandbox_path: Path where the code file should be created/written in the sandbox - content: Content to write to the file Returns a SandboxWriteCodeResponse containing: - success: Boolean indicating if code was written successfully - message: Descriptive message about the operation - file_path: Path where code was written in sandbox This tool is powerful for: - Rapid prototyping and code generation - Creating boilerplate code - Implementing algorithms from descriptions - Converting pseudocode to actual code - Generating test cases - Creating utility functions and helper code The tool will: 1. Verify the sandbox is running 2. Write content to specified path in sandbox 3. Handle errors and provide detailed feedback """
- Helper method to write file content to the sandbox asynchronously using a thread pool executor, called from the handler.async def _write_sandbox_file_in_thread(self, modal_sandbox, file_path: str, content, mode: str = 'wb'): def _sync_write(): with modal_sandbox.open(file_path, mode) as f: f.write(content) loop = asyncio.get_event_loop() return await loop.run_in_executor(self.thread_pool_executor, _sync_write)