LogoFreestyle

Mastra AI SDK

Code execution for Mastra AI SDK Agents

Install the required dependencies

npm install @mastra/core freestyle-sandboxes

Get your Freestyle API Key from the Freestyle Dashboard

Set up the Code Executor

The simplest code executor looks like this:

import { executeTool } from 'freestyle-sandboxes/mastra';
const codeExecutor = executeTool({
  apiKey: process.env.FREESTYLE_API_KEY!,
});

You can also pass in any nodeModules, environment variables, timeout, or network restrictions you need.

Here's an example with access to the resend and octokit node modules, and environment variables for RESEND_API_KEY and GITHUB_PERSONAL_ACCESS_TOKEN

import { executeTool } from "freestyle-sandboxes/mastra";
 
const codeExecutor = executeTool({
  apiKey: process.env.FREESTYLE_API_KEY!,
  nodeModules: {
    resend: "4.0.1",
    octokit: "4.1.0",
  },
  envVars: {
    RESEND_API_KEY: process.env.RESEND_API_KEY!,
    GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_PERSONAL_ACCESS_TOKEN!,
  },
});

Set up the Mastra AI SDK Agent

import { createMastra } from "@mastra/core";
 
const mastra = new Mastra();
 
const modelConfig: ModelConfig = {
  provider: "OPEN_AI",
  name: "gpt-4",
};
 
const llm = mastra.LLM(modelConfig);
 
const response = await llm.generate(
  "Calculate the sum of every number between 13 and 19 divided by the sum of every number between 8 and 13",
  {
    tools: {
      codeExecutor,
    },
  }
);

On this page