{
  "@context": "https://schema.org",
  "@id": "https://aggr.aymericbeaumet.com/items/philschmid-de/2025-07-22-code-sandbox-mcp-a-simple-code-interpreter-for-your-ai/#webpage",
  "@type": [
    "WebPage",
    "ArchiveComponent"
  ],
  "articleBody": "July 22, 2025 4 minute read [View Code](https://github.com/philschmid/code-sandbox-mcp)\n\nCode agents are transforming software development. But how do we safely let them execute code? Code sandboxes have evolved from basic security tools into essential development infrastructure.\n\nToday, I'm launching **[Code Sandbox MCP](https://github.com/philschmid/code-sandbox-mcp)**, a lightweight, STDIO-based Model Context Protocol (MCP) Server, allowing AI assistants and LLM applications to safely execute code snippets using containerized environments. It is uses the [llm-sandbox](https://github.com/vndee/llm-sandbox) package for the containerization and execution of the code snippets.\n\nIt exposes `run_python_code` and `run_javascript_code` tools, giving your AI agent the ability to execute code on your own infrastructure.\n\n## How it works\n\n1. Starts a container session (podman, docker, etc.) and ensures the session is open.\n2. Writes the code to a temporary file on the host.\n3. Copies this temporary file into the container at the configured workdir.\n4. Executes the language-specific commands to run the code, e.g. python `python3 -u code.py` or javascript `node -u code.js`\n5. Captures the output and error streams from the container.\n6. Returns the output and error streams to the client.\n\n![Code Sandbox MCP](https://www.philschmid.de/static/blog/code-sandbox-mcp/code-sandbox-mcp.png)\n\nThis entire workflow happens on your machine, locally or on your own server, keeping your data and code private.\n\n## Get Started with the Gemini SDK\n\nIntegrating Code Sandbox MCP with your Python application using the Gemini SDK is incredibly straightforward. First, install the package:\n\nBash\n\n```bash\npip install git+https://github.com/philschmid/code-sandbox-mcp.git\n```\n\nThen, you can use `fastmcp` to connect your Gemini client to the local code execution tool:\n\nPython\n\n```python\nfrom fastmcp import Client\nfrom google import genai\nimport asyncio\n \n# Configure the MCP client to use the local server\nmcp_client = Client(\n    {\n        \"local_server\": {\n            \"transport\": \"stdio\",\n            \"command\": \"code-sandbox-mcp\",\n        }\n    }\n)\ngemini_client = genai.Client()\n \nasync def main():\n    async with mcp_client:\n        response = await gemini_client.aio.models.generate_content(\n            model=\"gemini-1.5-flash\",\n            contents=\"Use Python to ping google.com and return the response.\",\n            config=genai.types.GenerateContentConfig(\n                tools=[mcp_client.session],  # Pass the FastMCP client session\n            ),\n        )\n        # The model's response will include the output from the code execution\n        print(response.text)\n \nif __name__ == \"__main__\":\n    asyncio.run(main())\n \n```\n\n## Empower the Gemini CLI\n\nYou can also supercharge the Gemini CLI with code execution capabilities. Simply add the server configuration to your `~/.gemini/settings.json` file:\n\nJSON\n\n```json\n{\n  \"mcpServers\": {\n    \"code-sandbox\": {\n      \"command\": \"code-sandbox-mcp\"\n    }\n  }\n}\n \n```\n\nNow, when you use the Gemini CLI, it can automatically discover and use the `run_python_code` tool to answer your questions!\n\n## Security\n\nBy leveraging `llm-sandbox`, Code Sandbox MCP benefits from multiple layers of security:\n\n1. **Container Isolation**: All code runs inside a sandboxed container (Docker, Podman, etc.), completely isolated from the host system's filesystem and processes.\n2. **Resource Limits**: The underlying sandbox can be configured with strict memory, CPU, and execution time limits to prevent resource exhaustion.\n3. **Network Controls**: You can define network policies to restrict or completely block outbound network access from the container.\n4. **Pre-execution Analysis**: The underlying `llm-sandbox` framework supports security policies that can analyze code for dangerous patterns (e.g., `os.system`, file system access) before it even runs.\n\n## Why Build This?\n\nI built Code Sandbox MCP primarily for my own use. When working with the Gemini CLI or other coding agents, I constantly struggled with testing code snippets in an isolated environment. Especially when you hand over more responsbility to the agent.\n\nWhile managed solutions like [Daytona](https://www.daytona.io/) and [E2B](https://e2b.dev/) are excellent for scaling or building customer facing agents, they didn't fit my specific needs. In my use cases, I wanted to give agents a way to execute code in an environment I can control and don't have to pay for.\n\nI mean:\n\n- **Having the libraries I need installed**: Unlike integrated code execution tools that come with limited packages, I can customize my environment with any dependencies.\n- **Access to secrets and credentials**: I can safely provide API keys or other sensitive information.\n- **Local code access**: Agents can work with my actual codebase, files, and local resources. I export files.\n- **No unnecessary costs**: There's simply no need to pay for a cloud solution when executing small code snippets for personal development work.\n\n* * *\n\nThanks for reading! If you have any questions or feedback, please let me know on [Twitter](https://twitter.com/_philschmid) or [LinkedIn](https://www.linkedin.com/in/philipp-schmid-a6a2bb196/).\n",
  "authors": [],
  "captured_at": "2026-09-27T19:29:15.293681927+00:00",
  "category": "programming",
  "content_html": "<p>July 22, 2025 4 minute read <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/philschmid/code-sandbox-mcp\">View Code</a></p>\n<p>Code agents are transforming software development. But how do we safely let them execute code? Code sandboxes have evolved from basic security tools into essential development infrastructure.</p>\n<p>Today, I'm launching <strong><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/philschmid/code-sandbox-mcp\">Code Sandbox MCP</a></strong>, a lightweight, STDIO-based Model Context Protocol (MCP) Server, allowing AI assistants and LLM applications to safely execute code snippets using containerized environments. It is uses the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/vndee/llm-sandbox\">llm-sandbox</a> package for the containerization and execution of the code snippets.</p>\n<p>It exposes <code>run_python_code</code> and <code>run_javascript_code</code> tools, giving your AI agent the ability to execute code on your own infrastructure.</p>\n<h2>How it works</h2>\n<ol>\n<li>Starts a container session (podman, docker, etc.) and ensures the session is open.</li>\n<li>Writes the code to a temporary file on the host.</li>\n<li>Copies this temporary file into the container at the configured workdir.</li>\n<li>Executes the language-specific commands to run the code, e.g. python <code>python3 -u code.py</code> or javascript <code>node -u code.js</code></li>\n<li>Captures the output and error streams from the container.</li>\n<li>Returns the output and error streams to the client.</li>\n</ol>\n<p><picture class=\"article-picture article-picture-fallback\" style=\"--image-ratio:16 / 9\"><img src=\"https://www.philschmid.de/static/blog/code-sandbox-mcp/code-sandbox-mcp.png\" alt=\"Code Sandbox MCP\" loading=\"eager\" decoding=\"async\" fetchpriority=\"high\" referrerpolicy=\"no-referrer\" class=\"progressive-image\"></picture></p>\n<p>This entire workflow happens on your machine, locally or on your own server, keeping your data and code private.</p>\n<h2>Get Started with the Gemini SDK</h2>\n<p>Integrating Code Sandbox MCP with your Python application using the Gemini SDK is incredibly straightforward. First, install the package:</p>\n<p>Bash</p>\n<pre><code class=\"language-bash\"><span class=\"code-snippet\" data-language=\"Shell\"><span class=\"syntax-source syntax-shell syntax-bash\"><span class=\"syntax-meta syntax-function-call syntax-shell\"><span class=\"syntax-variable syntax-function syntax-shell\">pip</span></span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-shell\"> install git+https://github.com/philschmid/code-sandbox-mcp.git</span>\n</span></span></code></pre>\n<p>Then, you can use <code>fastmcp</code> to connect your Gemini client to the local code execution tool:</p>\n<p>Python</p>\n<pre><code class=\"language-python\"><span class=\"code-snippet\" data-language=\"Python\"><span class=\"syntax-source syntax-python\"><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-keyword syntax-control syntax-import syntax-from syntax-python\">from</span></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-meta syntax-import-source syntax-python\"> <span class=\"syntax-meta syntax-import-path syntax-python\"><span class=\"syntax-meta syntax-import-name syntax-python\">fastmcp</span></span> <span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-keyword syntax-control syntax-import syntax-python\">import</span></span></span></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"> <span class=\"syntax-meta syntax-generic-name syntax-python\">Client</span></span>\n<span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-keyword syntax-control syntax-import syntax-from syntax-python\">from</span></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-meta syntax-import-source syntax-python\"> <span class=\"syntax-meta syntax-import-path syntax-python\"><span class=\"syntax-meta syntax-import-name syntax-python\">google</span></span> <span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-keyword syntax-control syntax-import syntax-python\">import</span></span></span></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"></span><span class=\"syntax-meta syntax-statement syntax-import syntax-python\"> <span class=\"syntax-meta syntax-generic-name syntax-python\">genai</span></span>\n<span class=\"syntax-meta syntax-statement syntax-import syntax-python\"><span class=\"syntax-keyword syntax-control syntax-import syntax-python\">import</span> <span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">asyncio</span></span></span>\n \n<span class=\"syntax-comment syntax-line syntax-number-sign syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-python\">#</span> Configure the MCP client to use the local server\n</span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">mcp_client</span></span> <span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span> <span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">Client</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-python\">\n    <span class=\"syntax-meta syntax-structure syntax-dictionary-or-set syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-dictionary-or-set syntax-begin syntax-python\">{</span>\n        <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">local_server<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-key-value syntax-python\">:</span> <span class=\"syntax-meta syntax-structure syntax-dictionary-or-set syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-dictionary-or-set syntax-begin syntax-python\">{</span>\n            <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">transport<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-key-value syntax-python\">:</span> <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">stdio<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-dictionary-or-set syntax-python\">,</span>\n            <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">command<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-key-value syntax-python\">:</span> <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">code-sandbox-mcp<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-dictionary-or-set syntax-python\">,</span>\n        <span class=\"syntax-punctuation syntax-section syntax-dictionary-or-set syntax-end syntax-python\">}</span></span>\n    <span class=\"syntax-punctuation syntax-section syntax-dictionary-or-set syntax-end syntax-python\">}</span></span>\n</span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span>\n<span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">gemini_client</span></span> <span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span> <span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">genai</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span></span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">Client</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span>\n \n<span class=\"syntax-meta syntax-function syntax-python\"><span class=\"syntax-storage syntax-modifier syntax-async syntax-python\">async</span> <span class=\"syntax-storage syntax-type syntax-function syntax-python\">def</span> <span class=\"syntax-entity syntax-name syntax-function syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">main</span></span></span><span class=\"syntax-meta syntax-function syntax-parameters syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-python\">(</span></span><span class=\"syntax-meta syntax-function syntax-parameters syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-python\">)</span></span><span class=\"syntax-meta syntax-function syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-function syntax-begin syntax-python\">:</span></span>\n    <span class=\"syntax-meta syntax-statement syntax-with syntax-python\"><span class=\"syntax-storage syntax-modifier syntax-async syntax-python\">async </span><span class=\"syntax-keyword syntax-control syntax-flow syntax-with syntax-python\">with</span> <span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">mcp_client</span></span><span class=\"syntax-punctuation syntax-section syntax-block syntax-with syntax-python\">:</span></span>\n        <span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">response</span></span> <span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span> <span class=\"syntax-keyword syntax-other syntax-await syntax-python\">await</span> <span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">gemini_client</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span><span class=\"syntax-meta syntax-generic-name syntax-python\">aio</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span><span class=\"syntax-meta syntax-generic-name syntax-python\">models</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span></span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">generate_content</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-python\">\n            <span class=\"syntax-variable syntax-parameter syntax-python\">model</span><span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">gemini-1.5-flash<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-arguments syntax-python\">,</span>\n            <span class=\"syntax-variable syntax-parameter syntax-python\">contents</span><span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">Use Python to ping google.com and return the response.<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-separator syntax-arguments syntax-python\">,</span>\n            <span class=\"syntax-variable syntax-parameter syntax-python\">config</span><span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span><span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">genai</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span><span class=\"syntax-meta syntax-generic-name syntax-python\">types</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span></span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">GenerateContentConfig</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-python\">\n                <span class=\"syntax-variable syntax-parameter syntax-python\">tools</span><span class=\"syntax-keyword syntax-operator syntax-assignment syntax-python\">=</span><span class=\"syntax-meta syntax-structure syntax-list syntax-python\"><span class=\"syntax-punctuation syntax-section syntax-list syntax-begin syntax-python\">[</span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">mcp_client</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span><span class=\"syntax-meta syntax-generic-name syntax-python\">session</span></span><span class=\"syntax-punctuation syntax-section syntax-list syntax-end syntax-python\">]</span></span><span class=\"syntax-punctuation syntax-separator syntax-arguments syntax-python\">,</span>  <span class=\"syntax-comment syntax-line syntax-number-sign syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-python\">#</span> Pass the FastMCP client session\n</span>            </span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-arguments syntax-python\">,</span>\n        </span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span>\n        <span class=\"syntax-comment syntax-line syntax-number-sign syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-python\">#</span> The model&#39;s response will include the output from the code execution\n</span>        <span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-support syntax-function syntax-builtin syntax-python\">print</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">response</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span><span class=\"syntax-meta syntax-generic-name syntax-python\">text</span></span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span>\n \n<span class=\"syntax-meta syntax-statement syntax-if syntax-python\"><span class=\"syntax-keyword syntax-control syntax-flow syntax-conditional syntax-python\">if</span> <span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-support syntax-variable syntax-magic syntax-python\">__name__</span></span> <span class=\"syntax-keyword syntax-operator syntax-comparison syntax-python\">==</span> <span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-python\">&quot;</span></span></span><span class=\"syntax-meta syntax-string syntax-python\"><span class=\"syntax-string syntax-quoted syntax-double syntax-python\">__main__<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-python\">&quot;</span></span></span><span class=\"syntax-punctuation syntax-section syntax-block syntax-conditional syntax-python\">:</span></span>\n    <span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-meta syntax-generic-name syntax-python\">asyncio</span><span class=\"syntax-punctuation syntax-accessor syntax-dot syntax-python\">.</span></span><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">run</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-python\"><span class=\"syntax-meta syntax-function-call syntax-python\"><span class=\"syntax-meta syntax-qualified-name syntax-python\"><span class=\"syntax-variable syntax-function syntax-python\">main</span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-begin syntax-python\">(</span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span></span><span class=\"syntax-punctuation syntax-section syntax-arguments syntax-end syntax-python\">)</span></span>\n \n</span></span></code></pre>\n<h2>Empower the Gemini CLI</h2>\n<p>You can also supercharge the Gemini CLI with code execution capabilities. Simply add the server configuration to your <code>~/.gemini/settings.json</code> file:</p>\n<p>JSON</p>\n<pre><code class=\"language-json\"><span class=\"code-snippet\" data-language=\"JSON\"><span class=\"syntax-source syntax-json\"><span class=\"syntax-meta syntax-structure syntax-dictionary syntax-json\"><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-begin syntax-json\">{</span>\n  <span class=\"syntax-meta syntax-structure syntax-dictionary syntax-key syntax-json\"><span class=\"syntax-string syntax-quoted syntax-double syntax-json\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-json\">&quot;</span>mcpServers<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-json\">&quot;</span></span></span><span class=\"syntax-meta syntax-structure syntax-dictionary syntax-value syntax-json\"><span class=\"syntax-punctuation syntax-separator syntax-dictionary syntax-key-value syntax-json\">:</span> <span class=\"syntax-meta syntax-structure syntax-dictionary syntax-json\"><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-begin syntax-json\">{</span>\n    <span class=\"syntax-meta syntax-structure syntax-dictionary syntax-key syntax-json\"><span class=\"syntax-string syntax-quoted syntax-double syntax-json\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-json\">&quot;</span>code-sandbox<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-json\">&quot;</span></span></span><span class=\"syntax-meta syntax-structure syntax-dictionary syntax-value syntax-json\"><span class=\"syntax-punctuation syntax-separator syntax-dictionary syntax-key-value syntax-json\">:</span> <span class=\"syntax-meta syntax-structure syntax-dictionary syntax-json\"><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-begin syntax-json\">{</span>\n      <span class=\"syntax-meta syntax-structure syntax-dictionary syntax-key syntax-json\"><span class=\"syntax-string syntax-quoted syntax-double syntax-json\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-json\">&quot;</span>command<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-json\">&quot;</span></span></span><span class=\"syntax-meta syntax-structure syntax-dictionary syntax-value syntax-json\"><span class=\"syntax-punctuation syntax-separator syntax-dictionary syntax-key-value syntax-json\">:</span> <span class=\"syntax-string syntax-quoted syntax-double syntax-json\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-json\">&quot;</span>code-sandbox-mcp<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-json\">&quot;</span></span>\n    </span><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-end syntax-json\">}</span></span>\n  </span><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-end syntax-json\">}</span></span>\n</span><span class=\"syntax-punctuation syntax-section syntax-dictionary syntax-end syntax-json\">}</span></span>\n \n</span></span></code></pre>\n<p>Now, when you use the Gemini CLI, it can automatically discover and use the <code>run_python_code</code> tool to answer your questions!</p>\n<h2>Security</h2>\n<p>By leveraging <code>llm-sandbox</code>, Code Sandbox MCP benefits from multiple layers of security:</p>\n<ol>\n<li><strong>Container Isolation</strong>: All code runs inside a sandboxed container (Docker, Podman, etc.), completely isolated from the host system's filesystem and processes.</li>\n<li><strong>Resource Limits</strong>: The underlying sandbox can be configured with strict memory, CPU, and execution time limits to prevent resource exhaustion.</li>\n<li><strong>Network Controls</strong>: You can define network policies to restrict or completely block outbound network access from the container.</li>\n<li><strong>Pre-execution Analysis</strong>: The underlying <code>llm-sandbox</code> framework supports security policies that can analyze code for dangerous patterns (e.g., <code>os.system</code>, file system access) before it even runs.</li>\n</ol>\n<h2>Why Build This?</h2>\n<p>I built Code Sandbox MCP primarily for my own use. When working with the Gemini CLI or other coding agents, I constantly struggled with testing code snippets in an isolated environment. Especially when you hand over more responsbility to the agent.</p>\n<p>While managed solutions like <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://www.daytona.io/\">Daytona</a> and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://e2b.dev/\">E2B</a> are excellent for scaling or building customer facing agents, they didn't fit my specific needs. In my use cases, I wanted to give agents a way to execute code in an environment I can control and don't have to pay for.</p>\n<p>I mean:</p>\n<ul>\n<li><strong>Having the libraries I need installed</strong>: Unlike integrated code execution tools that come with limited packages, I can customize my environment with any dependencies.</li>\n<li><strong>Access to secrets and credentials</strong>: I can safely provide API keys or other sensitive information.</li>\n<li><strong>Local code access</strong>: Agents can work with my actual codebase, files, and local resources. I export files.</li>\n<li><strong>No unnecessary costs</strong>: There's simply no need to pay for a cloud solution when executing small code snippets for personal development work.</li>\n</ul>\n<hr />\n<p>Thanks for reading! If you have any questions or feedback, please let me know on <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://twitter.com/_philschmid\">Twitter</a> or <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://www.linkedin.com/in/philipp-schmid-a6a2bb196/\">LinkedIn</a>.</p>\n",
  "content_markdown": "July 22, 2025 4 minute read [View Code](https://github.com/philschmid/code-sandbox-mcp)\n\nCode agents are transforming software development. But how do we safely let them execute code? Code sandboxes have evolved from basic security tools into essential development infrastructure.\n\nToday, I'm launching **[Code Sandbox MCP](https://github.com/philschmid/code-sandbox-mcp)**, a lightweight, STDIO-based Model Context Protocol (MCP) Server, allowing AI assistants and LLM applications to safely execute code snippets using containerized environments. It is uses the [llm-sandbox](https://github.com/vndee/llm-sandbox) package for the containerization and execution of the code snippets.\n\nIt exposes `run_python_code` and `run_javascript_code` tools, giving your AI agent the ability to execute code on your own infrastructure.\n\n## How it works\n\n1. Starts a container session (podman, docker, etc.) and ensures the session is open.\n2. Writes the code to a temporary file on the host.\n3. Copies this temporary file into the container at the configured workdir.\n4. Executes the language-specific commands to run the code, e.g. python `python3 -u code.py` or javascript `node -u code.js`\n5. Captures the output and error streams from the container.\n6. Returns the output and error streams to the client.\n\n![Code Sandbox MCP](https://www.philschmid.de/static/blog/code-sandbox-mcp/code-sandbox-mcp.png)\n\nThis entire workflow happens on your machine, locally or on your own server, keeping your data and code private.\n\n## Get Started with the Gemini SDK\n\nIntegrating Code Sandbox MCP with your Python application using the Gemini SDK is incredibly straightforward. First, install the package:\n\nBash\n\n```bash\npip install git+https://github.com/philschmid/code-sandbox-mcp.git\n```\n\nThen, you can use `fastmcp` to connect your Gemini client to the local code execution tool:\n\nPython\n\n```python\nfrom fastmcp import Client\nfrom google import genai\nimport asyncio\n \n# Configure the MCP client to use the local server\nmcp_client = Client(\n    {\n        \"local_server\": {\n            \"transport\": \"stdio\",\n            \"command\": \"code-sandbox-mcp\",\n        }\n    }\n)\ngemini_client = genai.Client()\n \nasync def main():\n    async with mcp_client:\n        response = await gemini_client.aio.models.generate_content(\n            model=\"gemini-1.5-flash\",\n            contents=\"Use Python to ping google.com and return the response.\",\n            config=genai.types.GenerateContentConfig(\n                tools=[mcp_client.session],  # Pass the FastMCP client session\n            ),\n        )\n        # The model's response will include the output from the code execution\n        print(response.text)\n \nif __name__ == \"__main__\":\n    asyncio.run(main())\n \n```\n\n## Empower the Gemini CLI\n\nYou can also supercharge the Gemini CLI with code execution capabilities. Simply add the server configuration to your `~/.gemini/settings.json` file:\n\nJSON\n\n```json\n{\n  \"mcpServers\": {\n    \"code-sandbox\": {\n      \"command\": \"code-sandbox-mcp\"\n    }\n  }\n}\n \n```\n\nNow, when you use the Gemini CLI, it can automatically discover and use the `run_python_code` tool to answer your questions!\n\n## Security\n\nBy leveraging `llm-sandbox`, Code Sandbox MCP benefits from multiple layers of security:\n\n1. **Container Isolation**: All code runs inside a sandboxed container (Docker, Podman, etc.), completely isolated from the host system's filesystem and processes.\n2. **Resource Limits**: The underlying sandbox can be configured with strict memory, CPU, and execution time limits to prevent resource exhaustion.\n3. **Network Controls**: You can define network policies to restrict or completely block outbound network access from the container.\n4. **Pre-execution Analysis**: The underlying `llm-sandbox` framework supports security policies that can analyze code for dangerous patterns (e.g., `os.system`, file system access) before it even runs.\n\n## Why Build This?\n\nI built Code Sandbox MCP primarily for my own use. When working with the Gemini CLI or other coding agents, I constantly struggled with testing code snippets in an isolated environment. Especially when you hand over more responsbility to the agent.\n\nWhile managed solutions like [Daytona](https://www.daytona.io/) and [E2B](https://e2b.dev/) are excellent for scaling or building customer facing agents, they didn't fit my specific needs. In my use cases, I wanted to give agents a way to execute code in an environment I can control and don't have to pay for.\n\nI mean:\n\n- **Having the libraries I need installed**: Unlike integrated code execution tools that come with limited packages, I can customize my environment with any dependencies.\n- **Access to secrets and credentials**: I can safely provide API keys or other sensitive information.\n- **Local code access**: Agents can work with my actual codebase, files, and local resources. I export files.\n- **No unnecessary costs**: There's simply no need to pay for a cloud solution when executing small code snippets for personal development work.\n\n* * *\n\nThanks for reading! If you have any questions or feedback, please let me know on [Twitter](https://twitter.com/_philschmid) or [LinkedIn](https://www.linkedin.com/in/philipp-schmid-a6a2bb196/).\n",
  "dateCreated": "2026-09-27T19:29:15.293681927+00:00",
  "date_created": "2025-07-22T00:00:00+00:00",
  "date_modified": "2025-07-22T00:00:00+00:00",
  "date_published": "2025-07-22T00:00:00+00:00",
  "description": "Code Sandbox MCP is a simple, self-hosted code interpreter for your AI agents. It allows you to execute code snippets in containerized environments.",
  "external_url": "https://www.philschmid.de/code-sandbox-mcp",
  "git": {
    "commit": "ebb0941c5c06a2ab2377342d607535262a3e969e",
    "path": "items/philschmid-de/2025/07/2025-07-22-code-sandbox-mcp-a-simple-code-interpreter-for-your-ai.md",
    "permalink": "https://github.com/aymericbeaumet/aggr-instance/blob/ebb0941c5c06a2ab2377342d607535262a3e969e/items/philschmid-de/2025/07/2025-07-22-code-sandbox-mcp-a-simple-code-interpreter-for-your-ai.md"
  },
  "id": "urn:aggr:item:9d58da1a47535b20a07bb33b6ad139c77f97f156",
  "isBasedOn": {
    "@id": "https://www.philschmid.de/code-sandbox-mcp"
  },
  "mainEntity": {
    "@id": "https://www.philschmid.de/code-sandbox-mcp",
    "@type": "CreativeWork",
    "archivedAt": {
      "@id": "https://aggr.aymericbeaumet.com/items/philschmid-de/2025-07-22-code-sandbox-mcp-a-simple-code-interpreter-for-your-ai/#webpage"
    },
    "dateModified": "2025-07-22T00:00:00+00:00",
    "datePublished": "2025-07-22T00:00:00+00:00",
    "headline": "Code Sandbox MCP: A Simple Code Interpreter for Your AI Agents",
    "timeRequired": "PT3M",
    "url": "https://www.philschmid.de/code-sandbox-mcp",
    "wordCount": 667
  },
  "name": "Archived snapshot: Code Sandbox MCP: A Simple Code Interpreter for Your AI Agents",
  "preview": null,
  "reading_minutes": 3,
  "replicated_at": null,
  "source": "philschmid-de",
  "source_name": "philschmid.de - RSS feed",
  "summary": "Code Sandbox MCP is a simple, self-hosted code interpreter for your AI agents. It allows you to execute code snippets in containerized environments.",
  "tags": [],
  "temporalCoverage": "2026-09-27T19:29:15.293681927+00:00",
  "title": "Code Sandbox MCP: A Simple Code Interpreter for Your AI Agents",
  "url": "https://aggr.aymericbeaumet.com/items/philschmid-de/2025-07-22-code-sandbox-mcp-a-simple-code-interpreter-for-your-ai/",
  "word_count": 667
}