Update execute_python_code tool
This commit is contained in:
@@ -259,21 +259,17 @@ async def get_active_page():
|
|||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def execute_python_code(code: str) -> str:
|
def execute_python_code(code: str) -> str:
|
||||||
"""Executes arbitrary Python 3 code in this isolated sandbox container."""
|
|
||||||
stdout_buffer = io.StringIO()
|
stdout_buffer = io.StringIO()
|
||||||
stderr_buffer = io.StringIO()
|
stderr_buffer = io.StringIO()
|
||||||
|
namespace = {} # shared globals + locals
|
||||||
with contextlib.redirect_stdout(stdout_buffer), contextlib.redirect_stderr(stderr_buffer):
|
with contextlib.redirect_stdout(stdout_buffer), contextlib.redirect_stderr(stderr_buffer):
|
||||||
try:
|
try:
|
||||||
exec(code, {}, {})
|
exec(code, namespace) # <-- single arg = globals==locals
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write(f"Runtime Error: {str(e)}")
|
sys.stderr.write(f"Runtime Error: {str(e)}")
|
||||||
|
|
||||||
output = stdout_buffer.getvalue()
|
output = stdout_buffer.getvalue()
|
||||||
errors = stderr_buffer.getvalue()
|
if stderr_buffer.getvalue():
|
||||||
|
return f"Errors caught during execution:\n{stderr_buffer.getvalue()}\nPartial Output:\n{output}"
|
||||||
if errors:
|
|
||||||
return f"Errors caught during execution:\n{errors}\nPartial Output:\n{output}"
|
|
||||||
return output if output else "Code executed successfully with no returned stdout."
|
return output if output else "Code executed successfully with no returned stdout."
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
|
|||||||
Reference in New Issue
Block a user