From a2da57ae30c84399a225080624946dcd46bbab71 Mon Sep 17 00:00:00 2001 From: Morpheus Sandmann Date: Tue, 25 Aug 2026 20:11:50 +0100 Subject: [PATCH] Update execute_python_code tool --- server.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/server.py b/server.py index 015d4ef..774b25d 100644 --- a/server.py +++ b/server.py @@ -259,21 +259,17 @@ async def get_active_page(): @mcp.tool() def execute_python_code(code: str) -> str: - """Executes arbitrary Python 3 code in this isolated sandbox container.""" stdout_buffer = io.StringIO() stderr_buffer = io.StringIO() - + namespace = {} # shared globals + locals with contextlib.redirect_stdout(stdout_buffer), contextlib.redirect_stderr(stderr_buffer): try: - exec(code, {}, {}) + exec(code, namespace) # <-- single arg = globals==locals except Exception as e: sys.stderr.write(f"Runtime Error: {str(e)}") - output = stdout_buffer.getvalue() - errors = stderr_buffer.getvalue() - - if errors: - return f"Errors caught during execution:\n{errors}\nPartial Output:\n{output}" + if stderr_buffer.getvalue(): + return f"Errors caught during execution:\n{stderr_buffer.getvalue()}\nPartial Output:\n{output}" return output if output else "Code executed successfully with no returned stdout." @mcp.tool()