monty
Execute Python code safely without filesystem/network access.
Run
uv run https://raw.githubusercontent.com/zhouzhuojie/monty-skill/main/monty.py "code here"
Note: Since
uv rundownloadsmonty.pyfrom the remote repo, the localfunctions.pydoes not exist by default. If you need external functions, create afunctions.pyfile and pass it with-f.
Options
| Flag | Description |
|---|---|
-f FILE | Path to external functions file (default: functions.py) |
-t SEC | Timeout in seconds |
External Functions
Define safe functions in functions.py for use in sandboxed code:
import random
def random_numbers(count: int) -> list:
"""Generate n random numbers between 0 and 1."""
return [random.random() for _ in range(count)]
async def fetch(url: str) -> dict:
return {"data": "example"}
Creating Custom Functions
If a feature isn't available in the built-ins, you can add your own safe functions to functions.py. Monty will automatically detect and load functions that are called in your code.
Example: Generating random numbers
# functions.py
import random
def random_numbers(count: int) -> list:
"""Generate n random numbers between 0 and 1."""
return [random.random() for _ in range(count)]
def random_int(min_val: int, max_val: int, count: int) -> list:
"""Generate n random integers between min and max (inclusive)."""
return [random.randint(min_val, max_val) for _ in range(count)]
Then use in monty:
uv run https://raw.githubusercontent.com/zhouzhuojie/monty-skill/main/monty.py "result = random_numbers(1000); print(f'Mean: {sum(result)/len(result):.4f}')" -f functions.py
Adding Extra Dependencies
If your external functions need packages like cryptography or requests, use uv run --with:
uv run --with cryptography --with requests https://raw.githubusercontent.com/zhouzhuojie/monty-skill/main/monty.py "encrypt('secret')" -f functions.py
Note: The
--withpackages are only available to your external functions running on the host. The sandboxed code (LLM-generated) still cannot access these packages directly - it must call through your external functions. This maintains the security guarantee.
Install for Agents
npx skills add zhouzhuojie/monty-skill
Or manually:
git clone https://github.com/zhouzhuojie/monty-skill.git cp -r monty-skill ~/.claude/skills/ # Claude cp -r monty-skill ~/.pi/agent/skills/ # Pi
Supported Python
- •Built-ins:
print,len,str,int,list,dict,range - •Control flow, functions, async/await, type hints
Not Supported
- •Third-party libraries (pandas, requests)
- •Classes, match statements
- •Direct filesystem/network access
Examples
See test_monty.py for comprehensive examples.