Loading...
Loading...
Execute Python code in isolated rootless containers with MCP server proxying for token-efficient agent workflows
npx skill4agent add aradotso/mcp-skills mcp-code-execution-modeSkill by ara.so — MCP Skills collection.
run_python# Podman (recommended)
brew install podman
podman machine init
podman machine start
# Docker Desktop (alternative)
# Download from docker.compython3 --version # Must be 3.11+pip install mcp-code-execution-modegit clone https://github.com/elusznik/mcp-server-code-execution-mode.git
cd mcp-server-code-execution-mode
pip install -e .~/Library/Application Support/Claude/claude_desktop_config.json{
"mcpServers": {
"code-execution": {
"command": "python",
"args": ["-m", "mcp_code_execution_mode"],
"env": {
"MCP_BRIDGE_RUNTIME": "podman",
"MCP_BRIDGE_IMAGE": "ghcr.io/elusznik/mcp-code-execution-mode:latest",
"MCP_BRIDGE_OUTPUT_MODE": "compact"
}
}
}
}| Variable | Default | Description |
|---|---|---|
| Auto-detect | |
| | Container image |
| | |
| | Execution timeout (seconds) |
| | Container memory limit |
| | Keep variables between calls |
claude_desktop_config.json{
"mcpServers": {
"code-execution": {
"command": "python",
"args": ["-m", "mcp_code_execution_mode"]
},
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/Documents"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}"
}
}
}
}from mcp import runtime
# List available MCP servers
servers = await runtime.discovered_servers()
# Returns: ["filesystem", "github", "slack"]
# Get tool schemas for a specific server
docs = await runtime.query_tool_docs("github")
# Returns: {"tools": [{"name": "create_issue", "description": "...", ...}]}
# Fuzzy search across all servers
matches = await runtime.search_tool_docs("list files", limit=5)
# Returns: [{"server": "filesystem", "tool": "list_directory", "description": "..."}]from mcp import mcp_servers
# Call a tool
result = await mcp_servers["github"].create_issue(
repo="owner/repo",
title="Bug report",
body="Description here"
)from mcp import mcp_github
result = await mcp_github.create_issue(
repo="owner/repo",
title="Feature request",
body="Add dark mode"
)from mcp.servers.github import create_issue
result = await create_issue(
repo="owner/repo",
title="Enhancement",
body="Improve performance"
)# First call
import pandas as pd
df = pd.DataFrame({"col": [1, 2, 3]})
df.to_csv("/tmp/data.csv", index=False)# Second call (same session)
import pandas as pd
df = pd.read_csv("/tmp/data.csv")
print(df.sum()) # Works! File still existsfrom mcp import runtime, mcp_servers
# Step 1: Find tools related to "calendar"
matches = await runtime.search_tool_docs("calendar events", limit=3)
# Step 2: Load full schema for the first match
server_name = matches[0]["server"]
docs = await runtime.query_tool_docs(server_name)
# Step 3: Call the tool
result = await mcp_servers[server_name].list_events(
start_date="2025-01-01",
end_date="2025-01-31"
)
print(result)import pandas as pd
import matplotlib.pyplot as plt
from mcp import mcp_filesystem
# Read data from filesystem server
csv_content = await mcp_filesystem.read_file(path="/data/sales.csv")
# Parse and analyze
df = pd.read_csv(pd.io.common.StringIO(csv_content))
monthly = df.groupby("month")["revenue"].sum()
# Generate chart
plt.bar(monthly.index, monthly.values)
plt.title("Monthly Revenue")
plt.savefig("/tmp/revenue.png")
# Write back
with open("/tmp/revenue.png", "rb") as f:
await mcp_filesystem.write_file(
path="/reports/revenue.png",
content=f.read()
)
print(f"Analyzed {len(df)} records, saved chart")from mcp import mcp_github, mcp_slack
# Get open issues
issues = await mcp_github.list_issues(
repo="myorg/myrepo",
state="open",
labels=["bug"]
)
# Post summary to Slack
await mcp_slack.post_message(
channel="#engineering",
text=f"📊 {len(issues)} open bugs:\n" +
"\n".join(f"• {i['title']}" for i in issues[:5])
)
print(f"Posted {len(issues)} issues to Slack")from mcp import mcp_servers
import asyncio
async def safe_call(server, tool, **kwargs):
for attempt in range(3):
try:
return await mcp_servers[server].__getattr__(tool)(**kwargs)
except Exception as e:
if attempt == 2:
raise
await asyncio.sleep(2 ** attempt)
result = await safe_call(
"github",
"create_issue",
repo="owner/repo",
title="Test",
body="Retry logic"
)import subprocess
# List files
result = subprocess.run(["ls", "-lh", "/tmp"], capture_output=True, text=True)
print(result.stdout)
# Parse JSON with jq
json_data = '{"name": "test", "count": 42}'
result = subprocess.run(
["jq", ".count"],
input=json_data,
capture_output=True,
text=True
)
print(f"Count: {result.stdout.strip()}")# Check status
podman machine list
podman machine start
# Or switch to Docker
export MCP_BRIDGE_RUNTIME=docker# Pre-pull the image
podman pull ghcr.io/elusznik/mcp-code-execution-mode:latest
# Or build locally
git clone https://github.com/elusznik/mcp-server-code-execution-mode.git
cd mcp-server-code-execution-mode
podman build -t mcp-code-execution:local -f Containerfile .
# Update config to use local image
export MCP_BRIDGE_IMAGE=mcp-code-execution:localfrom mcp import runtime
# Check what's actually available
docs = await runtime.query_tool_docs("github")
print([t["name"] for t in docs["tools"]])
# Use exact name from output
await mcp_github.create_or_update_file(...) # Not create_fileimport pickle
# Save state
state = {"counter": 42, "data": [1, 2, 3]}
with open("/tmp/state.pkl", "wb") as f:
pickle.dump(state, f)
# Restore in next call
with open("/tmp/state.pkl", "rb") as f:
state = pickle.load(f)# Increase timeout
export MCP_BRIDGE_TIMEOUT=300# Bad: process 1M rows in one call
df = pd.read_csv("huge.csv") # Times out
# Good: process in batches
for chunk in pd.read_csv("huge.csv", chunksize=10000):
process(chunk)/tmp# Bad
with open("/data/output.txt", "w") as f: # Read-only
f.write("data")
# Good
with open("/tmp/output.txt", "w") as f: # Writable
f.write("data")FROM ghcr.io/elusznik/mcp-code-execution-mode:latest
RUN pip install --no-cache-dir \
scikit-learn \
seaborn \
sqlalchemypodman build -t mcp-custom:latest .
export MCP_BRIDGE_IMAGE=mcp-custom:latest# Increase memory for ML workloads
export MCP_BRIDGE_MEMORY_LIMIT=2g
# CPU quota (% of one core)
export MCP_BRIDGE_CPU_QUOTA=50000 # 50%
# Max processes
export MCP_BRIDGE_PIDS_LIMIT=200--cap-drop=ALL--read-only--security-opt=no-new-privileges--network=none# Use SELinux labels (Fedora/RHEL)
export MCP_BRIDGE_SECURITY_OPT="label=type:container_runtime_t"
# Disable session persistence
export MCP_BRIDGE_SESSION_PERSIST=falsesearch_tool_docs()query_tool_docs()/tmp/python -m mcp_code_execution_modefrom mcp import runtime, mcp_github, mcp_slack
import pandas as pd
# 1. Discover GitHub tools
servers = await runtime.discovered_servers()
if "github" not in servers:
raise ValueError("GitHub MCP server not configured")
# 2. Fetch issues
issues = await mcp_github.list_issues(
repo="myorg/myrepo",
state="all",
since="2025-01-01"
)
# 3. Analyze with pandas
df = pd.DataFrame(issues)
df["created"] = pd.to_datetime(df["created_at"])
monthly_counts = df.groupby(df["created"].dt.to_period("M")).size()
# 4. Format report
report = "📈 Issue Report\n\n"
for month, count in monthly_counts.items():
report += f"{month}: {count} issues\n"
# 5. Post to Slack
if "slack" in servers:
await mcp_slack.post_message(
channel="#engineering",
text=report
)
print("✅ Report posted to Slack")
else:
print(report)