Run a stateful JavaScript interpreter on Modal¶
Deploy the published mcp-js Docker image once, then submit JavaScript
whenever you need it. Use the same terminal for both steps. When the
interpreter is idle, Modal can stop its container while keeping the endpoint
available. The next request starts the container again. The walkthrough
uses the same stateful pattern as Modal’s interpreter example, with V8
running the snippets. Original Modal example.
1. Set up the Modal CLI¶
Open a Bash terminal. You need a Modal account and Python 3.10 or later. Create a virtual environment, install the CLI, and sign in.
python -m venv .venv
source .venv/bin/activate
python -m pip install 'modal>=1.6.4'
modal setup
modal setup signs this terminal’s CLI into your Modal workspace so it can
create the app and manage volumes. Keep the virtual environment active for
the remaining steps. Modal setup guide.
2. Deploy the interpreter¶
Run the commands in this section in the same Bash terminal. First make a local directory for the policy files. You'll upload them to Modal before deploying the image.
policy_dir=$(mktemp -d)
printf "Policy files: %s\n" "$policy_dir"
2.1. Give JavaScript access to the workspace¶
Write a filesystem policy that allows reading, writing, listing, and
creating directories under /workspace. The rule checks the requested
operation and path on each call. It rejects .. path components, and the
separate rule for /workspace itself lets JavaScript list the volume root.
Filesystem policy inputs and path handling.
cat >"$policy_dir/filesystem.rego" <<'REGO'
package mcp.filesystem
default allow := false
allow if {
input.operation in {"readFile", "writeFile", "mkdir", "readdir"}
startswith(input.path, "/workspace/")
not ".." in split(input.path, "/")
}
allow if {
input.operation == "readdir"
input.path == "/workspace"
}
REGO
2.2. Allow outbound fetch to one host¶
The fetch policy permits GET requests to https://example.com on the
default HTTPS port. The server evaluates the method, scheme, host, and
port before sending a request. To use your own API, change these conditions
to match the requests it needs. Fetch policy examples.
cat >"$policy_dir/fetch.rego" <<'REGO'
package mcp.fetch
default allow := false
allow if {
input.method == "GET"
input.url_parsed.scheme == "https"
input.url_parsed.host == "example.com"
input.url_parsed.port == null
}
REGO
2.3. Allow one external module¶
The module policy checks the resolved import URL. This rule allows a
specific version of escape-html from jsDelivr. The deployment command
also enables the external module loader. External module imports.
cat >"$policy_dir/modules.rego" <<'REGO'
package mcp.modules
default allow := false
allow if {
input.resolved_url == "https://cdn.jsdelivr.net/npm/escape-html@1.0.3/+esm"
}
REGO
2.4. Create the volumes and upload the policies¶
Create the three Modal Volumes for session state, workspace files, and Rego policies. The create calls reuse existing volumes. Then copy the three local Rego files to the root of the policy volume. The upload replaces policy files with the same names so you can repeat this step after editing a rule. Modal Volumes.
python - <<'PYTHON'
import modal
for name in ("state", "workspace", "policies"):
modal.Volume.objects.create(f"mcp-js-interpreter-{name}", allow_existing=True)
PYTHON
for name in filesystem fetch modules; do
modal volume put --force mcp-js-interpreter-policies "$policy_dir/$name.rego" "/$name.rego"
done
Define the policy configuration in the shell. The paths below are inside
the server container, where the next step mounts the policy volume at
/policies. At startup, mcp-v8 reads the Rego files at these paths and
evaluates them in-process. mcp-v8 policy evaluation.
policies='{
"filesystem": {"policies": [{"url": "file:///policies/filesystem.rego"}]},
"fetch": {"policies": [{"url": "file:///policies/fetch.rego"}]},
"modules": {"policies": [{"url": "file:///policies/modules.rego"}]}
}'
2.5. Deploy the image and mount the volumes¶
Run modal server deploy with the published image. Modal uses the image’s
entrypoint to start mcp-v8 on port 8080. The deployment allows zero warm
containers when idle and at most one active replica writing to the volumes.
Modal Servers.
| Path in the container | What it holds |
|---|---|
/data |
V8 heap snapshots and the records that tie them to session names. |
/workspace |
Files JavaScript can access through the filesystem policy. |
/policies |
The three Rego files you uploaded. |
modal server deploy \
--app-name mcp-js-code-interpreter \
--image docker.io/wholelottahoopla/mcp-js:0.22.0 \
--port 8080 \
--min-containers 0 \
--max-containers 1 \
--scaledown-window 60 \
--volume mcp-js-interpreter-state:/data \
--volume mcp-js-interpreter-workspace:/workspace \
--volume mcp-js-interpreter-policies:/policies \
--env-var "MCP_V8_POLICIES_JSON=$policies" \
--env-var MCP_V8_ALLOW_EXTERNAL_MODULES=true \
--env-var PORT=8080 \
--env-var MCP_V8_HEAP_STORE=dir \
--env-var MCP_V8_HEAP_DIR=/data/heaps \
--env-var MCP_V8_SESSION_DB_PATH=/data/sessions
Modal prints an HTTPS URL and the deploy command exits. Save the URL for Step 3. You can inspect the deployed server’s mounts and autoscaling settings with this command.
modal server info mcp-js-code-interpreter/Server
3. Authenticate and run JavaScript¶
In the same terminal, create a Modal Proxy Token for the HTTP endpoint. You can reuse one you already have for the environment.
modal workspace proxy-tokens create --name mcp-js-interpreter
The CLI displays a proxy token ID beginning with wk- and a secret
beginning with ws-. Set the URL from deployment, then paste the ID and
secret joined by a dot when prompted. Bash reads the token without echoing
it. The client passes it in the HTTP Authorization header. Keep the token
private. Modal proxy authentication.
export MCP_JS_URL='https://YOUR-DEPLOYED-URL.modal.run'
read -rs -p 'Modal proxy token (wk-ID.ws-SECRET): ' MCP_JS_TOKEN
echo
export MCP_JS_TOKEN
Save this client as run_code.py. It uses the Python standard library.
"""Submit JavaScript to a deployed Modal interpreter."""
import argparse
import json
import os
import time
from functools import partial
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
STARTUP_RETRY_SECONDS = 45
STARTUP_RETRY_INTERVAL = 1
def request(base_url, token, path, body=None):
headers = {"Authorization": f"Bearer {token}"}
data = None
if body is not None:
headers["Content-Type"] = "application/json"
data = json.dumps(body).encode()
url = base_url.rstrip("/") + path
deadline = time.monotonic() + STARTUP_RETRY_SECONDS
timeout = 60
while True:
req = Request(url, data, headers)
try:
with urlopen(req, timeout=timeout) as response:
return json.load(response)
except HTTPError as error:
with error:
error_body = error.read(4096).decode("utf-8", errors="replace").strip()
try:
payload = json.loads(error_body)
except json.JSONDecodeError:
payload = {}
if not isinstance(payload, dict):
payload = {}
server_error = payload.get("error")
# Modal sends this response before forwarding the request to a container.
if (
error.code == 503
and server_error == "no upstreams available"
and deadline - time.monotonic() > STARTUP_RETRY_INTERVAL
):
time.sleep(STARTUP_RETRY_INTERVAL)
timeout = deadline - time.monotonic()
if timeout > 0:
continue
detail = server_error or payload.get("detail") or error_body
message = f"HTTP {error.code} {error.reason}"
if detail:
message += f": {detail}"
raise RuntimeError(message) from error
def run_js(call, code, *, session="tutorial"):
execution = call(
"/api/exec",
{"code": code, "session": session, "execution_timeout_secs": 5},
)
path = f"/api/executions/{execution['execution_id']}"
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
result = call(path)
status = result["status"].lower()
if status in {"completed", "failed", "timed_out", "cancelled"}:
output = call(f"{path}/output")["data"]
if output:
print(output.rstrip())
if status != "completed" or result.get("error"):
raise RuntimeError(result.get("error") or status)
return
time.sleep(0.25)
raise TimeoutError("The execution did not finish before the client deadline")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("code", help="JavaScript source")
parser.add_argument("--session", default="tutorial", help="Session name")
args = parser.parse_args()
url = os.environ.get("MCP_JS_URL")
token = os.environ.get("MCP_JS_TOKEN")
if not url or not token:
parser.error("Set MCP_JS_URL to the deployed URL and MCP_JS_TOKEN to wk-ID.ws-SECRET.")
call = partial(request, url, token)
try:
run_js(call, args.code, session=args.session)
except (RuntimeError, URLError, TimeoutError) as error:
parser.exit(1, f"{error}\n")
if __name__ == "__main__":
main()
Run the commands in order. Each command submits a snippet with
POST /api/exec, then uses the returned ID to poll for completion and read
console output. When no container is ready, Modal returns HTTP 503 with
the error no upstreams available. The client retries that response once
per second for up to 45 seconds while the container starts.
Modal cold starts ·
Asynchronous execution ·
HTTP API reference.
python run_code.py 'globalThis.x = 10'
python run_code.py 'globalThis.y = 5'
python run_code.py 'console.log(x + y)'
The first command sets x. The second restores the same heap and sets
y. The third restores both values and prints 15. All three use
the default session name tutorial. Try the same read under a new name.
It exits with a reference error because that session starts with a fresh
heap. Sessions and heap snapshots.
python run_code.py --session fresh 'console.log(x + y)'
4. Read and write files on the workspace volume¶
The deployment command mounts mcp-js-interpreter-workspace at /workspace.
The separate /data volume holds interpreter state, while /policies
holds the Rego files. MCP_V8_POLICIES_JSON maps each capability to the
file that controls it.
Mounting a volume makes it visible to the server process. The
mcp.filesystem policy lets JavaScript list directories, create
directories, and read or write files anywhere under /workspace,
including subdirectories. Each fs call is checked before the server
touches the filesystem.
python run_code.py 'await fs.mkdir("/workspace/reports", {recursive: true})'
python run_code.py 'await fs.writeFile("/workspace/reports/summary.txt", "hello from Modal")'
python run_code.py 'console.log(await fs.readdir("/workspace"))'
python run_code.py 'console.log(await fs.readFile("/workspace/reports/summary.txt", "utf8"))'
The first command creates a subdirectory. The second writes a file there.
The third lists the volume root and includes reports. The fourth reads the
file as UTF-8 and prints hello from Modal. The rule uses the /workspace/
prefix so a sibling such as /workspace-other stays outside the grant. It also rejects any
.. path component. Rego checks the supplied path before filesystem access,
and the OS can still follow symlinks. Keep the dedicated workspace volume
free of symlinks that point outside it. See
filesystem policy inputs and path handling.
5. Fetch data and import a module¶
The mcp.fetch rule permits HTTPS GET requests to example.com on its
default port. Changing the method to POST or the host to another domain
causes a policy denial.
python run_code.py 'console.log((await fetch("https://example.com/")).status)'
The command prints the HTTP status from the upstream site. The policy
checks the request fields before the server sends it. To allow your own
API, edit the host in fetch.rego and add path or method conditions as needed. Fetch policy examples.
To load an external module, the deployment enables
MCP_V8_ALLOW_EXTERNAL_MODULES and the mcp.modules policy permits one
exact URL. The module loader checks this separately from fetch().
python run_code.py 'import escape from "https://cdn.jsdelivr.net/npm/escape-html@1.0.3/+esm"; console.log(escape("<tag>"))'
The import example prints <tag>. To grant another capability, add
its Rego file to the upload list in Step 2.4 and register it under
the matching key in policies. External module imports ·
Recipes for other capabilities ·
Policy input fields.
After editing a policy, repeat Steps 2.4 and 2.5 and confirm the redeploy. A newly started server loads the uploaded rules. Keep the policy files on their separate volume so the workspace rule cannot modify them.
6. Connect an MCP client¶
The same deployed server exposes an MCP endpoint at /mcp. In an MCP
client that supports custom HTTP headers, enter these settings. Use the
server URL and proxy token from Step 3. mcp-v8 HTTP transport ·
Modal proxy authentication.
| Setting | Value |
|---|---|
| Transport | Streamable HTTP |
| URL | The deployed HTTPS URL followed by /mcp |
| HTTP header | Authorization: Bearer wk-ID.ws-SECRET |
To try the connection from the same terminal, use MCP Inspector with Node.js
and npm installed. First list the tools. The response should include
run_js. MCP Inspector CLI.
npx --yes @modelcontextprotocol/inspector --cli "${MCP_JS_URL%/}/mcp" \
--transport http \
--header "Authorization: Bearer $MCP_JS_TOKEN" \
--method tools/list
In your MCP client, select the run_js tool and pass the following
arguments.
{"code": "console.log(6 * 7)"}
The server runs the code asynchronously. If your client reports an
execution_id, call get_execution with that ID until the status is
completed, then call get_execution_output with the same ID. The
data field should contain 42. If your client reports an MCP task
instead, use the client’s task controls to retrieve the result. The same
filesystem, fetch, and module policies apply to code sent through MCP.
mcp-v8 tool reference ·
MCP tasks.
For a stateful MCP sequence, take the heap returned in the completed
execution result and pass it as the heap argument to the next run_js
call. The REST example uses a named
session to look up that heap automatically. Sessions and heap snapshots.
Modal’s FastMCP tutorial also shows how to explore a remote server with Inspector. In this deployment the published mcp-v8 image already serves the MCP endpoint. Modal’s MCP server example.
7. Check scale-to-zero and stop the app¶
After roughly 60 seconds without requests, Modal can stop the container. Wait until the deployment shows zero containers in the Modal dashboard, then run the read again with the default session.
python run_code.py 'console.log(x + y)'
This request starts a container. Once the container is ready, the client
prints 15 from the saved heap.
Modal commits volume changes in the background and on normal shutdown.
The new container can restore committed state. A sudden interruption can
lose writes since the last commit. Modal autoscaling ·
Modal Volume commits.
When you’re done with the endpoint, stop the app.
modal app stop mcp-js-code-interpreter
The volumes remain available for a later deployment.