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

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

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)
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden and does so comprehensively. It discloses key behavioral traits: state persistence across calls, asyncio event loop handling, sequential execution constraints, and the return format (output text or empty string). This goes well beyond what the input schema provides.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded with the core purpose. Each sentence adds value, such as state persistence details, async handling, and execution constraints. Minor improvements could include briefer phrasing, but it remains efficient with no wasted content.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (stateful kernel, async support) and the presence of an output schema (which covers return values), the description is complete. It addresses all critical aspects: purpose, usage, behavioral nuances, and parameter context, making it fully adequate for an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the baseline is 3. The description adds meaningful context beyond the schema: it explains that the 'code' parameter can include '!pip install' commands and async 'await' usage, and it implies the 'timeout' parameter's role in preventing hangs. This elevates the score above baseline.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Execute Python code') and resource ('in a stateful IPython kernel within a Docker container'), distinguishing it from sibling tools like download_file, upload_file, and reset. It precisely defines what the tool does beyond just the name.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly provides when to use this tool (for executing Python code with persistent state) and when to use alternatives (use reset() to clear kernel state, and it mentions using '!pip install' for package installation). It also clarifies that executions are sequential, not concurrent, due to shared kernel state.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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