From b24d88a2a151d4e75689485e554b271a94d08154 Mon Sep 17 00:00:00 2001 From: Morpheus Sandmann Date: Mon, 8 Jun 2026 17:04:16 +0100 Subject: [PATCH] Initial commit --- Dockerfile | 8 ++++++++ server.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Dockerfile create mode 100644 server.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dfd644d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +# Dockerfile +FROM python:3.11-slim +COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ +WORKDIR /app +RUN uv pip install --system fastmcp mcp +COPY server.py . +EXPOSE 8000 +CMD ["python", "server.py"] diff --git a/server.py b/server.py new file mode 100644 index 0000000..6994e6d --- /dev/null +++ b/server.py @@ -0,0 +1,34 @@ +# server.py +import sys +import io +import contextlib +from mcp.server.fastmcp import FastMCP + +mcp = FastMCP("Sandbox-Python") + +@mcp.tool() +def execute_python_code(code: str) -> str: + """ + Executes arbitrary Python 3 code in this isolated sandbox container. + Returns the standard output (stdout) and error streams (stderr). + """ + stdout_buffer = io.StringIO() + stderr_buffer = io.StringIO() + + with contextlib.redirect_stdout(stdout_buffer), contextlib.redirect_stderr(stderr_buffer): + try: + # Execute within isolated global/local frames + exec(code, {}, {}) + 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}" + return output if output else "Code executed successfully with no returned stdout." + +if __name__ == "__main__": + # Override standard stdio pipes to expose an HTTP/SSE server + mcp.run(transport="sse", host="0.0.0.0", port=8000)