Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
README.md
CHANGED
|
@@ -9,12 +9,40 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
short_description: creates a venv, install dependencies and run your script
|
| 12 |
-
tag: mcp-server-track
|
| 13 |
---
|
| 14 |
|
| 15 |
## run_my_script
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
```python
|
| 19 |
import os
|
| 20 |
import base64
|
|
@@ -43,7 +71,7 @@ flipped.save(os.path.join(output_path, "flipped.png"))
|
|
| 43 |
print("Image flipped and saved as flipped.png.")
|
| 44 |
```
|
| 45 |
|
| 46 |
-
input:
|
| 47 |
```json
|
| 48 |
{
|
| 49 |
"img": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC"
|
|
|
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
short_description: creates a venv, install dependencies and run your script
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
## run_my_script
|
| 15 |
|
| 16 |
+
|
| 17 |
+
Executes a user-provided self contained Python script inside an isolated virtual environment with automatic dependency management.
|
| 18 |
+
|
| 19 |
+
This function is intended to serve as a backend execution engine in a Model Context Protocol (MCP) server setting,
|
| 20 |
+
where a language model may submit scripts for evaluation. It creates a secure workspace, detects dependencies,
|
| 21 |
+
installs them using `uv`, executes the script, captures its output (including stdout and generated files), and
|
| 22 |
+
returns all relevant results.
|
| 23 |
+
|
| 24 |
+
⚠️ Limitations & Guidance for mcp server Use:
|
| 25 |
+
- Scripts should be self-contained, avoid system-level access, and primarily focus on data processing, text generation,
|
| 26 |
+
visualization, or machine learning tasks.
|
| 27 |
+
- The code can output logs, JSON, images, CSVs, or any other files, which are returned as artifacts.
|
| 28 |
+
- Avoid infinite loops or long-running background processes. Timeout support can be added externally.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
code (str): The Python script to execute. Should include all import statements and logic.
|
| 32 |
+
|
| 33 |
+
user_input (str, optional): A string input available to the script via the SCRIPT_INPUT environment variable.
|
| 34 |
+
Can be plain text, JSON, Markdown, or even base64-encoded images.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
Tuple[str, Dict[str, str], str]:
|
| 38 |
+
- logs (str): Full stdout and stderr logs of the executed script.
|
| 39 |
+
- artifacts (Dict[str, str]): A dictionary of output files with their names and summaries or indicators
|
| 40 |
+
(e.g., image or CSV placeholders). Includes a "__workdir__" key pointing to the working directory.
|
| 41 |
+
- zip_path (str): Path to a ZIP archive containing all output artifacts for download.
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
**test script:**
|
| 46 |
```python
|
| 47 |
import os
|
| 48 |
import base64
|
|
|
|
| 71 |
print("Image flipped and saved as flipped.png.")
|
| 72 |
```
|
| 73 |
|
| 74 |
+
**test input:**
|
| 75 |
```json
|
| 76 |
{
|
| 77 |
"img": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CM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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
base_dir = Path("./script_runs")
|
| 64 |
base_dir.mkdir(exist_ok=True)
|
| 65 |
if cleanup_enabled:
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
def run_script(code: str, user_input: str = "", cleanup_enabled: bool = True, cleanup_after: int = 600):
|
| 63 |
+
"""
|
| 64 |
+
Executes a user-provided self contained Python script inside an isolated virtual environment with automatic dependency management.
|
| 65 |
+
|
| 66 |
+
This function is intended to serve as a backend execution engine in a Model Context Protocol (MCP) server setting,
|
| 67 |
+
where a language model may submit scripts for evaluation. It creates a secure workspace, detects dependencies,
|
| 68 |
+
installs them using `uv`, executes the script, captures its output (including stdout and generated files), and
|
| 69 |
+
returns all relevant results.
|
| 70 |
+
|
| 71 |
+
⚠️ Limitations & Guidance for LLM-based Use:
|
| 72 |
+
- Scripts should be self-contained, avoid system-level access, and primarily focus on data processing, text generation,
|
| 73 |
+
visualization, or machine learning tasks.
|
| 74 |
+
- The code can output logs, JSON, images, CSVs, or any other files, which are returned as artifacts.
|
| 75 |
+
- Avoid infinite loops or long-running background processes. Timeout support can be added externally.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
code (str): The Python script to execute. Should include all import statements and logic.
|
| 79 |
+
Example:
|
| 80 |
+
```python
|
| 81 |
+
import json
|
| 82 |
+
import os
|
| 83 |
+
input_path = os.environ["SCRIPT_INPUT"]
|
| 84 |
+
with open(input_path) as f:
|
| 85 |
+
data = json.load(f)
|
| 86 |
+
print("Processed:", data["name"])
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
user_input (str, optional): A string input available to the script via the SCRIPT_INPUT environment variable.
|
| 90 |
+
Can be plain text, JSON, Markdown, or even base64-encoded images.
|
| 91 |
+
Example:
|
| 92 |
+
```json
|
| 93 |
+
{"img": "base64string..."}
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
cleanup_enabled (bool, optional):
|
| 97 |
+
Whether to automatically delete old execution directories.
|
| 98 |
+
|
| 99 |
+
cleanup_after (int, optional):
|
| 100 |
+
Number of seconds after which completed runs should be deleted, if cleanup is enabled.
|
| 101 |
+
|
| 102 |
+
Returns:
|
| 103 |
+
Tuple[str, Dict[str, str], str]:
|
| 104 |
+
- logs (str): Full stdout and stderr logs of the executed script.
|
| 105 |
+
- artifacts (Dict[str, str]): A dictionary of output files with their names and summaries or indicators
|
| 106 |
+
(e.g., image or CSV placeholders). Includes a "__workdir__" key pointing to the working directory.
|
| 107 |
+
- zip_path (str): Path to a ZIP archive containing all output artifacts for download.
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
|
| 111 |
base_dir = Path("./script_runs")
|
| 112 |
base_dir.mkdir(exist_ok=True)
|
| 113 |
if cleanup_enabled:
|