---
title: Let agents run code
description: Use the sandbox MCP to let a Python agent run code, with a complete LangGraph example and a reference for existing code interpreter integrations.
canonical_url: https://docs.databricks.com/gcp/en/agents/custom-agents/code-interpreter-tools
---

# Let agents run code

> [!NOTE]
> **Beta**
>
> `system.ai.dbsql`, `system.ai.sandbox`, and `system.ai.web_search` are in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md). To use them, an account admin must enable the **Enhanced Unity Gateway** preview from the account console **Previews** page. Unity Gateway itself is generally available. See [Manage account previews](https://docs.databricks.com/gcp/en/admin/workspace-settings/manage-previews.md#account).

Use the `system.ai.sandbox` MCP to let your agent run Python or shell (`sh`) code in an isolated environment. To run SQL, use [`system.ai.dbsql`](https://docs.databricks.com/gcp/en/agents/mcp-tools/databricks-sql.md). This example connects a LangGraph agent to the MCP and asks it to run a calculation in Python.

## <a id="requirements" />Before you start

* Use Python 3.12 and [sign in to your workspace](https://docs.databricks.com/gcp/en/agents/custom-agents/use-mcp-tools.md#set-up-your-environment).
* Make sure your identity has [access to the MCP](https://docs.databricks.com/gcp/en/ai-gateway/govern-mcp-service.md#control-callers).
* Choose a [model that supports tool calling](https://docs.databricks.com/gcp/en/machine-learning/model-serving/function-calling.md). The example uses `databricks-claude-sonnet-4-5`; replace it with a model endpoint available in your workspace.

## <a id="sandbox-mcp" />Run code with the sandbox MCP

Create a virtual environment and install the libraries:

```bash
python3.12 -m venv .venv
source .venv/bin/activate
pip install --upgrade databricks-langchain langchain langgraph "mcp>=1.24,<2"
```

Save the following as `code_agent.py`. `WorkspaceClient` uses your workspace sign-in to authenticate the MCP connection.

```python
import asyncio
from databricks.sdk import WorkspaceClient
from databricks_langchain import (
    ChatDatabricks,
    DatabricksMCPServer,
    DatabricksMultiServerMCPClient,
)
from langchain.agents import create_agent

workspace_client = WorkspaceClient()
server_url = f"{workspace_client.config.host}/ai-gateway/mcp-services/system.ai.sandbox"

async def main():
    mcp_client = DatabricksMultiServerMCPClient([
        DatabricksMCPServer(
            name="code-interpreter",
            url=server_url,
            workspace_client=workspace_client,
        ),
    ])
    agent = create_agent(
        model=ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
        tools=await mcp_client.get_tools(),
    )
    result = await agent.ainvoke({
        "messages": [{
            "role": "user",
            "content": "Run Python code to calculate the mean of [12, 18, 24].",
        }],
    })
    print(result["messages"][-1].content)

asyncio.run(main())
```

Run the agent:

```bash
python code_agent.py
```

The agent discovers the MCP's tools, runs Python code, and returns a mean of `18`. Network access from the sandbox isn't guaranteed and can depend on your workspace configuration, so don't rely on code that downloads packages or calls external APIs.

The MCP provides `create_sandbox`, `run_code`, and `delete_sandbox` tools. When your agent calls `run_code` without a `sandbox_id`, the MCP creates a new sandbox and returns its ID. Pass that `sandbox_id` in later calls to keep Python variables and files between calls. Shell commands share the sandbox's files but not its Python variables. To avoid leaving sandboxes running, have your agent call `delete_sandbox` when it's done.

For other frameworks, use this `server_url` with the [Python agent examples](https://docs.databricks.com/gcp/en/agents/custom-agents/use-mcp-tools.md#build-an-agent). For deployment, see [Build a custom agent](https://docs.databricks.com/gcp/en/agents/custom-agents/author-agent.md).

## <a id="add-code-interpreter-agent" />Use the Python function through MCP (legacy)

For an existing integration with the `system.ai.python_exec` function, use the agent example above with this URL instead:

```python
server_url = f"{workspace_client.config.host}/api/2.0/mcp/functions/system/ai/python_exec"
```

This endpoint requires the **Managed MCP Servers** workspace preview and access to the underlying function. It uses [workspace endpoint permissions](https://docs.databricks.com/gcp/en/agents/mcp-tools/built-in-mcp-services.md#legacy-workspace-endpoints). For agent deployment and function access, see [Agent authentication](https://docs.databricks.com/gcp/en/agents/custom-agents/agent-authentication.md).

## <a id="additional-resources" />Additional resources

* [Databricks-provided MCPs](https://docs.databricks.com/gcp/en/agents/mcp-tools/built-in-mcp-services.md).
* [Create tools with Unity Catalog functions](https://docs.databricks.com/gcp/en/agents/custom-agents/create-custom-tool.md).
