|
--- |
|
title: Run My Script |
|
emoji: 🏃 |
|
colorFrom: red |
|
colorTo: indigo |
|
sdk: gradio |
|
sdk_version: 5.33.0 |
|
app_file: app.py |
|
pinned: false |
|
tag: "mcp-server-track" |
|
license: mit |
|
short_description: creates a venv, install dependencies and run your script |
|
--- |
|
|
|
## run_my_script |
|
|
|
|
|
Executes a user-provided self contained Python script inside an isolated virtual environment with automatic dependency management. |
|
|
|
This function is intended to serve as a backend execution engine in a Model Context Protocol (MCP) server setting, |
|
where a language model may submit scripts for evaluation. It creates a secure workspace, detects dependencies, |
|
installs them using `uv`, executes the script, captures its output (including stdout and generated files), and |
|
returns all relevant results. |
|
|
|
⚠️ Limitations & Guidance for mcp server Use: |
|
- Scripts should be self-contained, avoid system-level access, and primarily focus on data processing, text generation, |
|
visualization, or machine learning tasks. |
|
- The code can output logs, JSON, images, CSVs, or any other files, which are returned as artifacts. |
|
- Avoid infinite loops or long-running background processes. Timeout support can be added externally. |
|
|
|
Args: |
|
code (str): The Python script to execute. Should include all import statements and logic. |
|
|
|
user_input (str, optional): A string input available to the script via the SCRIPT_INPUT environment variable. |
|
Can be plain text, JSON, Markdown, or even base64-encoded images. |
|
|
|
Returns: |
|
Tuple[str, Dict[str, str], str]: |
|
- logs (str): Full stdout and stderr logs of the executed script. |
|
- artifacts (Dict[str, str]): A dictionary of output files with their names and summaries or indicators |
|
(e.g., image or CSV placeholders). Includes a "__workdir__" key pointing to the working directory. |
|
- zip_path (str): Path to a ZIP archive containing all output artifacts for download. |
|
|
|
|
|
|
|
**test script:** |
|
```python |
|
import os |
|
import base64 |
|
from io import BytesIO |
|
from PIL import Image |
|
import json |
|
|
|
input_path = os.environ["SCRIPT_INPUT"] |
|
output_path = os.environ["SCRIPT_OUTPUT"] |
|
|
|
# Load JSON input |
|
with open(input_path, "r") as f: |
|
data = json.load(f) |
|
img_b64 = data["img"] |
|
|
|
# Decode base64 to image |
|
img_bytes = base64.b64decode(img_b64) |
|
img = Image.open(BytesIO(img_bytes)) |
|
img.load() # ⬅️ Ensure image is fully loaded before processing |
|
|
|
# Flip image horizontally |
|
flipped = img.transpose(Image.FLIP_LEFT_RIGHT) |
|
|
|
# Save output |
|
flipped.save(os.path.join(output_path, "flipped.png")) |
|
print("Image flipped and saved as flipped.png.") |
|
``` |
|
|
|
**test input:** |
|
```json |
|
{ |
|
"img": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CM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