Skip to content
Back to search

code-sandbox-mcp

The Code Sandbox MCP Server is a lightweight, STDIO-based Model Context Protocol (MCP) Server, allowing AI assistants and LLM applications to safely execute code snippets using containerized…

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/philschmid--code-sandbox-mcp/badge.svg)](/listings/philschmid--code-sandbox-mcp)

HTML

<a href="/listings/philschmid--code-sandbox-mcp"><img src="/api/listings/philschmid--code-sandbox-mcp/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

pip install git+https://github.com/philschmid/code-sandbox-mcp.git

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 190 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # Code Sandbox MCP Server
  2. The Code Sandbox MCP Server is 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 to execute the code snippets.
  3. ![Code Sandbox MCP](./assets/code-sandbox-mcp.png)
  4. **How It Works:**
  5. 1. Starts a container session (podman, docker, etc.) and ensures the session is open.
  6. 2. Writes the `code` to a temporary file on the host.
  7. 3. Copies this temporary file into the container at the configured `workdir`.
  8. 4. Executes the language-specific commands to run the code, e.g. python `python3 -u code.py` or javascript `node -u code.js`
  9. 5. Captures the output and error streams from the container.
  10. 6. Returns the output and error streams to the client.
  11. 7. Stops and removes the container.
  12. **Available Tools:**
  13. - **run_python_code** - Executes a snippet of Python code in a secure, isolated sandbox.
  14. - `code` (string, required): The Python code to execute.
  15. - **run_js_code** - Executes a snippet of JavaScript (Node.js) code in a secure, isolated sandbox.
  16. - `code` (string, required): The JavaScript code to execute.
  17. ## Installation
  18. ```bash
  19. pip install git+https://github.com/philschmid/code-sandbox-mcp.git
  20. ```
  21. ## Getting Started: Usage with an MCP Client
  22. Examples:
  23. - [Local Client Python](./examples/test_local_client_python.py) example for running python code
  24. - [Gemini SDK](./examples/test_gemini.py) example for running python code with the Gemini SDK
  25. - [Calling Gemini from a client](./examples/test_client_gemini_call.py) example for running python code that uses the Gemini SDK and passes through the Gemini API key
  26. - [Local Client Javascript](./examples/test_local_client_js.py) example for running javascript code
  27. To use the Code Sandbox MCP server, you need to add it to your MCP client's configuration file (e.g., in your AI assistant's settings). The server is designed to be launched on-demand by the client.
  28. Add the following to your `mcpServers` configuration:
  29. ```json
  30. {
  31. "mcpServers": {
  32. "code-sandbox": {
  33. "command": "code-sandbox-mcp",
  34. }
  35. }
  36. }
  37. ```
  38. ### Provide Secrets and pass through environment variables
  39. You can pass through environment variables to the sandbox by setting the `--pass-through-env` flag when starting the MCP server and providing the env when starting the server
  40. ```json
  41. {
  42. "mcpServers": {
  43. "code-sandbox": {
  44. "command": "code-sandbox-mcp",
  45. "args": ["--pass-through-env", "API_KEY,SECRET_TOKEN"]
  46. "env": {
  47. "API_KEY": "1234567890",
  48. "SECRET_TOKEN": "1234567890"
  49. }
  50. }
  51. }
  52. }
  53. ```
  54. ### Provide a custom container image
  55. You can provide a custom container image by setting the `CONTAINER_IMAGE` and `CONTAINER_LANGUAGE` environment variables when starting the MCP server. Both variables are required as the `CONTAINER_LANGUAGE` is used to determine the commands to run in the container and the `CONTAINER_IMAGE` is used to determine the image to use.
  56. Note: When providing a custom container image both tools will use the same container image.
  57. ```json
  58. {
  59. "mcpServers": {
  60. "code-sandbox": {
  61. "command": "code-sandbox-mcp",
  62. "env": {
  63. "CONTAINER_IMAGE": "your-own-image",
  64. "CONTAINER_LANGUAGE": "python" # or "javascript"
  65. }
  66. }
  67. }
  68. }
  69. ```
  70. ### Use with Gemini SDK
  71. The `code-sandbox-mcp` server can be used with the Gemini SDK by passing the `tools` parameter to the `generate_content` method.
  72. ```python
  73. from fastmcp import Client
  74. from google import genai
  75. import asyncio
  76. mcp_client = Client(
  77. {
  78. "local_server": {
  79. "transport": "stdio",
  80. "command": "code-sandbox-mcp",
  81. }
  82. }
  83. )
  84. gemini_client = genai.Client()
  85. async def main():
  86. async with mcp_client:
  87. response = await gemini_client.aio.models.generate_content(
  88. model="gemini-2.5-flash",
  89. contents="Use Python to ping the google.com website and return the response time.",
  90. config=genai.types.GenerateContentConfig(
  91. temperature=0,
  92. tools=[mcp_client.session], # Pass the FastMCP client session
  93. ),
  94. )
  95. print(response.text)
  96. if __name__ == "__main__":
  97. asyncio.run(main())
  98. ```
  99. ### Use with Gemini CLI
  100. The `code-sandbox-mcp` server can be used with the Gemini CLI. You can configure MCP servers at the global level in the `~/.gemini/settings.json` file or in your project's root directory, create or open the `.gemini/settings.json` file. Within the file, add the mcpServers configuration block.
  101. ![Gemini CLI Settings](./assets/gemini-cli.png)
  102. See [settings.json](.gemini/settings.json) for an example and read more about the [Gemini CLI](https://github.com/google-gemini/gemini-cli/blob/main/docs/tools/mcp-server.md)
  103. ```json
  104. {
  105. "mcpServers": {
  106. "code-sandbox": {
  107. "command": "code-sandbox-mcp",
  108. }
  109. }
  110. }
  111. ```
  112. ## Customize/Build new Container Images
  113. The repository comes with 2 container images, which are published on Docker Hub:
  114. - `philschmi/code-sandbox-python:latest`
  115. - `philschmi/code-sandbox-js:latest`
  116. ```bash
  117. docker build -t philschmi/code-sandbox-python:latest -f containers/Dockerfile.python .
  118. docker build -t philschmi/code-sandbox-js:latest -f containers/Dockerfile.nodejs .
  119. ```
  120. The script will build the image using the current user's account. To update the images you want to use you can either pass the --python-image or --js-image flags when starting the MCP server or update the [const.py](./src//code_sandbox_mcp/const.py) file.
  121. To push the images to Docker Hub you need to retag the images to your own account and push them.
  122. ```bash
  123. docker tag philschmi/code-sandbox-python:latest <your-account>/code-sandbox-python:latest
  124. docker push <your-account>/code-sandbox-python:latest
  125. ```
  126. To customize or install additional dependencies you can add them to the [Dockerfile](./Dockerfile) and build the image again.
  127. ## Testing
  128. ### With MCP Inspector
  129. Start the server with streamable-http and test your server using the MCP inspector. Alternatively start inspector and run the server with stdio.
  130. ```bash
  131. npx @modelcontextprotocol/inspector
  132. ```
  133. To run the test suite for `code-sandbox-mcp` and its components, clone the repository and run:
  134. ```bash
  135. # You may need to install development dependencies first
  136. pip install -e ".[dev]"
  137. # Run the tests
  138. pytest tests/
  139. ```
  140. ## License
  141. Code Sandbox MCP Server is open source software licensed under the [MIT License](https://github.com/vndee/llm-sandbox/blob/main/LICENSE).

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.