Skip to main content
Glama

execute_ipython_cell

Run Python code in a stateful IPython kernel within a Docker container. Maintain variables, imports, and definitions across executions for iterative workflows. Supports async code with 'await' and sequential executions with shared kernel state.

Instructions

Execute Python code in a stateful IPython kernel within a Docker container.

The kernel maintains state across executions - variables, imports, and definitions persist between calls. Each execution builds on the previous one, allowing you to build complex workflows step by step. Use '!pip install package_name' to install packages as needed. The kernel has an active asyncio event loop, so use 'await' directly for async code. DO NOT use asyncio.run() or create new event loops. Executions are sequential (not concurrent) as they share kernel state. Use the reset() tool to clear the kernel state and start fresh. Returns: str: Output text from execution, or empty string if no output.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesPython code to execute in the IPython kernel
timeoutNoMaximum execution time in seconds before the kernel is interrupted

Implementation Reference

  • The main asynchronous handler function for the 'execute_ipython_cell' tool. It executes the provided Python code in a stateful IPython kernel using ExecutionClient, handles timeouts, and raises appropriate errors. Includes input schema via Annotated Fields.
    async def execute_ipython_cell( self, code: Annotated[ str, Field(description="Python code to execute in the IPython kernel"), ], timeout: Annotated[ float, Field(description="Maximum execution time in seconds before the kernel is interrupted") ] = 120, ) -> str: """Execute Python code in a stateful IPython kernel within a Docker container. The kernel maintains state across executions - variables, imports, and definitions persist between calls. Each execution builds on the previous one, allowing you to build complex workflows step by step. Use '!pip install package_name' to install packages as needed. The kernel has an active asyncio event loop, so use 'await' directly for async code. DO NOT use asyncio.run() or create new event loops. Executions are sequential (not concurrent) as they share kernel state. Use the reset() tool to clear the kernel state and start fresh. Returns: str: Output text from execution, or empty string if no output. """ await self.setup_task assert self.execution_client is not None try: async with self.executor_lock: result = await self.execution_client.execute(code, timeout=timeout) return result.text or "" except Exception as e: match e: case ExecutionError(): raise ExecutionError(e.args[0] + "\n" + e.trace) case _: raise e
  • Registration of the 'execute_ipython_cell' method as an MCP tool using FastMCP's @tool decorator.
    self.mcp.tool()(self.execute_ipython_cell)

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gradion-ai/ipybox'

If you have feedback or need assistance with the MCP directory API, please join our Discord server