Skip to main content
Glama

execute_python

Execute Python code with persistent variables in a REPL environment. Reset the session to clear variables when needed.

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

  • The handler function for 'execute_python' tool. It executes the provided Python code in a persistent global namespace using exec(), handles session reset, captures stdout/stderr, formats output, and returns results or errors 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 ) ]
  • Registration of the 'execute_python' tool in the handle_list_tools method, defining its 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"], }, ),
  • Input schema for the 'execute_python' tool, defining required 'code' parameter and optional 'reset' flag.
    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"], },

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

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