Skip to main content
Blaxel Sandboxes provide tools for managing files and their contents, specialized for code generation (β€œcodegen”) use cases. These tools are accessible through the sandboxes’ MCP server and REST API.

Fast apply of file edits

With this tool, you can apply code changes suggested by an LLM to your existing code files fast (2000+ tokens/second). Traditional code generation requires generating the entire files every time, which can be slower for large files. With this approach your LLM only generates the specific changes needed, and this tool applies them to the original file.

Configure environment variables

Fast apply of file edits is powered by Morph or Relace and requires you to bring your own Morph/Relace account.
  • Morph
  • Relace
Pass your Morph API key and Morph model (default = morph-v2) set as environment variables when creating the sandbox.

import { SandboxInstance } from "@blaxel/core";

// Create sandbox with Morph API key for fast code edits
const sandbox = await SandboxInstance.createIfNotExists({
  name: "codegen-sandbox",
  image: "blaxel/nextjs:latest",
  memory: 4096,
  ports: [
    { name: "preview", target: 3000 }
  ],
  envs: [
    { name: "MORPH_API_KEY", value: process.env.MORPH_API_KEY || "" },
    { name: "MORPH_MODEL", value: process.env.MORPH_MODEL || "morph-v2" }
  ]
});

Use the tool via Blaxel SDK

Call the fastapply endpoint of the Sandbox API via the Blaxel SDK to fast-apply a targeted edit to a specified file with Morph or Relace, with instructions and partial contents.

import { SandboxInstance } from "@blaxel/core";

// Apply code changes
await sandbox.codegen.fastapply(
  "/tmp/test.ts",
  "// keep existing code // add a typescript function to calculate the area of a circle"
);

// Read file with changes
let content = await sandbox.fs.read("/tmp/test.ts");
console.log(content)

Use the tool via MCP

Call the codegenEditFile tool on the MCP server of a sandbox to fast-apply a targeted edit to a specified file, with instructions and partial contents. Use Blaxel SDK to retrieve the tool in any compatible agent framework (here in AI SDK format):
import { blTools } from '@blaxel/vercel';

// Get codegen tools from sandbox MCP
const allTools = await blTools([`sandboxes/${sandbox.metadata.name}`]);

// Filter for specific codegen tools
const codegenTools = Object.fromEntries(
  Object.entries(allTools).filter(([key]) =>
    key.startsWith('codegen')
  )
);

Other tools built for codegen

Use the following codegen-optimized functions by making tool calls through the MCP server or REST API of a sandbox. See example above on how to retrieve and execute the tools.
  1. codegenCodebaseSearch - Find semantic code snippets from the codebase based on a natural language query.
  2. codegenFileSearch - Fast fuzzy filename search in the project.
  3. codegenGrepSearch - Run fast, exact regex/text searches on files for locating patterns or strings.
  4. codegenListDir - List contents of a directory in the project.
  5. codegenParallelApply - Plan and apply similar changes to multiple locations/files simultaneously.
  6. codegenReadFileRange - Read a specific range of lines in a file (max 250 lines at once).
  7. codegenReapply - Retry the application of the last edit, in case it previously failed.
  8. codegenRerank - Performs semantic search/reranking on code files in a directory.