# CodeInterpreterTool

## Description
This tool is used to give the Agent the ability to run code (Python3) from the code generated by the Agent itself. The code is executed in a Docker container for secure isolation.

It is incredibly useful since it allows the Agent to generate code, run it in an isolated environment, get the result and use it to make decisions.

## ⚠️ Security Requirements

**Docker is REQUIRED** for safe code execution. The tool will refuse to execute code without Docker to prevent security vulnerabilities.

### Why Docker is Required

Previous versions included a "restricted sandbox" fallback when Docker was unavailable. This has been **removed** due to critical security vulnerabilities:

- The Python-based sandbox could be escaped via object introspection
- Attackers could recover the original `__import__` function and access any module
- This allowed arbitrary command execution on the host system

**Docker provides real process isolation** and is the only secure way to execute untrusted code.

## Requirements

- **Docker (REQUIRED)** - Install from [docker.com](https://docs.docker.com/get-docker/)

## Installation
Install the crewai_tools package
```shell
pip install 'crewai[tools]'
```

## Example

Remember that when using this tool, the code must be generated by the Agent itself. The code must be Python3 code. It will take some time the first time to run because it needs to build the Docker image.

### Basic Usage (Docker Container - Recommended)

```python
from crewai_tools import CodeInterpreterTool

Agent(
    ...
    tools=[CodeInterpreterTool()],
)
```

### Custom Dockerfile

If you need to pass your own Dockerfile:

```python
from crewai_tools import CodeInterpreterTool

Agent(
    ...
    tools=[CodeInterpreterTool(user_dockerfile_path="<Dockerfile_path>")],
)
```

### Manual Docker Host Configuration

If it is difficult to connect to the Docker daemon automatically (especially for macOS users), you can set up the Docker host manually:

```python 
from crewai_tools import CodeInterpreterTool

Agent(
    ...
    tools=[CodeInterpreterTool(
        user_docker_base_url="<Docker Host Base Url>",
        user_dockerfile_path="<Dockerfile_path>"
    )],
)
```

### Unsafe Mode (NOT RECOMMENDED)

If you absolutely cannot use Docker and **fully trust the code source**, you can use unsafe mode:

```python
from crewai_tools import CodeInterpreterTool

# WARNING: Only use with fully trusted code!
Agent(
    ...
    tools=[CodeInterpreterTool(unsafe_mode=True)],
)
```

**⚠️ SECURITY WARNING:** `unsafe_mode=True` executes code directly on the host without any isolation. Only use this if:
- You completely trust the code being executed
- You understand the security risks
- You cannot install Docker in your environment

For production use, **always use Docker** (the default mode).
