File size: 3,791 Bytes
ef64188 9c5c048 ef64188 b671043 159d213 b671043 159d213 b671043 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
---
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/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK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