Skip to main content
Glama
hdresearch

Python REPL MCP Server

by hdresearch

execute_python

Execute Python code in a persistent REPL environment with variable retention between runs. Use this tool to run Python scripts, test code snippets, and maintain state across multiple executions.

Instructions

Execute Python code and return the output. Variables persist between executions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesPython code to execute
resetNoReset the Python session (clear all variables)

Implementation Reference

  • Executes the provided Python code in a persistent namespace, captures stdout/stderr, handles reset option, formats output or errors, returns as TextContent.
    if name == "execute_python":
        code = arguments.get("code")
        if not code:
            raise ValueError("Missing code parameter")
    
        # Check if we should reset the session
        if arguments.get("reset", False):
            self.global_namespace.clear()
            self.global_namespace["__builtins__"] = __builtins__
            return [
                types.TextContent(
                    type="text",
                    text="Python session reset. All variables cleared."
                )
            ]
    
        # Capture stdout and stderr
        stdout = io.StringIO()
        stderr = io.StringIO()
        
        try:
            # Execute code with output redirection
            with redirect_stdout(stdout), redirect_stderr(stderr):
                exec(code, self.global_namespace)
            
            # Combine output
            output = stdout.getvalue()
            errors = stderr.getvalue()
            
            # Format response
            result = ""
            if output:
                result += f"Output:\n{output}"
            if errors:
                result += f"\nErrors:\n{errors}"
            if not output and not errors:
                # Try to get the value of the last expression
                try:
                    last_line = code.strip().split('\n')[-1]
                    last_value = eval(last_line, self.global_namespace)
                    result = f"Result: {repr(last_value)}"
                except (SyntaxError, ValueError, NameError):
                    result = "Code executed successfully (no output)"
            
            return [
                types.TextContent(
                    type="text",
                    text=result
                )
            ]
                
        except Exception as e:  # noqa: F841
            # Capture and format any exceptions
            error_msg = f"Error executing code:\n{traceback.format_exc()}"
            return [
                types.TextContent(
                    type="text",
                    text=error_msg
                )
            ]
  • Defines the input schema for execute_python tool: required 'code' string, optional 'reset' boolean.
    inputSchema={
        "type": "object",
        "properties": {
            "code": {
                "type": "string",
                "description": "Python code to execute",
            },
            "reset": {
                "type": "boolean",
                "description": "Reset the Python session (clear all variables)",
                "default": False
            }
        },
        "required": ["code"],
    },
  • Registers the execute_python tool in the list_tools handler, including name, description, and input schema.
    types.Tool(
        name="execute_python",
        description="Execute Python code and return the output. Variables persist between executions.",
        inputSchema={
            "type": "object",
            "properties": {
                "code": {
                    "type": "string",
                    "description": "Python code to execute",
                },
                "reset": {
                    "type": "boolean",
                    "description": "Reset the Python session (clear all variables)",
                    "default": False
                }
            },
            "required": ["code"],
        },
    ),
  • Initializes the shared global namespace used for persistent variable state across execute_python calls.
    self.global_namespace = {
        "__builtins__": __builtins__,
    }
Behavior3/5

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

No annotations are provided, so the description carries the full burden. It discloses key behavioral traits: execution of Python code, output return, and variable persistence. However, it lacks details on safety (e.g., sandboxing, timeout), error handling, or resource limits, which are important for a code execution tool. The description does not contradict any annotations.

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

Conciseness5/5

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

The description is appropriately sized and front-loaded, consisting of two concise sentences that directly convey the core functionality and a key behavioral trait. Every sentence earns its place by providing essential information without redundancy or unnecessary details.

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

Completeness3/5

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

Given the complexity of a code execution tool, no annotations, and no output schema, the description is incomplete. It covers the basic purpose and variable persistence but lacks details on output format, error responses, or execution environment. This leaves gaps for an AI agent to understand the full behavior and use cases effectively.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents both parameters ('code' and 'reset') with descriptions. The description adds no additional parameter semantics beyond what the schema provides, such as examples or constraints. With high schema coverage, the baseline score of 3 is appropriate.

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 the resource ('Python code'), with the additional detail that it 'return[s] the output'. It distinguishes from sibling tools by focusing on execution rather than package installation (install_package) or variable listing (list_variables).

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

Usage Guidelines4/5

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

The description provides clear context for usage by stating that 'Variables persist between executions', which implicitly suggests when to use this tool (for sequential code execution with shared state) versus alternatives like resetting the session. However, it does not explicitly name alternatives or state when-not to use it, such as for package management tasks handled by install_package.

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

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/hdresearch/mcp-python'

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