Skip to main content

Python code execution sandbox based on IPython and Docker

Project description

ipybox

Website PyPI - Version GitHub Release GitHub Actions Workflow Status GitHub License PyPI - Python Version

ipybox is a lightweight, stateful and secure Python code execution sandbox built with IPython and Docker. Designed for AI agents that interact with their environment through code execution, like freeact agents, it is also well-suited for general-purpose code execution. ipybox is fully open-source and free to use, distributed under the Apache 2.0 license.

logo

Features

  • Secure Execution: Executes code in Docker container locally or remotely
  • Stateful Execution: Maintains state across code executions using IPython kernels
  • Output Streaming: Provides immediate feedback through direct output streaming
  • Plotting Support: Enables downloading of plots created with visualization libraries
  • MCP Support: Generate Python functions from MCP tools and use them during code execution
  • Dependency Management: Supports package installation during runtime or at build time
  • Resource Management: Context manager based container and IPython kernel lifecycle management
  • Reproducible Environments: Ensures consistent execution environments across different systems

Find out more in the user guide.

Quickstart

Install ipybox Python package:

pip install ipybox

Execute Python code inside ipybox:

import asyncio
from ipybox import ExecutionClient, ExecutionContainer

async def main():
    async with ExecutionContainer(tag="ghcr.io/gradion-ai/ipybox:minimal") as container:
        async with ExecutionClient(port=container.executor_port) as client:
            result = await client.execute("print('Hello, world!')")
            print(f"Output: {result.text}")

if __name__ == "__main__":
    asyncio.run(main())

MCP Support

ipybox also supports the generation of Python functions from MCP tools. When called, they execute the corresponding tools on the MCP server.

import asyncio

from ipybox import ExecutionClient, ExecutionContainer, ResourceClient


async def main():
    server_params = {
        "command": "uvx",
        "args": ["mcp-server-fetch"],
    }

    async with ExecutionContainer(tag="ghcr.io/gradion-ai/ipybox:minimal") as container:
        async with ResourceClient(port=container.resource_port) as client:
            # generate Python functions from MCP server metadata
            generate_result = await client.generate_mcp_sources(
                relpath="mcpgen",
                server_name="fetchurl",
                server_params=server_params,
            )
            # tool names provided by MCP server
            assert generate_result == ["fetch"]

            # retrieve generated sources if needed
            generated_sources = await client.get_mcp_sources(
                relpath="mcpgen",
                server_name="fetchurl",
            )
            assert "def fetch(params: Params) -> str:" in generated_sources["fetch"]

        async with ExecutionClient(port=container.executor_port) as client:
            # call the generated function in the container
            result = await client.execute("""
                from mcpgen.fetchurl.fetch import Params, fetch
                print(fetch(Params(url="https://www.gradion.ai")))
            """)
            print(result.text[:375])


if __name__ == "__main__":
    asyncio.run(main())

The script executes the generated fetch function in the ipybox container and then prints the first 375 characters of the fetched content:

Contents of https://www.gradion.ai/:
```

                         ___                    _
   ____ __________ _____/ (_)___  ____   ____ _(_)
  / __ `/ ___/ __ `/ __  / / __ \/ __ \ / __ `/ /
 / /_/ / /  / /_/ / /_/ / / /_/ / / / // /_/ / /
 \__, /_/   \__,_/\__,_/_/\____/_/ /_(_)__,_/_/
/____/
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipybox-0.4.4.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipybox-0.4.4-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file ipybox-0.4.4.tar.gz.

File metadata

  • Download URL: ipybox-0.4.4.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ipybox-0.4.4.tar.gz
Algorithm Hash digest
SHA256 52f5aa8db2b0856e83d0fd15107fc0ae1d1adb3dc7b717af2f3d73aed8d2baa9
MD5 44dc2ebc2477a9f22053b94ad4a9f83e
BLAKE2b-256 1268a0637439a4b85e7003e2868f4833d92f3c0aa9b8395d421a1d4d9f52bc77

See more details on using hashes here.

Provenance

The following attestation bundles were made for ipybox-0.4.4.tar.gz:

Publisher: release.yml on gradion-ai/ipybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ipybox-0.4.4-py3-none-any.whl.

File metadata

  • Download URL: ipybox-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ipybox-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ed1ca3d10688720918239865b1cf16672ae9d0430ec8d5dc837c4404b958c7d1
MD5 77a5909f1b9557a09e5271bad269d9fe
BLAKE2b-256 9c18a6dc0ed74c5d169f54af3e70cb65be68ff51735377f16461d382a50b63ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for ipybox-0.4.4-py3-none-any.whl:

Publisher: release.yml on gradion-ai/ipybox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page