Skip to main content
Glama
mozicim

Node Code Sandbox MCP

by mozicim

run_js_ephemeral

Execute JavaScript code in a temporary container with optional npm dependencies, then automatically clean up. Use for one-shot executions without maintaining sandboxes or managing cleanup manually.

Instructions

Run a JavaScript snippet in a temporary disposable container with optional npm dependencies, then automatically clean up. The code must be valid ESModules (import/export syntax). Ideal for simple one-shot executions without maintaining a sandbox or managing cleanup manually. When reading and writing from the Node.js processes, you always need to read from and write to the "./files" directory to ensure persistence on the mounted volume. This includes images (e.g., PNG, JPEG) and other files (e.g., text, JSON, binaries).

Example:

import fs from "fs/promises";
await fs.writeFile("./files/hello.txt", "Hello world!");
console.log("Saved ./files/hello.txt");

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
imageNoDocker image to use for ephemeral execution. e.g. - **node:lts-slim**: Node.js LTS version, slim variant. (Lightweight and fast for JavaScript execution tasks.) - **mcr.microsoft.com/playwright:v1.52.0-noble**: Playwright image for browser automation. (Preconfigured for running Playwright scripts.) - **alfonsograziano/node-chartjs-canvas:latest**: Chart.js image for chart generation and mermaid charts generation. ('Preconfigured for generating charts with chartjs-node-canvas and Mermaid. Minimal Mermaid example: import fs from "fs"; import { run } from "@mermaid-js/mermaid-cli"; fs.writeFileSync("./files/diagram.mmd", "graph LR; A-->B;", "utf8"); await run("./files/diagram.mmd", "./files/diagram.svg");)node:lts-slim
dependenciesNoA list of npm dependencies to install before running the code. Each item must have a `name` (package) and `version` (range). If none, returns an empty array.
codeYesJavaScript code to run inside the ephemeral container.

Implementation Reference

  • Main handler function that spins up an ephemeral Docker container, installs dependencies, runs the provided JavaScript code, captures output and file changes, and cleans up.
    export default async function runJsEphemeral({
      image = DEFAULT_NODE_IMAGE,
      code,
      dependencies = [],
    }: {
      image?: string;
      code: string;
      dependencies?: NodeDependenciesArray;
    }): Promise<McpResponse> {
      if (!isDockerRunning()) {
        return { content: [textContent(DOCKER_NOT_RUNNING_ERROR)] };
      }
    
      const telemetry: Record<string, unknown> = {};
      const dependenciesRecord = preprocessDependencies({ dependencies, image });
      const containerId = `js-ephemeral-${randomUUID()}`;
      const tmpDir = tmp.dirSync({ unsafeCleanup: true });
      const { memFlag, cpuFlag } = computeResourceLimits(image);
    
      try {
        // Start an ephemeral container
        execSync(
          `docker run -d --network host ${memFlag} ${cpuFlag} ` +
            `--workdir /workspace -v ${getFilesDir()}:/workspace/files ` +
            `--name ${containerId} ${image} tail -f /dev/null`
        );
    
        // Prepare workspace locally
        const localWorkspace = await prepareWorkspace({ code, dependenciesRecord });
        execSync(`docker cp ${localWorkspace.name}/. ${containerId}:/workspace`);
    
        // Generate snapshot of the workspace
        const snapshotStartTime = Date.now();
        const snapshot = await getSnapshot(getMountPointDir());
    
        // Run install and script inside container
        const installCmd =
          'npm install --omit=dev --prefer-offline --no-audit --loglevel=error';
    
        if (dependencies.length > 0) {
          const installStart = Date.now();
          const installOutput = execSync(
            `docker exec ${containerId} /bin/sh -c ${JSON.stringify(installCmd)}`,
            { encoding: 'utf8' }
          );
          telemetry.installTimeMs = Date.now() - installStart;
          telemetry.installOutput = installOutput;
        } else {
          telemetry.installTimeMs = 0;
          telemetry.installOutput = 'Skipped npm install (no dependencies)';
        }
    
        const { output, error, duration } = safeExecNodeInContainer({
          containerId,
        });
        telemetry.runTimeMs = duration;
        if (error) return getContentFromError(error, telemetry);
    
        // Detect the file changed during the execution of the tool in the mounted workspace
        // and report the changes to the user
        const changes = await detectChanges(
          snapshot,
          getMountPointDir(),
          snapshotStartTime
        );
    
        const extractedContents = await changesToMcpContent(changes);
    
        return {
          content: [
            textContent(`Node.js process output:\n${output}`),
            ...extractedContents,
            textContent(`Telemetry:\n${JSON.stringify(telemetry, null, 2)}`),
          ],
        };
      } finally {
        execSync(`docker rm -f ${containerId}`);
        tmpDir.removeCallback();
      }
    }
  • Zod schema defining input parameters: optional image, array of dependencies (name/version), and code string.
    export const argSchema = {
      image: z
        .string()
        .optional()
        .default(DEFAULT_NODE_IMAGE)
        .describe(
          'Docker image to use for ephemeral execution. e.g. ' +
            generateSuggestedImages()
        ),
      // We use an array of { name, version } items instead of a record
      // because the OpenAI function-calling schema doesn’t reliably support arbitrary
      // object keys. An explicit array ensures each dependency has a clear, uniform
      // structure the model can populate.
      // Schema for a single dependency item
      dependencies: z
        .array(NodeDependency)
        .default([])
        .describe(
          'A list of npm dependencies to install before running the code. ' +
            'Each item must have a `name` (package) and `version` (range). ' +
            'If none, returns an empty array.'
        ),
      code: z
        .string()
        .describe('JavaScript code to run inside the ephemeral container.'),
    };
  • src/server.ts:82-98 (registration)
    Registration of the 'run_js_ephemeral' tool on the MCP server using server.tool, linking name, description, schema, and handler.
    server.tool(
      'run_js_ephemeral',
      `Run a JavaScript snippet in a temporary disposable container with optional npm dependencies, then automatically clean up. 
      The code must be valid ESModules (import/export syntax). Ideal for simple one-shot executions without maintaining a sandbox or managing cleanup manually.
      When reading and writing from the Node.js processes, you always need to read from and write to the "./files" directory to ensure persistence on the mounted volume.
      This includes images (e.g., PNG, JPEG) and other files (e.g., text, JSON, binaries).
    
      Example:
      \`\`\`js
      import fs from "fs/promises";
      await fs.writeFile("./files/hello.txt", "Hello world!");
      console.log("Saved ./files/hello.txt");
      \`\`\`
    `,
      ephemeralSchema,
      runJsEphemeral
    );
  • src/server.ts:16-18 (registration)
    Import of the runJsEphemeral handler and its schema (ephemeralSchema) from the implementation file.
    import runJsEphemeral, {
      argSchema as ephemeralSchema,
    } from './tools/runJsEphemeral.ts';
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It discloses key behavioral traits: the container is temporary and disposable, cleanup is automatic, code must be ESModules, and files must be read/written from './files' directory for persistence. However, it lacks details on execution limits, error handling, or output format, which are important for a tool with no output schema.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded, with the core purpose stated first. It includes an example that clarifies usage, but the example is lengthy and could be more concise. Overall, most sentences earn their place by adding useful context, though some details could be streamlined.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (executing JavaScript in containers with dependencies) and lack of annotations and output schema, the description is moderately complete. It covers the ephemeral nature, ESModules requirement, and file persistence, but misses details on execution behavior, error responses, or limitations, which are crucial for an agent to use it correctly without structured output guidance.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters (image, dependencies, code). The description adds minimal value beyond the schema: it mentions 'optional npm dependencies' and provides an example with code, but does not elaborate on parameter usage or constraints. Baseline 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Run a JavaScript snippet in a temporary disposable container with optional npm dependencies, then automatically clean up.' It specifies the verb ('Run'), resource ('JavaScript snippet'), and distinguishes it from siblings by emphasizing ephemeral execution and automatic cleanup, unlike persistent sandbox tools like run_js or sandbox_exec.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool: 'Ideal for simple one-shot executions without maintaining a sandbox or managing cleanup manually.' It implies usage for temporary tasks but does not explicitly state when not to use it or name alternatives among siblings like run_js or sandbox_exec, which might be better for persistent executions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mozicim/node-code-sandbox-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server