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

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 slow for large files. This approach is more efficient: your LLM only generates the specific changes needed, and this tool quickly applies them to the original file.

Get started

Fast apply of file edits is powered by MorphLLM. In order for this tool to work, make sure to pass your Morph API key and Morph model (default = morph-v2) set as environment variables when creating the sandbox.

import { SandboxInstance } from "@blaxel/core"

export async function createOrGetSandbox({sandboxName, wait = true}: {sandboxName: string, wait?: boolean}) {
  const envs = []
  if (process.env.MORPH_API_KEY) {
    envs.push({
      name: "MORPH_API_KEY",
      value: process.env.MORPH_API_KEY
    })
  }
  if (process.env.MORPH_MODEL) {
    envs.push({
      name: "MORPH_MODEL",
      value: process.env.MORPH_MODEL
    })
  }
  const sandboxModel = {
    metadata: {
      name: sandboxName
    },
    spec: {
      runtime: {
        image: "blaxel/prod-nextjs:latest",
        memory: 4096,
        ports: [
          {
            name: "sandbox-api",
            target: 8080,
            protocol: "HTTP",
          },
          {
            name: "preview",
            target: 3000,
            protocol: "HTTP",
          }
        ]
      },
      envs
    }
  }
  const sandbox = await SandboxInstance.createIfNotExists(sandboxModel)
  if (wait) {
    await sandbox.wait({ maxWait: 120000, interval: 1000 })
  }
  return sandbox
}
If you do not pass the MORPH_API_KEY, this specific tool will not be available.

Use the tool

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'; // or any compatible agent framework

// Get all tools in the sandbox MCP
const allTools = await blTools([`sandboxes/YOUR-SANDBOX-ID`], maxDuration * 1000)

// Filter on just codegenEditFile
const tools = Object.fromEntries(Object.entries(allTools).filter(([key]) => key.startsWith('codegenEditFile')))

// You can then pass 'tools' in your agent, so it can autonomously use it
...

Check out the following file on GitHub to see a full example of using the fast apply tool in an agent.

Other tools built for codegen

Use the following codegen-optimized functions by making tool calls through the MCP server 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.